HDU 1005 Number Sequence(基础矩阵快速幂)
2016-03-24 16:52
483 查看
题目链接:
HDU 1005 Number Sequence
HDU 1005 Number Sequence
//HDU 1005 15MS 1424K #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const long long mod=7; long long A,B,n; struct Matrix{ int row,col; long long data[10][10]; }; inline Matrix mul(Matrix a,Matrix b) { Matrix ans; ans.row=a.row,ans.col=b.col; memset(ans.data,0,sizeof(ans.data)); for(int i=1;i<=ans.row;i++){ for(int j=1;j<=ans.col;j++){ for(int k=1;k<=a.col;k++){ ans.data[i][j]+=a.data[i][k]*b.data[k][j]%mod; ans.data[i][j]%=mod; } } } return ans; } inline Matrix quick_power(Matrix a,long long m) { Matrix ans,tmp=a; ans.row=ans.col=a.row; memset(ans.data,0,sizeof(ans.data)); for(int i=1;i<=ans.row;i++) ans.data[i][i]=1; while(m){ if(m&1) ans=mul(ans,tmp); tmp=mul(tmp,tmp); m>>=1; } return ans; } int main() { //freopen("1005in.txt","r",stdin); while(~scanf("%lld%lld%lld",&A,&B,&n)){ if(A==0&&B==0&&n==0) break; Matrix ans,tmp; ans.row=ans.col=tmp.row=2,tmp.col=1; ans.data[1][1]=A,ans.data[1][2]=B; ans.data[2][1]=1,ans.data[2][2]=0; tmp.data[1][1]=tmp.data[2][1]=1; if(n==1){ printf("1\n"); continue; } ans=quick_power(ans,n-2); tmp=mul(ans,tmp); printf("%lld\n",tmp.data[1][1]); } return 0; }
相关文章推荐
- HDU 1005 Number Sequence
- hdu1757 矩阵快速幂--
- HDU 4979 A simple math problem.
- HDU 1575 Tr A
- HDU 2604 Queuing
- poj_3070 Fibonacci
- 矩阵快速幂求斐波那契数列 poj3070
- hihocoder骨牌覆盖问题·三
- HDU 5318 The Goddess Of The Moon
- UVA 10689 Yet another Number Sequence(矩阵快速幂求Fib数列)
- 矩阵的运算 --- 倍增法(UVA11149 - Power of Matrix)
- 例题2.23 细胞自动机 LA3740
- HDU 5451 Best Solver (2015年沈阳赛区网络赛B题)
- POJ 3070 Fibonacci
- HDU 5171 GTY's birthday gift
- hdu 5564 && bestcode 62 Clarke and digits
- 矩阵快速幂
- 矩阵快速幂求解任意初始值f1,f2及a,b的斐波那契数列
- bnu新生赛D 柯南的精灵(矩阵快速幂)
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵快速幂)