-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path072-counting-fractions.js
More file actions
49 lines (40 loc) · 1017 Bytes
/
072-counting-fractions.js
File metadata and controls
49 lines (40 loc) · 1017 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Counting Fractions
* Time Complexity: O(N log log N + T)
* Space Complexity: O(N)
*/
function processData(input) {
const parts = input.trim().split(/\s+/).map(Number);
let T = parts[0];
let Ns = parts.slice(1);
let maxN = Math.max(...Ns);
let phi = new Array(maxN + 1);
for (let i = 0; i <= maxN; i++) phi[i] = i;
for (let i = 2; i <= maxN; i++) {
if (phi[i] === i) {
for (let j = i; j <= maxN; j += i) {
phi[j] -= phi[j] / i;
}
}
}
let pref = new Array(maxN + 1);
pref[0] = 0;
pref[1] = 0;
for (let i = 2; i <= maxN; i++) {
pref[i] = pref[i - 1] + phi[i];
}
let out = [];
for (let n of Ns) {
out.push(pref[n]);
}
console.log(out.join("\n"));
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});