Posted 05/03/2015Updated 07/19/2023 Semprathlon / Simfae Dean ACM-ICPC / Programing2 minutes read (About 239 words)看起来比较舒服的矩阵乘法模板转载源页面poj 3070 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#define SIZE 4 #define mod 10000using 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;}看起来比较舒服的矩阵乘法模板https://devblog.citruxonve.net/posts/c8084c88/AuthorSemprathlon / Simfae DeanPosted on05/03/2015Updated on07/19/2023Licensed under