POJ 2118 矩阵乘法
题意:
an=Σ1<=i<=k (an-i*bi) mod 10000
分析:
典型的矩阵乘法解线性递推式~
无限YM MATRIX67神犇~
http://www.matrix67.com/blog/archives/276/comment-page-1#comment-223435
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <iostream> 5 6 #define SIZE 110 7 #define mod 10000 8 9 using namespace std; 10 11 int n,m,a[SIZE],b[SIZE]; 12 13 struct MT 14 { 15 int x,y; 16 int mt[SIZE][SIZE]; 17 }ans,def; 18 19 inline MT operator *(MT a,MT b) 20 { 21 MT c; 22 memset(c.mt,0,sizeof c.mt); 23 c.x=a.x; c.y=b.y; 24 for(int i=1;i<=a.x;i++) 25 for(int j=1;j<=b.y;j++) 26 for(int k=1;k<=b.x;k++) 27 c.mt[i][j]=(c.mt[i][j]+(a.mt[i][k]%mod)*(b.mt[k][j]%mod))%mod; 28 return c; 29 } 30 31 bool read() 32 { 33 for(int i=0;i<n;i++) scanf("%d",&a[i]); 34 for(int i=0;i<n;i++) scanf("%d",&b[i]); 35 scanf("%d",&m); 36 if(m<=n-1) {printf("%d\n",a[m]);return false;} 37 m=m-(n-1); 38 for(int i=1;i<=n;i++) ans.mt[i][1]=a[i-1]; 39 ans.x=n; ans.y=1; 40 memset(def.mt,0,sizeof def.mt); 41 for(int i=1;i<n;i++) def.mt[i][i+1]=1; 42 for(int i=1;i<=n;i++) def.mt[n][i]=b[n-i]; 43 def.x=def.y=n; 44 return true; 45 } 46 47 void go() 48 { 49 while(m) 50 { 51 if(m&1) ans=def*ans; 52 def=def*def; 53 m>>=1; 54 } 55 printf("%d\n",ans.mt[n][1]); 56 } 57 58 int main() 59 { 60 while(scanf("%d",&n),n) 61 { 62 if(!read()) continue; 63 else go(); 64 } 65 return 0; 66 }
没有人能阻止我前进的步伐,除了我自己!