0 1 2
0 1 42837
long long x3, x1=0,x2=1; long long temp=1000000007; for(long long i=2;;i++) { x3=(3*x2+x1)%temp; if(x3==1&&x2==0) { printf("%I64d\n",i-1); break; } x1=x2; x2=x3; }
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; struct matrix { long long ma[3][3]; }; long long mod[3]; matrix multi(matrix x,matrix y,long long m) { matrix ans; memset(ans.ma,0,sizeof(ans.ma)); for(int i=1; i<=2; i++) { for(int j=1; j<=2; j++) { if(x.ma[i][j]) { for(int k=1; k<=2; k++) { ans.ma[i][k]=(ans.ma[i][k]+(x.ma[i][j]*y.ma[j][k])%m)%m; } } } } return ans; } int main() { long long n; while(~scanf("%I64d",&n)) { mod[0]=183120; mod[1]=222222224; mod[2]=1000000007; for(int l=0; l<3; l++) { if(n==0) continue; n=n-1; matrix a; a.ma[1][1]=1; a.ma[1][2]=a.ma[2][1]=a.ma[2][2]=0; matrix b; b.ma[1][1]=3; b.ma[1][2]=1; b.ma[2][1]=1; b.ma[2][2]=0; matrix ans; for(int i=1; i<=2; i++) { for(int j=1; j<=2; j++) { if(i==j) ans.ma[i][j]=1; else ans.ma[i][j]=0; } } while(n) { if(n&1) ans=multi(ans,b,mod[l]); b=multi(b,b,mod[l]); n=n>>1; } n=ans.ma[1][1]; } printf("%I64d\n",n); } return 0; }
hdu 4291 A Short problem(矩阵+取模循环节)
原文地址:http://blog.csdn.net/caduca/article/details/40679925