1005: [HNOI2008] The troubles of the Ming
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3217 Solved: 1290
Description
Ever since I learned the structure of a tree, I have been interested in strange trees ... Give the point labeled 1 to N, and the final degree of some points, allowing the connection between any two points, how many trees can be produced to meet the requirements?
Input
The first behavior n (0 < n < = 1000), the next n rows, the first row of i+1 gives the number of degrees Di, if the number of degrees is not required, then enter-1
Output
An integer that represents the number of different trees that satisfy the requirement, no output 0
Sample Input
31-1-1
Sample Output
2
HINT
两棵树分别为1-2-3;1-3-2
Exercises
Bitter decadent for a noon. All kinds of not ah. So I worshipped the puzzle--very god.
And, by the way, learned a magical tree-free sequence-->prufer sequence.
To write high-precision by high-precision is too lazy to write, so lazy wrote a pressure.
Code:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <algorithm>using namespace STD;#define N 1010#define LL Long Long#define P 100000intn,m=0, s=0, d[n],su[n],ys[n],ans[n*Ten];intIn () {intx=0;CharCh=getchar ();BOOLf=true; while(ch<' 0 '|| Ch>' 9 '){if(ch=='-') f=false; Ch=getchar (); } while(ch>=' 0 '&& ch<=' 9 ') x=x*Ten+ch-' 0 ', Ch=getchar ();if(!f) X=-x;returnx;}voidInit () { for(intI=2; i<=n; i++) {BOOLf=true; for(intj=2; j<=sqrt(i); J + +)if(! (I%J)) {f=false; Break; }if(f) su[++su[0]]=i; }}voidChengintx) { for(intI=1; i<=ans[0]; i++) ans[i]*=x; for(intI=1; i<=ans[0]; i++) ans[i+1]+=ans[i]/p,ans[i]%=p; while(ans[ans[0]+1]) {ans[0]++; ans[ans[0]+1]+=ans[ans[0]]/p; ans[ans[0]]%=p; }}voidFenjie (intXintY) { for(intI=1; i<=x; i++) {intXx=i; for(intj=1; j<=su[0]; J + +) {if(xx<=1) Break; while(! (Xx%su[j])) YS[J]+=Y,XX/=SU[J]; } }}voidOut () {printf("%d", ans[ans[0]]); for(inti=ans[0]-1; i>=1; i--)printf("%05d", Ans[i]);printf("\ n");}intMain () {n=in (); ans[++ans[0]]=1;if(n==1){intX=in ();if(!x)printf("1\n");Else printf("0\n");return 0; } for(intI=1; i<=n; i++) {d[i]=in ();if(!d[i]) {printf("0\n");return 0; }if(d[i]==-1) m++;ElseD[i]--, s+=d[i]; }if(s>n-2){printf("0\n");return 0; } init (); Fenjie (n2,1), Fenjie (n2-s,-1); for(intI=1; i<=n; i++)if(d[i]>0) Fenjie (d[i],-1); for(intI=1; i<=su[0]; i++) while(ys[i]--) Cheng (su[i]); for(intI=1; i<=n-2-S; i++) Cheng (m); Out ();return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Combinatorial math" "high-precision" "Prufer series" "Hnoi 2008" "Bzoj 1005" obviously trouble