矩阵快速幂
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<iostream> 6 using namespace std; 7 struct node{ 8 int a[3][3]; 9 }; 10 struct node mulpity(struct node a,struct node b) 11 { 12 int i,j,k; 13 struct node mid; 14 memset(mid.a,0,sizeof(mid.a)); 15 for(i=0;i<2;i++) 16 for(j=0;j<2;j++) 17 for(k=0;k<2;k++) 18 { 19 mid.a[i][j]=mid.a[i][j]+a.a[i][k]*b.a[k][j]; 20 mid.a[i][j]=mid.a[i][j]%10000; 21 } 22 return mid; 23 } 24 int answer(int n) 25 { 26 int i,p,j; 27 struct node a,b; 28 memset(b.a,0,sizeof(b.a)); 29 b.a[0][0]=1; 30 b.a[0][1]=1; 31 b.a[1][0]=1; 32 memset(a.a,0,sizeof(a.a)); 33 for(i=0;i<2;i++) 34 a.a[i][i]=1; 35 while(n) 36 { 37 if(n&1) 38 a=mulpity(a,b); 39 b=mulpity(b,b); 40 n>>=1; 41 } 42 return a.a[0][1]; 43 } 44 int main() 45 { 46 int i,p,j,n; 47 48 while(scanf("%d",&n)!=EOF) 49 { 50 if(n==-1) 51 break; 52 printf("%d\n",answer(n)); 53 } 54 return 0; 55 }