結果

問題 No.458 異なる素数の和
ユーザー cra77756176cra77756176
提出日時 2022-12-19 22:16:05
言語 Rust
(1.77.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 900 bytes
コンパイル時間 3,801 ms
コンパイル使用メモリ 149,156 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 10:03:22
合計ジャッジ時間 6,082 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 29 ms
4,376 KB
testcase_02 AC 38 ms
4,376 KB
testcase_03 AC 7 ms
4,376 KB
testcase_04 AC 9 ms
4,380 KB
testcase_05 AC 83 ms
4,376 KB
testcase_06 AC 36 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 84 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 101 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 34 ms
4,376 KB
testcase_28 AC 97 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 21 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn get_primes(max: usize) -> Vec<usize> {
    let mut is_prime = vec![true; max + 1];
    is_prime[0] = false;
    is_prime[1] = false;

    for n in 2..=max {
        if !is_prime[n] {
            continue;
        }
        if n * n > max {
            break;
        }
        for i in ((n * n)..=max).step_by(n) {
            is_prime[i] = false;
        }
    }

    (0..=max).filter(|&n| is_prime[n]).collect()
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();

    let primes = get_primes(n);
    let mut dp = vec![None; n + 1];
    dp[0] = Some(0);

    for &p in &primes {
        for i in (p..=n).rev() {
            if let Some(k) = dp[i - p] {
                dp[i] = Some(dp[i].unwrap_or(0).max(k + 1));
            }
        }
    }

    dp[n].map_or_else(|| println!("-1"), |k| println!("{k}"));
}
0