結果

問題 No.2 素因数ゲーム
ユーザー バカらっく
提出日時 2019-08-24 06:49:10
言語 Kotlin
(2.1.0)
結果
RE  
実行時間 -
コード長 2,112 bytes
コンパイル時間 15,662 ms
コンパイル使用メモリ 442,920 KB
実行使用メモリ 289,984 KB
最終ジャッジ日時 2024-11-20 19:21:29
合計ジャッジ時間 41,289 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16 RE * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:4:10: warning: parameter 'args' is never used
fun main(args: Array<String>){
         ^

ソースコード

diff #

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> {
    val primeList = getPrime(num)
    if(primeList.contains(num)) {
        return mutableListOf(1)
    }
    val list = mutableListOf<Int>()
    var temp = num
    for(prime in primeList) {
        if(temp < prime) {
            break
        }
        var cnt = 0
        while (temp % prime == 0) {
            cnt++
            temp = temp / prime
        }
        if(cnt  > 0) {
            if(cnt <= 2) {
                list.add(cnt)
            } else  {
                list.add(3)
            }
        }
    }
    return list.sortedDescending()
}

fun getPrime(num:Int):List<Int> {
    val ret = mutableListOf<Int>()
    val flags = Array<Boolean>(num + 1) {false}
    for(i in 2..num) {
        if(flags[i]) {
            continue
        }
        for(j in 1..num/i) {
            flags[j*i] = true
        }
        ret.add(i)
    }
    return ret.toList()
}
0