結果

問題 No.2405 Minimal Matrix Decomposition
ユーザー koba-e964koba-e964
提出日時 2023-08-07 11:47:53
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 4,703 bytes
コンパイル時間 13,819 ms
コンパイル使用メモリ 378,884 KB
実行使用メモリ 6,912 KB
最終ジャッジ日時 2024-11-07 15:43:03
合計ジャッジ時間 18,430 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 947 ms
6,912 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 1 ms
5,248 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: call to `.clone()` on a reference in this situation does nothing
  --> src/main.rs:71:17
   |
71 |     let aold = a.clone();
   |                 ^^^^^^^^ help: remove this redundant call
   |
   = note: the type `[Vec<i64>]` does not implement `Clone`, so calling `clone` on `&[Vec<i64>]` copies the reference, which does not do anything and can be removed
   = note: `#[warn(noop_method_call)]` on by default

ソースコード

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 powmod(x: i64, mut e: i64, m: i64) -> i64 {
    let mut sum = 1;
    let mut cur = x % m;
    while e > 0 {
        if e % 2 != 0 {
            sum = sum * cur % m;
        }
        cur = cur * cur % m;
        e /= 2;
    }
    sum
}

// p must be prime.
// Returns (B, H) where HB = A, rows(B) = rank(A)
// A: n * m, B: r * m, H: n * r
fn find_basis_mod_p(a: &[Vec<i64>], p: i64) -> (Vec<Vec<i64>>, Vec<Vec<i64>>) {
    let n = a.len();
    let m = a[0].len();
    let aold = a.clone();
    let mut a = a.to_vec();
    let mut pos = vec![];
    let mut c = 0;
    let mut r = n;
    // TODO no pivoting
    for i in 0..n {
        while c < m && a[i][c] == 0 {
            c += 1;
        }
        if c >= m {
            r = i;
            break;
        }
        pos.push(c);
        let aic = a[i][c];
        let aicinv = powmod(aic, p - 2, p);
        a[i][c] = 1.into();
        for j in c + 1..m {
            a[i][j] *= aicinv;
            a[i][j] %= p;
        }
        for j in 0..n {
            if i == j { continue; }
            let ajc = a[j][c];
            a[j][c] = 0;
            for k in c + 1..m {
                let val = ajc * a[i][k] % p;
                a[j][k] -= val;
                if a[j][k] < 0 {
                    a[j][k] += p;
                }
            }
        }
    }
    let mut tr = vec![vec![0; r]; n];
    for i in 0..n {
        let mut cur = aold[i].clone();
        for j in 0..r {
            let idx = pos[j];
            let val = cur[idx];
            tr[i][j] = val;
            for k in idx + 1..m {
                cur[k] -= val * a[j][k] % p;
                if cur[k] < 0 {
                    cur[k] += p;
                }
            }
        }
    }
    (a[..r].to_vec(), tr)
}

// https://yukicoder.me/problems/no/2405 (3.5)
// 行列のランクを r としたとき、最適値は NM あるいは r(N + M) のどちらかと思われる。(つまり行列 1 個か 2 個)
fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    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! {
        p: i64,
        n: usize, m: usize,
        a: [[i64; m]; n],
    }
    let (b, h) = find_basis_mod_p(&a, p);
    let r = b.len();
    if r * (m + n) > n * m {
        puts!("1\n{} {}\n", n, m);
        for i in 0..n {
            putvec!(a[i]);
        }
        return;
    }
    puts!("2\n{} {}\n", n, r);
    for i in 0..n {
        putvec!(h[i]);
    }
    puts!("{} {}\n", r, m);
    for i in 0..r {
        putvec!(b[i]);
    }
}
0