Submission #29630307


Source Code Expand

use proconio::input;

type Matrix = Vec<Vec<usize>>;

fn mul(a: Matrix, b: Matrix) -> Matrix {
    let h = a.len();
    let w = b[0].len();
    if a[0].len() != b.len() {
        unreachable!();
    }
    let len = b.len();
    let mut ab = vec![vec![0; w]; h];
    for r in 0..h {
        for c in 0..w {
            for i in 0..len {
                ab[r][c] += a[r][i] * b[i][c];
                ab[r][c] %= 1_000_000_007;
            }
        }
    }
    ab
}

fn pow(a: Matrix, n: usize) -> Matrix {
    let mut p = vec![vec![1, 0], vec![0, 1]];
    let mut q = a;
    for i in 0..60 {
        if (n & (1 << i)) != 0 {
            p = mul(p, q.clone());
        }
        q = mul(q.clone(), q);
    }
    p
}

fn resolve(n: usize) -> usize {
    let m = vec![vec![2, 1], vec![1, 0]];
    let x = pow(m, n - 1);
    (x[1][0] + x[1][1]) % 1_000_000_007
}

fn main() {
    input! {
        n: usize,
    };
    let ans = resolve(n);
    println!("{}", ans);
}

Submission Info

Submission Time
Task 055 - Recurrence Formula 1
User bouzuya
Language Rust (1.42.0)
Score 1000
Code Size 963 Byte
Status AC
Exec Time 6 ms
Memory 2168 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 2
AC × 10
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All sample_01.txt, sample_02.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt
Case Name Status Exec Time Memory
sample_01.txt AC 6 ms 2072 KiB
sample_02.txt AC 1 ms 2128 KiB
test_01.txt AC 1 ms 2040 KiB
test_02.txt AC 2 ms 2032 KiB
test_03.txt AC 1 ms 2016 KiB
test_04.txt AC 1 ms 2140 KiB
test_05.txt AC 1 ms 1984 KiB
test_06.txt AC 2 ms 2168 KiB
test_07.txt AC 1 ms 2076 KiB
test_08.txt AC 1 ms 2048 KiB