GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — code-library/Math/BerleKamp Massey.cpp at master · SuckSolace/code-library

Expand file tree

/

BerleKamp Massey.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
113 lines (110 loc) · 4.65 KB
/

BerleKamp Massey.cpp

Copy path

File metadata and controls

113 lines (110 loc) · 4.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
110
111
112
113
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9, mod = 1e9 + 7;
template <int32_t MOD>
struct modint {
int32_t value;
modint() = default;
modint(int32_t value_) : value(value_) {}
inline modint<MOD> operator + (modint<MOD> other) const { int32_t c = this->value + other.value; return modint<MOD>(c >= MOD ? c - MOD : c); }
inline modint<MOD> operator - (modint<MOD> other) const { int32_t c = this->value - other.value; return modint<MOD>(c < 0 ? c + MOD : c); }
inline modint<MOD> operator * (modint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return modint<MOD>(c < 0 ? c + MOD : c); }
inline modint<MOD> & operator += (modint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
inline modint<MOD> & operator -= (modint<MOD> other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; }
inline modint<MOD> & operator *= (modint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
inline modint<MOD> operator - () const { return modint<MOD>(this->value ? MOD - this->value : 0); }
modint<MOD> pow(uint64_t k) const {
modint<MOD> x = *this, y = 1;
for (; k; k >>= 1) {
if (k & 1) y *= x;
x *= x;
}
return y;
}
modint<MOD> inv() const { return pow(MOD - 2); } // MOD must be a prime
inline modint<MOD> operator / (modint<MOD> other) const { return *this * other.inv(); }
inline modint<MOD> operator /= (modint<MOD> other) { return *this *= other.inv(); }
inline bool operator == (modint<MOD> other) const { return value == other.value; }
inline bool operator != (modint<MOD> other) const { return value != other.value; }
inline bool operator < (modint<MOD> other) const { return value < other.value; }
inline bool operator > (modint<MOD> other) const { return value > other.value; }
};
template <int32_t MOD> modint<MOD> operator * (int64_t value, modint<MOD> n) { return modint<MOD>(value) * n; }
template <int32_t MOD> modint<MOD> operator * (int32_t value, modint<MOD> n) { return modint<MOD>(value % MOD) * n; }
template <int32_t MOD> istream & operator >> (istream & in, modint<MOD> &n) { return in >> n.value; }
template <int32_t MOD> ostream & operator << (ostream & out, modint<MOD> n) { return out << n.value; }
using mint = modint<mod>;
vector<mint> BerlekampMassey(vector<mint> S) {
int n = (int)S.size(), L = 0, m = 0;
vector<mint> C(n), B(n), T;
C[0] = B[0] = 1;
mint b = 1;
for(int i = 0; i < n; i++) {
++m; mint d = S[i];
for(int j = 1; j <= L; j++) d += C[j] * S[i - j];
if (d == 0) continue;
T = C; mint coef = d * b.inv();
for(int j = m; j < n; j++) C[j] -= coef * B[j - m];
if (2 * L > i) continue;
L = i + 1 - L; B = T; b = d; m = 0;
}
C.resize(L + 1); C.erase(C.begin());
for(auto &x: C) x *= -1;
return C;
}
vector<mint> combine (int n, vector<mint> &a, vector<mint> &b, vector<mint> &tr) {
vector<mint> res(n * 2 + 1, 0);
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n + 1; j++) res[i + j] += a[i] * b[j];
}
for (int i = 2 * n; i > n; --i) {
for (int j = 0; j < n; j++) res[i - 1 - j] += res[i] * tr[j];
}
res.resize(n + 1);
return res;
};
// transition -> for(i = 0; i < x; i++) f[n] += tr[i] * f[n-i-1]
// S contains initial values, k is 0 indexed
mint LinearRecurrence(vector<mint> &S, vector<mint> &tr, long long k) {
int n = S.size(); assert(n == (int)tr.size());
if (n == 0) return 0;
if (k < n) return S[k];
vector<mint> pol(n + 1), e(pol);
pol[0] = e[1] = 1;
for (++k; k; k /= 2) {
if (k % 2) pol = combine(n, pol, e, tr);
e = combine(n, e, e, tr);
}
mint res = 0;
for (int i = 0; i < n; i++) res += pol[i + 1] * S[i];
return res;
}
int prime[] = {2, 3, 5, 7, 11, 13, 17, 19}, ok[20];
int dp[2000][20];
int yo(int i, int last) {
if (i == 0) return 1;
int &ret = dp[i][last];
if (ret != -1) return ret;
ret = 0;
for (int k = 1; k <= 9; k++) {
if (!last || ok[last + k]) ret = (ret + yo(i - 1, k)) % mod;
}
return ret;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(dp, -1, sizeof dp);
for (int i = 0; i < 8; i++) ok[prime[i]] = 1;
vector<mint> S; S.push_back(4); mint sum = 4;
for (int i = 2; i < 100; i++) sum += yo(i, 0), S.push_back(sum);
auto tr = BerlekampMassey(S);
S.resize((int)tr.size());
int q; cin >> q;
while (q--) {
int n; cin >> n; --n;
cout << LinearRecurrence(S, tr, n) << '\n';
}
return 0;
}
// https://www.spoj.com/problems/ITRIX12E/
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.