[boj] 21919. 小数最小公倍数(node.js)


問題の概要
[boj] 21919. 小数最小公倍数(node.js)
  • 与えられた数字の中で少数の人の最小公倍数を求める
  • に答える
  • A * B === GCD(A, B) * LCM(A ,B).
  • そのため、少数者を先に救い、
  • ユークリッドアーク除算によるGCD関数
  • LCM == A * B / GCDimを使用してLCMを取得します.
  • 注意:質問数の範囲内で、BigIntに変換して回答する必要があります.
  • 説明する
    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;
      }
    });
    くどくど言う
  • GCD,LCM処理後もBigIntに変換!!!!確認範囲を忘れないでください.
  • 変換後、BigIntもfor loop!