結果

問題 No.1141 田グリッド
ユーザー phsplsphspls
提出日時 2022-12-31 01:27:45
言語 Rust
(1.77.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 2,239 bytes
コンパイル時間 10,317 ms
コンパイル使用メモリ 149,408 KB
実行使用メモリ 6,904 KB
最終ジャッジ日時 2023-08-17 05:40:58
合計ジャッジ時間 5,528 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 128 ms
4,376 KB
testcase_14 AC 144 ms
6,904 KB
testcase_15 AC 125 ms
4,380 KB
testcase_16 AC 126 ms
4,376 KB
testcase_17 AC 127 ms
4,376 KB
testcase_18 AC 79 ms
4,376 KB
testcase_19 AC 80 ms
4,376 KB
testcase_20 AC 80 ms
4,380 KB
testcase_21 AC 125 ms
4,380 KB
testcase_22 AC 126 ms
4,376 KB
testcase_23 AC 125 ms
4,380 KB
testcase_24 AC 80 ms
4,380 KB
testcase_25 AC 81 ms
4,376 KB
testcase_26 AC 81 ms
4,376 KB
testcase_27 AC 80 ms
4,380 KB
testcase_28 AC 80 ms
4,380 KB
testcase_29 AC 79 ms
4,376 KB
testcase_30 AC 79 ms
4,380 KB
testcase_31 AC 80 ms
4,380 KB
testcase_32 AC 81 ms
4,380 KB
testcase_33 AC 80 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unnecessary trailing semicolon
  --> Main.rs:44:22
   |
44 |             continue;;
   |                      ^ help: remove this semicolon
   |
   = note: `#[warn(redundant_semicolons)]` on by default

warning: 1 warning emitted

ソースコード

diff #

const MOD: usize = 1e9 as usize + 7;

fn power(base: usize, times: usize) -> usize {
    if times == 0 { return 1usize; }
    if times == 1 { return base; }
    let temp = power(base, times/2);
    temp * temp % MOD * power(base, times%2) % MOD
}

fn main() {
    let mut hw = String::new();
    std::io::stdin().read_line(&mut hw).ok();
    let hw: Vec<usize> = hw.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let h = hw[0];
    let w = hw[1];
    let grid = (0..h).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
            temp
        })
        .collect::<Vec<_>>();
    let mut q = String::new();
    std::io::stdin().read_line(&mut q).ok();
    let q: usize = q.trim().parse().unwrap();
    let queries = (0..q).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
            (temp[0]-1, temp[1]-1)
        })
        .collect::<Vec<_>>();

    let zeros = (0..h).map(|i| grid[i].iter().filter(|&&v| v == 0).count()).sum::<usize>();
    let xzeros = (0..h).map(|i| grid[i].iter().filter(|&&v| v == 0).count()).collect::<Vec<_>>();
    let yzeros = (0..w).map(|j| (0..h).filter(|&i| grid[i][j] == 0).count()).collect::<Vec<_>>();
    let xmap = (0..h).map(|i| grid[i].iter().filter(|&&v| v > 0).fold(1usize, |x, y| x * y % MOD)).collect::<Vec<_>>();
    let ymap = (0..w).map(|j| (0..h).filter(|&i| grid[i][j] > 0).map(|i| grid[i][j]).fold(1usize, |x, y| x * y % MOD)).collect::<Vec<_>>();
    let summary = xmap.iter().fold(1usize, |x, &y| x * y % MOD);
    for &(x, y) in queries.iter() {
        let zero_cnt = zeros + if grid[x][y] == 0 { 1 } else { 0 } - xzeros[x] - yzeros[y];
        if zero_cnt > 0 {
            println!("0");
            continue;;
        }
        let revcell = if grid[x][y] == 0 { 1 } else { power(grid[x][y], MOD-2) };
        let result = summary * power(xmap[x] * ymap[y] % MOD * revcell % MOD, MOD-2) % MOD;
        println!("{}", result);
    }
}
0