-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2514.js
executable file
·55 lines (45 loc) · 1.25 KB
/
2514.js
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
const { readFileSync } = require("fs")
const lines = readFileSync("/dev/stdin", "utf8")
.split("\n")
.map((line) => line.split(" "))
const MyMath = Object.create(Math, {
gcd: {
/**
* @param {number} x
* @param {number} y
*/
value: function (x, y) {
if (isNaN(x) || isNaN(y)) return Number.NaN
x = Math.abs(x)
y = Math.abs(y)
while (y) [x, y] = [y, x % y]
return x
},
configurable: false,
enumerable: false,
writable: false
},
lcm: {
/** @param {number[]} nums */
value: function (...nums) {
if (nums.includes(0)) return 0
return nums.reduce((lcm, value) => (lcm * value) / this.gcd(value, lcm))
},
configurable: false,
enumerable: false,
writable: false
}
})
function main() {
const responses = new Array()
for (let i = 0; i < lines.length; i += 2) {
if (lines[i].includes("")) break // EOFile
const [pA, pB, pC] = lines[i + 1].map((period) => Number.parseInt(period, 10))
const timeFromTheLastPlanetsAlignment = Number.parseInt(lines[i][0], 10)
const intervalForEachPlanetsAlignment = MyMath.lcm(pA, pB, pC)
const timeToNextPlanetsAlignment = intervalForEachPlanetsAlignment - timeFromTheLastPlanetsAlignment
responses.push(timeToNextPlanetsAlignment)
}
console.log(responses.join("\n"))
}
main()