結果
問題 |
No.1275 綺麗な式
|
ユーザー |
|
提出日時 | 2024-08-19 01:59:01 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 1 ms / 2,000 ms |
コード長 | 1,374 bytes |
コンパイル時間 | 10,484 ms |
コンパイル使用メモリ | 394,196 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-08-19 01:59:16 |
合計ジャッジ時間 | 12,197 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 60 |
ソースコード
// 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); }