-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLagrange.cpp
More file actions
28 lines (27 loc) · 819 Bytes
/
Lagrange.cpp
File metadata and controls
28 lines (27 loc) · 819 Bytes
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
std::vector<int> lagrange(std::vector<int> x, std::vector<int> y) {
const int n = x.size();
std::vector<int> l(n + 1);
l[0] = 1;
for (int i = 0; i < n; ++i) {
for (int j = i; j >= 0; --j) {
inc(l[j + 1], l[j]);
l[j] = 1ll * l[j] * (P - x[i]) % P;
}
}
std::vector<int> f(n);
for (int i = 0; i < n; ++i) {
int prod = 1;
for (int j = 0; j < n; ++j) {
if (j != i) prod = 1ll * prod * (x[i] - x[j] + P) % P;
}
int cof = 1ll * y[i] * inverse(prod) % P;
int inv = inverse(P - x[i]);
int last = 0;
for (int j = 0; j < n; ++j) {
int now = 1ll * (l[j] - last + P) * inv % P;
f[j] = (f[j] + 1ll * now * cof) % P;
last = now;
}
}
return f;
}