結果

問題 No.308 素数は通れません
ユーザー Min_25Min_25
提出日時 2015-12-01 03:38:29
言語 Python2
(2.7.18)
結果
AC  
実行時間 13 ms / 1,000 ms
コード長 928 bytes
コンパイル時間 127 ms
コンパイル使用メモリ 6,592 KB
実行使用メモリ 6,244 KB
最終ジャッジ日時 2023-08-18 12:57:51
合計ジャッジ時間 5,066 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
5,856 KB
testcase_01 AC 13 ms
5,900 KB
testcase_02 AC 13 ms
5,960 KB
testcase_03 AC 12 ms
5,944 KB
testcase_04 AC 12 ms
5,940 KB
testcase_05 AC 12 ms
5,924 KB
testcase_06 AC 11 ms
5,896 KB
testcase_07 AC 12 ms
5,928 KB
testcase_08 AC 12 ms
5,964 KB
testcase_09 AC 11 ms
5,944 KB
testcase_10 AC 12 ms
5,940 KB
testcase_11 AC 12 ms
5,856 KB
testcase_12 AC 11 ms
5,964 KB
testcase_13 AC 12 ms
5,980 KB
testcase_14 AC 11 ms
5,856 KB
testcase_15 AC 12 ms
5,940 KB
testcase_16 AC 12 ms
5,944 KB
testcase_17 AC 12 ms
5,892 KB
testcase_18 AC 12 ms
5,856 KB
testcase_19 AC 12 ms
5,940 KB
testcase_20 AC 12 ms
5,980 KB
testcase_21 AC 12 ms
6,056 KB
testcase_22 AC 12 ms
5,896 KB
testcase_23 AC 12 ms
5,960 KB
testcase_24 AC 12 ms
6,024 KB
testcase_25 AC 12 ms
5,868 KB
testcase_26 AC 12 ms
6,088 KB
testcase_27 AC 12 ms
6,068 KB
testcase_28 AC 12 ms
6,020 KB
testcase_29 AC 11 ms
5,948 KB
testcase_30 AC 12 ms
5,960 KB
testcase_31 AC 13 ms
5,928 KB
testcase_32 AC 12 ms
5,980 KB
testcase_33 AC 12 ms
5,900 KB
testcase_34 AC 13 ms
6,068 KB
testcase_35 AC 12 ms
5,948 KB
testcase_36 AC 12 ms
5,864 KB
testcase_37 AC 12 ms
5,872 KB
testcase_38 AC 11 ms
5,960 KB
testcase_39 AC 11 ms
5,872 KB
testcase_40 AC 12 ms
6,004 KB
testcase_41 AC 12 ms
5,964 KB
testcase_42 AC 12 ms
5,864 KB
testcase_43 AC 12 ms
5,980 KB
testcase_44 AC 12 ms
5,872 KB
testcase_45 AC 12 ms
6,068 KB
testcase_46 AC 12 ms
5,980 KB
testcase_47 AC 12 ms
5,928 KB
testcase_48 AC 12 ms
5,944 KB
testcase_49 AC 12 ms
5,964 KB
testcase_50 AC 13 ms
5,944 KB
testcase_51 AC 12 ms
5,892 KB
testcase_52 AC 12 ms
5,896 KB
testcase_53 AC 12 ms
5,928 KB
testcase_54 AC 12 ms
6,064 KB
testcase_55 AC 12 ms
5,940 KB
testcase_56 AC 13 ms
6,024 KB
testcase_57 AC 11 ms
5,896 KB
testcase_58 AC 11 ms
6,096 KB
testcase_59 AC 12 ms
5,900 KB
testcase_60 AC 12 ms
5,976 KB
testcase_61 AC 12 ms
5,860 KB
testcase_62 AC 11 ms
5,944 KB
testcase_63 AC 11 ms
5,924 KB
testcase_64 AC 11 ms
5,896 KB
testcase_65 AC 11 ms
5,924 KB
testcase_66 AC 12 ms
5,980 KB
testcase_67 AC 11 ms
5,944 KB
testcase_68 AC 12 ms
6,116 KB
testcase_69 AC 11 ms
5,984 KB
testcase_70 AC 12 ms
6,244 KB
testcase_71 AC 12 ms
6,088 KB
testcase_72 AC 12 ms
6,116 KB
testcase_73 AC 11 ms
5,988 KB
testcase_74 AC 12 ms
6,132 KB
testcase_75 AC 12 ms
6,084 KB
testcase_76 AC 12 ms
5,948 KB
testcase_77 AC 12 ms
5,964 KB
testcase_78 AC 12 ms
5,948 KB
testcase_79 AC 12 ms
5,924 KB
testcase_80 AC 11 ms
5,924 KB
testcase_81 AC 12 ms
6,064 KB
testcase_82 AC 12 ms
5,868 KB
testcase_83 AC 12 ms
6,000 KB
testcase_84 AC 12 ms
5,960 KB
testcase_85 AC 11 ms
5,868 KB
testcase_86 AC 12 ms
5,868 KB
testcase_87 AC 12 ms
6,004 KB
testcase_88 AC 12 ms
5,872 KB
testcase_89 AC 12 ms
5,964 KB
testcase_90 AC 13 ms
5,864 KB
testcase_91 AC 12 ms
5,996 KB
testcase_92 AC 12 ms
6,068 KB
testcase_93 AC 11 ms
6,152 KB
testcase_94 AC 13 ms
5,992 KB
testcase_95 AC 12 ms
6,160 KB
testcase_96 AC 12 ms
5,976 KB
testcase_97 AC 13 ms
6,016 KB
testcase_98 AC 13 ms
5,992 KB
testcase_99 AC 13 ms
6,032 KB
testcase_100 AC 12 ms
6,084 KB
testcase_101 AC 12 ms
6,016 KB
testcase_102 AC 12 ms
6,116 KB
testcase_103 AC 13 ms
6,068 KB
testcase_104 AC 12 ms
6,148 KB
testcase_105 AC 12 ms
6,064 KB
testcase_106 AC 12 ms
6,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def prob308():
  def witness(base, s, d, n):
    base = pow(base, d, n)
    if base == 1 or base == n - 1:
      return True
    for _ in range(s - 1):
      base = base * base % n
      if base == n - 1:
        return True
    return False

  def miller_rabin(n):
    if n <= 1:
      return False
    if n <= 3:
      return True

    d = n - 1
    s = d & -d
    s = s.bit_length() - 1
    d >>= s

    if n < 3317044064679887385961981:
      bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
    else:
      assert(0)

    for base in bases:
      if n % base == 0:
        return n == base
      if not witness(base, s, d, n):
        return False
    return True

  n = int(sys.stdin.readline())
  pre = [0, 0, 0, 0, 3, 0, 5, 0, 7, 7, 7, 0, 11, 0, 13, 7, 7, 0, 8, 0, 19, 19, 7, 0, 23, 23]
  ans = pre[n] if n < len(pre) else (8 if n % 8 != 1 or not miller_rabin(n - 8) else 14)
  print(ans)

prob308()
0