結果

問題 No.1141 田グリッド
ユーザー StrorkisStrorkis
提出日時 2020-07-31 23:40:02
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,198 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 153,464 KB
実行使用メモリ 5,872 KB
最終ジャッジ日時 2023-09-21 03:14:22
合計ジャッジ時間 4,939 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,380 KB
testcase_03 AC 1 ms
4,380 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 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 30 ms
4,380 KB
testcase_14 AC 39 ms
5,872 KB
testcase_15 AC 30 ms
4,380 KB
testcase_16 AC 30 ms
4,376 KB
testcase_17 AC 30 ms
4,376 KB
testcase_18 AC 15 ms
4,380 KB
testcase_19 AC 15 ms
4,380 KB
testcase_20 AC 16 ms
4,376 KB
testcase_21 AC 30 ms
4,380 KB
testcase_22 AC 30 ms
4,380 KB
testcase_23 AC 30 ms
4,376 KB
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 -
権限があれば一括ダウンロードができます

ソースコード

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 count = 0;
    let (mut zr, mut zc) = (0, 0);
    let mut row: Vec<usize> = Vec::with_capacity(h);
    for i in 0..h {
        let mut tmp = 1;
        for j in 0..w {
            tmp = tmp * a[i][j] % MOD;
            res = res * a[i][j] % MOD;
            if a[i][j] == 0 {
                count += 1;
                zr = i;
                zc = j;
            }
        }
        row.push(tmp);
    }

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

    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 count > 1 {
            writeln!(writer, "{}", 0).unwrap();                    
        } else if count == 1 {
            if r == zr && c == zc {
                let mut ans = 1;
                for i in 0..h {
                    if i == r { continue; }
                    for j in 0..w {
                        if j == c { continue; }
                        ans = ans * a[i][j] % MOD;
                    }
                }
                writeln!(writer, "{}", ans).unwrap();        
            } else {
                writeln!(writer, "{}", 0).unwrap();                    
            }
        } else {
            let mut ans = res;
            ans = ans * pow(row[r], MOD - 2) % MOD;
            ans = ans * pow(column[c], MOD - 2) % MOD;
            ans = ans * a[r][c] % MOD;
            writeln!(writer, "{}", ans).unwrap();        
        }
    }
}
0