結果

問題 No.44 DPなすごろく
ユーザー iwot
提出日時 2019-12-20 17:50:40
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 693 bytes
コンパイル時間 12,813 ms
コンパイル使用メモリ 404,624 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-16 00:30:14
合計ジャッジ時間 13,851 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

fn read<T: std::str::FromStr>() -> T {
  let mut s = String::new();
  std::io::stdin().read_line(&mut s).ok();
  s.trim().parse().ok().unwrap()
}

fn main() {
  let n: i64 = read();
  println!("{}", no44(n));
}

fn no44(n: i64) -> i64 {
  use std::collections::HashMap;
  let mut cache = HashMap::new();
  
  fn consumer(n: i64, cache: &mut HashMap<i64, i64>) -> i64 {
    if let Some(ans) = cache.get(&n) {
      return *ans;
    }
    if n - 2 > 0 {
      let ans = consumer(n - 1, cache) + consumer(n - 2, cache);
      cache.entry(n).or_insert(ans);
      ans
    } else if n - 2 == 0 {
      2
    } else if n - 1 == 0 {
      1
    } else {
      0
    }
  }
  consumer(n, &mut cache)
}
0