标签:
2 1 4 100 2 0 4 100
21 12
#include<iostream> #include<stdio.h> #include<string.h> struct matrix{ __int64 m[2][2];//之前定义的 int 类型,结果WA了好多次,以后不能不加思索地看到数据范围:Each of them will not exceed 1,000,000,000.就主观认为。。。<img alt="尴尬" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif" /> }ans; __int64 M; matrix e={1,0,0,1}; /*void out(matrix a){ printf("****\n"); for(int i=0;i<2;++i){ for(int j=0;j<2;++j) printf("%d ",a.m[i][j]); printf("\n"); } printf("*****\n"); }*/ matrix multi(matrix a,matrix b){ matrix temp; for(int i=0;i<2;++i){ for(int j=0;j<2;++j){ temp.m[i][j]=0; for(int k=0;k<2;++k) temp.m[i][j]=(temp.m[i][j]+a.m[i][k]*b.m[k][j])%M; } } return temp; } matrix fast(matrix base,int n){ ans.m[0][0]=ans.m[1][1]=1; ans.m[0][1]=ans.m[1][0]=0; while(n){ if(n&1) ans=multi(ans,base); base=multi(base,base); n>>=1; } return ans; } matrix add(matrix a,matrix b){ matrix c; memset(c.m,0,sizeof(c.m)); for(int i=0;i<2;++i){ for(int j=0;j<2;++j) c.m[i][j]=(a.m[i][j]+b.m[i][j])%M; } return c; } matrix op(matrix a,int k){ if(k==1) return a; if(k&1) return add(op(a,k-1),fast(a,k)); return multi(op(a,k>>1),add(e,fast(a,k>>1))); } int main(){ __int64 k,b,n; matrix base; base.m[0][0]=base.m[0][1]=base.m[1][0]=1; base.m[1][1]=0; while(~scanf("%I64d%I64d%I64d%I64d",&k,&b,&n,&M)){ matrix x=fast(base,k); //A^k matrix y=fast(base,b); //A^b matrix z=op(x,n-1); //S(A^[k*(n-1)]) // Sn: A^(k*0+b) + A^(k*1+b) + A^(k*2+b) +...+ A^(k*(n-1)+b); matrix u=multi(y,add(z,e)); printf("%I64d\n",u.m[0][1]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdoj-1588-Gauss Fibonacci【矩阵快速幂】
标签:
原文地址:http://blog.csdn.net/qq_18062811/article/details/47131907