結果

問題 No.978 Fibonacci Convolution Easy
ユーザー fukafukatanifukafukatani
提出日時 2020-01-31 21:52:13
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,280 bytes
コンパイル時間 24,363 ms
コンパイル使用メモリ 376,768 KB
実行使用メモリ 33,104 KB
最終ジャッジ日時 2024-09-18 20:55:50
合計ジャッジ時間 15,512 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i64>();
    let (n, p) = (v[0] as usize, v[1]);
    if n == 1 {
        println!("0");
        return;
    }
    let big_int = 1000_000_000 + 7;
    let mut a = vec![0; n + 1];
    a[2] = 1;
    for i in 3..n + 1 {
        a[i] = p * a[i - 1] + a[i - 2];
        a[i] %= big_int;
    }
    let mut sum = vec![0; n + 1];
    for i in 1..n + 1 {
        sum[i] = a[i] + sum[i - 1];
        sum[i] %= big_int;
    }
    let mut ans = 0;
    for i in 0..n + 1 {
        ans += (a[i] * sum[i]) % big_int;
        ans %= big_int;
    }
    println!("{}", ans);
}

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 read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0