結果

問題 No.458 異なる素数の和
ユーザー htkbhtkb
提出日時 2018-06-04 22:18:46
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 597 ms / 2,000 ms
コード長 820 bytes
コンパイル時間 13,937 ms
コンパイル使用メモリ 445,708 KB
実行使用メモリ 57,984 KB
最終ジャッジ日時 2024-04-30 18:35:13
合計ジャッジ時間 26,468 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 345 ms
57,856 KB
testcase_01 AC 431 ms
57,760 KB
testcase_02 AC 447 ms
57,908 KB
testcase_03 AC 399 ms
57,860 KB
testcase_04 AC 401 ms
57,876 KB
testcase_05 AC 543 ms
57,892 KB
testcase_06 AC 453 ms
57,920 KB
testcase_07 AC 352 ms
57,748 KB
testcase_08 AC 573 ms
57,892 KB
testcase_09 AC 413 ms
57,984 KB
testcase_10 AC 338 ms
55,236 KB
testcase_11 AC 597 ms
57,920 KB
testcase_12 AC 354 ms
57,700 KB
testcase_13 AC 337 ms
55,260 KB
testcase_14 AC 353 ms
57,748 KB
testcase_15 AC 352 ms
57,848 KB
testcase_16 AC 401 ms
57,784 KB
testcase_17 AC 353 ms
57,748 KB
testcase_18 AC 356 ms
57,668 KB
testcase_19 AC 356 ms
57,804 KB
testcase_20 AC 352 ms
57,644 KB
testcase_21 AC 355 ms
57,760 KB
testcase_22 AC 350 ms
57,772 KB
testcase_23 AC 352 ms
57,732 KB
testcase_24 AC 351 ms
57,744 KB
testcase_25 AC 367 ms
57,776 KB
testcase_26 AC 355 ms
57,724 KB
testcase_27 AC 455 ms
57,900 KB
testcase_28 AC 594 ms
57,900 KB
testcase_29 AC 405 ms
57,892 KB
testcase_30 AC 434 ms
57,784 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^

ソースコード

diff #

import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val N = sc.nextInt()
    when (N) {
        1 -> { println(-1); return }
        2, 3 -> { println(1); return }
    }
    val primes = mutableListOf<Int>(2, 3)
    for (i in 5 until N step 6) primes.add(i)
    for (i in 7 until N step 6) primes.add(i)
    for (i in 5 until N step 6) {
        if (primes.contains(i)) {
            for (j in i*3 until N step i*2) primes.remove(j)
        }
        if (primes.contains(i+2)) {
            for (j in i*3+6 until N step i*2+4) primes.remove(j)
        }
    }

    val dp = Array(N+1, {0})
    for (n in primes) {
        for (i in N-n downTo 0) if (dp[i] > 0) dp[i+n] = Math.max(dp[i+n], dp[i]+1)
        dp[n] = Math.max(dp[n], 1)
    }
    println(if (dp[N] > 0) dp[N] else -1)
}
0