-
Notifications
You must be signed in to change notification settings - Fork 2
/
berlekamp.cpp
39 lines (39 loc) · 949 Bytes
/
berlekamp.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
vector<int> massey(vector<int> dp) {
dp.erase(dp.begin(), dp.begin() + 1);
vector<int> C(1, 1);
int L = 0;
vector<int> B(1, 1);
int b = 1;
for (int n = 0; n < dp.size(); ++n) {
int d = 0;
for (int i = 0; i <= L; ++i) {
d += C[i] * dp[n - i];
d %= mod;
if (d < 0) {
d += mod;
}
}
B.insert(B.begin(), 0);
if (d == 0) {
continue;
}
auto prevC = C;
if (C.size() < B.size()) {
C.resize(B.size(), 0);
}
int cur_mult = d * binpow(b, mod - 2) % mod;
for (int i = 0; i < B.size(); ++i) {
C[i] -= B[i] * cur_mult;
C[i] %= mod;
if (C[i] < 0) {
C[i] += mod;
}
}
if (2 * L <= n) {
b = d;
L = n - L + 1;
B = prevC;
}
}
return C;
}