結果

問題 No.3056 量子コンピュータで素因数分解 Easy
ユーザー 👑 tatyamtatyam
提出日時 2020-02-04 19:29:50
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,194 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 11,076 KB
実行使用メモリ 25,168 KB
平均クエリ数 2.27
最終ジャッジ日時 2023-08-30 06:44:01
合計ジャッジ時間 4,455 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
24,476 KB
testcase_01 AC 66 ms
25,068 KB
testcase_02 AC 178 ms
24,640 KB
testcase_03 AC 179 ms
25,052 KB
testcase_04 AC 69 ms
24,544 KB
testcase_05 AC 70 ms
24,888 KB
testcase_06 AC 73 ms
25,024 KB
testcase_07 AC 72 ms
25,100 KB
testcase_08 AC 76 ms
24,704 KB
testcase_09 AC 76 ms
24,960 KB
testcase_10 AC 79 ms
25,168 KB
testcase_11 AC 79 ms
24,424 KB
testcase_12 AC 88 ms
24,608 KB
testcase_13 AC 82 ms
25,020 KB
testcase_14 AC 101 ms
24,436 KB
testcase_15 AC 110 ms
25,024 KB
testcase_16 AC 105 ms
25,048 KB
testcase_17 AC 105 ms
24,676 KB
testcase_18 AC 132 ms
24,704 KB
testcase_19 AC 120 ms
24,580 KB
testcase_20 AC 140 ms
25,076 KB
testcase_21 AC 106 ms
25,060 KB
testcase_22 AC 103 ms
24,760 KB
testcase_23 AC 122 ms
24,676 KB
testcase_24 AC 113 ms
25,048 KB
testcase_25 AC 59 ms
25,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from random import randint
from sys import exit
from math import gcd
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139]
def suspect(a, s, d, n):
    x = pow(a, d, n)
    if x == 1:
        return True
    for _ in range(s):
        if x == n - 1:
            return True
        x = x * x % n
    return False
def is_prime(n):
    if n <= 1:
        return False
    if n <= 140:
        return n in p
    if (n & 1) == 0:
        return False
    d = n - 1
    s = 0
    while (d & 1) == 0:
        s += 1
        d >>= 1
    for i in p:
        if not suspect(i, s, d, n):
            return False
    return True
def answer(p):
    q = n // p
    assert is_prime(p) and is_prime(q)
    for x, r in query:
        assert pow(x, r, n) == 1
    print('!', p, q)
    exit(0)
n = int(input())
query = []
assert n <= 1 << 1024 and n & 1
while True:
    x = randint(2, n - 2)
    if gcd(n, x) > 1:
        answer(gcd(n, x))
    print('?', x, flush = True)
    r = int(input())
    query.append((x, r))
    if r % 2 == 0:
        x = pow(x, r // 2, n) + 1
        if x != n:
            answer(gcd(n, x))
0