标签:memset 斐波那契数列 数列 php using inf 测试数据 mat otto
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=2e5+10,M=1e6+10,inf=1e9+7; const ll INF=1e18+10,mod=1e9+7; ll MOD; struct Matrix { ll a[2][2]; Matrix() { memset(a,0,sizeof(a)); } void init() { for(int i=0;i<2;i++) for(int j=0;j<2;j++) a[i][j]=(i==j); } Matrix operator + (const Matrix &B)const { Matrix C; for(int i=0;i<2;i++) for(int j=0;j<2;j++) C.a[i][j]=(a[i][j]+B.a[i][j])%MOD; return C; } Matrix operator * (const Matrix &B)const { Matrix C; for(int i=0;i<2;i++) for(int k=0;k<2;k++) for(int j=0;j<2;j++) C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%MOD; return C; } Matrix operator ^ (const ll &t)const { Matrix A=(*this),res; res.init(); ll p=t; while(p) { if(p&1)res=res*A; A=A*A; p>>=1; } return res; } }; ll quickmod(ll a,ll b,ll c) { ll ans=1; while(b) { if(b&1)ans=(ans*a)%c; b>>=1; a=(a*a)%c; } return ans; } int main() { ll a,b,n; Matrix base,ans; base.a[0][0]=1;base.a[0][1]=1; base.a[1][0]=1;base.a[1][1]=0; MOD=1e9+6; while(~scanf("%lld%lld%lld",&a,&b,&n)) { if(n==0) printf("%lld\n",a); else if(n==1) printf("%lld\n",b); else { ans=base^(n-1); ll x=quickmod(b,ans.a[0][0],1e9+7); ll y=quickmod(a,ans.a[0][1],1e9+7); printf("%lld\n",(x*y)%mod); } } return 0; }
标签:memset 斐波那契数列 数列 php using inf 测试数据 mat otto
原文地址:http://www.cnblogs.com/jhz033/p/6418598.html