【POJ3070】Fibonacci
题意
求Fibn mod m的值
m=10000,0<=0<=2*109
分析
矩阵快速幂模板
设f(n)={Fibn,Fibn+1},可得到单位矩阵为{(0,1)(1,1) }
代码
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define mod 10000 #define ll long long ll n; ll a[2][2],f[2]; inline void mul(ll f[2],ll a[2][2]) { ll c[2]; memset(c,0,sizeof(c)); for(ll i=0;i<2;i++) for(ll j=0;j<2;j++) c[i]+=f[j]*a[j][i],c[i]%=mod; memcpy(f,c,sizeof(c)); } inline void mulself(ll a[2][2]) { ll c[2][2]; memset(c,0,sizeof(c)); for(ll i=0;i<2;i++) for(ll j=0;j<2;j++) for(ll k=0;k<2;k++) c[i][j]+=a[i][k]*a[k][j],c[i][j]%=mod; memcpy(a,c,sizeof(c)); } int main() { while(scanf("%lld",&n)&&n!=-1) { f[0]=0,f[1]=1; a[0][0]=0,a[0][1]=a[1][0]=a[1][1]=1; while(n) { if(n&1)mul(f,a); mulself(a); n>>=1; } printf("%lld\n",f[0]); } return 0; }
“Make my parents proud,and impress the girl I like.”