uva 10870 Recurrences

解题思路:矩阵快速幂

  1 ///////////////////////////////////////////////////////////////////////////
  2 //problem_id: uva 10870
  3 //user_id: SCNU20102200088
  4 ///////////////////////////////////////////////////////////////////////////
  5 
  6 #include <algorithm>
  7 #include <iostream>
  8 #include <iterator>
  9 #include <iomanip>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <string>
 13 #include <vector>
 14 #include <cstdio>
 15 #include <cctype>
 16 #include <cmath>
 17 #include <queue>
 18 #include <stack>
 19 #include <list>
 20 #include <set>
 21 #include <map>
 22 using namespace std;
 23 
 24 ///////////////////////////////////////////////////////////////////////////
 25 #pragma comment(linker,"/STACK:1024000000,1024000000")
 26 
 27 #define lson l,m,rt<<1
 28 #define rson m+1,r,rt<<1|1
 29 ///////////////////////////////////////////////////////////////////////////
 30 
 31 ///////////////////////////////////////////////////////////////////////////
 32 const double EPS=1e-8;
 33 const double PI=acos(-1.0);
 34 
 35 const int x4[]={-1,0,1,0};
 36 const int y4[]={0,1,0,-1};
 37 const int x8[]={-1,-1,0,1,1,1,0,-1};
 38 const int y8[]={0,1,1,1,0,-1,-1,-1};
 39 ///////////////////////////////////////////////////////////////////////////
 40 
 41 ///////////////////////////////////////////////////////////////////////////
 42 typedef long long LL;
 43 
 44 typedef int T;
 45 T max(T a,T b){ return a>b? a:b; }
 46 T min(T a,T b){ return a<b? a:b; }
 47 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
 48 T lcm(T a,T b){ return a/gcd(a,b)*b; }
 49 ///////////////////////////////////////////////////////////////////////////
 50 
 51 ///////////////////////////////////////////////////////////////////////////
 52 //Add Code:
 53 int d,m;
 54 
 55 struct Matrix{
 56     LL a[20][20];
 57 }origin,res;
 58 
 59 Matrix multiply(Matrix x,Matrix y){
 60     Matrix ret;
 61     for(int i=0;i<d;i++){
 62         for(int j=0;j<d;j++){
 63             ret.a[i][j]=0;
 64             for(int k=0;k<d;k++) ret.a[i][j]+=x.a[i][k]*y.a[k][j];
 65             ret.a[i][j]%=m;
 66         }
 67     }
 68     return ret;
 69 }
 70 
 71 Matrix cal(int k){
 72     while(k){
 73         if(k&1) res=multiply(res,origin);
 74         origin=multiply(origin,origin);
 75         k>>=1;
 76     }
 77 }
 78 ///////////////////////////////////////////////////////////////////////////
 79 
 80 int main(){
 81     ///////////////////////////////////////////////////////////////////////
 82     //Add Code:
 83     int n,i,f[20];
 84     while(scanf("%d%d%d",&d,&n,&m)!=EOF){
 85         if(d==0 && n==0 && m==0) break;
 86         memset(origin.a,0,sizeof(origin.a));
 87         for(i=0;i<d;i++){
 88             scanf("%d",&origin.a[0][i]);
 89             origin.a[0][i]%=m;
 90         }
 91         for(i=1;i<d;i++) origin.a[i][i-1]=1;
 92         for(i=1;i<=d;i++){
 93             scanf("%d",&f[i]);
 94             f[i]%=m;
 95         }
 96         if(n<=d){
 97             printf("%d
",f[n]);
 98             continue;
 99         }
100         memset(res.a,0,sizeof(res.a));
101         for(i=0;i<d;i++) res.a[i][i]=1;
102         cal(n-d);
103         LL ans=0;
104         for(i=0;i<d;i++) ans+=res.a[0][i]*f[d-i];
105         ans%=m;
106         printf("%lld
",ans);
107     }
108     ///////////////////////////////////////////////////////////////////////
109     return 0;
110 }
111 
112 ///////////////////////////////////////////////////////////////////////////
113 /*
114 Testcase:
115 Input:
116 1 1 100
117 2
118 1
119 
120 2 10 100
121 1 1
122 1 1
123 
124 3 2147483647 12345
125 12345678 0 12345
126 1 2 3
127 
128 0 0 0
129 Output:
130 1
131 55
132 423
133 */
134 ///////////////////////////////////////////////////////////////////////////
原文地址:https://www.cnblogs.com/linqiuwei/p/3325146.html