結果

問題 No.1141 田グリッド
ユーザー StrorkisStrorkis
提出日時 2020-07-31 23:19:21
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 3,030 bytes
コンパイル時間 3,891 ms
コンパイル使用メモリ 146,512 KB
実行使用メモリ 5,924 KB
最終ジャッジ日時 2023-09-21 02:47:30
合計ジャッジ時間 3,385 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 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,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 31 ms
4,380 KB
testcase_14 AC 44 ms
5,924 KB
testcase_15 AC 30 ms
4,376 KB
testcase_16 AC 31 ms
4,376 KB
testcase_17 AC 30 ms
4,380 KB
testcase_18 AC 13 ms
4,376 KB
testcase_19 AC 14 ms
4,376 KB
testcase_20 AC 14 ms
4,376 KB
testcase_21 AC 31 ms
4,380 KB
testcase_22 AC 30 ms
4,380 KB
testcase_23 AC 30 ms
4,380 KB
testcase_24 RE -
testcase_25 WA -
testcase_26 RE -
testcase_27 RE -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{BufRead, Write};

const MOD: usize = 1_000_000_007;

fn main () {
    let stdin = std::io::stdin();
    let stdout = std::io::stdout();
    let mut reader = std::io::BufReader::new(stdin.lock());
    let mut writer = std::io::BufWriter::new(stdout.lock());

    let (h, w): (usize, usize) = {
        let mut buf = String::new();
        reader.read_line(&mut buf).unwrap();
        let mut iter = buf.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    };

    let mut a: Vec<Vec<usize>> = Vec::with_capacity(h);
    for _ in 0..h {
        let b: Vec<usize> = {
            let mut buf = String::new();
            reader.read_line(&mut buf).unwrap();
            let iter = buf.split_whitespace();
            iter.map(|x| x.parse().unwrap()).collect()
        };
        a.push(b);
    }

    let pow = |mut a: usize, mut n: usize| -> usize {
        let mut res: usize = 1;
        while n > 0 {
            if (n & 1) > 0 { res = res * a % MOD; }
            a = a * a % MOD;
            n >>= 1;
        }
        res
    };

    let mut res = 1;
    let mut row: Vec<usize> = Vec::with_capacity(h);
    let mut rzc: usize = 0;
    for i in 0..h {
        let mut tmp = 1;
        for j in 0..w {
            tmp = tmp * a[i][j] % MOD;
        }
        row.push(tmp);
        if tmp == 0 {
            rzc += 1;
        } else {
            res = res * tmp % MOD;
        }
    }

    let mut column: Vec<usize> = Vec::with_capacity(w);
    let mut czc: usize = 0;
    for j in 0..w {
        let mut tmp = 1;
        for i in 0..h {
            tmp = tmp * a[i][j] % MOD;
        }
        column.push(tmp);
        if tmp == 0 {
            czc += 1;
        }
    }

    let q: usize = {
        let mut buf = String::new();
        reader.read_line(&mut buf).unwrap();
        buf.trim_end().parse().unwrap()
    };

    for _ in 0..q {
        let (r, c): (usize, usize) = {
            let mut buf = String::new();
            reader.read_line(&mut buf).unwrap();
            let mut iter = buf.split_whitespace();
            (
                iter.next().unwrap().parse::<usize>().unwrap() - 1,
                iter.next().unwrap().parse::<usize>().unwrap() - 1,
            )
        };
    
        if rzc > 1 || czc > 1 {
            writeln!(writer, "0").unwrap();
        } else {
            let mut ans = res;
            ans = {
                if rzc > 0 {
                    if row[r] > 0 { 0 } else { ans }
                } else {
                    ans * pow(row[r], MOD - 2) % MOD
                }
            };
            ans = {
                if czc > 0 {
                    if column[r] > 0 { 0 } else { ans }
                } else {
                    ans * pow(column[c], MOD - 2) % MOD
                }
            };
            if a[r][c] > 0 {
                ans = ans * a[r][c] % MOD;
            }
            writeln!(writer, "{}", ans).unwrap();    
        }
    }
}
0