結果

問題 No.308 素数は通れません
コンテスト
ユーザー ctyl_0
提出日時 2015-12-03 03:14:02
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 182 ms / 1,000 ms
コード長 634 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 277 ms
コンパイル使用メモリ 77,460 KB
最終ジャッジ日時 2025-12-03 18:13:39
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 107
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# -*- coding: utf-8 -*-
import random

N = int(input())
    
def isPrime(n, k = 50):
    d = (n - 1) >> 1
    while d & 1 == 0:
        d >>= 1
    for i in range(k):
        a = random.randint(1, n - 1)
        t = d
        y = pow(a, t, n)
        while t != n - 1 and y != 1 and y != n - 1: 
            y = pow(y, 2, n)
            t <<= 1
        if y != n - 1 and t & 1 == 0:
            return False
    return True

A = [0, 0, 0, 0, 3, 0, 5, 0, 7, 7, 7, 0, 11, 0, 13, 7, 7, 0, 8, 0, 19, 19, 7, 0, 23, 23]

if N < 26:
    print A[N]
else:
    if N % 8 != 1:
        print 8
    else:
        print 14 if isPrime(N - 8) else 8
0