Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 419 Accepted Submission(s): 148
php
http://www.cnblogs.com/liuxueyang/archive/2013/08/20/3270893.htmlhtml
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int mod=1000000007; struct Matrix{ long long arr[5][5]; }; Matrix init,unit; long long n,a0,ax,ay,b0,bx,by; //注意用long long,不然會溢出 Matrix Mul(Matrix a,Matrix b){ Matrix c; for(int i=0;i<5;i++) for(int j=0;j<5;j++){ c.arr[i][j]=0; for(int k=0;k<5;k++) c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j]%mod)%mod; c.arr[i][j]%=mod; } return c; } Matrix Pow(Matrix a,Matrix b,long long k){ while(k){ if(k&1){ b=Mul(b,a); } a=Mul(a,a); k>>=1; } return b; } void Init(){ for(int i=0;i<5;i++) for(int j=0;j<5;j++){ init.arr[i][j]=0; unit.arr[i][j]=0; } unit.arr[0][0]=1, unit.arr[0][1]=a0%mod, unit.arr[0][2]=b0%mod, unit.arr[0][3]=a0*b0%mod, unit.arr[0][4]=a0*b0%mod; init.arr[0][0]=1, init.arr[0][1]=ay%mod, init.arr[0][2]=by%mod, init.arr[0][3]=ay*by%mod, init.arr[0][4]=ay*by%mod, init.arr[1][1]=ax%mod, init.arr[1][3]=ax*by%mod, init.arr[1][4]=ax*by%mod, init.arr[2][2]=bx%mod, init.arr[2][3]=ay*bx%mod, init.arr[2][4]=ay*bx%mod, init.arr[3][3]=ax*bx%mod, init.arr[3][4]=ax*bx%mod, init.arr[4][4]=1; } int main(){ //freopen("input.txt","r",stdin); while(cin>>n){ //scanf("%d%d%d%d%d%d",&a0,&ax,&ay,&b0,&bx,&by); cin>>a0>>ax>>ay>>b0>>bx>>by; if(n==0){ puts("0"); continue; } Init(); Matrix ans=Pow(init,unit,n-1); cout<<ans.arr[0][4]<<endl; } return 0; }