結果

問題 No.458 異なる素数の和
ユーザー pekempeypekempey
提出日時 2018-02-27 00:34:46
言語 Rust
(1.77.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 1,111 bytes
コンパイル時間 18,221 ms
コンパイル使用メモリ 377,240 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-04 00:25:54
合計ジャッジ時間 12,641 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

const INF: i32 = 1010101010;

fn main() {
  use std::cmp::max;
  let n: usize = input();
  
  let p = primes(n + 1);
  let m = p.len();

  let mut dp = Box::new([-INF; 20001]);
  dp[0] = 0;

  for i in 0..m {
    for j in (0 .. n - p[i] + 1).rev() {
      dp[j + p[i]] = max(dp[j + p[i]], dp[j] + 1);
    }
  }

  if dp[n] >= 0 {
    println!("{}", dp[n]);
  } else {
    println!("-1");
  }
}

fn primes(n: usize) -> Vec<usize> {
  let mut res = Vec::new();
  let mut table = vec![true; n];
  for i in 2..n {
    if table[i] {
      res.push(i);
      let mut j = i * 2;
      while j < n {
        table[j] = false;
        j += i;
      }
    }
  }
  res
}

fn input<T: std::str::FromStr>() -> T {
  use std::io::Read;
  let stdin = std::io::stdin();
  let stdin = stdin.bytes();
  let token = stdin
    .skip_while(|x| (*x.as_ref().unwrap() as char).is_whitespace())
    .take_while(|x| !(*x.as_ref().unwrap() as char).is_whitespace())
    .map(|x| x.unwrap())
    .collect();
  let token = String::from_utf8(token).unwrap();
  match token.parse() {
    Ok(x) => x,
    Err(_) => panic!("{}", token),
  }
}
0