結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー miscalcmiscalc
提出日時 2022-04-04 01:23:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 600 ms / 9,973 ms
コード長 552 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 77,440 KB
最終ジャッジ日時 2024-04-28 09:50:07
合計ジャッジ時間 3,078 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
51,840 KB
testcase_01 AC 41 ms
51,584 KB
testcase_02 AC 42 ms
52,224 KB
testcase_03 AC 44 ms
52,352 KB
testcase_04 AC 387 ms
77,440 KB
testcase_05 AC 372 ms
77,056 KB
testcase_06 AC 220 ms
77,184 KB
testcase_07 AC 220 ms
77,440 KB
testcase_08 AC 214 ms
76,800 KB
testcase_09 AC 600 ms
77,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def isprime(n):
  if n == 1:
    return False
  aa = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
  for a in aa:
    if n == a:
      return True
    if n % a == 0:
      return False
  
  d, s = n - 1, 0
  while d % 2 == 0:
    d >>= 1
    s += 1
  
  for a in aa:
    a0 = pow(a, d, n)
    if a0 == 1 or a0 == n - 1:
      continue
    for r in range(1, s):
      a0 = a0 * a0 % n
      if a0 == n - 1:
        break
    else:
      return False
  return True

t = int(input())
for _ in range(t):
  x = int(input())
  print(x, 1 if isprime(x) else 0)
0