Fallen Star

Time limit: 0.8sMemory limit: 256MBInput: Output:

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 NN nodes numbered from 00 to N−1N-1 with node 00 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 vv, the wave function of node vv 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 22).

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.

  • NN: the number of nodes in the tree.
  • parentparent: a vector of length N−1N - 1, where parent[i−1]parent[i - 1] is the parent node of node ii, for 1≤i<N1 \leq i < N.

The function must return a vector of integers ansans where ans[i]ans[i] is the probability of detecting the star in node ii. If the probability is pq\frac{p}{q} then ans[i]ans[i] should be p⋅q−1mod  998 244 353p \cdot q ^ {-1} \mod 998 \ 244 \ 353 where q−1q ^ {-1} is the modular inverse of qq (i.e. q⋅q−1≡1(mod998 244 353)q \cdot q ^ {-1} \equiv 1 \pmod{998 \ 244 \ 353}).

Sample grader

The sample grader reads the input from standard input in the following format:

  • line 11: NN;
  • line 22: parent[0] parent[1] … parent[N−2]parent[0] \ parent[1] \ \ldots \ parent[N-2];

The sample grader calls solve, then prints the NN returned values to standard output, one per line.

The grader used for evaluation may differ from the sample grader.

Restrictions

  • 2≤N≤3 0002 \leq N \leq 3 \ 000.
  • parent[i−1]parent[i - 1] is the parent of node ii.
  • 0≤parent[i−1]<i0 \leq parent[i - 1] < i for all 0<i<N0 < i < N.
  • 998 244 353998 \ 244 \ 353 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 998 244 353998 \ 244 \ 353.
  • 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 N≤20N \leq 20
3 6 Every node has at most 22 children and at most one node at each depth has children.
4 5 N≤100N \leq 100 and there are at most 44 leaf nodes in the tree.
5 7 Excepting the root, every other node has at most 11 child.
6 13 N≤1 000N \leq 1 \ 000 and every node has at most 22 children.
7 14 N≤300N \leq 300
8 18 N≤1 000N \leq 1 \ 000
9 21 N≤3 000N \leq 3 \ 000
10 9 The test cases were (specially) designed with knowledge that values are computed modulo 998 244 353998 \ 244 \ 353.

Example 1

Sample grader input

5
0 1 1 0

Sample grader output

0
0
873463809
873463809
249561089

Explanation

In the first example nodes 22 and 33 each have a probability of 18\frac{1}{8} of detecting the star and node 44 has a probability of 34\frac{3}{4} of detecting the star. Thus, the answers are 1⋅8−1≡1⋅873 463 809≡873 463 809(mod998 244 353)1 \cdot 8 ^ {-1} \equiv 1 \cdot 873 \ 463 \ 809 \equiv 873 \ 463 \ 809 \pmod{998 \ 244 \ 353} and 3⋅4−1≡3⋅748 683 265≡249 561 089(mod998 244 353)3 \cdot 4 ^ {-1} \equiv 3 \cdot 748 \ 683 \ 265 \equiv 249 \ 561 \ 089 \pmod{998 \ 244 \ 353}. All other nodes have a probability of 01\frac{0}{1} to detect the star which comes out to 0⋅1−1≡0⋅1≡0(mod998 244 353)0 \cdot 1 ^ {-1} \equiv 0 \cdot 1 \equiv 0 \pmod{998 \ 244 \ 353}.

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 22 has a probability to detect the star of around 81.25%81.25\%, node 55 a probability to detect the star of around 3.125%3.125\% and node 66 a probability to detect the star of around 15.625%15.625\%. All other nodes have a probability of 0%0\% to detect the star.

Log In or Sign Up to be able to send submissions!