結果

問題 No.2 素因数ゲーム
ユーザー バカらっくバカらっく
提出日時 2019-08-24 06:49:10
言語 Kotlin
(1.9.23)
結果
RE  
実行時間 -
コード長 2,112 bytes
コンパイル時間 14,304 ms
コンパイル使用メモリ 445,088 KB
実行使用メモリ 291,108 KB
最終ジャッジ日時 2024-04-30 21:21:45
合計ジャッジ時間 44,391 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 325 ms
57,016 KB
testcase_01 AC 330 ms
56,936 KB
testcase_02 AC 349 ms
60,412 KB
testcase_03 AC 356 ms
60,484 KB
testcase_04 AC 351 ms
60,468 KB
testcase_05 AC 383 ms
62,396 KB
testcase_06 AC 1,133 ms
147,552 KB
testcase_07 AC 1,263 ms
151,564 KB
testcase_08 AC 1,222 ms
145,356 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 3,191 ms
278,900 KB
testcase_12 AC 3,177 ms
278,896 KB
testcase_13 RE -
testcase_14 AC 839 ms
118,032 KB
testcase_15 AC 1,839 ms
201,080 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 3,369 ms
287,104 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 2,041 ms
211,508 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 3,389 ms
291,108 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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