結果

問題 No.308 素数は通れません
ユーザー maspy
提出日時 2020-01-02 17:54:35
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 33 ms / 1,000 ms
コード長 1,003 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-11-22 18:13:47
合計ジャッジ時間 5,459 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 107
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(read())

def MillerRabinTest(n, maxiter = 10):
    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 F(N):
    A = [
        0,0,0,0,3,0,5,0,7,7, # 0 ~ 9
        7,0,11,0,13,7,7,0,8,0, # ~19
        19,19,7,0,23,23,8,8,8
    ]
    if N < 30:
        return A[N]
    for x in [8,14]:
        if N % x != 1:
            return x
        if not MillerRabinTest(N - x):
            return x
    raise Exception # ないはず

answer = F(N)
print(answer)
0