这题其实就是求题目中给出的那个矩阵的n次方,我们利用矩阵的快速幂乘法即可求出(我的代码中是用二分法来实现的)。
#include <stdio.h>
#include <string.h>
const int a1[2][2]={{1,0},{0,1}};
const int a2[2][2]={{1,1},{1,0}};
struct node
{
int a[2][2];
}init,plus;
node mul(node p1,node p2)
{
node p3;
memset(p3.a,0,sizeof(p3.a));
int i,j,l;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(l=0;l<2;l++)
{
p3.a[i][j]=(p3.a[i][j]+p1.a[i][l]*p2.a[l][j])%10000;
}
return p3;
}
node multi(int k1)
{
node init1=init;
while(k1)
{
if(k1&1)
{
init1=mul(init1,plus);
}
k1>>=1;
plus=mul(plus,plus);
}
return init1;
}
int main()
{
int k;
while(~scanf("%d",&k))
{
memcpy(init.a,a1,sizeof(a1));
memcpy(plus.a,a2,sizeof(a1));
if(k==-1)
break;
node ans=multi(k);
printf("%d\n",ans.a[0][1]);
}
return 0;
}