結果

問題 No.12 限定された素数
ユーザー バカらっく
提出日時 2019-08-31 11:22:45
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 2,229 ms / 5,000 ms
コード長 1,674 bytes
コンパイル時間 16,538 ms
コンパイル使用メモリ 447,624 KB
実行使用メモリ 151,528 KB
最終ジャッジ日時 2024-11-24 10:18:52
合計ジャッジ時間 49,571 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
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:20: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(stattIndex <= i && numberList.all { numberMap.filter { it.value > 0 }.keys.contains(it) }) {
            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