結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー surumeika1987surumeika1987
提出日時 2020-11-19 17:37:58
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 1,130 bytes
コンパイル時間 14,560 ms
コンパイル使用メモリ 440,500 KB
実行使用メモリ 92,112 KB
最終ジャッジ日時 2024-04-29 14:35:12
合計ジャッジ時間 19,441 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 265 ms
50,132 KB
testcase_01 AC 255 ms
50,056 KB
testcase_02 AC 276 ms
50,164 KB
testcase_03 AC 282 ms
50,392 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger
import kotlin.random.Random

fun main() {
    val n_num = readLine()!!.toInt()
    repeat(n_num) {
        val bg = readLine()!!.toBigInteger()
        val isp = IsPrime.Miller_Rabin.isPrime(bg)
        println(if (isp) "${bg} 1" else "${bg} 0")
    }
}

class IsPrime {
    companion object Miller_Rabin {
        fun isPrime(num: BigInteger) : Boolean{
            if (1.toBigInteger() == num) return false
            if (2.toBigInteger() == num) return true
            if (num.mod(2.toBigInteger()) == BigInteger.ZERO) return false
            var s = 0
            var t = num - 1.toBigInteger()
            while (t.and(1.toBigInteger()) == BigInteger.ZERO) {
                s = s + 1
                t = t.shr(1)
            }
            val a = Random.nextLong(num.minus(1.toBigInteger()).min(Long.MAX_VALUE.toBigInteger()).toLong()).toBigInteger()
            if (a.modPow(t, num) == 1.toBigInteger()) return true
            for (i in 0..s-1)
                if (a.modPow(2.toBigInteger().pow(i), num) == num.minus(1.toBigInteger())) return true

            return false
        }
    }
}
0