結果

問題 No.7 プライムナンバーゲーム
ユーザー komkomh
提出日時 2019-04-26 13:21:37
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 1,025 ms / 5,000 ms
コード長 755 bytes
コンパイル時間 14,421 ms
コンパイル使用メモリ 432,524 KB
実行使用メモリ 152,044 KB
最終ジャッジ日時 2024-10-01 16:21:58
合計ジャッジ時間 24,843 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder

fun main() {
    val n = readLine()!!.toInt()

    val primeNumbers: List<Int> = (2..n).filter { isPrimeNumber(it) }
    val cache: MutableMap<Int, Boolean> = mutableMapOf()

    fun isWin(currentNumber: Int): Boolean {
        if (!cache.contains(currentNumber)) {
            val result = primeNumbers
                .filter { it < currentNumber }
                .map { currentNumber - it }
                .filter { it > 1 }
                .any { !isWin(it) }
            cache.put(currentNumber, result)
        }
        return cache.get(currentNumber)!!
    }

    when (isWin(n)) {
        true -> println("Win")
        false -> println("Lose")
    }
}

fun isPrimeNumber(n: Int): Boolean = !(2 until n).any { n % it == 0 }

0