-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSEQ-d.cpp
101 lines (80 loc) · 1.52 KB
/
SEQ-d.cpp
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
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<vector<ll> > vii;
typedef vector<ll> vi;
const int m = (int)1e9;
int k;
vi b;
vi c;
//matrix mul
vii mul(const vii& A, const vii& B) {
vii C(k, vi(k, 0));
for (int r = 0; r < k; ++r) {
for (int c = 0; c < k; ++c) {
for (int x = 0; x < k; ++x) {
C[r][c] += (A[r][x] * B[x][c]) % m;
C[r][c] %= m;
}
}
}
return C;
}
//matrix exponentiation
vii pow(vii& A, int n) {
if (n <= 1) return A;
//if odd
if (n & 1) return mul(A, pow(A, n - 1));
vii ans = pow(A, n / 2);
return mul(ans, ans);
}
int sol(int n){
vii t(k, vi(k, 0));
if (n <= k) return b[n - 1];
//setting transformation matrix
reverse(c.begin(), c.end());
for(int r = 0; r < k; ++r){
for(int col = 0; col < k; ++col){
//Make sure that T.Fn-1 = Fn
//no skipping should be there else formula needs to be modified
t[r][col] = 0;
if (col == r + 1) t[r][col] = 1;
//last row
if (r == k - 1) t[r][col] = c[col];
}
}
//setting f
vi f(k);
copy(b.begin(), b.end(), f.begin());
//compute T^n
int exp = n-1;
vii tn = pow(t, exp);
ll ans = 0;
// getting the first row
for(int i = 0; i < k; ++i){
ans += (tn[0][i] * f[i]) % m;
ans %= m;
}
return ans;
}
int main() {
int t;
cin >> t;
while (t--)
{
cin >> k;
b.resize(k);
c.resize(k);
for (int i = 0; i < k; ++i) {
cin >> b[i];
}
for (int i = 0; i < k; ++i) {
cin >> c[i];
}
int n;
cin >> n;
cout << sol(n) << "\n"; //never use endl
b.clear();
c.clear();
}
}