-
Notifications
You must be signed in to change notification settings - Fork 522
/
linear_recurrence.cpp
74 lines (68 loc) · 1.77 KB
/
linear_recurrence.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
#include <bits/stdc++.h>
#include "polynom.h"
using namespace std;
// returns f[n] = f[n-1]*a[k-1] + ... + f[n-k]*a[0], where f[0], ..., f[k-1] are provided
// O(k*log(k)*log(n)) complexity
template <class T>
T nth_element_of_recurrence(vector<T> a, const vector<T> &f, long long n) {
if (n < f.size())
return f[n];
a = -a;
a.emplace_back(1);
vector<T> xn = power({0, 1}, n, a);
return inner_product(f.begin(), f.begin() + min(f.size(), xn.size()), xn.begin(), T{0});
}
// https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Massey_algorithm
template <typename M>
vector<M> berlekamp_massey(const vector<M> &a) {
int n = a.size();
vector<M> C(n), B(n);
C[0] = B[0] = 1;
M b = 1;
int L = 0;
for (int i = 0, m = 1; i < n; ++i) {
M d = a[i];
for (int j = 1; j <= L; ++j)
d = d + C[j] * a[i - j];
if (d == 0) {
++m;
continue;
}
vector<M> T = C;
M coef = d / b;
for (int j = m; j < n; ++j)
C[j] -= coef * B[j - m];
if (2 * L > i) {
++m;
continue;
}
L = i + 1 - L;
B = T;
b = d;
m = 1;
}
C.resize(L + 1);
C.erase(C.begin());
reverse(C.begin(), C.end());
return -C;
}
// usage example
constexpr int mod = (int)1e9 + 7;
using mint = modint<mod>;
int main() {
{
// Fibonacci numbers
vector<mint> f{1, 1};
vector<mint> a{1, 1};
for (int i = 0; i < 10; ++i) {
cout << (int)nth_element_of_recurrence(a, f, i) << endl;
}
cout << endl;
}
{
vector<mint> f = berlekamp_massey(vector<mint>({1, 1, 3, 5, 11}));
for (auto v : f)
cout << (int)v << " ";
cout << endl;
}
}