1. 程式人生 > >bzoj 1211 [HNOI2004]樹的計數

bzoj 1211 [HNOI2004]樹的計數

div spa 題解 pri rod void mes pos while

[HNOI2004]樹的計數

Description

一個有n個結點的樹,設它的結點分別為v1, v2, …, vn,已知第i個結點vi的度數為di,問滿足這樣的條件的不同的樹有多少棵。給定n,d1, d2, …, dn,編程需要輸出滿足d(vi)=di的樹的個數。

Input

第一行是一個正整數n,表示樹有n個結點。第二行有n個數,第i個數表示di,即樹的第i個結點的度數。其中1<=n<=150,輸入數據保證滿足條件的樹不超過10^17個。

Output

輸出滿足條件的樹有多少棵。

Sample Input

4
2 1 2 1

Sample Output

2

題解

prufer編碼,水題,不知道哪裏wrong了,答案是(n-2)!/∏ (di-1)!

 1 #include<cstring>
 2 #include<cmath>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstdio>
 6 
 7 #define N 157
 8 #define ll long long
 9 using namespace std;
10 inline int
read() 11 { 12 int x=0,f=1;char ch=getchar(); 13 while(ch>9||ch<0){if (ch==-) f=-1;ch=getchar();} 14 while(ch<=9&&ch>=0) 15 { 16 x=(x<<3)+(x<<1)+ch-0; 17 ch=getchar(); 18 } 19 return x*f; 20 } 21 22 int n;
23 int a[157]; 24 int sz,pri[N],num[N],boo[N]; 25 26 void solve_fj() 27 { 28 for (int i=2;i<=n;i++) 29 { 30 if (!boo[i]) pri[++sz]=i; 31 for (int j=1;j<=sz&&pri[j]*i<=n;j++) 32 { 33 int t=pri[j]*i; 34 boo[t]=1; 35 if (i%pri[j]==0) break; 36 } 37 } 38 for (int i=2;i<=n-2;i++) 39 { 40 int x=i; 41 for (int j=1;j<=sz;j++) 42 while(x%pri[j]==0) 43 { 44 num[j]++; 45 x/=pri[j]; 46 } 47 } 48 } 49 ll ksm(ll a,int b) 50 { 51 ll res=1LL; 52 while(b) 53 { 54 if (b%2) res=res*a; 55 a=a*a; 56 b>>=1; 57 } 58 return res; 59 } 60 void solve() 61 { 62 for (int t=1;t<=n;t++) 63 { 64 if (a[t]<=2) continue; 65 for (int i=2;i<=a[t]-1;i++) 66 { 67 int x=i; 68 for (int j=1;j<=sz;j++) 69 while(x%pri[j]==0) 70 { 71 num[j]--; 72 x/=pri[j]; 73 } 74 } 75 } 76 ll ans=1; 77 for (int i=1;i<=sz;i++) 78 ans*=ksm(pri[i],num[i]); 79 printf("%lld",ans); 80 } 81 int main() 82 { 83 n=read();for (int i=1;i<=n;i++) a[i]=read(); 84 solve_fj(); 85 86 int num=0; 87 for (int i=1;i<=n;i++) 88 num+=a[i]-1; 89 if (num!=n-2) printf("0"); 90 else solve(); 91 }

bzoj 1211 [HNOI2004]樹的計數