标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1672 Accepted Submission(s): 482
#include<cstring> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define LL __int64 const __int64 mod=1000000007; struct matrix { LL mat[2][2]; }; matrix multiply(matrix a,matrix b) { matrix c; memset(c.mat,0,sizeof(c.mat)); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { if(a.mat[i][j]==0)continue; for(int k=0;k<2;k++) { if(b.mat[j][k]==0)continue; c.mat[i][k]=(c.mat[i][k]+a.mat[i][j]*b.mat[j][k])%(mod-1);//加法不能简写成..+=...%.. } } } return c; } matrix matrixquicklymod(matrix x,LL m) { matrix res; // memset(res.mat,0,sizeof(res.mat)); for(int i=0;i<2;i++) for(int j=0;j<2;j++) res.mat[i][j]=(i==j); while(m) { if(m&1) res=multiply(res,x); m>>=1; x=multiply(x,x); } return res; } LL qmod(LL x, LL y) { LL z=1; while(y) { if(y&1) z=(z*x)%mod; y>>=1; x=(x*x)%mod; } return z; } int main() { LL a,b,n,ma,mb; while(scanf("%I64d%I64d%I64d",&a,&b,&n)!=EOF) { matrix ans; ma=mb=1;//幂 ans.mat[0][0]=1; ans.mat[0][1]=1; ans.mat[1][0]=1; ans.mat[1][1]=0; if(n==0) printf("%I64d\n",a%mod); else if(n==1) printf("%I64d\n",b%mod); else if(n==2) printf("%I64d\n",((a%mod)*(b%mod))%mod); else { ans=matrixquicklymod(ans,n-2); mb=ans.mat[0][0]+ans.mat[1][0]; ma=ans.mat[0][1]+ans.mat[1][1]; ma=ma%(mod-1); mb=mb%(mod-1); a=qmod(a,ma); b=qmod(b,mb); printf("%I64d\n",((a%mod)*(b%mod))%mod); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/yuyixingkong/p/4335905.html