結果

問題 No.1141 田グリッド
ユーザー StrorkisStrorkis
提出日時 2020-08-01 00:16:36
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 3,740 bytes
コンパイル時間 2,500 ms
コンパイル使用メモリ 155,272 KB
実行使用メモリ 16,456 KB
最終ジャッジ日時 2023-09-21 03:59:53
合計ジャッジ時間 4,768 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 51 ms
5,864 KB
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 -
権限があれば一括ダウンロードができます

ソースコード

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; }
        } else 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