矩阵乘法。
这题关键在于矩阵的构造。
以 k = 4 为例:
类似地,对于任意 k 都可以构造出相应的矩阵。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
inline void set_file_IO(string);
inline void close_IO(void);
inline void work(void);
int main(void) {
#ifndef ONLINE_JUDGE
set_file_IO("recurrence");
#endif
ios::sync_with_stdio(false);
work();
#ifndef ONLINE_JUDGE
close_IO();
#endif
return 0;
}
typedef long long ll;
const ll Mod = 10000;
template<int Size>
struct matrix {
ll data[Size][Size];
matrix() {
memset(data, 0, sizeof data);
}
matrix<Size> operator *(const matrix<Size> b) {
matrix<Size> res;
for (int i=0; i<Size; ++i) {
for (int j=0; j<Size; ++j) {
for (int k=0; k<Size; ++k) {
(res.data[i][j] += data[i][k] * b.data[k][j]) %= Mod;
}
}
}
return res;
}
void operator *= (const matrix<Size> b) {
*this = *this * b;
}
matrix<Size> operator ^(ll exp) {
matrix<Size> res;
for (int i=0; i<Size; ++i) {
res.data[i][i] = 1;
}
for (matrix<Size> base=*this; exp; base*=base, exp>>=1LL) {
if (exp & 1LL) {
res *= base;
}
}
return res;
}
matrix operator ^=(const ll exp) {
return *this = *this ^ exp;
}
};
matrix<101> ans, ast;
inline void single(void) {
ll n, k;
cin >> k >> n;
for (int i=1; i<k; ++i) {
ast.data[i][i-1] = 1;
}
for (int i=0; i<k; ++i) {
cin >> ast.data[k-i-1][k-1];
}
for (int i=0; i<k; ++i) {
cin >> ans.data[0][i];
}
if (n <= k) {
cout << ans.data[0][n] << endl;
return ;
}
ans *= (ast ^ (n-k+1));
cout << ans.data[0][k-1] << endl;
}
inline void work(void) {
int T = 1;
//cin >> T;
while (T--) {
single();
}
}
inline void set_file_IO(string name) {
freopen((name + ".in" ).c_str(), "r", stdin );
freopen((name + ".out").c_str(), "w", stdout);
}
inline void close_IO(void) {
fclose(stdin);
fclose(stdout);
}