Bzoj 1211: Count of [HNOI2004] trees (combinatorial mathematics)

Source: Internet
Author: User

Know Prufer sequence can write ... is to find the permutation of a re-set ... First the strange situation, then the answer is (N-2)!/π (d[i]-1)!

---------------------------------------------------------------------------

#include <cstdio>#include <algorithm>#include <cstring>using namespace std;typedef long Long ll;const int MAXN = n; int D[MAXN], N, m;int P[MAXN], PN, CNT[MAXN];bool F[MAXN];void Init () {pn = 0;memset (f, 0, sizeof f);for (int i = 2; i < MAXN; i++) {if (! F[i])p[pn++] = i;For (int j = 0; J < pn && I * p[j] < MAXN; J + +) {f[i * P[j]] = true;if (i% p[j] = = 0) break;}}}void Calculate (int t, bool Typ) {if (T < 2) return;for (int i = 2; I <= t; i++)for (int j = 0, v = i; j < pn && V! = 1; j + +)for (; v% p[j] = = 0; v/= p[j], Typ cnt[j]++: cnt[j]--);} ll Power (ll x, int t) {LL ret = 1;for (; t; t >>= 1, x *= x)if (T & 1) RET *= x;return ret;}int main () {Init ();m = 0;scanf ("%d", &n);for (int i = 0; i < N; i++) {scanf ("%d", D + i);m + = d[i]-1;}if (m! = N-2) {puts ("0"); return 0;}if (N = = 1 &&!d[0]) {puts ("1"); return 0;}for (int i = 0; i < N; i++) if (!d[i]) {puts ("0"); return 0;}memset (CNT, 0, sizeof CNT);Calculate (M, 1);for (int i = 0; i < N; i++)Calculate (D[i]-1, 0);ll ans = 1;for (int i = 0; i < pn; i++)if (Cnt[i]) ans *= Power (LL (P[i]), cnt[i]);printf ("%lld\n", ans);return 0;}

---------------------------------------------------------------------------

1211: Count of [HNOI2004] tree time limit: ten Sec Memory Limit: 162 MB
Submit: 1752 Solved: 578
[Submit] [Status] [Discuss] Description

A tree with n nodes, with its nodes of V1, v2, ..., vn, known as the degree of the I node VI as DI, asking how many different trees meet such conditions. Given N,D1, D2, ..., DN, programming needs to output the number of trees that meet the D (vi) =di.

Input

The first line is a positive integer n, which indicates that the tree has n nodes. The second line has n numbers, and the number of I represents Di, which is the degree of the first node of the tree. Where 1<=n<=150, the input data ensures that the tree that satisfies the criteria does not exceed 10^17.

Output

How many trees are available for the output to meet the criteria.

Sample Input4
2 1 2 1
Sample Output2HINT

Source

Combinatorial mathematics

Bzoj 1211: Count of [HNOI2004] trees (combinatorial mathematics)

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.