// https://yukicoder.me/problems/no/1275 use proconio::input; use std::i64; const MOD:i64 = 1000000007; fn prod(left: &[[i64;2];2], right : &[[i64;2];2]) -> [[i64;2]; 2] { let mut new_matrix = [[0, 0], [0, 0]]; for i in 0..2 { for j in 0..2 { for k in 0..2{ new_matrix[i][j] += (left[i][k] * right[k][j]) % MOD; new_matrix[i][j] %= MOD } } } return new_matrix; } fn main() { input! { a: i64, b: i64, n: i64, } // (a + sqrt(b))^n について計算する let mut base_matrix = [[a, b], [1, a]]; let mut ans_matrix = [[1, 0], [0, 1]]; let mut n1 = n; while n1 > 0 { if n1 % 2 == 1 { ans_matrix = prod(&base_matrix, &ans_matrix); } base_matrix = prod(&base_matrix, &base_matrix); n1 /= 2; } let a1 = ans_matrix[0][0]; // (a - sqrt(b))^n について計算する let mut base_matrix2= [[a, -b], [-1, a]]; let mut ans_matrix2 = [[1, 0], [0, 1]]; let mut n2 = n; while n2 > 0 { if n2 % 2 == 1 { ans_matrix2 = prod(&base_matrix2, &ans_matrix2); } base_matrix2 = prod(&base_matrix2, &base_matrix2); n2 /= 2; } let a2 = ans_matrix2[0][0]; let answer = (a1 + a2) % MOD; println!("{}", answer); }