标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2461 Accepted Submission(s): 864
#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; typedef long long LL; #define INF 0x3f3f3f3f const long long mod=1000000007; struct mat { LL pos[2][2]; mat(){memset(pos,0,sizeof(pos));} }; inline mat mul(const mat &a,const mat &b,const LL &mod) { 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])%mod*(b.pos[k][j])%mod)%(mod); } } } return c; } inline mat matpow(mat a,LL b,const LL &mod) { mat r; r.pos[1][1]=r.pos[0][0]=1; mat bas=a; while (b!=0) { if(b&1) r=mul(r,bas,mod); bas=mul(bas,bas,mod); b>>=1; } return r; } int main(void) { mat one,t; LL n,ans; one.pos[0][0]=1; one.pos[0][1]=0; t.pos[0][0]=3; t.pos[0][1]=t.pos[1][0]=1; mat poww,initt; while (cin>>n) { if(n==0) { cout<<"0"<<endl; continue; } poww=t,initt=one; poww=matpow(poww,n-1,183120); initt=mul(initt,poww,183120); ans=initt.pos[0][0]; ans+=183120; poww=t,initt=one; poww=matpow(poww,ans-1,222222224); initt=mul(initt,poww,222222224); ans=initt.pos[0][0]; ans+=222222224; poww=t,initt=one; poww=matpow(poww,ans-1,1000000007); initt=mul(initt,poww,1000000007); cout<<initt.pos[0][0]%1000000007<<endl; } return 0; }
HDU——4291A Short problem(矩阵快速幂+循环节)
标签:
原文地址:http://www.cnblogs.com/Blackops/p/5468297.html