-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2149.js
executable file
·37 lines (26 loc) · 896 Bytes
/
2149.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
const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n")
/** @type { Map<number, bigint> } */
const TioPhillBonatiMap = new Map()
TioPhillBonatiMap.set(0, 0n)
TioPhillBonatiMap.set(1, 1n)
TioPhillBonatiMap.set(2, 1n)
/** @param { number | string } num */
const isOdd = (num) => Math.abs(Number(num)) % 2 === 1
function TioPhillBonati(nth = 0) {
if (nth <= 0) return TioPhillBonatiMap.get(0)
if (TioPhillBonatiMap.has(nth) == false) {
const philA = TioPhillBonati(nth - 1)
const philB = TioPhillBonati(nth - 2)
TioPhillBonatiMap.set(nth, isOdd(nth) ? philA * philB : philA + philB)
}
return TioPhillBonatiMap.get(nth)
}
function main() {
const responses = []
for (const line of input)
if (line == "") break // EOFile
else responses.push(TioPhillBonati(Number.parseInt(line, 10) - 1))
console.log(responses.join("\n"))
}
main()