結果

問題 No.1141 田グリッド
コンテスト
ユーザー trineutron
提出日時 2020-08-02 07:17:49
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 732 ms / 2,000 ms
コード長 1,196 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 401 ms
コンパイル使用メモリ 20,952 KB
実行使用メモリ 43,824 KB
最終ジャッジ日時 2026-04-01 19:06:02
合計ジャッジ時間 16,335 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

mod = 10**9 + 7

h, w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]

product_all = 1
product_row = [1] * h
product_column = [1] * w
zero_all = 0
zero_row = [0] * h
zero_column = [0] * w
for i in range(h):
    for j in range(w):
        if a[i][j]:
            product_all *= a[i][j]
            product_all %= mod
            product_row[i] *= a[i][j]
            product_row[i] %= mod
            product_column[j] *= a[i][j]
            product_column[j] %= mod
        else:
            zero_all += 1
            zero_row[i] += 1
            zero_column[j] += 1

q = int(input())
for i in range(q):
    r, c = map(int, input().split())
    zero_point = 0
    if a[r - 1][c - 1] == 0:
        zero_point += 1
    if a[r - 1][c - 1] and zero_all - zero_row[r - 1] - zero_column[c - 1] + zero_point == 0:
        print(product_all * pow(product_row[r - 1], mod - 2, mod) * pow(product_column[c - 1], mod - 2, mod) * a[r - 1][c - 1] % mod)
    elif zero_all - zero_row[r - 1] - zero_column[c - 1] + zero_point == 0:
        print(product_all * pow(product_row[r - 1], mod - 2, mod) * pow(product_column[c - 1], mod - 2, mod) % mod)
    else:
        print(0)
0