[boj] 21919. 小数最小公倍数(node.js)
12012 ワード
問題の概要
[boj] 21919. 小数最小公倍数(node.js)与えられた数字の中で少数の人の最小公倍数を求める に答える そのため、少数者を先に救い、 ユークリッドアーク除算によるGCD関数 注意:質問数の範囲内で、BigIntに変換して回答する必要があります. 説明するGCD,LCM処理後もBigIntに変換!!!!確認範囲を忘れないでください. 変換後、BigIntもfor loop!
[boj] 21919. 小数最小公倍数(node.js)
A * B === GCD(A, B) * LCM(A ,B)
.LCM == A * B / GCD
imを使用してLCMを取得します.const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let cnt = 0;
const input = () => stdin[cnt++];
const gcd = (a, b) => {
let x, y;
if (a > b) {
x = a;
y = b;
} else {
x = b;
y = a;
}
return x % y == 0 ? y : gcd(y, x % y);
};
let stdin = [];
rl.on("line", function (line) {
stdin.push(line);
}).on("close", function () {
const N = input();
const arr = input().split(" ").map(BigInt);
let cand = [];
let result = -1;
arr.forEach((elem) => {
if (isPrime(elem)) {
cand.push(elem);
}
});
if (cand.length) {
result = cand[0];
for (let i = 1; i < cand.length; i++) {
result = (result * cand[i]) / gcd(result, cand[i]);
}
}
console.log(result.toString());
process.exit();
function isPrime(num) {
for (
let i = BigInt(2);
i <= BigInt(Math.trunc(Math.sqrt(Number(num))));
i += BigInt(1)
) {
if (num % i == 0) {
return false;
}
}
return true;
}
});
くどくど言うReference
この問題について([boj] 21919. 小数最小公倍数(node.js)), 我々は、より多くの情報をここで見つけました https://velog.io/@greenish0902/boj-21919.-소수-최소-공배수-node.jsテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol