#P6712. [THUPC 2019] 摆家具
[THUPC 2019] 摆家具
Description
You have different pieces of furniture that need to be placed into 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 different arrangements in total (note that it is not ).
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 arrangements.
Now you suddenly want to change the layout. Given an initial arrangement, you will repeat the following operation times: each time, you may choose any one piece of furniture and move it to any other room. In each round there are choices (number of ways to choose the furniture times number of ways to choose another room), so in total there are decision sequences. You need to compute the sum of the scores of the resulting arrangements after each decision sequence.
Moreover, we will give queries. In each query, you are given the initial arrangement and , and you need to answer online the sum of scores after the 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 to , and the rooms with distinct integers from to . Suppose in an arrangement, the furniture is placed in room . Then we define the ID of this arrangement as . It can be seen that the IDs of all arrangements are exactly the distinct integers from to .
Also, let .
Input Format
The first line contains three positive integers .
In the next lines, each line contains a positive integer less than , giving the scores of arrangements with IDs in order.
In the next lines, each line contains two non-negative integers. Suppose a line contains (guaranteed ). Then in this query, the ID of the initial arrangement is , and , where is your previous output (for the first query, ).
Adjacent numbers on the same line are separated by one space.
Constraints: , ; ; .
Output Format
For each query, output one line containing a non-negative integer, which is the sum of scores modulo 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 , and .
Initially, furniture is in room , furniture is in room , and furniture is in room . After operation, the possible cases are:
- Move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room . The resulting arrangement ID is , and the score is .
So, the total score over all cases is , and modulo it is .
In the second query, the ID of the initial arrangement is , and .
Initially, furniture is in room , furniture is in room , and furniture is in room . After operations, the possible cases are:
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
- Move furniture to room , then move furniture to room . The resulting arrangement ID is , and the score is .
So, the total score over all cases is , and modulo it is .
In the third query, the ID of the initial arrangement is , and . Initially, furniture is in room , furniture is in room , and furniture is in room .
... (at least lines omitted)
Copyright Information
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
京公网安备 11011102002149号