一个模板
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<string>
#include<queue>
#include<map>
using namespace std;
const int MOD=10000;
struct matrix
{
int m[2][2];
}base,one;
matrix calculate(matrix a,matrix b)
{
matrix ans;
for(int i=0;i<2;++i)
for(int j=0;j<2;++j)
{
ans.m[i][j]=0;
for(int k=0;k<2;++k)
ans.m[i][j]=(ans.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
}
return ans;
}
int fastpow(int n)
{
one.m[0][0]=one.m[0][1]=one.m[1][0]=1;
one.m[1][1]=0;
base.m[0][0]=base.m[1][1]=1;
base.m[0][1]=base.m[1][0]=0;
while(n!=0)
{
if(n&1)
{
base=calculate(base,one);
}
one=calculate(one,one);
n=n/2;
}
return base.m[0][1];
}
int main()
{
int n;
while(true)
{
scanf("%d",&n);
if (n==-1) break;
else printf("%d\n",fastpow(n));
}
return 0;
}