結果

問題 No.1141 田グリッド
ユーザー tamatotamato
提出日時 2020-07-31 21:58:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 486 ms / 2,000 ms
コード長 2,901 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 100,992 KB
最終ジャッジ日時 2024-07-06 17:59:49
合計ジャッジ時間 12,465 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    H, W = map(int, input().split())
    A = []
    for _ in range(H):
        A.append(list(map(int, input().split())))

    cm1 = [[1] * W for _ in range(H)]
    cm2 = [[1] * W for _ in range(H)]
    cm3 = [[1] * W for _ in range(H)]
    cm4 = [[1] * W for _ in range(H)]

    # cm1
    for h in range(H):
        for w in range(W):
            if h == 0:
                if w == 0:
                    cm1[h][w] = A[h][w]
                else:
                    cm1[h][w] = (cm1[h][w-1] * A[h][w])%mod
            else:
                if w == 0:
                    cm1[h][w] = (cm1[h-1][w] * A[h][w])%mod
                else:
                    cm1[h][w] = ((cm1[h-1][w] * cm1[h][w-1])%mod * (pow(cm1[h-1][w-1], mod-2, mod) * A[h][w])%mod)%mod

    # cm2
    for h in range(H):
        for w in range(W-1, -1, -1):
            if h == 0:
                if w == W-1:
                    cm2[h][w] = A[h][w]
                else:
                    cm2[h][w] = (cm2[h][w+1] * A[h][w])%mod
            else:
                if w == W-1:
                    cm2[h][w] = (cm2[h-1][w] * A[h][w])%mod
                else:
                    cm2[h][w] = ((cm2[h-1][w] * cm2[h][w+1])%mod * (pow(cm2[h-1][w+1], mod-2, mod) * A[h][w])%mod)%mod

    # cm3
    for h in range(H-1, -1, -1):
        for w in range(W):
            if h == H-1:
                if w == 0:
                    cm3[h][w] = A[h][w]
                else:
                    cm3[h][w] = (cm3[h][w-1] * A[h][w])%mod
            else:
                if w == 0:
                    cm3[h][w] = (cm3[h+1][w] * A[h][w])%mod
                else:
                    cm3[h][w] = ((cm3[h+1][w] * cm3[h][w-1])%mod * (pow(cm3[h+1][w-1], mod-2, mod) * A[h][w])%mod)%mod

    # cm4
    for h in range(H - 1, -1, -1):
        for w in range(W-1, -1, -1):
            if h == H - 1:
                if w == W-1:
                    cm4[h][w] = A[h][w]
                else:
                    cm4[h][w] = (cm4[h][w + 1] * A[h][w]) % mod
            else:
                if w == W-1:
                    cm4[h][w] = (cm4[h + 1][w] * A[h][w]) % mod
                else:
                    cm4[h][w] = ((cm4[h + 1][w] * cm4[h][w + 1]) % mod * (
                                pow(cm4[h + 1][w + 1], mod - 2, mod) * A[h][w]) % mod) % mod

    for _ in range(int(input())):
        h, w = map(int, input().split())
        h -= 1
        w -= 1
        ans = 1
        if h-1 >= 0 and w-1 >= 0:
            ans = (ans * cm1[h-1][w-1])%mod
        if h-1 >= 0 and w+1 < W:
            ans = (ans * cm2[h - 1][w + 1]) % mod
        if h+1 < H and w-1 >= 0:
            ans = (ans * cm3[h + 1][w - 1]) % mod
        if h+1 < H and w+1 < W:
            ans = (ans * cm4[h + 1][w + 1]) % mod
        print(ans)


if __name__ == '__main__':
    main()
0