結果

問題 No.308 素数は通れません
ユーザー kimiyukikimiyuki
提出日時 2015-12-01 20:25:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,146 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 11,016 KB
実行使用メモリ 8,660 KB
最終ジャッジ日時 2023-10-12 06:45:11
合計ジャッジ時間 5,580 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,156 KB
testcase_01 AC 16 ms
8,448 KB
testcase_02 AC 16 ms
8,492 KB
testcase_03 AC 15 ms
8,472 KB
testcase_04 AC 16 ms
8,476 KB
testcase_05 AC 16 ms
8,644 KB
testcase_06 AC 15 ms
8,476 KB
testcase_07 AC 16 ms
8,504 KB
testcase_08 AC 16 ms
8,436 KB
testcase_09 AC 16 ms
8,540 KB
testcase_10 AC 16 ms
8,468 KB
testcase_11 AC 16 ms
8,596 KB
testcase_12 AC 16 ms
8,504 KB
testcase_13 AC 16 ms
8,488 KB
testcase_14 AC 16 ms
8,472 KB
testcase_15 AC 16 ms
8,444 KB
testcase_16 AC 16 ms
8,592 KB
testcase_17 AC 16 ms
8,492 KB
testcase_18 AC 16 ms
8,540 KB
testcase_19 AC 16 ms
8,548 KB
testcase_20 AC 16 ms
8,448 KB
testcase_21 AC 16 ms
8,436 KB
testcase_22 AC 15 ms
8,540 KB
testcase_23 AC 16 ms
8,476 KB
testcase_24 AC 16 ms
8,616 KB
testcase_25 AC 15 ms
8,476 KB
testcase_26 AC 15 ms
8,656 KB
testcase_27 AC 16 ms
8,520 KB
testcase_28 AC 16 ms
8,596 KB
testcase_29 AC 16 ms
8,520 KB
testcase_30 AC 16 ms
8,588 KB
testcase_31 AC 16 ms
8,508 KB
testcase_32 AC 16 ms
8,188 KB
testcase_33 AC 16 ms
8,584 KB
testcase_34 AC 16 ms
8,156 KB
testcase_35 AC 16 ms
8,460 KB
testcase_36 AC 16 ms
8,480 KB
testcase_37 AC 16 ms
8,452 KB
testcase_38 AC 16 ms
8,468 KB
testcase_39 AC 16 ms
8,660 KB
testcase_40 AC 16 ms
8,500 KB
testcase_41 AC 15 ms
8,516 KB
testcase_42 AC 16 ms
8,468 KB
testcase_43 AC 16 ms
8,512 KB
testcase_44 AC 15 ms
8,588 KB
testcase_45 AC 17 ms
8,472 KB
testcase_46 AC 17 ms
8,660 KB
testcase_47 AC 15 ms
8,452 KB
testcase_48 AC 16 ms
8,524 KB
testcase_49 AC 16 ms
8,440 KB
testcase_50 AC 16 ms
8,436 KB
testcase_51 AC 16 ms
8,592 KB
testcase_52 AC 16 ms
8,600 KB
testcase_53 AC 17 ms
8,592 KB
testcase_54 AC 17 ms
8,556 KB
testcase_55 AC 16 ms
8,600 KB
testcase_56 AC 16 ms
8,480 KB
testcase_57 AC 16 ms
8,452 KB
testcase_58 AC 17 ms
8,504 KB
testcase_59 AC 21 ms
8,532 KB
testcase_60 TLE -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
testcase_93 -- -
testcase_94 -- -
testcase_95 -- -
testcase_96 -- -
testcase_97 -- -
testcase_98 -- -
testcase_99 -- -
testcase_100 -- -
testcase_101 -- -
testcase_102 -- -
testcase_103 -- -
testcase_104 -- -
testcase_105 -- -
testcase_106 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True
def bfs(n, w, initial, accept, deny=None):
    que = [initial]
    i = 0
    pushed = set(que)
    while i < len(que):
        if accept(que, i):
            return True
        if deny is not None and deny(que, i):
            return False
        x = que[i]
        i += 1
        def f(y):
            if y not in pushed and not is_prime(y):
                que.append(y)
                pushed.add(y)
        if x-1 >  0 and x % w != 1: f(x-1)
        if x+1 <= n and x % w != 0: f(x+1)
        if x-w >  0:                f(x-w)
        if x+w <= n:                f(x+w)
def solve(n):
    if n < 300: # magic number
        for w in range(1,n):
            if bfs(n, w, 1, lambda que, i: que[i] == n):
                return w
    else:
        for w in range(2,n):
            if bfs(n, w, 1, lambda que, i: que[i] % w == 0) \
                    and bfs(n, w, n, lambda que, i: len(que) > 80): # magic number
                return w
print(solve(int(input())))
0