标签:closed gif play tom 矩阵 数列 斐波那契数列 利用 cte
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 const long long SMod=1000000006; 8 const long long Smod=1000000007; 9 struct Matrix 10 { 11 long long m[7][7]; 12 }; 13 Matrix Mul(Matrix a,Matrix b) 14 { 15 Matrix c; 16 memset(c.m,0,sizeof(c.m)); 17 for(int i=0; i<2; i++) 18 for(int j=0; j<2; j++) 19 for(int k=0; k<2; k++) 20 c.m[i][j]+=((a.m[i][k]*b.m[k][j])%SMod+SMod)%SMod; 21 return c; 22 } 23 Matrix fastm(Matrix a,int n) 24 { 25 Matrix res; 26 memset(res.m,0,sizeof(res.m)); 27 for(int i=0; i<2; i++) 28 res.m[i][i]=1; 29 while(n) 30 { 31 if(n&1) 32 res=Mul(res,a); 33 n>>=1; 34 a=Mul(a,a); 35 } 36 return res; 37 } 38 int main() 39 { 40 Matrix a; 41 int x,y,n; 42 while(~scanf("%d%d%d",&x,&y,&n)) 43 { 44 if(n<2) 45 { 46 if(n==0) 47 printf("%d\n",x); 48 else 49 printf("%d\n",y); 50 continue; 51 } 52 memset(a.m,0,sizeof(a.m)); 53 a.m[0][0]=a.m[0][1]=a.m[1][0]=1; 54 a=fastm(a,n-1); 55 long long k=a.m[0][0],tmp=y; 56 long long ans=1; 57 while(k) 58 { 59 if(k&1) 60 ans=(ans*tmp)%Smod; 61 k>>=1; 62 tmp=(tmp*tmp)%Smod; 63 } 64 k=a.m[1][0]; 65 tmp=x; 66 while(k) 67 { 68 if(k&1) 69 ans=(ans*tmp)%Smod; 70 k>>=1; 71 tmp=(tmp*tmp)%Smod; 72 } 73 printf("%lld\n",ans%Smod); 74 } 75 return 0; 76 }
标签:closed gif play tom 矩阵 数列 斐波那契数列 利用 cte
原文地址:http://www.cnblogs.com/wang-ya-wei/p/6511749.html