forked from lzyrapx/Algorithmic_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBerlekamp-Massey.cpp
66 lines (66 loc) · 1.77 KB
/
Berlekamp-Massey.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-7;
const int maxn = 1e5 + 5;
vector<double> ps[maxn];
int fail[maxn];
double x[maxn], delta[maxn];
int n;
int pn;
/*
前提:必须符合线性递推,如:
fib: 1 1 2 3 5 8 13 21 34 55 89
*/
int main()
{
while (~scanf("%d", &n)) // n 项
{
pn = 0;
for (int i = 1; i <= n; i++) {
scanf("%lf", &x[i]); //前 n项
}
for (int i = 1; i <= n; i++)
{
double dt = -x[i];
for (int j = 0; j < (int)ps[pn].size(); j++)
{
dt += x[i - j - 1] * ps[pn][j];
}
delta[i] = dt;
if (fabs(dt) <= eps) continue;
fail[pn] = i;
if(!pn)
{
ps[++pn].resize(1);
continue;
}
vector<double>&ls = ps[pn - 1];
double k = -dt / delta[fail[pn - 1]];
vector<double> cur;
cur.resize(i - fail[pn - 1] - 1);
cur.push_back(-k);
for(int j = 0; j < (int)ls.size();j++)
{
cur.push_back(ls[j] * k);
}
if((int)cur.size() < (int)ps[pn].size())
{
cur.resize(ps[pn].size());
}
for(int j = 0; j < (int)ps[pn].size(); j++)
{
cur[j] += ps[pn][j];
}
ps[++pn] = cur;
}
int len = (int)ps[pn].size();
std::cout << "f[i]=" ;
for (int i = 0; i < len; i++){
if(ps[pn][i] > 0 && i > 0)std::cout << "+";
if(ps[pn][i]==0)continue;
printf("%g*f[i-%d]", ps[pn][i], i+1);
}
}
return 0;
}