-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path039-integer-right-triangles.js
More file actions
74 lines (61 loc) · 1.6 KB
/
039-integer-right-triangles.js
File metadata and controls
74 lines (61 loc) · 1.6 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
68
69
70
71
72
73
74
/**
* Integer Right Triangles
* Time Complexity: O(N log N + T)
* Space Complexity: O(N)
*/
function processData(input) {
const lines = input.trim().split(/\s+/);
let idx = 0;
const T = Number(lines[idx++]);
const queries = [];
let maxN = 0;
for (let i = 0; i < T; i++) {
const n = Number(lines[idx++]);
queries.push(n);
if (n > maxN) maxN = n;
}
const counts = new Array(maxN + 1).fill(0);
const limit = Math.floor(Math.sqrt(maxN * 0.5)) + 1;
for (let m = 2; m <= limit; m++) {
for (let n = (m % 2 === 0 ? 1 : 2); n < m; n += 2) {
if (gcd(m, n) !== 1) continue;
const primitivePerimeter = 2 * m * (m + n);
if (primitivePerimeter > maxN) break;
for (let p = primitivePerimeter; p <= maxN; p += primitivePerimeter) {
counts[p]++;
}
}
}
const best = new Array(maxN + 1).fill(0);
let bestP = 0;
let bestCount = 0;
for (let p = 0; p <= maxN; p++) {
if (counts[p] > bestCount) {
bestCount = counts[p];
bestP = p;
}
best[p] = bestP;
}
let output = [];
for (const n of queries) {
output.push(String(best[n]));
}
console.log(output.join("\n"));
};
function gcd(a, b) {
while (b !== 0) {
const t = b;
b = a % b;
a = 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);
});