Dreamy Putata

시간 제한6초메모리 제한2048 MB

요약
각 칸마다 주어진 확률로 상하좌우로 움직이는 토러스 격자(m은 최대 5)에서, 한 칸의 확률을 바꾸는 갱신과 두 칸 사이의 기대 도달 시간을 묻는 질의를 10^9+7로 나눈 값으로 처리한다.
난이도

어려움10점 중 9점

유형
수학, 행렬, 동적 계획법, 세그먼트 트리
정답자
아직 제출이 없습니다

문제

Putata is dreaming that he got lost in a phantom grid world of size n×mn \times m. The rows and columns of the grid are numbered from 00 to n−1n - 1 and 00 to m−1m - 1, respectively. Putata has no idea how to escape from the phantom world, so he decides to walk randomly. Assuming Putata is now at (x,y)(x, y), he will:

  • Move to (x,(y−1) mod m)(x, (y - 1) \bmod m) with probability ℓ(x,y)100\frac{\ell(x, y)}{100}.
  • Move to (x,(y+1) mod m)(x, (y + 1) \bmod m) with probability r(x,y)100\frac{r(x, y)}{100}.
  • Move to ((x−1) mod n,y)((x - 1) \bmod n, y) with probability u(x,y)100\frac{u(x, y)}{100}.
  • Move to ((x+1) mod n,y)((x + 1) \bmod n, y) with probability d(x,y)100\frac{d(x, y)}{100}.

You need to perform qq operations. Each operation is one of the following:

  • "1 xx yy cℓ\mathit{c\ell} cr\mathit{cr} cu\mathit{cu} cd\mathit{cd}}' (0≤x<n0 \leq x < n, 0≤y<m0 \leq y < m, 1≤cℓ,cr,cu,cd≤1001 \leq \mathit{c\ell}, \mathit{cr}, \mathit{cu}, \mathit{cd} \leq 100, cℓ+cr+cu+cd=100\mathit{c\ell} + \mathit{cr} + \mathit{cu} + \mathit{cd} = 100): Change the values of ℓ(x,y)\ell(x, y), r(x,y)r(x, y), u(x,y)u(x, y), and d(x,y)d(x, y) into cℓ\mathit{c\ell}, cr\mathit{cr}, cu\mathit{cu}, and cd\mathit{cd}, respectively.
  • "2 sx\mathit{sx} sy\mathit{sy} tx\mathit{tx} ty\mathit{ty}" (0≤sx,tx<n0 \leq \mathit{sx}, \mathit{tx} < n, 0≤sy,ty<m0 \leq \mathit{sy}, \mathit{ty} < m, (sx,sy)≠(tx,ty)(\mathit{sx}, \mathit{sy}) \neq (\mathit{tx}, \mathit{ty})): Assuming Putata is now at (sx,sy)(\mathit{sx}, \mathit{sy}), he is wondering what is the expected number of steps that he will take when he reaches the target position (tx,ty)(\mathit{tx}, \mathit{ty}) for the first time.

Please write a program to answer his questions.

입력

The first line of the input contains two integers nn and mm (3≤n≤1053 \leq n \leq 10^5, 3≤m≤53 \leq m \leq 5) denoting the size of the phantom grid world.

In the next nn lines, the ii-th line contains mm integers ℓ(i−1,0),ℓ(i−1,1),…,ℓ(i−1,m−1)\ell(i - 1, 0), \ell(i - 1, 1), \ldots, \ell(i - 1, m - 1) (1≤i≤n1 \leq i \leq n, 1≤ℓ(⋅,⋅)≤1001 \leq \ell(\cdot, \cdot) \leq 100).

In the next nn lines, the ii-th line contains mm integers r(i−1,0),r(i−1,1),…,r(i−1,m−1)r(i - 1, 0), r(i - 1, 1), \ldots, r(i - 1, m - 1) (1≤i≤n1 \leq i \leq n, 1≤r(⋅,⋅)≤1001 \leq r(\cdot, \cdot) \leq 100).

In the next nn lines, the ii-th line contains mm integers u(i−1,0),u(i−1,1),…,u(i−1,m−1)u(i - 1, 0), u(i - 1, 1), \ldots, u(i - 1, m - 1) (1≤i≤n1 \leq i \leq n, 1≤u(⋅,⋅)≤1001 \leq u(\cdot, \cdot) \leq 100).

In the next nn lines, the ii-th line contains mm integers d(i−1,0),d(i−1,1),…,d(i−1,m−1)d(i - 1, 0), d(i - 1, 1), \ldots, d(i - 1, m - 1) (1≤i≤n1 \leq i \leq n, 1≤d(⋅,⋅)≤1001 \leq d(\cdot, \cdot) \leq 100).

It is guaranteed that ℓ(i,j)+r(i,j)+u(i,j)+d(i,j)=100\ell(i, j) + r(i, j) + u(i, j) + d(i, j) = 100 holds for all pairs of (i,j)(i, j) where 0≤i<n0 \leq i < n and 0≤j<m0 \leq j < m.

The next line contains a single integer qq (1≤q≤3⋅1041 \leq q \leq 3 \cdot 10^4) denoting the number of operations.

Each of the next qq lines describes an operation in the format described in the statement above.

출력

For each test query, print a single line containing an integer: the expected number of steps that Putata will take when he reaches the target position (tx,ty)(\mathit{tx}, \mathit{ty}) for the first time.

More precisely, assuming the reduced fraction of the answer is pq\frac{p}{q}, you should output the minimum non-negative integer rr such that q⋅r≡p(mod109+7)q \cdot r \equiv p \pmod{10^9 + 7}. You may safely assume that such rr always exists in all test cases.

예제1

  1. 예제 1

    입력
    4 3
    1 2 3
    4 5 6
    7 8 9
    10 11 12
    23 24 25
    26 27 28
    29 30 31
    32 33 34
    10 11 12
    13 14 15
    16 17 18
    19 20 21
    66 63 60
    57 54 51
    48 45 42
    39 36 33
    4
    2 0 1 1 1
    2 0 0 3 2
    1 1 1 25 25 25 25
    2 0 0 3 2
    
    예상 출력
    76426175
    344136684
    555192113