标签:multi span include bsp fas pku div style code
矩阵的快速幂
#include<cstdio> using namespace std; struct matrix { int m[2][2]; }ans,base; matrix multi( matrix a,matrix b )//矩阵乘法 { matrix temp; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { temp.m[i][j]=0; for(int k=0;k<2;k++) temp.m[i][j]=(temp.m[i][j]+a.m[i][k]*b.m[k][j])%10000; } } return temp; } int fast(matrix a, int n)//矩阵快速幂 { ans.m[0][0]=ans.m[1][1]=1;//初始化为单位矩阵 ans.m[0][1]=ans.m[1][0]=0; while(n) { if(n&1) { ans=multi(ans,a); } a=multi(a,a); n>>=1; } return ans.m[0][1]; } int main() { int n; while(scanf("%d",&n)&&n!=-1) { base.m[0][0]=base.m[1][0]=base.m[0][1]=1; base.m[1][1]=0; printf("%d\n",fast(base,n)); } return 0; }
标签:multi span include bsp fas pku div style code
原文地址:https://www.cnblogs.com/bxd123/p/10346763.html