結果

問題 No.1667 Forest
ユーザー phsplsphspls
提出日時 2022-10-30 19:40:35
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 676 ms / 3,000 ms
コード長 1,722 bytes
コンパイル時間 16,869 ms
コンパイル使用メモリ 385,600 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-07 11:41:43
合計ジャッジ時間 15,970 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 676 ms
6,812 KB
testcase_01 AC 655 ms
6,816 KB
testcase_02 AC 673 ms
6,944 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 648 ms
6,940 KB
testcase_05 AC 373 ms
6,944 KB
testcase_06 AC 211 ms
6,940 KB
testcase_07 AC 127 ms
6,944 KB
testcase_08 AC 56 ms
6,940 KB
testcase_09 AC 31 ms
6,944 KB
testcase_10 AC 14 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable `MOD` should have a snake case name
 --> src/main.rs:2:37
  |
2 | fn power(base: usize, times: usize, MOD: usize) -> usize {
  |                                     ^^^
  |
  = note: `#[warn(non_snake_case)]` on by default
help: rename the identifier or convert it to a snake case raw identifier
  |
2 | fn power(base: usize, times: usize, r#mod: usize) -> usize {
  |                                     ~~~~~

warning: variable `MOD` should have a snake case name
 --> src/main.rs:9:26
  |
9 | fn fact_inv(size: usize, MOD: usize) -> (Vec<usize>, Vec<usize>) {
  |                          ^^^
  |
help: rename the identifier or convert it to a snake case raw identifier
  |
9 | fn fact_inv(size: usize, r#mod: usize) -> (Vec<usize>, Vec<usize>) {
  |                          ~~~~~

warning: variable `MOD` should have a snake case name
  --> src/main.rs:26:73
   |
26 | fn comb(n: usize, r: usize, facts: &Vec<usize>, fact_invs: &Vec<usize>, MOD: usize) -> usize {
   |                                                                         ^^^
   |
help: rename the identifier or convert it to a snake case raw identifier
   |
26 | fn comb(n: usize, r: usize, facts: &Vec<usize>, fact_invs: &Vec<usize>, r#mod: usize) -> usize {
   |                                                                         ~~~~~

warning: variable `MOD` should have a snake case name
  --> src/main.rs:35:9
   |
35 |     let MOD = nm[1];
   |         ^^^
   |
help: rename the identifier or convert it to a snake case raw identifier
   |
35 |     let r#mod = nm[1];
   |         ~~~~~

ソースコード

diff #

fn power(base: usize, times: usize, MOD: usize) -> usize {
    if times == 0 { return 1usize; }
    if times == 1 { return base; }
    let temp = power(base, times/2, MOD);
    temp * temp % MOD * power(base, times%2, MOD) % MOD
}

fn fact_inv(size: usize, MOD: usize) -> (Vec<usize>, Vec<usize>) {
    let mut facts = vec![0usize; size+1];
    let mut fact_invs = vec![0usize; size+1];
    let mut invs = vec![0usize; size+1];
    facts[0] = 1;
    facts[1] = 1;
    fact_invs[0] = 1;
    fact_invs[1] = 1;
    invs[1] = 1;
    for i in 2..=size {
        facts[i] = facts[i-1] * i % MOD;
        invs[i] = MOD - invs[MOD % i] * (MOD / i) % MOD;
        fact_invs[i] = fact_invs[i - 1] * invs[i] % MOD;
    }
    (facts, fact_invs)
}

fn comb(n: usize, r: usize, facts: &Vec<usize>, fact_invs: &Vec<usize>, MOD: usize) -> usize {
    facts[n] * fact_invs[r] % MOD * fact_invs[n-r] % MOD
}

fn main() {
    let mut nm = String::new();
    std::io::stdin().read_line(&mut nm).ok();
    let nm: Vec<usize> = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nm[0];
    let MOD = nm[1];

    let mut dp = vec![vec![0usize; n]; n+1];
    dp[0][0] = 1;
    let (facts, fact_invs) = fact_inv(2*n, MOD);
    for i in 0..n {
        for j in 0..n {
            if dp[i][j] == 0 { continue; }
            dp[i+1][j] += dp[i][j];
            dp[i+1][j] %= MOD;
            for k in 2..=n {
                if i + k > n || j + k - 1 >= n { break; }
                dp[i+k][j+k-1] += dp[i][j] * power(k, k-2, MOD) % MOD * comb(n-i-1, k-1, &facts, &fact_invs, MOD) % MOD;
                dp[i+k][j+k-1] %= MOD;
            }
        }
    }
    for &v in dp[n].iter() {
        println!("{}", v);
    }
}
0