結果

問題 No.2448 一次変換と面積
ユーザー koba-e964koba-e964
提出日時 2023-08-25 22:12:58
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,816 bytes
コンパイル時間 1,242 ms
コンパイル使用メモリ 156,096 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-25 22:13:03
合計ジャッジ時間 4,133 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 1 ms
4,376 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 232 ms
4,376 KB
testcase_24 AC 217 ms
4,376 KB
testcase_25 AC 203 ms
4,376 KB
testcase_26 AC 232 ms
4,376 KB
testcase_27 AC 234 ms
4,376 KB
testcase_28 AC 233 ms
4,376 KB
testcase_29 AC 81 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn squmul(a: [[i64; 2]; 2], b: [[i64; 2]; 2], mo: i64) -> [[i64; 2]; 2] {
    let n = a.len();
    let mut ret = [[0; 2]; 2];
    for i in 0..n {
        for j in 0..n {
            for k in 0..n {
                ret[i][k] += a[i][j] * b[j][k];
                ret[i][k] %= mo;
            }
        }
    }
    ret
}

fn squpow(a: [[i64; 2]; 2], mut e: i64, mo: i64) -> [[i64; 2]; 2] {
    let n = a.len();
    let mut sum = [[0; 2]; 2];
    for i in 0..n { sum[i][i] = 1; }
    let mut cur = a;
    while e > 0 {
        if e % 2 == 1 {
            sum = squmul(sum, cur, mo);
        }
        cur = squmul(cur, cur, mo);
        e /= 2;
    }
    sum
}

fn powsum(a: [[i64; 2]; 2], n: i64, b: i64) -> [[i64; 2]; 2] {
    if n == 0 {
        return [[0; 2]; 2];
    }
    if n % 2 == 0 {
        let mut tmp = squpow(a, n / 2, b);
        tmp[0][0] = (tmp[0][0] + 1) % b;
        tmp[1][1] = (tmp[1][1] + 1) % b;
        let sub = powsum(a, n / 2, b);
        return squmul(tmp, sub, b);
    }
    let sub = powsum(a, n - 1, b);
    let mut tmp = squmul(a, sub, b);
    tmp[0][0] = (tmp[0][0] + 1) % b;
    tmp[1][1] = (tmp[1][1] + 1) % b;
    tmp
}

fn det(a: [[i64; 2]; 2], b: i64) -> i64 {
    let s = a[0][0] * a[1][1] - a[0][1] * a[1][0];
    (s % b + b) % b
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    #[allow(unused)]
    macro_rules! putvec {
        ($v:expr) => {
            for i in 0..$v.len() {
                puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "});
            }
        }
    }
    input! {
        t: usize,
        nba: [(i64, i64, [[i64; 2]; 2]); t],
    }
    for (n, b, a) in nba {
        let mut aa = [[0; 2]; 2];
        for i in 0..2 {
            for j in 0..2 {
                aa[i][j] = (a[i][j] % b + b) % b;
            }
        }
        let s = powsum(aa, n, b);
        let mut det = det(s, b) * det(aa, b) % b;
        if a[0][0] * a[1][1] < a[1][0] * a[0][1] && n % 2 == 1 {
            det = (b - det) % b;
        }
        puts!("{}\n", det);
    }
}
0