#P6712. [THUPC 2019] 摆家具

[THUPC 2019] 摆家具

Description

You have kk different pieces of furniture that need to be placed into nn different rooms. Assume each room is large enough, and we only care about which room each piece of furniture is in (not the exact position inside the room). Then there are nkn^k different arrangements in total (note that it is not knk^n).

Arranging furniture is also a kind of knowledge, and you cannot just place them randomly. For each arrangement, we can give it a score. For example, an arrangement that puts the dining table in the bathroom, or one bedroom has two beds while another bedroom has no bed, would get a relatively low score. Since the score is not independent across each piece of furniture and each room, we will input the scores for all nkn^k arrangements.

Now you suddenly want to change the layout. Given an initial arrangement, you will repeat the following operation TT times: each time, you may choose any one piece of furniture and move it to any other room. In each round there are k(n1)k(n-1) choices (number of ways to choose the furniture times number of ways to choose another room), so in total there are kT(n1)Tk^T(n-1)^T decision sequences. You need to compute the sum of the scores of the resulting arrangements after each decision sequence.

Moreover, we will give qq queries. In each query, you are given the initial arrangement and TT, and you need to answer online the sum of scores after the kT(n1)Tk^T(n-1)^T decision sequences (modulo). See the input and output formats for details.

We define the ID of an arrangement as follows:

We label the furniture with distinct integers from 00 to k1k-1, and the rooms with distinct integers from 00 to n1n-1. Suppose in an arrangement, the furniture ii is placed in room pip_i. Then we define the ID of this arrangement as i=0k1pini\sum_{i=0}^{k-1} p_i n^i. It can be seen that the IDs of all nkn^k arrangements are exactly the distinct integers from 00 to nk1n^k - 1.

Also, let P=998244353P=998244353.

Input Format

The first line contains three positive integers n,k,qn, k, q.

In the next nkn^k lines, each line contains a positive integer less than PP, giving the scores of arrangements with IDs 0,1,,nk10, 1, \dots, n^k - 1 in order.

In the next qq lines, each line contains two non-negative integers. Suppose a line contains a,ba, b (guaranteed 0a<nk,0b<P0 \leq a < n^k, 0 \leq b < P). Then in this query, the ID of the initial arrangement is aa, and T=brmodPT=b \cdot r \bmod P, where rr is your previous output (for the first query, r=1r=1).

Adjacent numbers on the same line are separated by one space.

Constraints: n2n \geq 2, k1k \geq 1; nk106n^k \leq 10^6; q5×105q \leq 5 \times 10^5.

Output Format

For each query, output one line containing a non-negative integer, which is the sum of scores modulo PP for that query.

2 3 3
1
10
100
1000
998244245
100000
1000000
10000000
0 1
0 1
1 233
2
2202003
444957911

Hint

Sample Explanation

In the first query, the ID of the initial arrangement is 00, and T=1T=1.

Initially, furniture 00 is in room 00, furniture 11 is in room 00, and furniture 22 is in room 00. After 11 operation, the possible cases are:

  • Move furniture 00 to room 11. The resulting arrangement ID is 11, and the score is 1010.
  • Move furniture 11 to room 11. The resulting arrangement ID is 22, and the score is 100100.
  • Move furniture 22 to room 11. The resulting arrangement ID is 44, and the score is 998244245998244245.

So, the total score over all cases is 998244355998244355, and modulo PP it is 22.

In the second query, the ID of the initial arrangement is 00, and T=2T=2.

Initially, furniture 00 is in room 00, furniture 11 is in room 00, and furniture 22 is in room 00. After 22 operations, the possible cases are:

  • Move furniture 00 to room 11, then move furniture 00 to room 00. The resulting arrangement ID is 00, and the score is 11.
  • Move furniture 00 to room 11, then move furniture 11 to room 11. The resulting arrangement ID is 33, and the score is 10001000.
  • Move furniture 00 to room 11, then move furniture 22 to room 11. The resulting arrangement ID is 55, and the score is 100000100000.
  • Move furniture 11 to room 11, then move furniture 00 to room 11. The resulting arrangement ID is 33, and the score is 10001000.
  • Move furniture 11 to room 11, then move furniture 11 to room 00. The resulting arrangement ID is 00, and the score is 11.
  • Move furniture 11 to room 11, then move furniture 22 to room 11. The resulting arrangement ID is 66, and the score is 10000001000000.
  • Move furniture 22 to room 11, then move furniture 00 to room 11. The resulting arrangement ID is 55, and the score is 100000100000.
  • Move furniture 22 to room 11, then move furniture 11 to room 11. The resulting arrangement ID is 66, and the score is 10000001000000.
  • Move furniture 22 to room 11, then move furniture 22 to room 00. The resulting arrangement ID is 00, and the score is 11.

So, the total score over all cases is 22020032202003, and modulo PP it is 22020032202003.

In the third query, the ID of the initial arrangement is 11, and T=513066699T=513066699. Initially, furniture 00 is in room 11, furniture 11 is in room 00, and furniture 22 is in room 00.

... (at least 35130666993^{513066699} lines omitted)

From THUPC (THU Programming Contest, Tsinghua University Programming Contest) 2019.

Resources such as solutions can be found at https://github.com/wangyurzee7/THUPC2019.

Translated by ChatGPT 5