提出 #54403547


ソースコード 拡げる

#![allow(unused)]

fn main() {
    // S(k - 1) = A^0 + A^1 + ... + A^(k-2)
    // S(k) = A^0 + A^1 + ... + A^(k-2) + A^(k-1)
    //      = A * S(k - 1) + 1

    // [ S(k) ] = [ A   1 ] [ S(k-1) ]
    // [  1   ] = [ 0   1 ] [   1    ]

    let inp = readv::<u64>();
    let (a, x, m) = (inp[0], inp[1], inp[2]);

    let mat = vec![vec![a, 1], vec![0, 1]];
    let mat = matpow(&mat, x - 1, m);
    let ans = (mat[0][0] + mat[0][1]) % m;
    println!("{}", ans);
}

type Mat = Vec<Vec<u64>>;

fn matmul(a: &Mat, b: &Mat, m: u64) -> Mat {
    let (h, w) = (a.len(), b[0].len());
    let mut res = vec![vec![0; w]; h];
    for r in 0..h {
        for c in 0..w {
            for k in 0..b.len() {
                res[r][c] += (a[r][k] * b[k][c]) % m;
                res[r][c] %= m;
            }
        }
    }
    res
}

fn matpow(mat: &Mat, mut exp: u64, m: u64) -> Mat {
    let n = mat.len();
    let mut base = mat.clone();
    let mut ans = vec![vec![0; n]; n];
    for i in 0..n {
        ans[i][i] = 1;
    }
    while exp > 0 {
        if exp & 1 == 1 {
            ans = matmul(&ans, &base, m);
        }
        base = matmul(&base, &base, m);
        exp >>= 1;
    }
    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 readv<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_ascii_whitespace()
        .map(|t| t.parse().ok().unwrap())
        .collect()
}

fn reads() -> Vec<char> {
    read::<String>().chars().collect()
}

fn mapv<T, S, F: Fn(&T) -> S>(arr: &Vec<T>, f: F) -> Vec<S> {
    arr.iter().map(f).collect()
}

fn join<T: ToString>(arr: &[T], sep: &str) -> String {
    arr.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(sep)
}

提出情報

提出日時
問題 E - Geometric Progression
ユーザ amoshuangyc
言語 Rust (rustc 1.70.0)
得点 500
コード長 1909 Byte
結果 AC
実行時間 1 ms
メモリ 2064 KiB

ジャッジ結果

セット名 Sample All AfterContest
得点 / 配点 0 / 0 500 / 500 0 / 0
結果
AC × 3
AC × 31
AC × 1
セット名 テストケース
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt
AfterContest after_contest_01.txt
ケース名 結果 実行時間 メモリ
after_contest_01.txt AC 1 ms 1956 KiB
sample00.txt AC 1 ms 1840 KiB
sample01.txt AC 1 ms 1948 KiB
sample02.txt AC 0 ms 1900 KiB
testcase00.txt AC 0 ms 1896 KiB
testcase01.txt AC 0 ms 1908 KiB
testcase02.txt AC 1 ms 1952 KiB
testcase03.txt AC 1 ms 1896 KiB
testcase04.txt AC 1 ms 1892 KiB
testcase05.txt AC 1 ms 1956 KiB
testcase06.txt AC 1 ms 1892 KiB
testcase07.txt AC 1 ms 1872 KiB
testcase08.txt AC 1 ms 1900 KiB
testcase09.txt AC 1 ms 1876 KiB
testcase10.txt AC 0 ms 1804 KiB
testcase11.txt AC 0 ms 2036 KiB
testcase12.txt AC 1 ms 1808 KiB
testcase13.txt AC 0 ms 1884 KiB
testcase14.txt AC 0 ms 1912 KiB
testcase15.txt AC 1 ms 2064 KiB
testcase16.txt AC 1 ms 1876 KiB
testcase17.txt AC 1 ms 1776 KiB
testcase18.txt AC 1 ms 1944 KiB
testcase19.txt AC 1 ms 1880 KiB
testcase20.txt AC 1 ms 1876 KiB
testcase21.txt AC 0 ms 1816 KiB
testcase22.txt AC 0 ms 1876 KiB
testcase23.txt AC 0 ms 1828 KiB
testcase24.txt AC 1 ms 1860 KiB
testcase25.txt AC 0 ms 1832 KiB
testcase26.txt AC 1 ms 1876 KiB
testcase27.txt AC 1 ms 1912 KiB