这里有提供了一种求解斐波那契的高效方法,矩阵来求.
结果对10000取余
0 9 999999999 1000000000 -1
Sample Output
0 34 626 6875
同上一题目.只不过f1,f2是任意的,注意n==0时的特判
#include<iostream> #include<cstdio> #include<cstring> #define mod 10000 #define LL long long using namespace std; struct node { LL Map[4][4]; }; LL a,b,c,d,f1,f2,n; node mul(node a,node b) { node tmp; for(int i=0;i<3;i++) for(int j=0;j<3;j++) { tmp.Map[i][j]=0; for(int k=0;k<3;k++) { tmp.Map[i][j]+=a.Map[i][k]*b.Map[k][j]; tmp.Map[i][j]%=mod; } } return tmp; } node qp(node a,LL n) { node tmp={1,0,0,0,1,0,0,0,1}; while(n) { if(n&1) tmp=mul(tmp,a); a=mul(a,a); n=n>>1; } return tmp; } int main() { while(~scanf("%lld",&n)) { if(n==0) { printf("0\n");continue; } if(n==-1) break; node arr,arr2; memset(arr.Map,0,sizeof(arr.Map)); memset(arr2.Map,0,sizeof(arr2.Map)); arr.Map[0][0]=1;arr.Map[2][0]=1; arr2.Map[0][0]=1;arr2.Map[0][1]=1; arr2.Map[1][0]=arr2.Map[2][2]=1; node p=qp(arr2,n-1); p=mul(p,arr); printf("%lld\n",p.Map[0][0]); } return 0; }