HDU 5667 Sequence
来源:互联网 发布:人工智能程序设计java 编辑:程序博客网 时间:2024/06/11 01:45
Problem Description
Input
Output
Sample Input
15 3 3 3 233
Sample Output
190把次数拿下来就可以用矩阵递推了,要注意会有a%p=0的情况
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;LL n,a,b,c,p;int T;struct Matrix{ LL a[3][3];}A,B;//A,初始矩阵,B,构造矩阵Matrix operator*(const Matrix&a,const Matrix&b)//矩阵乘法加费马小定理{ Matrix c; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { c.a[i][j]=0; for(int k=0; k<3; k++) { (c.a[i][j]+=a.a[i][k]*b.a[k][j]%(p-1))%=(p-1); } } } return c;}Matrix pow(Matrix a,LL x){ Matrix c; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(i==j)c.a[i][j]=1; else c.a[i][j]=0; } } while(x) { if(x&1) c=c*a; a=a*a; x>>=1; } return c;}//LL get(LL x,LL y)//{// LL res=x;// while(y)// {// if(y&1)res=(res*x)%p;// x=((x%p)*(x%p))%p;// y>>=1;// }// return res;//}LL powmod(LL a,LL b,LL c){ if(b==0)b=c-1;//没有这句话,会WA. LL ans=1; while (b) { if (b%2==1) ans=ans*a%c; b/=2; a=a*a%c; } return ans;}int main(){ scanf("%d",&T); while(T--) { cin>>n>>a>>b>>c>>p; A.a[0][0]=0,A.a[0][1]=b,A.a[0][2]=b; B.a[0][0]=0,B.a[0][1]=1,B.a[0][2]=0; B.a[1][0]=1,B.a[1][1]=c,B.a[1][2]=0; B.a[2][0]=0,B.a[2][1]=1,B.a[2][2]=1; if(n==1){printf("1\n");continue;} B = pow(B,n-2); A = A*B; LL ans = powmod(a,A.a[0][1],p); cout<<ans<<endl; } return 0;}
1 0
- HDU 5667 Sequence
- hdu 5667Sequence
- HDU 5667:Sequence
- HDU 5667 Sequence
- hdu 5667 sequence
- HDU 5667 Sequence
- hdu 5667 Sequence
- HDU 5667 Sequence
- HDU 5667 Sequence
- hdu 5667 Sequence【矩阵快速幂】
- HDU 5667 Sequence(矩阵快速幂)
- hdu 5667 Sequence 矩阵快速幂
- HDU-5667 Sequence (矩阵快速幂)
- hdu 5667 Sequence(矩阵快速幂)
- HDU 5667 Sequence 【矩阵快速幂】
- HDU 5667 Sequence(矩阵快速幂)
- HDU 5667 Sequence(构造矩阵+费马小定理)
- hdu 1711 Number Sequence
- json转化为字符串
- Label设置行间距
- PHP学习(十八)--常见的关键字和魔术方法
- Android音乐播放器(一):搜索手机存储的音乐
- super,superClass,class
- HDU 5667 Sequence
- 阻塞 非阻塞 同步 异步 线程 进程 任务
- android中关于listView复用时出现错乱问题
- 凸优化和非凸优化
- Android问题集锦(1)
- cocoNode节点函数注释
- 我的记单词方法
- Java中普通代码块,构造代码块,静态代码块区别
- iOS本地化翻译xliff文件