結果

問題 No.2 素因数ゲーム
コンテスト
ユーザー バカらっく
提出日時 2019-08-24 07:10:56
言語 Kotlin
(2.3.20)
コンパイル:
kotlinc _filename_ -include-runtime -d main.jar
実行:
kotlin main.jar
結果
AC  
実行時間 371 ms / 5,000 ms
コード長 1,561 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 10,174 ms
コンパイル使用メモリ 478,864 KB
実行使用メモリ 56,876 KB
最終ジャッジ日時 2026-05-14 22:43:09
合計ジャッジ時間 17,480 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.util.*
import kotlin.math.sqrt

fun main(args: Array<String>){
    var num = readLine()!!.toInt()
    var ans = "Bob"
    if(isWin(divPrime(num))) {
        ans = "Alice"
    }
    println(ans)
}

val dictionary = mutableMapOf<Int,MutableMap<String,Boolean>>()
fun isWin(primeList:List<Int>):Boolean {
    if(primeList.size <= 0) {
        return false
    }
    if(!dictionary.containsKey(primeList.size)) {
        dictionary[primeList.size] = mutableMapOf()
    }
    val key = getKey(primeList)
    if(dictionary[primeList.size]!!.containsKey(key)) {
        return dictionary[primeList.size]!![key]!!
    }
    var canWin = false
    for(i in primeList.indices) {
        for(j in 1..primeList[i]) {
            var sublist = primeList.toMutableList()
            sublist[i] -= j
            if(sublist[i] <= 0) {
                sublist.removeAt(i)
            }
            sublist = sublist.sortedDescending().toMutableList()
            if(!isWin(sublist)) {
                canWin = true
                break
            }
        }
    }
    dictionary[primeList.size]!![key] = canWin
    return canWin
}

fun getKey(list: List<Int>):String {
    return list.joinToString("-")
}

fun divPrime(num:Int):List<Int> {
    var temp = num
    var div = 2
    val list = mutableListOf<Int>()
    while (temp > 1) {
        var cnt = 0
        while (temp % div == 0) {
            cnt++
            temp = (temp / div)
        }
        if(cnt > 0) {
            list.add(cnt)
        }
        div++
    }
    return list.sortedDescending()
}
0