-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101-optimum-polynomial.js
More file actions
67 lines (56 loc) · 1.5 KB
/
101-optimum-polynomial.js
File metadata and controls
67 lines (56 loc) · 1.5 KB
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
/**
* Optimum Polynomial
* Time Complexity: O(N^2)
* Space Complexity: O(N^2)
*/
function processData(input) {
const MOD = 1000000007n;
const lines = input.trim().split(/\s+/).map(BigInt);
let idx = 0;
const N = Number(lines[idx++]);
const A = [];
for (let i = 0; i <= N; i++) A.push(lines[idx++]);
const u = new Array(N + 2);
for (let n = 1; n <= N + 1; n++) {
let x = BigInt(n);
let val = 0n;
let pow = 1n;
for (let i = 0; i <= N; i++) {
val = (val + A[i] * pow) % MOD;
pow = (pow * x) % MOD;
}
u[n] = val;
}
const diff = Array.from({ length: N + 1 }, () => new Array(N + 2).fill(0n));
for (let j = 1; j <= N + 1; j++) diff[0][j] = u[j];
for (let i = 1; i <= N; i++) {
for (let j = 1; j <= N + 1 - i; j++) {
diff[i][j] = (diff[i - 1][j + 1] - diff[i - 1][j] + MOD) % MOD;
}
}
const C = Array.from({ length: N + 1 }, () => new Array(N + 1).fill(0n));
for (let i = 0; i <= N; i++) {
C[i][0] = C[i][i] = 1n;
for (let j = 1; j < i; j++) {
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
}
}
const res = [];
for (let k = 1; k <= N; k++) {
let fit = 0n;
for (let i = 0; i < k; i++) {
fit = (fit + C[k][i] * diff[i][1]) % MOD;
}
res.push(fit.toString());
}
console.log(res.join(" "));
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});