結果

問題 No.1141 田グリッド
ユーザー qumazakiqumazaki
提出日時 2020-09-08 18:02:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 1,051 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 111,364 KB
最終ジャッジ日時 2024-11-30 01:54:27
合計ジャッジ時間 6,234 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

h,w = list(map(int,readline().split()))
data = list(map(int,read().split()))
a = data[:h*w]
q = data[h*w]
rc = data[h*w+1:]
mod = 10**9+7

cnt0 = 0
tot = 1
cnt0_h = [0]*h
cnt0_w = [0]*w
tot_h = [1]*h
tot_w = [1]*w
for i in range(h):
    for j in range(w):
        aij = a[i*w+j]
        if(aij==0):
            cnt0 += 1
            cnt0_h[i] += 1
            cnt0_w[j] += 1
        else:
            tot *= aij
            tot %= mod
            tot_h[i] *= aij
            tot_h[i] %= mod
            tot_w[j] *= aij
            tot_w[j] %= mod

ans =[]
it = iter(rc)
for r,c in zip(it,it):
    r -= 1
    c -= 1
    num0 = cnt0 - cnt0_h[r] - cnt0_w[c] + (a[r*w+c]==0)
    if(num0>0):
        ans.append(0)
        continue
    ans_i = tot * pow(tot_h[r],mod-2,mod) * pow(tot_w[c],mod-2,mod)
    ans_i %= mod
    if(a[r*w+c] != 0):
        ans_i *= a[r*w+c]
        ans_i %= mod
    ans.append(ans_i)

print('\n'.join(map(str,ans)))
0