1211: Count of [HNOI2004] trees
Time Limit:10 Sec Memory limit:162 MB
submit:1565 solved:512
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 Input
4
2 1) 2 1
Sample Output
2
The following:
Is the weakening version of the obvious annoyance. The practice is basically the same, without high precision is really happy.
Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring
> #include <algorithm> using namespace std;
#define N #define LL Long long int n,s,d[n],su[n],ys[n];
LL Jc[30],ans;
int in () {int x=0; char Ch=getchar ();
while (ch< ' 0 ' | | ch> ' 9 ') Ch=getchar ();
while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();
return x;
} void Init () {jc[0]=jc[1]=1;
for (int i=2; i<=25; i++) jc[i]=jc[i-1]* (LL) i;
for (int i=2; i<=n; i++) {bool f=true; for (int j=2; j<=sqrt (i); j + +) if (! (
I%J) {f=false;
} if (f) su[++su[0]]=i;
}} void Fenjie (int x,int y) {for (int i=1; i<=su[0]; i++) {if (x<=1) return; while (!) (
X%su[i]) ys[i]+=y,x/=su[i];
}} int main () {scanf ("%d", &n);
if (n==1) {int x=in ();
if (!x) Ans=1;
else ans=0; printf ("%d\n", ans);
return 0;
} for (int i=1; i<=n; i++) {d[i]=in ();
if (!d[i]) {printf ("0\n");
return 0;
} D[i]--, s+=d[i];
} if (s!=n-2) {printf ("0\n");
return 0; } init ();
Ans=1;
if (n-2<=25) Fenjie (jc[n-2],1);
for (int i=1; i<=n; i++) Fenjie (jc[d[i]],-1);
for (int i=1, i<=su[0]; i++) while (ys[i]--) ans*=su[i];
printf ("%d\n", ans);
return 0; }