-
Notifications
You must be signed in to change notification settings - Fork 0
/
F.cpp
89 lines (72 loc) · 1.91 KB
/
F.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
#include <iostream>
#include <vector>
const long long MOD = 104857601;
size_t k;
long long n;
std::vector<long long> A;
std::vector<long long> Q;
void getNegative(std::vector<long long>& Q, std::vector<long long>& negativeQ) {
for (int i = 0; i < Q.size(); ++i) {
negativeQ[i] = (i % 2 == 0 ? Q[i] : (-Q[i] + MOD) % MOD);
}
}
void filter(std::vector<long long>& A) {
int freeI = 0;
for (int i = 0; i < A.size(); ++i) {
if (i % 2 == n % 2) {
A[freeI] = A[i];
freeI++;
}
}
}
long long getNth(std::vector<long long>& A, std::vector<long long>& Q) {
std::vector<long long> R(k + 1);
std::vector<long long> negativeQ(k + 1);
while (n >= k) {
for (int i = k; i < A.size(); ++i) {
A[i] = 0;
for (int j = 1; j < Q.size(); ++j) {
A[i] = (A[i] - Q[j] * A[i - j]) % MOD;
while (A[i] < 0) {
A[i] += MOD;
}
}
}
getNegative(Q, negativeQ);
for (int i = 0; i <= 2 * k; i += 2) {
long long coefR = 0;
for (int j = 0; j <= i; ++j) {
long long qq = (j > k ? 0 : Q[j]);
long long neq = (i - j > k ? 0 : negativeQ[i - j]);
coefR = (coefR + qq * neq + MOD) % MOD;
}
R[i / 2] = coefR;
}
Q = R;
filter(A);
n = n / 2;
}
return A[n];
}
int main() {
//freopen("test.in", "R", stdin);
//freopen("test.out", "w", stdout);
std::cin >> k >> n;
n--;
A.resize(2 * k);
Q.resize(k + 1);
for (int i = 0; i < A.size() / 2; ++i) {
long long a;
std::cin >> a;
A[i] = a;
}
Q[0] = 1;
for (int i = 1; i < Q.size(); ++i) {
long long c;
std::cin >> c;
Q[i] = (-c + MOD);
Q[i] %= MOD;
}
std::cout << getNth(A, Q);
return 0;
}