結果

問題 No.1141 田グリッド
ユーザー stng
提出日時 2022-09-04 22:30:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 386 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 641 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 86,904 KB
最終ジャッジ日時 2024-11-19 05:56:41
合計ジャッジ時間 9,861 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

h,w = map(int,input().split())
a = [[int(i) for i in input().split()] for j in range(h)]
q = int(input())
rc = [[int(i)-1 for i in input().split()] for i in range(q)]

mod = 10**9+7
ttl = 1
fz = False
zc = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == 0:
            zc += 1
            fz = True
            continue
        ttl *= a[i][j]
        ttl %= mod

tate = [1]*h
tz = [0]*h
yoko = [1]*w
yz = [0]*w

for i in range(h):
    for j in range(w):
        if a[i][j] == 0:
            tz[i] += 1
            continue
        tmp = pow(a[i][j],mod-2,mod)
        tate[i] *= tmp
        tate[i] %= mod

for i in range(w):
    for j in range(h):
        if a[j][i] == 0:
            yz[i] += 1
            continue
        tmp = pow(a[j][i],mod-2,mod)
        yoko[i] *= tmp
        yoko[i] %= mod

for i in range(q):
    r,c = rc[i]
    if fz == True:
        add = 0
        if a[r][c] == 0:
            add = 1
        if zc != tz[r]+yz[c]-add:
            print(0)
            continue
    ans = ttl*tate[r]
    ans %= mod
    ans *= yoko[c]
    ans %= mod
    if a[r][c] != 0:
        ans *= a[r][c]
    ans %= mod
    print(ans)
0