#P10890. 【烂题杯 Round 1】可持久化糖果树
【烂题杯 Round 1】可持久化糖果树
Problem Description
Xiao A has a candy tree with nodes, numbered . In each node, there are children, numbered . Each child can make wishes, numbered . For the -th wish of the -th child in node , the child can get candies. We call the candy tree that has not been modified version .
A child is called happy if and only if, after making rounds of wishes, the total number of candies she can get is divisible by , so that the candies can be evenly shared among her and her parents. That is, the -th child in node is happy if and only if .
A node is called joyful if and only if all the children in it are happy.
Xiao A can cast magic: he will perform modifications, indexed from . The -th modification is described by , meaning that in version of the tree, for all nodes and all children, the number of candies obtained from the -th wish is multiplied by , producing version of the tree. You are required to output how many nodes are joyful in each version of the tree.
Input Format
The first line contains integers , , and , representing the number of nodes, the number of children in each node, and the number of wishes per child.
Then read lines, each containing integers, representing , indexed starting from .
Then one line contains a positive integer , representing the number of modifications.
Then read lines. The -th line contains three positive integers , meaning that in version of the tree, for all nodes and all children, the number of candies obtained from the -th wish is multiplied by , producing version of the tree, indexed starting from .
Output Format
Output lines, each containing a positive integer. The -th line denotes the number of joyful nodes in version of the tree.
2 2 3
1 2 3 4 5 6
7 8 9 10 11 12
5
0 1 13
0 2 14
1 2 15
1 3 16
2 3 17
2
2
0
0
2
0
10 1 2
568508003 500481779
296073373 42467215
878878423 182698953
810051825 300278778
506619835 300576052
878109763 816209514
722729481 557555287
810227870 524124026
693592304 92818139
971977946 139368888
3
0 1 524124026
0 1 500481779
0 2 816209514
1
6
6
2
Hint
Constraints:
For of the testdata, , .
For of the testdata, , .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, .
For of the testdata, , , , , . For the -th modification, , , .
The input and output are large, so a fast I/O method is required.
Translated by ChatGPT 5