M斐波那契数列
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3087 Accepted Submission(s): 953
Problem Description
M斐波那契数列F[n]是一种整数数列,它的定义如下:
F[0] = a
F[1] = b
F[n] = F[n-1] * F[n-2] ( n > 1 )
现在给出a, b, n,你能求出F[n]的值吗?
F[0] = a
F[1] = b
F[n] = F[n-1] * F[n-2] ( n > 1 )
现在给出a, b, n,你能求出F[n]的值吗?
Input
输入包含多组测试数据;
每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 )
每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 )
Output
对每组测试数据请输出一个整数F[n],由于F[n]可能很大,你只需输出F[n]对1000000007取模后的值即可,每组数据输出一行。
Sample Input
0 1 0
6 10 2
Sample Output
0
60
Source
题意:给你 a ,b,n 求f[n];
题解:矩阵快速幂+快速幂+欧拉函数
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <map> 10 #define ll __int64 11 #define mod 1000000007 12 #define dazhi 2147483647 13 using namespace std; 14 ll a,b,n; 15 struct matrix 16 { 17 ll m[5][5]; 18 } ans,exm; 19 ll phi(ll nn) 20 { 21 ll i,rea=nn; 22 for(i=2;i*i<=nn;i++) 23 { 24 if(nn%i==0) 25 { 26 rea=rea-rea/i; 27 while(nn%i==0) 28 nn/=i; 29 } 30 } 31 if(nn>1) 32 rea=rea-rea/nn; 33 return rea; 34 } 35 ll zha=phi(mod); 36 struct matrix matrix_mulit(struct matrix aa, struct matrix bb) 37 { 38 struct matrix there; 39 for(int i=0; i<2; i++) 40 { 41 for(int j=0; j<2; j++) 42 { 43 there.m[i][j]=0; 44 for(int k=0; k<2; k++) 45 there.m[i][j]=(there.m[i][j]+aa.m[i][k]*bb.m[k][j]%zha)%zha; 46 } 47 } 48 return there; 49 }; 50 ll matrix_quick(ll gg) 51 { 52 exm.m[0][0]=exm.m[0][1]=exm.m[1][0]=1; 53 exm.m[1][1]=0; 54 ans.m[0][0]=ans.m[1][1]=1; 55 ans.m[0][1]=ans.m[1][0]=0; 56 while(gg) 57 { 58 if(gg&1) 59 { 60 ans=matrix_mulit(ans,exm); 61 } 62 exm=matrix_mulit(exm,exm); 63 gg>>=1; 64 } 65 return ans.m[0][0]; 66 } 67 ll quickmod(ll aa,ll bb) 68 { 69 ll re=1; 70 while(bb) 71 { 72 if(bb&1) 73 re=(re*aa)%mod; 74 aa=(aa*aa)%mod; 75 bb>>=1; 76 } 77 return re; 78 } 79 80 int main() 81 { 82 while(scanf("%I64d %I64d %I64d",&a,&b,&n)!=EOF) 83 { 84 if(n==0) 85 printf("%I64d\n",a); 86 else 87 { 88 if(n==1) 89 printf("%I64d\n",b); 90 else 91 { 92 printf("%I64d\n",quickmod(a,matrix_quick(n-2)+zha)*quickmod(b,matrix_quick(n-1)+zha)%mod); 93 } 94 } 95 } 96 return 0; 97 }