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]的值吗?
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
6 10 2
Sample Output
0
60
60
Source
题意:给你 a ,b,n 求f[n];
题解:矩阵快速幂+快速幂+欧拉函数
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll a,b,n;
struct matrix
{
ll m[][];
} ans,exm;
ll phi(ll nn)
{
ll i,rea=nn;
for(i=;i*i<=nn;i++)
{
if(nn%i==)
{
rea=rea-rea/i;
while(nn%i==)
nn/=i;
}
}
if(nn>)
rea=rea-rea/nn;
return rea;
}
ll zha=phi(mod);
struct matrix matrix_mulit(struct matrix aa, struct matrix bb)
{
struct matrix there;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
there.m[i][j]=;
for(int k=; k<; k++)
there.m[i][j]=(there.m[i][j]+aa.m[i][k]*bb.m[k][j]%zha)%zha;
}
}
return there;
};
ll matrix_quick(ll gg)
{
exm.m[][]=exm.m[][]=exm.m[][]=;
exm.m[][]=;
ans.m[][]=ans.m[][]=;
ans.m[][]=ans.m[][]=;
while(gg)
{
if(gg&)
{
ans=matrix_mulit(ans,exm);
}
exm=matrix_mulit(exm,exm);
gg>>=;
}
return ans.m[][];
}
ll quickmod(ll aa,ll bb)
{
ll re=;
while(bb)
{
if(bb&)
re=(re*aa)%mod;
aa=(aa*aa)%mod;
bb>>=;
}
return re;
} int main()
{
while(scanf("%I64d %I64d %I64d",&a,&b,&n)!=EOF)
{
if(n==)
printf("%I64d\n",a);
else
{
if(n==)
printf("%I64d\n",b);
else
{
printf("%I64d\n",quickmod(a,matrix_quick(n-)+zha)*quickmod(b,matrix_quick(n-)+zha)%mod);
}
}
}
return ;
}