結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
Golf13
|
| 提出日時 | 2025-08-05 23:19:25 |
| 言語 | Scala(Beta) (3.6.2) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,308 bytes |
| コンパイル時間 | 15,621 ms |
| コンパイル使用メモリ | 285,044 KB |
| 実行使用メモリ | 290,024 KB |
| 最終ジャッジ日時 | 2025-08-05 23:21:01 |
| 合計ジャッジ時間 | 88,294 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 6 |
ソースコード
import scala.util.boundary
import scala.util.boundary.break
import scala.io.StdIn
def powMod(a: Long, b: Long, m: Long): Long =
var r = 1L
var base = a % m
var exp = b
while (exp > 0) {
if ((exp & 1) == 1) {
r = (r * base) % m
}
base = (base * base) % m
exp = exp >>> 1
}
r
def millerRabin(n: Long, bases: Seq[Long]): Boolean =
boundary[Boolean] {
var d = n - 1
var s = 0
while ((d & 1) == 0) {
d /= 2
s += 1
}
for (base <- bases) {
if (n <= base) {
break(true)
}
var a = powMod(base, d, n)
if (a != 1) {
var r = 1
while (a != n - 1) {
if (r == s) {
break(false)
}
r += 1
a = powMod(a, 2, n)
}
}
}
true
}
def isPrime(n: Long): Boolean =
if (n <= 1) {
false
} else if (n <= 3) {
true
} else if ((n & 1) == 0) {
false
} else if (n < 4759123141L) {
val bases = Seq(2L, 7L, 61L)
millerRabin(n, bases)
} else {
val bases = Seq(2L, 325L, 9375L, 28178L, 450775L, 9780504L, 1795265022L)
millerRabin(n, bases)
}
@main def main(): Unit =
val n = StdIn.readInt()
for (_ <- 1 to n) {
val x = StdIn.readLong()
val p = if (isPrime(x)) 1 else 0
println(s"$x $p")
}
Golf13