Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1919 Accepted Submission(s): 738
#include<cstdio> #include<iostream> #include<cstring> using namespace std; #define LL __int64 #define mod 10007 struct matrix { LL mat[4][4]; }; matrix multiply(matrix a,matrix b) { matrix c; memset(c.mat,0,sizeof(c.mat)); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(a.mat[i][j]==0)continue; for(int k=0;k<4;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; } } } return c; } matrix quickmod(matrix a,LL n) { matrix res; for(int i=0;i<4;i++) for(int j=0;j<4;j++) res.mat[i][j]=(i==j); while(n) { if(n&1) res=multiply(res,a); n>>=1; a=multiply(a,a); } return res; } int main() { LL n,x,y; while(scanf("%I64d%I64d%I64d",&n,&x,&y)!=EOF) { if(n==0||n==1) { printf("%I64d\n",n+1); continue; } matrix ans; ans.mat[0][0]=ans.mat[1][2]=1; ans.mat[0][1]=ans.mat[0][2]=ans.mat[0][3]=0; ans.mat[2][2]=ans.mat[2][3]=ans.mat[3][2]=0; ans.mat[1][0]=ans.mat[1][1]=x*x%mod; ans.mat[2][0]=ans.mat[2][1]=y*y%mod; ans.mat[3][0]=ans.mat[3][1]=2*x*y%mod; ans.mat[1][3]=x%mod; ans.mat[3][3]=y%mod; ans=quickmod(ans,n-1); printf("%I64d\n",(2*ans.mat[0][0]+ans.mat[1][0]+ans.mat[2][0]+ans.mat[3][0])%mod); } return 0; }
原文:http://www.cnblogs.com/yuyixingkong/p/4338510.html