http://www.cnblogs.com/proverbs/archive/2012/10/10/2718799.html
poj 3070
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define SIZE 4
#define mod 10000
using namespace std;
struct MATRIX
{
int mt[SIZE][SIZE];
int x,y;
}ans,def;
int n;
inline MATRIX operator *(MATRIX a,MATRIX b)
{
MATRIX c;
memset(c.mt,0,sizeof c.mt);
c.x=a.x; c.y=b.y;
for(int i=1;i<=a.x;i++)
for(int j=1;j<=b.y;j++)
for(int k=1;k<=a.y;k++)
c.mt[i][j]=(c.mt[i][j]+(a.mt[i][k]%mod)*(b.mt[k][j]%mod))%mod;
return c;
}
inline MATRIX operator +(MATRIX a,MATRIX b)
{
MATRIX c;
memset(c.mt,0,sizeof c.mt);
c.x=a.x; c.y=a.y;
for(int i=1;i<=c.x;i++)
for(int j=1;j<=c.y;j++)
c.mt[i][j]=(a.mt[i][j]+b.mt[i][j])%mod;
return c;
}
inline bool prt(MATRIX &c)
{
for(int i=1;i<=c.x;i++)
{
for(int j=1;j<=c.y;j++) printf("%d ",c.mt[i][j]);
puts("");
}
}
void go()
{
n-=2;
def.mt[1][1]=def.mt[1][2]=def.mt[2][1]=1;
def.mt[2][2]=0; def.x=def.y=2;
ans.mt[1][1]=ans.mt[1][2]=ans.mt[2][1]=1; ans.mt[2][2]=0;
ans.x=ans.y=2;
while(n)
{
if(n&1) ans=ans*def;
def=def*def;
n>>=1;
}
printf("%d\n",ans.mt[1][1]);
}
int main()
{
while(scanf("%d",&n))
{
if(n==-1) break;
else if(n==0) puts("0");
else if(n==1) puts("1");
else go();
}
system("pause");
return 0;
}