結果

問題 No.36 素数が嫌い!
ユーザー Min_25Min_25
提出日時 2014-10-29 06:06:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,226 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 11,064 KB
実行使用メモリ 9,072 KB
最終ジャッジ日時 2023-08-28 23:00:32
合計ジャッジ時間 2,174 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,824 KB
testcase_01 AC 19 ms
8,900 KB
testcase_02 AC 19 ms
8,896 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 19 ms
9,012 KB
testcase_08 AC 19 ms
8,960 KB
testcase_09 AC 19 ms
8,956 KB
testcase_10 AC 19 ms
8,844 KB
testcase_11 AC 19 ms
9,068 KB
testcase_12 AC 19 ms
8,828 KB
testcase_13 WA -
testcase_14 AC 19 ms
8,964 KB
testcase_15 AC 19 ms
9,004 KB
testcase_16 AC 19 ms
9,072 KB
testcase_17 AC 19 ms
8,836 KB
testcase_18 AC 19 ms
8,952 KB
testcase_19 AC 19 ms
8,956 KB
testcase_20 AC 19 ms
8,984 KB
testcase_21 AC 19 ms
8,900 KB
testcase_22 AC 19 ms
8,896 KB
testcase_23 AC 19 ms
9,012 KB
testcase_24 AC 19 ms
8,888 KB
testcase_25 AC 19 ms
8,944 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import random

def miller_rabin_pass(base, s, d, n, pow=pow):
  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 < 316349281:
    if n < 1373653:
      bases = [2, 3]
    else:
      bases = [11000544, 31481107]
  elif n < 154639673381:
    if n < 4759123141:
      bases = [2, 7, 61]
    else:
      bases = [15, 176006322, 4221622697]
  elif n < 47636622961201:
    bases = [2, 2570940, 211991001, 3749873356]
  elif n < 3770579582154547:
    bases = [2, 2570940, 880937, 610386380, 4130785767]
  elif n < (1 << 64):
    bases = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
  else:
    bases = [random.randrange(n - 1) + 1 for _ in range(30)]

  for base in bases:
    if not miller_rabin_pass(base, s, d, n):
      return False
  return True

def solve():
  stdin = sys.stdin
  N = int(stdin.readline())
  print("YES" if not miller_rabin(N) and N != 1 else "NO")

solve()
0