-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path075-singular-integer-right-triangles.js
More file actions
65 lines (53 loc) · 1.38 KB
/
075-singular-integer-right-triangles.js
File metadata and controls
65 lines (53 loc) · 1.38 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
/**
* Singular Integer Right Triangles
* Time Complexity: O(N log N)
* Space Complexity: O(N)
*/
function processData(input) {
const arr = input.trim().split(/\s+/).map(Number);
const T = arr[0];
const queries = arr.slice(1);
const maxN = Math.max(...queries);
const limit = maxN;
const counts = new Array(limit + 1).fill(0);
const sqrtLimit = Math.floor(Math.sqrt(limit / 2)) + 1;
for (let m = 2; m <= sqrtLimit; m++) {
for (let n = 1; n < m; n++) {
if (((m - n) & 1) === 1 && gcd(m, n) === 1) {
const p0 = 2 * m * (m + n);
if (p0 > limit) continue;
for (let p = p0; p <= limit; p += p0) {
counts[p]++;
}
}
}
}
const prefix = new Array(limit + 1);
let running = 0;
for (let i = 0; i <= limit; i++) {
if (counts[i] === 1) running++;
prefix[i] = running;
}
let out = [];
for (let N of queries) {
out.push(prefix[N]);
}
console.log(out.join("\n"));
};
function gcd(a, b) {
while (b !== 0) {
let t = a % b;
a = b;
b = t;
}
return a;
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});