結果

問題 No.308 素数は通れません
ユーザー chocoruskchocorusk
提出日時 2019-03-31 10:31:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 41 ms / 1,000 ms
コード長 1,374 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,444 KB
実行使用メモリ 56,500 KB
最終ジャッジ日時 2024-05-01 05:11:49
合計ジャッジ時間 6,722 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,368 KB
testcase_01 AC 36 ms
54,500 KB
testcase_02 AC 36 ms
55,144 KB
testcase_03 AC 36 ms
54,820 KB
testcase_04 AC 35 ms
55,044 KB
testcase_05 AC 35 ms
55,652 KB
testcase_06 AC 36 ms
54,552 KB
testcase_07 AC 35 ms
54,368 KB
testcase_08 AC 34 ms
55,336 KB
testcase_09 AC 34 ms
54,912 KB
testcase_10 AC 34 ms
55,416 KB
testcase_11 AC 34 ms
55,788 KB
testcase_12 AC 35 ms
54,760 KB
testcase_13 AC 35 ms
55,172 KB
testcase_14 AC 35 ms
55,020 KB
testcase_15 AC 36 ms
55,892 KB
testcase_16 AC 35 ms
55,060 KB
testcase_17 AC 35 ms
54,992 KB
testcase_18 AC 40 ms
55,632 KB
testcase_19 AC 40 ms
54,520 KB
testcase_20 AC 37 ms
55,124 KB
testcase_21 AC 36 ms
56,268 KB
testcase_22 AC 36 ms
54,576 KB
testcase_23 AC 36 ms
55,108 KB
testcase_24 AC 36 ms
55,968 KB
testcase_25 AC 39 ms
55,512 KB
testcase_26 AC 38 ms
56,152 KB
testcase_27 AC 36 ms
54,312 KB
testcase_28 AC 35 ms
55,352 KB
testcase_29 AC 34 ms
54,620 KB
testcase_30 AC 36 ms
55,056 KB
testcase_31 AC 34 ms
55,224 KB
testcase_32 AC 35 ms
55,724 KB
testcase_33 AC 38 ms
55,380 KB
testcase_34 AC 39 ms
54,952 KB
testcase_35 AC 38 ms
55,024 KB
testcase_36 AC 37 ms
55,460 KB
testcase_37 AC 37 ms
55,500 KB
testcase_38 AC 36 ms
54,696 KB
testcase_39 AC 37 ms
54,824 KB
testcase_40 AC 35 ms
54,868 KB
testcase_41 AC 36 ms
55,772 KB
testcase_42 AC 36 ms
55,984 KB
testcase_43 AC 35 ms
55,192 KB
testcase_44 AC 35 ms
55,276 KB
testcase_45 AC 35 ms
55,168 KB
testcase_46 AC 36 ms
55,860 KB
testcase_47 AC 35 ms
55,800 KB
testcase_48 AC 34 ms
54,972 KB
testcase_49 AC 35 ms
55,176 KB
testcase_50 AC 34 ms
54,972 KB
testcase_51 AC 36 ms
54,764 KB
testcase_52 AC 36 ms
55,392 KB
testcase_53 AC 35 ms
55,632 KB
testcase_54 AC 38 ms
55,416 KB
testcase_55 AC 37 ms
54,452 KB
testcase_56 AC 38 ms
55,000 KB
testcase_57 AC 37 ms
54,620 KB
testcase_58 AC 37 ms
55,568 KB
testcase_59 AC 36 ms
54,756 KB
testcase_60 AC 37 ms
55,764 KB
testcase_61 AC 37 ms
55,696 KB
testcase_62 AC 37 ms
55,172 KB
testcase_63 AC 39 ms
54,816 KB
testcase_64 AC 39 ms
55,820 KB
testcase_65 AC 38 ms
55,084 KB
testcase_66 AC 36 ms
54,436 KB
testcase_67 AC 38 ms
54,572 KB
testcase_68 AC 36 ms
55,268 KB
testcase_69 AC 34 ms
56,196 KB
testcase_70 AC 35 ms
54,760 KB
testcase_71 AC 34 ms
55,464 KB
testcase_72 AC 36 ms
55,272 KB
testcase_73 AC 35 ms
55,460 KB
testcase_74 AC 37 ms
56,128 KB
testcase_75 AC 36 ms
55,580 KB
testcase_76 AC 35 ms
54,840 KB
testcase_77 AC 36 ms
56,500 KB
testcase_78 AC 36 ms
54,884 KB
testcase_79 AC 36 ms
55,136 KB
testcase_80 AC 36 ms
54,620 KB
testcase_81 AC 40 ms
54,780 KB
testcase_82 AC 39 ms
55,828 KB
testcase_83 AC 38 ms
54,728 KB
testcase_84 AC 36 ms
55,032 KB
testcase_85 AC 36 ms
54,308 KB
testcase_86 AC 36 ms
55,368 KB
testcase_87 AC 38 ms
55,264 KB
testcase_88 AC 36 ms
55,244 KB
testcase_89 AC 38 ms
55,676 KB
testcase_90 AC 37 ms
55,288 KB
testcase_91 AC 36 ms
54,700 KB
testcase_92 AC 37 ms
55,788 KB
testcase_93 AC 36 ms
56,180 KB
testcase_94 AC 37 ms
55,892 KB
testcase_95 AC 38 ms
55,000 KB
testcase_96 AC 36 ms
56,120 KB
testcase_97 AC 34 ms
54,840 KB
testcase_98 AC 38 ms
54,360 KB
testcase_99 AC 37 ms
54,548 KB
testcase_100 AC 37 ms
55,452 KB
testcase_101 AC 36 ms
55,396 KB
testcase_102 AC 35 ms
54,456 KB
testcase_103 AC 36 ms
55,148 KB
testcase_104 AC 41 ms
54,308 KB
testcase_105 AC 39 ms
54,764 KB
testcase_106 AC 37 ms
55,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random
import sys
def is_prime(n):
    if n == 2: return True
    if n == 1 or n & 1 == 0: return False

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

    for k in range(10):
        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 = (y * y) % n
            t <<= 1

        if y != n - 1 and t & 1 == 0:
            return False

    return True

n=int(input())
used=[False for i in range(102)]
isprime=[False for i in range(102)]
for i in range(3, 102, 2):
    isprime[i]=True
isprime[2]=True
for i in range(3, 102):
    if isprime[i]:
        for j in range(2*i, 102, i):
            isprime[j]=False

def dfs(x, w):
    used[x]=True
    if x%w!=1 and (not used[x-1]) and (not isprime[x-1]):
        dfs(x-1, w)
    if x%w!=0 and x+1<=n and (not used[x+1]) and (not isprime[x+1]):
        dfs(x+1, w)
    if x>w and (not used[x-w]) and (not isprime[x-w]):
        dfs(x-w, w)
    if x+w<=n and (not used[x+w]) and (not isprime[x+w]):
        dfs(x+w, w)
def solve(w):
    for i in range(1, n+1):
        used[i]=False
    dfs(1, w)
    return used[n]

if n<=100:
    for i in range(3, n):
        if solve(i):
            print(i)
            sys.exit()
else:
    if n%8!=1:
        print(8)
    elif not is_prime(n-8):
        print(8)
    else:
        print(14)
0