poj3070(矩阵乘法求fib)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int mod=10000; void mul(int f[2],int a[2][2]){ int c[2]; memset(c,0,sizeof(c)); for (int i=0;i<2;i++){ for (int j=0;j<2;j++){ c[i]=(c[i]+f[j]*a[i][j])%mod; } } memcpy(f,c,sizeof(c)); } void mull(int a[2][2]){ int c[2][2]; memset(c,0,sizeof(c)); for (int i=0;i<2;i++){ for (int j=0;j<2;j++){ for (int k=0;k<2;k++){ c[i][j]=(c[i][j]+a[i][k]*a[k][j])%mod; } } } memcpy(a,c,sizeof(c)); } int main(){ int n; while(scanf("%d",&n)&&n!=-1){ int f[2]={0,1}; int a[2][2]={{0,1},{1,1}}; for (;n;n>>=1){ if(n&1) mul(f,a); mull(a); } printf("%d\n",f[0]); } return 0; }