#P8875. [传智杯 #5 初赛] G-二人的花纹纸游戏
[传智杯 #5 初赛] G-二人的花纹纸游戏
Background
Meili bought a special piece of patterned paper. The pattern on the whole paper can be seen as being formed by infinitely repeating a smaller basic pattern horizontally and vertically. Also, this basic pattern is partially transparent and partially opaque.
So, if you place this paper over a book, some characters can be seen while others cannot.
Lianzi suddenly had an idea: if she makes a very, very large table of numbers and covers it with the patterned paper, many numbers will be blocked. What is the sum of the numbers that are not blocked?
Problem Description
In fact, their problem can be converted into the following.
You are given a normal matrix with rows and columns, and a matrix with rows and columns. In , cells with value are black and opaque, and cells with value are transparent.

Using matrix , generate an infinite matrix by repeating cyclically:

Now there are queries. For each query, align the top-left corner of matrix with . At this time, some elements in will be blocked, and others will be visible.

You need to find, in the submatrix of with top-left corner and bottom-right corner , the sum of the elements that are visible. Take the result modulo .
In the example above, and . The sum of visible elements is $a_{2,3}+a_{2,5}+a_{2,6}+a_{3,5}+a_{4,3}+a_{4,5}+a_{4,6}$.
Formal Statement
You are given a normal matrix with rows and columns, and a matrix with rows and columns. Using matrix , generate an infinite matrix :
$$M= \begin{pmatrix} B & B & B &\cdots \\ B & B & B &\cdots \\ B & B & B &\cdots \\ \vdots &\vdots &\vdots & \end{pmatrix} =\begin{pmatrix} b_{1,1} & b_{1,2} & \cdots & b_{1,c} & b_{1,1} & b_{1,2} & \cdots & b_{1,c} & b_{1,1} & \cdots \\ b_{2,1} & b_{2,2} & \cdots & b_{2,c} & b_{2,1} & b_{2,2} & \cdots & b_{2,c} & b_{2,1} & \cdots \\ \vdots & \vdots & & \vdots & \vdots & \vdots & & \vdots & \vdots & \\ b_{r,1} & b_{r,2} & \cdots & b_{r,c} & b_{r,1} & b_{r,2} & \cdots & b_{r,c} & b_{r,1} & \cdots \\ b_{1,1} & b_{1,2} & \cdots & b_{1,c} & b_{1,1} & b_{1,2} & \cdots & b_{1,c} & b_{1,1} & \cdots \\ b_{2,1} & b_{2,2} & \cdots & b_{2,c} & b_{2,1} & b_{2,2} & \cdots & b_{2,c} & b_{2,1} & \cdots \\ \vdots & \vdots & & \vdots & \vdots & \vdots & & \vdots & \vdots & \\ b_{r,1} & b_{r,2} & \cdots & b_{r,c} & b_{r,1} & b_{r,2} & \cdots & b_{r,c} & b_{r,1} & \cdots \\ \vdots & \vdots & & \vdots & \vdots & \vdots & & \vdots & \vdots & \\ \end{pmatrix}$$Now there are queries. Each query gives the top-left coordinate and the bottom-right coordinate of a submatrix. You need to compute:
$$S=\left(\sum_{i=x_1}^{x_2}\sum_{j=y_1}^{y_2}a_{i,j}\times [M_{i-x_1+1,j-y_1+1}=0] \right)\bmod 998{,}244{,}353$$Here denotes the Iverson bracket. If is true, then , otherwise .
Input Format
- The first line contains two positive integers , describing the size of matrix .
- The next lines each contain non-negative integers, describing the elements in .
- The next line contains two positive integers , describing the size of matrix .
- The next lines each contain non-negative integers, describing the elements in .
- The next line contains a positive integer , the number of queries.
- The next lines each contain four positive integers , describing a query. It is guaranteed that and .
Output Format
- Output lines in total. Each line prints the answer for that query.
3 4
1 2 3 4
5 6 7 8
9 10 11 12
2 2
1 0
0 1
2
1 1 3 4
1 2 3 3
40
20
4 4
1 3 2 4
5 4 2 3
4 1 2 3
3 4 4 3
1 3
1 0 0
3
1 1 3 4
2 2 4 4
1 2 3 2
14
17
0
Hint
Explanation for Sample 1

- For the first query, the result is .
- For the second query, the result is .
Constraints and Notes
For all data, it is guaranteed that , , , , and .
Translated by ChatGPT 5