結果

問題 No.308 素数は通れません
ユーザー chocoruskchocorusk
提出日時 2019-03-31 10:24:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 58 ms / 1,000 ms
コード長 1,463 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 69,132 KB
最終ジャッジ日時 2024-05-01 05:07:11
合計ジャッジ時間 7,592 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
61,164 KB
testcase_01 AC 45 ms
62,052 KB
testcase_02 AC 46 ms
62,072 KB
testcase_03 AC 46 ms
61,700 KB
testcase_04 AC 45 ms
61,328 KB
testcase_05 AC 45 ms
62,424 KB
testcase_06 AC 46 ms
61,404 KB
testcase_07 AC 47 ms
62,208 KB
testcase_08 AC 46 ms
62,404 KB
testcase_09 AC 49 ms
61,728 KB
testcase_10 AC 45 ms
61,404 KB
testcase_11 AC 46 ms
62,556 KB
testcase_12 AC 47 ms
62,544 KB
testcase_13 AC 46 ms
62,280 KB
testcase_14 AC 46 ms
61,788 KB
testcase_15 AC 45 ms
62,012 KB
testcase_16 AC 46 ms
62,076 KB
testcase_17 AC 46 ms
61,984 KB
testcase_18 AC 46 ms
61,328 KB
testcase_19 AC 48 ms
62,240 KB
testcase_20 AC 45 ms
62,116 KB
testcase_21 AC 47 ms
62,164 KB
testcase_22 AC 47 ms
62,716 KB
testcase_23 AC 47 ms
62,792 KB
testcase_24 AC 47 ms
62,080 KB
testcase_25 AC 45 ms
61,512 KB
testcase_26 AC 46 ms
62,728 KB
testcase_27 AC 46 ms
62,172 KB
testcase_28 AC 45 ms
61,728 KB
testcase_29 AC 45 ms
61,820 KB
testcase_30 AC 45 ms
61,824 KB
testcase_31 AC 45 ms
61,588 KB
testcase_32 AC 47 ms
63,272 KB
testcase_33 AC 47 ms
61,360 KB
testcase_34 AC 46 ms
61,948 KB
testcase_35 AC 47 ms
61,712 KB
testcase_36 AC 49 ms
63,308 KB
testcase_37 AC 48 ms
62,792 KB
testcase_38 AC 49 ms
63,304 KB
testcase_39 AC 47 ms
63,152 KB
testcase_40 AC 47 ms
64,736 KB
testcase_41 AC 48 ms
63,004 KB
testcase_42 AC 58 ms
69,132 KB
testcase_43 AC 47 ms
62,912 KB
testcase_44 AC 46 ms
62,608 KB
testcase_45 AC 45 ms
62,180 KB
testcase_46 AC 46 ms
61,276 KB
testcase_47 AC 45 ms
62,668 KB
testcase_48 AC 47 ms
62,500 KB
testcase_49 AC 45 ms
62,336 KB
testcase_50 AC 46 ms
61,748 KB
testcase_51 AC 44 ms
61,984 KB
testcase_52 AC 45 ms
62,716 KB
testcase_53 AC 45 ms
61,648 KB
testcase_54 AC 47 ms
61,032 KB
testcase_55 AC 45 ms
62,412 KB
testcase_56 AC 45 ms
61,540 KB
testcase_57 AC 45 ms
62,560 KB
testcase_58 AC 45 ms
61,560 KB
testcase_59 AC 45 ms
62,256 KB
testcase_60 AC 45 ms
62,236 KB
testcase_61 AC 46 ms
62,800 KB
testcase_62 AC 46 ms
63,032 KB
testcase_63 AC 48 ms
61,664 KB
testcase_64 AC 45 ms
61,752 KB
testcase_65 AC 45 ms
61,484 KB
testcase_66 AC 45 ms
62,204 KB
testcase_67 AC 45 ms
61,856 KB
testcase_68 AC 45 ms
61,976 KB
testcase_69 AC 46 ms
62,464 KB
testcase_70 AC 45 ms
62,140 KB
testcase_71 AC 46 ms
61,648 KB
testcase_72 AC 46 ms
61,436 KB
testcase_73 AC 46 ms
63,084 KB
testcase_74 AC 46 ms
62,852 KB
testcase_75 AC 47 ms
62,688 KB
testcase_76 AC 45 ms
61,756 KB
testcase_77 AC 47 ms
61,428 KB
testcase_78 AC 46 ms
63,084 KB
testcase_79 AC 46 ms
62,196 KB
testcase_80 AC 46 ms
61,684 KB
testcase_81 AC 54 ms
62,812 KB
testcase_82 AC 46 ms
63,316 KB
testcase_83 AC 45 ms
61,544 KB
testcase_84 AC 45 ms
61,620 KB
testcase_85 AC 45 ms
61,824 KB
testcase_86 AC 45 ms
61,876 KB
testcase_87 AC 46 ms
61,652 KB
testcase_88 AC 45 ms
61,856 KB
testcase_89 AC 46 ms
62,756 KB
testcase_90 AC 47 ms
61,800 KB
testcase_91 AC 46 ms
61,804 KB
testcase_92 AC 46 ms
62,696 KB
testcase_93 AC 47 ms
61,772 KB
testcase_94 AC 47 ms
62,580 KB
testcase_95 AC 47 ms
61,344 KB
testcase_96 AC 46 ms
61,304 KB
testcase_97 AC 45 ms
62,680 KB
testcase_98 AC 46 ms
61,356 KB
testcase_99 AC 46 ms
62,740 KB
testcase_100 AC 46 ms
62,476 KB
testcase_101 AC 46 ms
62,536 KB
testcase_102 AC 46 ms
63,252 KB
testcase_103 AC 46 ms
62,764 KB
testcase_104 AC 46 ms
61,456 KB
testcase_105 AC 46 ms
63,216 KB
testcase_106 AC 46 ms
61,364 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(2002)]
isprime=[False for i in range(2002)]
for i in range(3, 2002, 2):
    isprime[i]=True
isprime[2]=True
for i in range(3, 2002):
    if isprime[i]:
        for j in range(2*i, 2002, 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<=2000:
    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)
    elif n%14!=1:
        print(14)
    elif not is_prime(n-14):
        print(14)
    else:
        print(17)
0