結果

問題 No.3056 量子コンピュータで素因数分解 Easy
ユーザー 👑 MizarMizar
提出日時 2022-09-11 20:51:27
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 1,324 bytes
コンパイル時間 54 ms
コンパイル使用メモリ 5,300 KB
実行使用メモリ 60,340 KB
平均クエリ数 2.65
最終ジャッジ日時 2023-08-30 06:59:24
合計ジャッジ時間 4,836 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
58,520 KB
testcase_01 AC 98 ms
58,560 KB
testcase_02 AC 147 ms
60,284 KB
testcase_03 AC 149 ms
59,860 KB
testcase_04 AC 96 ms
58,872 KB
testcase_05 AC 96 ms
59,144 KB
testcase_06 AC 99 ms
58,668 KB
testcase_07 AC 95 ms
59,304 KB
testcase_08 AC 99 ms
59,352 KB
testcase_09 AC 98 ms
59,284 KB
testcase_10 AC 100 ms
58,984 KB
testcase_11 AC 117 ms
60,052 KB
testcase_12 AC 123 ms
59,380 KB
testcase_13 AC 100 ms
59,644 KB
testcase_14 AC 119 ms
59,872 KB
testcase_15 AC 138 ms
60,180 KB
testcase_16 AC 108 ms
59,320 KB
testcase_17 AC 111 ms
59,524 KB
testcase_18 AC 114 ms
59,068 KB
testcase_19 AC 155 ms
59,860 KB
testcase_20 AC 135 ms
59,624 KB
testcase_21 AC 115 ms
59,416 KB
testcase_22 AC 127 ms
60,340 KB
testcase_23 AC 124 ms
59,176 KB
testcase_24 AC 117 ms
59,700 KB
testcase_25 AC 93 ms
58,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const modpow = (a, b, n) => {
    while((b & 1n) == 0n) { b >>= 1n; a = a * a % n; }
    let r = a;
    while(true) {
        b >>= 1n;
        if(b == 0n) { return r; }
        a = a * a % n;
        if((b & 1n) != 0) { r = r * a % n; }
    }
};
const gcd = (a, b) => {
    while(b != 0n) { let t = a % b; a = b; b = t; }
    return a;
};
const genrand = (start, end) => {
    if (start >= end) { return start; }
    let diff = end - start;
    let r = 1n;
    while (r < diff) {
        r = (r << 32n) + BigInt(Math.random() * 4294967296 >>> 0);
    }
    return (start + r % diff);
}

const hrStart = process.hrtime();
let n = null;
let a = 2n;
process.stdin.setEncoding("utf8");
const reader = require("readline").createInterface({input: process.stdin});
const checkgcd = (x) => {
    let g = gcd(n, x);
    if (g > 1n && g < n) {
        console.log(`! ${g} ${n/g}`);
        const hrEnd = process.hrtime(hrStart);
        console.error(`${hrEnd[0]*1e3+hrEnd[1]/1e6}ms`);
        process.exit();
    }
}
reader.on("line", (l) => {
    if (n === null) {
        n = BigInt(l);
        checkgcd(a);
    } else {
        let r = BigInt(l);
        let t = modpow(a, r >> 1n, n);
        checkgcd(t - 1n);
        checkgcd(t + 1n);
        a = genrand(2n, n - 1n);
        checkgcd(a);
    }
    console.log(`? ${a}`);
});
0