結果

問題 No.554 recurrence formula
ユーザー taotao54321taotao54321
提出日時 2018-10-11 09:01:05
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,061 bytes
コンパイル時間 563 ms
コンパイル使用メモリ 151,808 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2024-04-20 19:40:28
合計ジャッジ時間 1,469 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 0 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case)]

use std::io::{ self, prelude::* };

macro_rules! pick {
    ($tokens:expr) => {
        $tokens.next().unwrap().parse().expect("parse error")
    }
}

struct Solver {
    memo: Vec<Option<i64>>,
}

impl Solver {
    const MOD: i64 = 1_000_000_007;

    fn new() -> Self {
        let mut memo = vec![None; 100001];
        memo[1] = Some(1);
        memo[2] = Some(2);
        memo[3] = Some(6);

        Self {
            memo,
        }
    }

    fn f(&mut self, n: i64) -> i64 {
        if let Some(res) = self.memo[n as usize] { return res; }

        let mut res = 0;
        res += n*self.f(n-2) / (n-2);
        res %= Solver::MOD;
        res += n*self.f(n-1);
        res %= Solver::MOD;
        self.memo[n as usize] = Some(res);
        res
    }
}

fn main() {
    let mut s = String::new();
    io::stdin().read_to_string(&mut s).expect("i/o error");
    let mut tokens = s.split_whitespace();

    let N: i64 = pick!(tokens);

    let mut solver = Solver::new();
    let ans = solver.f(N);

    println!("{}", ans);
}
0