結果

問題 No.12 限定された素数
ユーザー バカらっくバカらっく
提出日時 2019-08-31 10:54:23
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 1,764 bytes
コンパイル時間 17,605 ms
コンパイル使用メモリ 423,812 KB
実行使用メモリ 92,924 KB
最終ジャッジ日時 2023-08-15 21:04:08
合計ジャッジ時間 54,360 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,043 ms
88,244 KB
testcase_01 AC 1,248 ms
90,112 KB
testcase_02 WA -
testcase_03 AC 1,160 ms
83,820 KB
testcase_04 AC 1,128 ms
88,104 KB
testcase_05 AC 1,251 ms
89,772 KB
testcase_06 AC 1,326 ms
88,520 KB
testcase_07 AC 1,397 ms
88,176 KB
testcase_08 WA -
testcase_09 AC 1,638 ms
88,572 KB
testcase_10 WA -
testcase_11 AC 1,911 ms
92,924 KB
testcase_12 AC 1,654 ms
88,432 KB
testcase_13 AC 1,299 ms
90,468 KB
testcase_14 WA -
testcase_15 AC 1,276 ms
90,320 KB
testcase_16 AC 1,425 ms
90,336 KB
testcase_17 AC 1,486 ms
86,024 KB
testcase_18 AC 1,086 ms
86,916 KB
testcase_19 AC 1,084 ms
86,940 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1,110 ms
86,764 KB
testcase_23 AC 1,108 ms
86,372 KB
testcase_24 AC 1,079 ms
85,868 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:23: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 numberMap = mutableMapOf<Int, Int>()
    var stattIndex = 0
    var ans = 0
    for(i in primeList.indices) {
        primeList[i].toString().map { it.toString().toInt() }.forEach {
            val key = it
            numberMap[key]?.also { numberMap[key] = it+1 } ?: run { numberMap[key] = 1 }
        }
        while (numberMap.filter { it.value > 0 }.any { !numberList.contains(it.key) }) {
            primeList[stattIndex].toString().map { it.toString().toInt() }.forEach { numberMap[it] = numberMap[it]!!-1 }
            stattIndex++
        }
        if(!numberMap.filter { it.value > 0 }.all { numberList.contains(it.key) }) {
            stattIndex = i + 1
        }
        if(stattIndex <= i) {
            var startNum = 0
            if(stattIndex == 0) {
                startNum = 1
            } else {
                startNum = 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