結果

問題 No.1141 田グリッド
ユーザー StrorkisStrorkis
提出日時 2020-08-01 00:39:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 3,743 bytes
コンパイル時間 2,685 ms
コンパイル使用メモリ 153,844 KB
実行使用メモリ 16,436 KB
最終ジャッジ日時 2023-09-21 04:24:06
合計ジャッジ時間 6,312 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,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 49 ms
5,856 KB
testcase_14 AC 75 ms
16,436 KB
testcase_15 AC 77 ms
6,316 KB
testcase_16 AC 76 ms
6,352 KB
testcase_17 AC 75 ms
6,052 KB
testcase_18 AC 75 ms
6,088 KB
testcase_19 AC 75 ms
6,388 KB
testcase_20 AC 76 ms
6,652 KB
testcase_21 AC 77 ms
6,360 KB
testcase_22 AC 77 ms
6,312 KB
testcase_23 AC 77 ms
6,408 KB
testcase_24 AC 75 ms
5,828 KB
testcase_25 AC 74 ms
6,232 KB
testcase_26 AC 75 ms
5,932 KB
testcase_27 AC 75 ms
6,136 KB
testcase_28 AC 75 ms
6,044 KB
testcase_29 AC 75 ms
6,120 KB
testcase_30 AC 74 ms
6,052 KB
testcase_31 AC 75 ms
6,620 KB
testcase_32 AC 75 ms
6,412 KB
testcase_33 AC 75 ms
6,420 KB
権限があれば一括ダウンロードができます

ソースコード

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 = 1;
        while n > 0 {
            if (n & 1) > 0 { res = res * a % MOD; }
            a = a * a % MOD;
            n >>= 1;
        }
        res
    };

    let mut lu: Vec<Vec<usize>> = vec![vec![1; w]; h];
    let mut ru: Vec<Vec<usize>> = vec![vec![1; w]; h];
    for i in 0..h {
        for j in 0..w {
            if i > 0 { lu[i][j] = lu[i][j] * lu[i - 1][j] % MOD; }
            if j > 0 { lu[i][j] = lu[i][j] * lu[i][j - 1] % MOD; }
            if i > 0 && j > 0 {
                lu[i][j] = lu[i][j] * pow(lu[i - 1][j - 1], MOD - 2) % MOD;
            }
            lu[i][j] = lu[i][j] * a[i][j] % MOD;
        }
        for j in (0..w).rev() {
            if i > 0 { ru[i][j] = ru[i][j] * ru[i - 1][j] % MOD; }
            if j + 1 < w { ru[i][j] = ru[i][j] * ru[i][j + 1] % MOD; }
            if i > 0 && j + 1 < w {
                ru[i][j] = ru[i][j] * pow(ru[i - 1][j + 1], MOD - 2) % MOD;
            }
            ru[i][j] = ru[i][j] * a[i][j] % MOD;
        }
    }

    let mut ld: Vec<Vec<usize>> = vec![vec![1; w]; h];
    let mut rd: Vec<Vec<usize>> = vec![vec![1; w]; h];
    for i in (0..h).rev() {
        for j in 0..w {
            if i + 1 < h { ld[i][j] = ld[i][j] * ld[i + 1][j] % MOD; }
            if j > 0 { ld[i][j] = ld[i][j] * ld[i][j - 1] % MOD; }
            if i + 1 < h && j > 0 {
                ld[i][j] = ld[i][j] * pow(ld[i + 1][j - 1], MOD - 2) % MOD;
            }
            ld[i][j] = ld[i][j] * a[i][j] % MOD;
        }
        for j in (0..w).rev() {
            if i + 1 < h { rd[i][j] = rd[i][j] * rd[i + 1][j] % MOD; }
            if j + 1 < w { rd[i][j] = rd[i][j] * rd[i][j + 1] % MOD; }
            if i + 1 < h && j + 1 < w {
                rd[i][j] = rd[i][j] * pow(rd[i + 1][j + 1], MOD - 2) % MOD;
            }
            rd[i][j] = rd[i][j] * a[i][j] % MOD;
        }
    }

    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,
            )
        };
    
        let mut ans = 1;
        if r > 0 {
            if c > 0 { ans = ans * lu[r - 1][c - 1] % MOD; }
            if c + 1 < w { ans = ans * ru[r - 1][c + 1] % MOD; }
        }
        if r + 1 < h {
            if c > 0 { ans = ans * ld[r + 1][c - 1] % MOD; }
            if c + 1 < w { ans = ans * rd[r + 1][c + 1] % MOD; }
        }
        writeln!(writer, "{}", ans).unwrap();
    }
}
0