結果

問題 No.12 限定された素数
ユーザー バカらっくバカらっく
提出日時 2019-08-31 11:18:15
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 1,776 bytes
コンパイル時間 17,074 ms
コンパイル使用メモリ 425,928 KB
実行使用メモリ 119,800 KB
最終ジャッジ日時 2023-08-15 21:41:16
合計ジャッジ時間 62,236 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,450 ms
117,600 KB
testcase_01 AC 2,142 ms
117,232 KB
testcase_02 AC 1,458 ms
113,604 KB
testcase_03 WA -
testcase_04 AC 1,468 ms
116,324 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1,568 ms
117,468 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1,389 ms
113,552 KB
testcase_18 AC 1,469 ms
116,200 KB
testcase_19 AC 1,454 ms
116,272 KB
testcase_20 AC 1,505 ms
113,220 KB
testcase_21 WA -
testcase_22 AC 1,510 ms
117,144 KB
testcase_23 AC 1,503 ms
117,712 KB
testcase_24 AC 1,488 ms
117,364 KB
testcase_25 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args: Array<String>){
         ^
Main.kt:4:9: warning: variable 'numberCount' is never used
    val numberCount = readLine()!!.toInt()
        ^
Main.kt:24:28: warning: variable 'startNum' initializer is redundant
            var startNum = 0
                           ^

ソースコード

diff #

import kotlin.math.max

fun main(args: Array<String>){
    val numberCount = readLine()!!.toInt()
    val numberList = readLine()!!.trim().split(" ").map { it.toInt() }
    val primeList = getPrimeList()
    val primeSplitedList = primeList.map { it.toString().map { it.toString().toInt() } }
    val numberMap = mutableMapOf<Int, Int>()
    (0..9).forEach{numberMap[it] = 0}
    var stattIndex = 0
    var ans = 0
    for(i in primeList.indices) {
        primeSplitedList[i].forEach {numberMap[it] = numberMap[it]!! + 1}

        while (numberMap.filter { it.value > 0 }.keys.any { !numberList.contains(it) }) {
            primeSplitedList[stattIndex].forEach { numberMap[it] = numberMap[it]!!-1 }
            stattIndex++
        }
        if(!numberList.all { numberMap.filter { it.value > 0 }.keys.contains(it) }) {
            stattIndex = i + 1
            (0..9).forEach { numberMap[it] = 0 }
        }
        if(stattIndex <= i) {
            var startNum = 0
            startNum = if(stattIndex == 0) {
                1
            } else {
                primeList[stattIndex-1] + 1
            }
            var endNum = 5000000
            if(i < primeList.lastIndex) {
                endNum = primeList[i + 1] - 1
            }
            ans = Math.max(ans, endNum - startNum)
        }
    }
    if(ans == 0) {
        ans = -1
    }
    println(ans)
}


fun getPrimeList():List<Int> {
    val max = 5000000
    val primeList = mutableListOf<Int>()
    val flags = arrayOfNulls<Boolean>(max + 1)
    for(i in 2..max) {
        if(flags[i] != null && flags[i]!!) {
            continue
        }
        val maxIndex = max / i
        for(j in 1..maxIndex) {
            flags[i * j] = true
        }
        primeList.add(i)
    }
    return primeList
}
0