結果

問題 No.103 素因数ゲーム リターンズ
ユーザー バカらっくバカらっく
提出日時 2019-09-30 09:07:44
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 2,112 bytes
コンパイル時間 14,003 ms
コンパイル使用メモリ 455,616 KB
実行使用メモリ 137,964 KB
最終ジャッジ日時 2024-04-14 07:59:33
合計ジャッジ時間 33,617 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 642 ms
80,568 KB
testcase_01 AC 643 ms
114,700 KB
testcase_02 AC 643 ms
83,912 KB
testcase_03 WA -
testcase_04 AC 648 ms
76,992 KB
testcase_05 AC 640 ms
76,852 KB
testcase_06 WA -
testcase_07 AC 646 ms
76,892 KB
testcase_08 AC 648 ms
76,876 KB
testcase_09 AC 657 ms
76,832 KB
testcase_10 AC 660 ms
76,972 KB
testcase_11 WA -
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'arr' is never used
fun main(arr:Array<String>) {
         ^
Main.kt:4:9: warning: variable 'itemCount' is never used
    val itemCount = readLine()!!.toInt()
        ^

ソースコード

diff #

import kotlin.math.max

fun main(arr:Array<String>) {
    val itemCount = readLine()!!.toInt()
    val numberList = readLine()!!.split(" ").map { convDic[it.toInt()] }.sorted()
    val ans = if(canWin(numberList.sorted())) "Alice" else "Bob"
    println(ans)
}

val dic = mutableMapOf<String, Boolean>()
fun canWin(numberList:List<Int>):Boolean {
    if(numberList.isEmpty() || numberList.all { it == 1 }) {
        return false
    }
    val key = numberList.joinToString("-")
    dic[key]?.let { return it }

    val maxIndex = numberList.lastIndex
    var canWin = false
    for(i in (0..maxIndex)) {
        if(numberList[i] == 1) {
            continue
        }
        val prime = getPrimeDivide(numberList[i]).toMutableMap()
        for(p in prime.keys) {
            for(j in (1..Math.min(2,prime[p]!!))) {
                val dv = Math.pow(p.toDouble(), j.toDouble()).toInt()
                val newList = numberList.toMutableList()
                newList[i] = numberList[i] / dv
                if(!canWin(newList.sorted())) {
                    canWin = true
                    break
                }
            }
            if(canWin) {
                break
            }
        }
        if(canWin) {
            break
        }
    }
    dic[key] = canWin
    return canWin
}

val primeDic = (0..10000).map { getPrimeDivide(it) }
val convDic = (0..10000).map { getNewValue(primeDic[it]) }
fun getNewValue(prime: Map<Int,Int>):Int {
    val list = prime.map { Math.pow(it.key.toDouble(), it.value.toDouble()).toInt() }
    if(prime.isEmpty()) {
        return 1
    }
    val ans = list.reduce { acc, i -> acc * i }
    return ans
}
fun getPrimeDivide(num:Int):Map<Int, Int> {
    var div = 1
    var current = num
    var ret = mutableMapOf<Int, Int>()
    while (current > 1) {
        div++
        var cnt = 0
        while (current % div == 0) {
            cnt++
            current /= div
        }
        if(cnt > 0) {
            ret[div] = cnt % 3
        }
    }
    ret = (2 until 2 + ret.count()).map { it to ret.values.sorted()[it-2]}.toMap().toMutableMap()
    return ret
}
0