結果

問題 No.1275 綺麗な式
ユーザー fukafukatanifukafukatani
提出日時 2020-10-30 23:13:14
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 2,003 bytes
コンパイル時間 1,120 ms
コンパイル使用メモリ 149,348 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 08:18:34
合計ジャッジ時間 3,677 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 1 ms
4,376 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 1 ms
4,380 KB
testcase_56 AC 1 ms
4,380 KB
testcase_57 AC 1 ms
4,376 KB
testcase_58 AC 1 ms
4,376 KB
testcase_59 AC 1 ms
4,380 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type `mat` should have an upper camel case name
  --> Main.rs:43:6
   |
43 | type mat = Vec<Vec<i64>>;
   |      ^^^ help: convert the identifier to upper camel case: `Mat`
   |
   = note: `#[warn(non_camel_case_types)]` on by default

warning: 1 warning emitted

ソースコード

diff #

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

#[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 (a, b) = (v[0], v[1]);
    let n = read::<u64>();
    if n == 0 {
        println!("{}", 2);
        return;
    }
    if n == 1 {
        println!("{}", (2 * a).rem_euclid(1000_000_007));
        return;
    }

    let mut c = mat_zeros(2, 2);
    c[0][0] = (2 * a).rem_euclid(1000_000_007);
    c[0][1] = (b - a * a).rem_euclid(1000_000_007);
    c[1][0] = 1;
    c[1][1] = 0;
    c = matpow(&c, n - 1);
    println!(
        "{}",
        ((c[0][0] * 2 * a) % 1000_000_007 + c[0][1] * 2) % (1000_000_007)
    );
}

type mat = Vec<Vec<i64>>;

fn mat_zeros(n: usize, m: usize) -> mat {
    vec![vec![0i64; m]; n]
}

fn matmul(a: &mat, b: &mat) -> mat {
    let mut c = mat_zeros(a.len(), b[0].len());
    for i in 0..a.len() {
        for k in 0..b.len() {
            for j in 0..b[0].len() {
                c[i][j] += (a[i][k] * b[k][j]) % 1000_000_007;
                c[i][j] %= 1000_000_007;
            }
        }
    }
    c
}

fn matpow(a: &mat, n: u64) -> mat {
    let mut b = mat_zeros(a.len(), a.len());
    for i in 0..a.len() {
        b[i][i] = 1;
    }
    let mut n = n;
    let mut a: mat = a.clone();
    while n > 0 {
        if n & 1 == 1 {
            b = matmul(&a, &b);
        }
        a = matmul(&a, &a);
        n >>= 1;
    }
    b
}

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