結果
問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
ユーザー |
![]() |
提出日時 | 2020-01-14 18:29:48 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 4,930 ms / 9,973 ms |
コード長 | 814 bytes |
コンパイル時間 | 38 ms |
コンパイル使用メモリ | 7,552 KB |
実行使用メモリ | 12,288 KB |
最終ジャッジ日時 | 2024-11-16 23:21:46 |
合計ジャッジ時間 | 13,095 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
コンパイルメッセージ
Syntax OK
ソースコード
def modular_exponentiation(base, n, mod) res = 1 while n > 0 if n[0] == 1 res = (res * base) % mod end base = (base ** 2) % mod n >>= 1 end res end def witness(a, n) v = n - 1 t = 0 while v % 2 == 0 t += 1 v /= 2 end u = n / (2 ** t) x = modular_exponentiation(a, u, n) t.times do bx = x x = (x ** 2) % n if x == 1 && bx != 1 && bx != n - 1 return true end end return true if x != 1 false end def miller_rabin(n, s = 10) return false if n <= 1 return true if n == 2 return false if n.even? s.times do a = rand(n - 1) + 1 return false if witness(a, n) end true end N = gets.to_i N.times do x = gets.to_i if miller_rabin(x) puts [x, 1].join(' ') else puts [x, 0].join(' ') end end