結果

問題 No.308 素数は通れません
ユーザー matsu7874matsu7874
提出日時 2015-11-19 10:18:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,475 bytes
コンパイル時間 861 ms
コンパイル使用メモリ 11,088 KB
実行使用メモリ 16,252 KB
最終ジャッジ日時 2023-10-11 18:03:48
合計ジャッジ時間 6,177 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
16,252 KB
testcase_01 AC 21 ms
9,268 KB
testcase_02 AC 21 ms
9,216 KB
testcase_03 AC 21 ms
9,088 KB
testcase_04 AC 21 ms
9,088 KB
testcase_05 AC 21 ms
9,224 KB
testcase_06 AC 21 ms
9,032 KB
testcase_07 AC 21 ms
9,088 KB
testcase_08 AC 22 ms
9,092 KB
testcase_09 AC 21 ms
9,312 KB
testcase_10 AC 21 ms
9,132 KB
testcase_11 AC 22 ms
9,084 KB
testcase_12 AC 21 ms
9,144 KB
testcase_13 AC 21 ms
9,212 KB
testcase_14 AC 21 ms
9,088 KB
testcase_15 AC 21 ms
9,088 KB
testcase_16 AC 21 ms
9,180 KB
testcase_17 AC 21 ms
9,032 KB
testcase_18 AC 21 ms
9,264 KB
testcase_19 AC 21 ms
9,096 KB
testcase_20 AC 21 ms
9,216 KB
testcase_21 AC 21 ms
9,092 KB
testcase_22 AC 22 ms
9,092 KB
testcase_23 AC 21 ms
9,028 KB
testcase_24 AC 20 ms
9,100 KB
testcase_25 AC 21 ms
9,260 KB
testcase_26 AC 21 ms
9,096 KB
testcase_27 AC 21 ms
9,092 KB
testcase_28 AC 21 ms
9,136 KB
testcase_29 AC 21 ms
9,088 KB
testcase_30 AC 22 ms
9,228 KB
testcase_31 AC 21 ms
9,032 KB
testcase_32 AC 21 ms
9,216 KB
testcase_33 AC 21 ms
9,148 KB
testcase_34 AC 21 ms
9,216 KB
testcase_35 AC 22 ms
9,100 KB
testcase_36 AC 25 ms
9,240 KB
testcase_37 AC 26 ms
9,100 KB
testcase_38 AC 26 ms
9,100 KB
testcase_39 AC 27 ms
9,164 KB
testcase_40 AC 29 ms
9,248 KB
testcase_41 AC 31 ms
9,260 KB
testcase_42 AC 32 ms
9,128 KB
testcase_43 AC 33 ms
9,140 KB
testcase_44 AC 34 ms
9,356 KB
testcase_45 AC 37 ms
9,360 KB
testcase_46 AC 41 ms
9,160 KB
testcase_47 AC 48 ms
9,184 KB
testcase_48 AC 48 ms
9,296 KB
testcase_49 AC 72 ms
9,396 KB
testcase_50 AC 81 ms
9,352 KB
testcase_51 AC 87 ms
9,400 KB
testcase_52 AC 151 ms
9,544 KB
testcase_53 AC 153 ms
9,500 KB
testcase_54 AC 171 ms
9,524 KB
testcase_55 AC 173 ms
9,476 KB
testcase_56 TLE -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
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 #

import collections
import random

def prime_sieve(n):
    is_prime = [True for i in range(n + 1)]
    is_prime[0] = False
    is_prime[1] = False
    for i in range(4, n + 1, 2):
        is_prime[i] = False
    for i in range(3, int(n**0.5 + 1), 2):
        if is_prime[i]:
            for j in range(i * i, n + 1, i):
                is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]


def miller_rabin_test(n, k=1000):
    if n == 2:
        return True
    if n < 2 or n & 1 == 0:
        return False
    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


N = int(input())
primes = prime_sieve(N)
maze = [i not in primes for i in range(N+1)]
for w in range(3, N):
    dq = collections.deque()
    dq.append(1)
    visited = [False] * (N + 1)
    visited[1] = True
    while dq:
        v = dq.popleft()
        if v == N:
            break
        for d in [w, 1, -1, -w]:
            if 0 < v + d <= N and not visited[v + d] and maze[v + d]:
                if d in [1, -1] and (v - 1) // w != (v - 1 + d) // w:
                    continue
                dq.append(v + d)
                visited[v + d] = True
    if visited[N]:
        print(w)
        exit()
0