結果

問題 No.8114 Prime Checker+1
ユーザー 👑 KA37RI
提出日時 2025-02-21 21:22:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,031 bytes
コンパイル時間 647 ms
コンパイル使用メモリ 82,648 KB
実行使用メモリ 56,208 KB
最終ジャッジ日時 2025-02-21 21:22:10
合計ジャッジ時間 3,523 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

# modified from https://judge.yosupo.jp/submission/205424
from random import randrange


def is_prime(n):
    if n < 2:
        return False
    if n == 2 or n == 3:
        return True
    if n % 2 == 0:
        return False

    s, d = 0, n - 1
    while d % 2 == 0:
        d >>= 1
        s += 1

    bases = []
    while len(bases) < 3:
        a = randrange(2, n - 1)
        if a >= n:
            continue
        bases.append(a)

    for a in bases:
        x = pow(a, d, n)
        if x == 1 or x == n - 1:
            continue

        for _ in range(s - 1):
            x = pow(x, 2, n)
            if x == n - 1:
                break
        else:
            return False
    return True


def main():
    import io, os

    # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

    S = input()
    if len(S) <= 18:
        N = int(S)
        f = 1 - (N % 2)
        N += f
        ans = "Yes" if is_prime(N) else "No"
    else:
    	f = int(S[-1]) % 2
    	ans = "No"
    print(f)
    print(ans)


main()
0