首页 > 代码库 > hdu 4990 Reading comprehension (矩阵快速幂)
hdu 4990 Reading comprehension (矩阵快速幂)
//f[n]=2*f[n-2]+f[n-1]+1 //矩阵快速幂 # include<stdio.h> # include<string.h> # include<algorithm> # include<iostream> using namespace std; struct node { __int64 m[3][3]; }; __int64 mod; node answ,origin,d; node f(node a,node b) { __int64 i,j,k; node c; for(i=0; i<3; i++) { for(j=0; j<3; j++) { c.m[i][j]=0; for(k=0; k<3; k++) { c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod; c.m[i][j]%=mod; } } } return c; } node quick(node answ,node origin,__int64 n) { while(n) { if(n%2) answ=f(answ,origin); origin=f(origin,origin); n/=2; } return answ; } int main() { __int64 i,j,n; while(~scanf("%I64d %I64d",&n,&mod)) { memset(answ.m,0,sizeof(answ.m)); memset(d.m,0,sizeof(d.m)); memset(origin.m,0,sizeof(origin.m)); for(i=0; i<3; i++) answ.m[i][i]=1; d.m[0][0]=1; d.m[0][1]=2; d.m[0][2]=1; origin.m[0][1]=2; origin.m[1][0]=1; origin.m[1][1]=1; origin.m[2][1]=1; origin.m[2][2]=1; if(n==1) printf("%I64d\n",1%mod); else if(n==2) printf("%I64d\n",2%mod); else { answ=quick(answ,origin,n-2); answ=f(d,answ); printf("%I64d\n",answ.m[0][1]%mod); } } return 0; }
hdu 4990 Reading comprehension (矩阵快速幂)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。