Statement
Scientists are trying to find experimental evidence of quantum string theory. Their newest idea has to do with muons (which we'll call fallen stars). They fall from the cosmos and move so fast they experience a significant level of length contraction. Because of this we can detect them at ground level before they decay into other particles.
The atmosphere can be modeled as a rooted tree made of nodes numbered from to with node as the root. The current theory states that a fallen star falls to the ground in a strange and convoluted way:
- The star starts at the root (in cosmos).
- If the star is in a leaf (defined as a node with no children) then the star is detected at that node (at ground level). By getting detected the star decays and the falling process stops.
- If this is the first time the star is in a node , the wave function of node collapses and the star is teleported instantly into cosmos (into the root).
- If this is not the first time the star is in a node, the many universes theory kicks in and the star chooses one of the node's children uniformly at random and goes there (repeating from step ).
The scientists calculated the probability of detecting a star in each node. They want to check that they did not mess up their computation so they ask you to calculate that yourself to confirm their theories. They said they will not steal your calculation and we should trust them, they are scientists.
Implementation details
This is a function-implementation task. Your program must not read from standard input or write to standard output. You must include the header fallen-star.h and implement the following function, without implementing a main function:
std::vector<int> solve(int N, std::vector<int> parent);
The function solve is called exactly once.
- : the number of nodes in the tree.
- : a vector of length , where is the parent node of node , for .
The function must return a vector of integers where is the probability of detecting the star in node . If the probability is then should be where is the modular inverse of (i.e. ).
Sample grader
The sample grader reads the input from standard input in the following format:
- line : ;
- line : ;
The sample grader calls solve, then prints the returned values to standard output, one per line.
The grader used for evaluation may differ from the sample grader.
Restrictions
- .
- is the parent of node .
- for all .
- is a prime number.
- With the exception of the last subtask, each test case's tree was chosen without taking into account that the values are computed modulo .
- Real life is more complicated, we work on a simplified model...
| # | Points | Restrictions |
|---|---|---|
| 0 | 0 | Examples. |
| 1 | 3 | The atmosphere is a complete binary tree. |
| 2 | 4 | |
| 3 | 6 | Every node has at most children and at most one node at each depth has children. |
| 4 | 5 | and there are at most leaf nodes in the tree. |
| 5 | 7 | Excepting the root, every other node has at most child. |
| 6 | 13 | and every node has at most children. |
| 7 | 14 | |
| 8 | 18 | |
| 9 | 21 | |
| 10 | 9 | The test cases were (specially) designed with knowledge that values are computed modulo . |
Example 1
Sample grader input
5
0 1 1 0
Sample grader output
0
0
873463809
873463809
249561089
Explanation
In the first example nodes and each have a probability of of detecting the star and node has a probability of of detecting the star. Thus, the answers are and . All other nodes have a probability of to detect the star which comes out to .
Example 2
Sample grader input
7
0 1 1 3 4 3
Sample grader output
0
0
187170817
0
0
967049217
842268673
Explanation
In the second example node has a probability to detect the star of around , node a probability to detect the star of around and node a probability to detect the star of around . All other nodes have a probability of to detect the star.