結果

問題 No.577 Prime Powerful Numbers
ユーザー maspymaspy
提出日時 2020-03-25 22:46:02
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 388 ms / 2,000 ms
コード長 1,210 bytes
コンパイル時間 121 ms
コンパイル使用メモリ 10,820 KB
実行使用メモリ 8,968 KB
最終ジャッジ日時 2023-08-30 11:50:34
合計ジャッジ時間 2,119 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
8,912 KB
testcase_01 AC 38 ms
8,932 KB
testcase_02 AC 23 ms
8,912 KB
testcase_03 AC 109 ms
8,912 KB
testcase_04 AC 32 ms
8,916 KB
testcase_05 AC 324 ms
8,832 KB
testcase_06 AC 104 ms
8,868 KB
testcase_07 AC 388 ms
8,816 KB
testcase_08 AC 99 ms
8,864 KB
testcase_09 AC 90 ms
8,968 KB
testcase_10 AC 20 ms
8,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def MillerRabinTest(n, maxiter=20):
    import random
    if n == 1:
        return False
    elif n == 2:
        return True
    if not n & 1:
        return False

    d = n - 1
    while not (d & 1):
        d >>= 1

    for _ in range(maxiter):
        a = random.randint(1, n - 1)
        t = d
        x = pow(a, t, n)
        while (t != n - 1) and (x != 1) and (x != n - 1):
            x = x * x % n
            t <<= 1
        if (x != n - 1) and not (t & 1):
            return False
    return True


def is_pp(N):
    if N <= 1:
        return False
    for n in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]:
        m = int(N ** (1 / n) + .5)
        if m ** n == N:
            return is_pp(m)
    return MillerRabinTest(N)


def solve(N):
    if N == 2:
        return False
    if N % 2 == 0:
        return True
    for n in range(1, 64):
        x = 1 << n
        if x >= N:
            return False
        if is_pp(N - x):
            return True


Q, *N = map(int, read().split())
print('\n'.join('Yes' if solve(n) else 'No' for n in N))
0