結果

問題 No.1141 田グリッド
ユーザー 👑 tamatotamato
提出日時 2020-07-31 21:58:51
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 493 ms / 2,000 ms
コード長 2,901 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 87,296 KB
実行使用メモリ 102,468 KB
最終ジャッジ日時 2023-09-20 23:13:27
合計ジャッジ時間 13,214 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,404 KB
testcase_01 AC 74 ms
71,492 KB
testcase_02 AC 73 ms
71,456 KB
testcase_03 AC 74 ms
71,760 KB
testcase_04 AC 74 ms
71,772 KB
testcase_05 AC 76 ms
71,620 KB
testcase_06 AC 74 ms
71,508 KB
testcase_07 AC 72 ms
71,496 KB
testcase_08 AC 128 ms
78,616 KB
testcase_09 AC 117 ms
78,240 KB
testcase_10 AC 131 ms
78,544 KB
testcase_11 AC 134 ms
78,312 KB
testcase_12 AC 135 ms
78,388 KB
testcase_13 AC 323 ms
91,924 KB
testcase_14 AC 408 ms
102,468 KB
testcase_15 AC 487 ms
82,988 KB
testcase_16 AC 487 ms
82,940 KB
testcase_17 AC 485 ms
83,416 KB
testcase_18 AC 472 ms
83,432 KB
testcase_19 AC 475 ms
83,884 KB
testcase_20 AC 472 ms
83,928 KB
testcase_21 AC 471 ms
82,948 KB
testcase_22 AC 474 ms
82,852 KB
testcase_23 AC 479 ms
82,976 KB
testcase_24 AC 481 ms
82,856 KB
testcase_25 AC 471 ms
82,920 KB
testcase_26 AC 493 ms
83,572 KB
testcase_27 AC 492 ms
83,920 KB
testcase_28 AC 471 ms
82,912 KB
testcase_29 AC 478 ms
83,196 KB
testcase_30 AC 487 ms
84,048 KB
testcase_31 AC 465 ms
82,604 KB
testcase_32 AC 477 ms
83,880 KB
testcase_33 AC 477 ms
83,584 KB
権限があれば一括ダウンロードができます

ソースコード

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