結果
| 問題 |
No.2441 行列累乗
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-08-25 21:24:32 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,561 bytes |
| コンパイル時間 | 14,786 ms |
| コンパイル使用メモリ | 379,096 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-24 07:46:35 |
| 合計ジャッジ時間 | 16,072 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
pub mod scanner {
pub struct Scanner {
buf: Vec<String>,
}
impl Scanner {
pub fn new() -> Self {
Self { buf: vec![] }
}
pub fn new_from(source: &str) -> Self {
let source = String::from(source);
let buf = Self::split(source);
Self { buf }
}
pub fn next<T: std::str::FromStr>(&mut self) -> T {
loop {
if let Some(x) = self.buf.pop() {
return x.parse().ok().expect("");
}
let mut source = String::new();
std::io::stdin().read_line(&mut source).expect("");
self.buf = Self::split(source);
}
}
fn split(source: String) -> Vec<String> {
source
.split_whitespace()
.rev()
.map(String::from)
.collect::<Vec<_>>()
}
}
}
use crate::scanner::Scanner;
use std::io::Write;
fn main() {
let mut scanner = Scanner::new();
let out = std::io::stdout();
let mut out = std::io::BufWriter::new(out.lock());
let t: usize = 1;
for _ in 0..t {
solve(&mut scanner, &mut out);
}
}
pub struct SquareMatrix(Vec<Vec<isize>>);
impl SquareMatrix {
pub fn dot(&self, rhs: &SquareMatrix) -> SquareMatrix {
let size = self.0.len();
let mut res = SquareMatrix(vec![vec![0; size]; size]);
for i in 0..size {
for j in 0..size {
for k in 0..size {
res.0[i][j] += self.0[i][k] * rhs.0[k][j];
}
}
}
res
}
pub fn pow(&self, n: usize) -> SquareMatrix {
let size = self.0.len();
let mut res = SquareMatrix(vec![vec![0; size]; size]);
for i in 0..size {
res.0[i][i] = 1;
}
let mut now = SquareMatrix(self.0.clone());
let mut n = n;
while n > 0 {
if (n & 1) == 1 {
res = res.dot(&now);
}
now = now.dot(&now);
n >>= 1;
}
res
}
}
fn solve(scanner: &mut Scanner, out: &mut std::io::BufWriter<std::io::StdoutLock>) {
let a: isize = scanner.next();
let b: isize = scanner.next();
let c: isize = scanner.next();
let d: isize = scanner.next();
let ans = SquareMatrix(vec![vec![a, b], vec![c, d]]).pow(3);
writeln!(out, "{} {}", ans.0[0][0], ans.0[0][1]).unwrap();
writeln!(out, "{} {}", ans.0[1][0], ans.0[1][1]).unwrap();
}