HDU——4549M斐波那契数列(矩阵快速幂+快速幂+费马小定理)
2016-06-25 15:08
363 查看
M斐波那契数列
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2598 Accepted Submission(s): 774
Problem Description
M斐波那契数列F
是一种整数数列,它的定义如下:
F[0] = a
F[1] = b
F
= F[n-1] * F[n-2] ( n > 1 )
现在给出a, b, n,你能求出F
的值吗?
Input
输入包含多组测试数据;
每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 )
Output
对每组测试数据请输出一个整数F
,由于F
可能很大,你只需输出F
对1000000007取模后的值即可,每组数据输出一行。
Sample Input
0 1 0
6 10 2
Sample Output
0
60
水题一道
代码:
#include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<cstdio> #include<string> #include<deque> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define INF 0x3f3f3f3f #define MM(x) memset(x,0,sizeof(x)) #define MMINF(x) memset(x,INF,sizeof(x)) typedef long long LL; const double PI=acos(-1.0); const LL mod=1000000007; struct mat { LL pos[2][2]; mat(){MM(pos);} }; mat operator*(const mat &a,const mat &b) { mat c; for (int i=0; i<2; i++) { for (int j=0; j<2; j++) { for (int k=0; k<2; k++) c.pos[i][j]+=(a.pos[i][k]*b.pos[k][j])%(mod-1); } } return c; } mat operator^(mat a,LL b) { mat r; for (int i=0; i<2; i++) r.pos[i][i]=1; while (b!=0) { if(b&1) r=r*a; a=a*a; b>>=1; } return r; } LL qpow(LL a,LL b) { LL r=1; a%=mod; while (b) { if(b&1) r=(r*a)%mod; a=(a*a)%mod; b>>=1; } return r; } int main(void) { LL pa,pb; LL a,b,c,n; while (~scanf("%I64d%I64d%I64d",&a,&b,&n)) { if(n==0) printf("%I64d\n",a); else if(n==1) printf("%I64d\n",b); else { mat t,one; t.pos[0][0]=1; t.pos[0][1]=1; t.pos[1][0]=1; one.pos[0][0]=1; one.pos[1][0]=1; t=t^(n-2); one=t*one; pa=one.pos[1][0]%(mod-1); pb=one.pos[0][0]%(mod-1); printf("%I64d\n",(qpow(a,pa)*qpow(b,pb))%mod); } } return 0; }
相关文章推荐
- HDU1575 Tr A
- 矩阵快速幂
- HDU 5525 Product(数论)
- HDU 1005 Number Sequence
- 判断大数是否为素数 (费马小定理+Carmichael数判断)
- hdu1757 矩阵快速幂--
- HDU 4979 A simple math problem.
- HDU 1575 Tr A
- HDU 2604 Queuing
- poj_3070 Fibonacci
- HDU2604 Queuing(矩阵快速幂模板)
- hdu 5667 Sequence【费马小定理+矩阵快速幂】
- LightOj 1006(矩阵快速幂)
- 同余取模。。
- 同余式。。
- 矩阵快速幂求斐波那契数列 poj3070
- 数论基础知识与定理
- poj 3070 矩阵快速幂求斐波拉契数列
- hihocoder骨牌覆盖问题·三
- HDU6050 Funny Function