UV 1436-counting heaps (count)

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.