Link to the question: Ultraviolet A 1436-counting heaps
Give the shape of a tree. Now we can label this tree to make sure that the root node's label value is larger than the label value of the child node.
Solution: the idea of queuing with the village name is a uva11174. The final problem is only directly related to the shude structure. F (Root) = (S (Root )? 1 )! (S (1 )? S (2 )??? S (n)
However, the given modulus is not a prime number, so the inverse element cannot be used. Instead, the denominator of the numerator can be split into a prime factor, and then the score is determined by the factor. Because the final answer must be a positive integer, for each qualitative factor, the number of Factorization factors must be greater than or equal to that of the denominator. The final score must be the numerator, then use the quick power to solve the problem.
Pruning. Because there are many times of decomposing a quality factor, pruning of the decomposition function is required. When U is a prime number, the return can be terminated directly.
#include <cstdio>#include <cstring>#include <queue>using namespace std;const int N = 500005;typedef long long ll;int P = 0, prime[N], ispri[N];int n, f[N], vis[N], cnt[N];ll mod;void getPrime (int N) { memset(ispri, 0, sizeof(ispri)); for (int i = 2; i < N; i++) { if (ispri[i]) continue; prime[P++] = i; for (int j = 2 * i; j < N; j += i) ispri[j] = 1; }}void getNode () { memset(cnt, 0, sizeof(cnt)); queue<int> que; for (int i = 1; i <= n; i++) if (vis[i] == 0) que.push(i); while (!que.empty()) { int u = que.front(); que.pop(); cnt[u]++; int v = f[u]; cnt[v] += cnt[u]; vis[v]--; if (vis[v] == 0) que.push(v); }}void init () { memset(vis, 0, sizeof(vis)); scanf("%d%lld", &n, &mod); f[1] = 0; for (int i = 2; i <= n; i++) { scanf("%d", &f[i]); vis[f[i]]++; } getNode(); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) vis[cnt[i]]++;}ll power (ll x, ll m) { ll ans = 1; while (m) { if (m&1) ans = ans * x % mod; x = x * x % mod; m /= 2; } return ans;}void cal (int u, int v) { for (int i = 0; i < P; i++) { int k = prime[i]; while (u % k == 0) { cnt[k] += v; u /= k; } if (ispri[u] == 0) { cnt[u] += v; return; } }}ll solve () { memset(cnt, 0, sizeof(cnt)); for (int i = 2; i <= n; i++) cal(i, 1); for (int i = 2; i <= n; i++) if (vis[i]) cal(i, -vis[i]); ll ans = 1; for (int i = 0; i < P; i++) { ll u = prime[i]; if (cnt[u]) ans = (ans * power(u, cnt[u])) % mod; } return ans;}int main () { getPrime(N); int cas; scanf("%d", &cas); while (cas--) { init(); printf("%lld\n", solve()); } return 0;}