結果

問題 No.308 素数は通れません
ユーザー tjaketjake
提出日時 2015-12-01 03:18:52
言語 Python2
(2.7.18)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,262 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 6,756 KB
実行使用メモリ 9,412 KB
最終ジャッジ日時 2023-10-12 07:07:47
合計ジャッジ時間 5,667 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
9,268 KB
testcase_01 AC 17 ms
9,152 KB
testcase_02 AC 17 ms
9,312 KB
testcase_03 AC 17 ms
9,080 KB
testcase_04 AC 18 ms
9,228 KB
testcase_05 AC 18 ms
9,196 KB
testcase_06 AC 17 ms
9,352 KB
testcase_07 AC 17 ms
9,144 KB
testcase_08 AC 17 ms
9,320 KB
testcase_09 AC 17 ms
9,200 KB
testcase_10 AC 17 ms
9,116 KB
testcase_11 AC 17 ms
9,264 KB
testcase_12 AC 17 ms
9,152 KB
testcase_13 AC 17 ms
9,196 KB
testcase_14 AC 18 ms
9,260 KB
testcase_15 AC 17 ms
9,288 KB
testcase_16 AC 16 ms
9,308 KB
testcase_17 AC 17 ms
9,332 KB
testcase_18 AC 16 ms
9,196 KB
testcase_19 AC 17 ms
9,248 KB
testcase_20 AC 17 ms
9,336 KB
testcase_21 AC 17 ms
9,148 KB
testcase_22 AC 17 ms
9,148 KB
testcase_23 AC 17 ms
9,260 KB
testcase_24 AC 17 ms
9,100 KB
testcase_25 AC 18 ms
9,076 KB
testcase_26 AC 17 ms
9,196 KB
testcase_27 AC 17 ms
9,096 KB
testcase_28 AC 17 ms
9,248 KB
testcase_29 AC 17 ms
9,252 KB
testcase_30 AC 17 ms
9,148 KB
testcase_31 AC 17 ms
9,152 KB
testcase_32 AC 18 ms
9,096 KB
testcase_33 AC 17 ms
9,200 KB
testcase_34 AC 17 ms
9,256 KB
testcase_35 AC 17 ms
9,100 KB
testcase_36 AC 17 ms
9,112 KB
testcase_37 AC 17 ms
9,160 KB
testcase_38 AC 17 ms
9,264 KB
testcase_39 AC 19 ms
9,260 KB
testcase_40 AC 19 ms
9,260 KB
testcase_41 AC 17 ms
9,212 KB
testcase_42 AC 17 ms
9,324 KB
testcase_43 AC 18 ms
9,328 KB
testcase_44 AC 19 ms
9,312 KB
testcase_45 AC 17 ms
9,108 KB
testcase_46 AC 18 ms
9,208 KB
testcase_47 AC 17 ms
9,244 KB
testcase_48 AC 19 ms
9,160 KB
testcase_49 AC 19 ms
9,328 KB
testcase_50 AC 17 ms
9,260 KB
testcase_51 AC 16 ms
9,160 KB
testcase_52 AC 18 ms
9,272 KB
testcase_53 AC 16 ms
9,272 KB
testcase_54 AC 16 ms
9,268 KB
testcase_55 AC 16 ms
9,236 KB
testcase_56 AC 17 ms
9,244 KB
testcase_57 AC 16 ms
9,156 KB
testcase_58 AC 17 ms
9,208 KB
testcase_59 AC 17 ms
9,156 KB
testcase_60 AC 16 ms
9,208 KB
testcase_61 AC 17 ms
9,160 KB
testcase_62 AC 16 ms
9,116 KB
testcase_63 AC 17 ms
9,160 KB
testcase_64 AC 31 ms
9,268 KB
testcase_65 AC 17 ms
9,144 KB
testcase_66 AC 16 ms
9,244 KB
testcase_67 AC 16 ms
9,096 KB
testcase_68 AC 17 ms
9,220 KB
testcase_69 AC 16 ms
9,112 KB
testcase_70 AC 43 ms
9,284 KB
testcase_71 AC 17 ms
9,336 KB
testcase_72 AC 16 ms
9,148 KB
testcase_73 AC 16 ms
9,284 KB
testcase_74 AC 16 ms
9,284 KB
testcase_75 AC 16 ms
9,268 KB
testcase_76 AC 18 ms
9,332 KB
testcase_77 AC 16 ms
9,332 KB
testcase_78 AC 17 ms
9,160 KB
testcase_79 AC 16 ms
9,144 KB
testcase_80 AC 19 ms
9,336 KB
testcase_81 AC 26 ms
9,288 KB
testcase_82 AC 16 ms
9,276 KB
testcase_83 AC 17 ms
9,280 KB
testcase_84 AC 27 ms
9,216 KB
testcase_85 AC 16 ms
9,260 KB
testcase_86 AC 29 ms
9,168 KB
testcase_87 AC 31 ms
9,100 KB
testcase_88 AC 16 ms
9,336 KB
testcase_89 AC 16 ms
9,160 KB
testcase_90 AC 39 ms
9,340 KB
testcase_91 AC 17 ms
9,164 KB
testcase_92 AC 17 ms
9,160 KB
testcase_93 AC 16 ms
9,208 KB
testcase_94 AC 17 ms
9,208 KB
testcase_95 AC 16 ms
9,256 KB
testcase_96 AC 16 ms
9,212 KB
testcase_97 AC 17 ms
9,116 KB
testcase_98 AC 16 ms
9,316 KB
testcase_99 AC 17 ms
9,164 KB
testcase_100 WA -
testcase_101 AC 41 ms
9,168 KB
testcase_102 AC 17 ms
9,264 KB
testcase_103 AC 16 ms
9,332 KB
testcase_104 AC 17 ms
9,092 KB
testcase_105 AC 16 ms
9,112 KB
testcase_106 AC 16 ms
9,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from random import randint
from collections import deque
from math import sqrt
n = input()

M = min(1003, n)
prime = [1]*(M+1)
prime[0] = prime[1] = 0
p_num = 0
for i in xrange(2, int(sqrt(M))+1):
    if prime[i]:
        for j in xrange(i*i, M+1, i):
            prime[j] = 0

def fermat(k):
    for t in xrange(1000):
        a = randint(2, k-1)
        if pow(a, k-1, k)!=1:
            return 0
    return 1

i = 2
while i<=40:
    deq = deque()
    deq.append(1)
    used = [0]*(M+1)
    ok = 0
    while deq:
        v = deq.popleft()
        if n>M and v%i==0:
            ok = 1
            break
        if v==n:
            print i
            exit(0)
        if v+1 <= M and v%i!=0 and not prime[v+1] and not used[v+1]:
            used[v+1] = 1
            deq.append(v+1)
        if v-1 >= 1 and v%i!=1 and not prime[v-1] and not used[v-1]:
            used[v-1] = 1
            deq.append(v-1)
        if v+i <= M and not prime[v+i] and not used[v+i]:
            used[v+i] = 1
            deq.append(v+i)
        if v-i >= 1 and not prime[v-i] and not used[v-i]:
            used[v-i] = 1
            deq.append(v-i)
    if ok:
        if (n%i!=1 and not fermat(n-1)) or not fermat(n-i):
            print i
            exit(0)
    i += 1
exit(1)
0