結果

問題 No.458 異なる素数の和
ユーザー phsplsphspls
提出日時 2020-07-17 17:15:27
言語 Rust
(1.77.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 3,538 ms
コンパイル使用メモリ 143,044 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 10:26:12
合計ジャッジ時間 5,317 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 19 ms
4,384 KB
testcase_02 AC 24 ms
4,380 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 53 ms
4,380 KB
testcase_06 AC 23 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 52 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 63 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 22 ms
4,376 KB
testcase_28 AC 62 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 14 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::max;

fn primes(n: usize) -> Vec<usize> {
    let mut flgs: Vec<bool> = vec![true; n+1];
    flgs[0] = false;
    flgs[1] = false;
    let limit: usize = (n as f64).sqrt().ceil() as usize + 1;
    for i in 0..limit {
        if !flgs[i] { continue; }
        for j in i..=n/i {
            flgs[j*i] = false;
        }
    }
    flgs.iter().enumerate()
        .filter(|&pair| *pair.1)
        .map(|pair| pair.0)
        .collect()
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    if n == 1 {
        println!("-1");
        return;
    }

    let primes: Vec<usize> = primes(n);
    let mut dp: Vec<usize> = vec![0; n+1];
    for &p in primes.iter() {
        for i in (0..=n-p).rev() {
            if dp[i] > 0 && i + p <= n {
                dp[i + p] = max(dp[i] + 1, dp[i+p]);
            }
        }
        dp[p] = max(dp[p], 1);
    }
    println!("{}", if dp[n] == 0 { -1 } else { dp[n] as isize });
}
0