結果

問題 No.673 カブトムシ
ユーザー tonyu0
提出日時 2020-04-25 15:12:10
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,411 bytes
コンパイル時間 23,101 ms
コンパイル使用メモリ 388,768 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-07 03:16:37
合計ジャッジ時間 17,567 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;
const MOD: u64 = 1_000_000_007;

// mul(a_x*y, b_z*w) = c_x*w
fn mul(a: Vec<Vec<u64>>, b: Vec<Vec<u64>>) -> Vec<Vec<u64>> {
    let mut res: Vec<Vec<u64>> = vec![vec![0; b[0].len()]; a.len()];
    for i in 0..a.len() {
        for j in 0..b[0].len() {
            let mut val = 0;
            for k in 0..b.len() {
                val += a[i][k] * b[k][j] % MOD;
                val %= MOD;
            }
            res[i][j] = val;
        }
    }
    res
}
// 正方行列のみ
fn pow(mut a: Vec<Vec<u64>>, mut e: u64) -> Vec<Vec<u64>> {
    let mut res: Vec<Vec<u64>> = vec![vec![0; a.len()]; a.len()];
    for i in 0..a.len() {
        res[i][i] = 1;
    }
    while e > 0 {
        if e & 1 == 1 {
            res = mul(res, a.clone());
        }
        a = mul(a.clone(), a.clone());
        e >>= 1;
    }
    res
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let b: u64 = itr.next().unwrap().parse::<u64>().unwrap() % MOD;
    let c: u64 = itr.next().unwrap().parse::<u64>().unwrap() % MOD;
    let d: u64 = itr.next().unwrap().parse().unwrap();

    let mat1 = [[c, b].to_vec(), [0, 1].to_vec()].to_vec();
    let mat2 = [[b].to_vec(), [1].to_vec()].to_vec();

    let mat3 = pow(mat1, d);
    let mat4 = mul(mat3, mat2);
    println!("{}", (mat4[0][0] + MOD - b) % MOD);
}
0