Retrospective Sequence
64-bit integer IO format: %I64d Java class name: (Any)
Retrospective sequence is a recursive sequence that is defined through itself. For example Fibonacci specifies the rate at which a population of rabbits reproduces and it can be generalized to a retrospective sequence. In this problem you will have to find the n-th Retrospective Sequence modulo MOD = 1000000009. The first (1 ≤ N ≤ 20) elements of the sequence are specified. The remaining elements of the sequence depend on some of the previous N elements. Formally, the sequence can be written as Fm = Fm - k1 + Fm - k2 + ... + Fm - ki + ... + Fm - kC - 1 + Fm - kC. Here, C is the number of previous elements the m-th element depends on, 1 ≤ ki ≤ N.
Input
The first line of each test case contains 3 numbers, the number (1 ≤ N ≤ 20) of elements of the retrospective sequence that are specified, the index (1 ≤ M ≤ 1018) of the sequence element that has to be found modulo MOD, the number (1 ≤ C ≤ N) of previous elements the i-th element of the sequence depends on.
The second line of each test case contains N integers specifying 0 ≤ Fi ≤ 10, (1 ≤ i ≤ N).
The third line of each test case contains C ≥ 1 integers specifying k1, k2, ..., kC - 1, kC (1 ≤ ki ≤ N).
Output
Output single integer R, where R is FM modulo MOD.
Sample Input
2 2 2
1 1
1 2
1
2 7 2
1 1
1 2
13
3 100000000000 3
0 1 2
1 2 3
48407255
Source
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const LL mod = 1000000009; 5 LL n,N,M,C; 6 struct Matrix{ 7 LL m[60][60]; 8 void init(){ 9 memset(m,0,sizeof m); 10 } 11 void setOne(){ 12 init(); 13 for(int i = 0; i < 60; ++i) m[i][i] = 1; 14 } 15 Matrix(){ 16 init(); 17 } 18 Matrix operator*(const Matrix &rhs) const{ 19 Matrix ret; 20 for(int k = 0; k <= n; ++k) 21 for(int i = 0; i <= n; ++i) 22 for(int j = 0; j <= n; ++j) 23 ret.m[i][j] = (ret.m[i][j] + m[i][k]*rhs.m[k][j]%mod)%mod; 24 return ret; 25 } 26 void print(){ 27 for(int i = 0; i <= n; ++i){ 28 for(int j = 0; j <= n; ++j) 29 cout<<m[i][j]<<" "; 30 cout<<endl; 31 } 32 cout<<endl; 33 } 34 }; 35 Matrix a,b; 36 void quickPow(LL index){ 37 //Matrix ret; 38 //ret.setOne(); 39 while(index){ 40 if(index&1) a = a*b; 41 index >>= 1; 42 b = b*b; 43 } 44 //a = a*ret; 45 } 46 int main(){ 47 while(~scanf("%I64d%I64d%I64d",&N,&M,&C)){ 48 a.init(); 49 b.init(); 50 n = N; 51 for(int i = 1; i <= N; ++i){ 52 scanf("%I64d",&a.m[0][i]); 53 b.m[i+1][i]++; 54 } 55 for(int i = 1,tmp; i <= C; ++i){ 56 scanf("%d",&tmp); 57 b.m[N + 1 - tmp][n]++; 58 } 59 if(M <= N){ 60 printf("%I64d ",a.m[0][M]%mod); 61 continue; 62 } 63 quickPow(M - N); 64 printf("%I64d ",a.m[0][n]%mod); 65 } 66 return 0; 67 } 68 /* 69 2 3 2 70 1 1 71 1 2 72 73 3 5 3 74 0 1 2 75 1 2 3 76 */