#P3700. [CQOI2017] 小 Q 的表格
[CQOI2017] 小 Q 的表格
Description
Xiao Q is a programmer.
As a young programmer, Xiao Q is always bullied by old C, who often assigns him troublesome tasks. Whenever Xiao Q does not know how to solve them, he has to ask you for help.
To complete the task, Xiao Q needs to make a table with infinitely many rows and infinitely many columns, both indexed starting from . Every cell in the table contains an integer. For convenience, Xiao Q denotes the integer in row , column as . The table must satisfy the following conditions:
- For any positive integers , it must hold that .
- For any positive integers , it must hold that .
Initially, the numbers in the table are very regular: the number in row , column is exactly , which obviously satisfies the two conditions above. To complete the task, Xiao Q will keep modifying the numbers in the table. Each time a cell is changed, in order to keep the table satisfying the two conditions, Xiao Q must also update all cells that can be affected by this modification to appropriate values. By some mysterious power, it is guaranteed that after each round of modification all entries remain integers. Xiao Q also needs to query, at any time, the sum of all numbers in the finite region of the first rows and first columns; since the answer may be large, it suffices to output the result .
Input Format
The first line contains two integers , indicating that there are operations, and that all row and column indices mentioned in the operations and queries do not exceed .
Then follow lines, each containing four integers , meaning: set the number in row , column to , then update all cells that can be affected by this change, with the guarantee that after the modification all entries are still integers. After the modification is completed, compute the sum of all numbers in the first rows and first columns.
Output Format
Output lines. After each operation, output one line containing the answer modulo .
3 3
1 1 1 2
2 2 4 3
1 2 4 2
9
36
14
4 125
1 2 4 8
1 3 9 27
1 4 16 64
1 5 25 125
2073
316642
12157159
213336861
Hint
Sample Explanation #1
Initially, the top-left submatrix is as shown on the left. There is no change after the first operations. After the rd operation, the table becomes as shown on the right.
1 2 3 2 4 6
2 4 6 4 4 12
3 6 9 6 12 9
Constraints
For of the testdata, , , .

Translated by ChatGPT 5
京公网安备 11011102002149号