結果

問題 No.308 素数は通れません
ユーザー iwashi31iwashi31
提出日時 2015-12-01 22:46:16
言語 Python2
(2.7.18)
結果
AC  
実行時間 15 ms / 1,000 ms
コード長 1,496 bytes
コンパイル時間 65 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2023-11-27 09:04:36
合計ジャッジ時間 4,210 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,040 KB
testcase_01 AC 15 ms
7,040 KB
testcase_02 AC 14 ms
7,040 KB
testcase_03 AC 14 ms
7,040 KB
testcase_04 AC 13 ms
7,040 KB
testcase_05 AC 14 ms
7,040 KB
testcase_06 AC 13 ms
7,040 KB
testcase_07 AC 14 ms
7,040 KB
testcase_08 AC 13 ms
7,040 KB
testcase_09 AC 13 ms
7,040 KB
testcase_10 AC 14 ms
7,040 KB
testcase_11 AC 14 ms
7,040 KB
testcase_12 AC 14 ms
7,040 KB
testcase_13 AC 13 ms
7,040 KB
testcase_14 AC 14 ms
7,040 KB
testcase_15 AC 14 ms
7,040 KB
testcase_16 AC 13 ms
7,040 KB
testcase_17 AC 14 ms
7,040 KB
testcase_18 AC 14 ms
7,040 KB
testcase_19 AC 14 ms
7,040 KB
testcase_20 AC 14 ms
7,040 KB
testcase_21 AC 14 ms
7,040 KB
testcase_22 AC 14 ms
7,040 KB
testcase_23 AC 14 ms
7,040 KB
testcase_24 AC 14 ms
7,040 KB
testcase_25 AC 13 ms
7,040 KB
testcase_26 AC 13 ms
7,040 KB
testcase_27 AC 14 ms
7,040 KB
testcase_28 AC 14 ms
7,040 KB
testcase_29 AC 14 ms
7,040 KB
testcase_30 AC 13 ms
7,040 KB
testcase_31 AC 14 ms
7,040 KB
testcase_32 AC 14 ms
7,040 KB
testcase_33 AC 14 ms
7,040 KB
testcase_34 AC 14 ms
7,040 KB
testcase_35 AC 14 ms
7,040 KB
testcase_36 AC 14 ms
7,040 KB
testcase_37 AC 13 ms
7,040 KB
testcase_38 AC 14 ms
7,040 KB
testcase_39 AC 14 ms
7,040 KB
testcase_40 AC 14 ms
7,040 KB
testcase_41 AC 14 ms
7,040 KB
testcase_42 AC 14 ms
7,040 KB
testcase_43 AC 13 ms
7,040 KB
testcase_44 AC 14 ms
7,040 KB
testcase_45 AC 14 ms
7,040 KB
testcase_46 AC 14 ms
7,040 KB
testcase_47 AC 13 ms
7,040 KB
testcase_48 AC 13 ms
7,040 KB
testcase_49 AC 14 ms
7,040 KB
testcase_50 AC 14 ms
7,040 KB
testcase_51 AC 13 ms
7,040 KB
testcase_52 AC 14 ms
7,040 KB
testcase_53 AC 13 ms
7,040 KB
testcase_54 AC 14 ms
7,040 KB
testcase_55 AC 13 ms
7,040 KB
testcase_56 AC 14 ms
7,040 KB
testcase_57 AC 13 ms
7,040 KB
testcase_58 AC 14 ms
7,040 KB
testcase_59 AC 14 ms
7,040 KB
testcase_60 AC 14 ms
7,040 KB
testcase_61 AC 14 ms
7,040 KB
testcase_62 AC 14 ms
7,040 KB
testcase_63 AC 14 ms
7,040 KB
testcase_64 AC 13 ms
7,040 KB
testcase_65 AC 14 ms
7,040 KB
testcase_66 AC 14 ms
7,040 KB
testcase_67 AC 15 ms
7,040 KB
testcase_68 AC 14 ms
7,040 KB
testcase_69 AC 14 ms
7,040 KB
testcase_70 AC 14 ms
7,040 KB
testcase_71 AC 14 ms
7,040 KB
testcase_72 AC 14 ms
7,040 KB
testcase_73 AC 13 ms
7,040 KB
testcase_74 AC 14 ms
7,040 KB
testcase_75 AC 13 ms
7,040 KB
testcase_76 AC 14 ms
7,040 KB
testcase_77 AC 13 ms
7,040 KB
testcase_78 AC 13 ms
7,040 KB
testcase_79 AC 14 ms
7,040 KB
testcase_80 AC 13 ms
7,040 KB
testcase_81 AC 13 ms
7,040 KB
testcase_82 AC 14 ms
7,040 KB
testcase_83 AC 14 ms
7,040 KB
testcase_84 AC 13 ms
7,040 KB
testcase_85 AC 14 ms
7,040 KB
testcase_86 AC 13 ms
7,040 KB
testcase_87 AC 14 ms
7,040 KB
testcase_88 AC 13 ms
7,040 KB
testcase_89 AC 14 ms
7,040 KB
testcase_90 AC 13 ms
7,040 KB
testcase_91 AC 14 ms
7,040 KB
testcase_92 AC 14 ms
7,040 KB
testcase_93 AC 13 ms
7,040 KB
testcase_94 AC 14 ms
7,040 KB
testcase_95 AC 13 ms
7,040 KB
testcase_96 AC 14 ms
7,040 KB
testcase_97 AC 14 ms
7,040 KB
testcase_98 AC 13 ms
7,040 KB
testcase_99 AC 14 ms
7,040 KB
testcase_100 AC 13 ms
7,040 KB
testcase_101 AC 14 ms
7,040 KB
testcase_102 AC 13 ms
7,040 KB
testcase_103 AC 13 ms
7,040 KB
testcase_104 AC 13 ms
7,040 KB
testcase_105 AC 13 ms
7,040 KB
testcase_106 AC 14 ms
7,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from random import seed, randrange

def modpow(base, power, mod):
    result = 1
    while power > 0:
        if power & 1 == 1:
            result = (result * base) % mod
        base = (base * base) % mod
        power >>= 1
    return result

def isPrime(n):
    if n % 2 == 0:
        return False
    
    def MR_core(d, a, s = 1):
        if s == 1:
            return MR_core(d >> 1, a, 2) == n
        elif s == 2:
            if d & 1:
                m = MR_core(d >> 1, a, 3)
                s = m * m % n * a % n
                return n if s == n - 1 or s == 1 else s
            else:
                m = MR_core(d >> 1, a, 2)
                if m == n:
                    return n
                else:
                    s = m * m % n
                    return n if s == n - 1 else s
        elif s == 3:
            if d == 0:
                return 1
            else:
                m = MR_core(d >> 1, a, 3)
                if d & 1:
                    return m * m % n * a % n
                else:
                    return m * m % n
    
    k = 3
    return all(MR_core(n - 1, randrange(2, n)) for i in range(k))

n = input()
umekomi = [ 1, 1, 2, 3, 3, 5, 3, 7, 7, 7, \
            5,11,11,13, 7, 7, 7, 8, 8,19, \
           19, 7, 7,23,23, 8, 8, 8, 7, 8]
if n <= 30:
    print umekomi[n-1]
    sys.exit()

m = 8
while True:
    if isPrime(m+1) == False:
        if n%m != 1 or isPrime(n-m) == False:
            print m
            sys.exit()
    m += 2
0