1211: [HNOI2004]樹的計數

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3432  Solved: 1295
[Submit][Status][Discuss]

4
2 1 2 1

2

題解：

$\frac{\left ( n - 2 \right )!}{\prod \left ( d[i] - 1 \right )! }$

 1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<cstring>
5 #include<vector>
6 #include<queue>
7 using namespace std;
8 int d[205];
9 struct node{
10     int m[500005];
11     friend void operator *= (node &a,int b){
12         int x=0;
13         for(int i=1;i<=a.m[0];i++){
14             int y=a.m[i]*b+x;
15             a.m[i]=y%10;
16             x=y/10;
17         }
18         while(x){
19             a.m[++a.m[0]]=x%10;
20             x/=10;
21         }
22     }
23     friend void operator /= (node &a,int b){
24         int x=0;
25         for(int i=a.m[0];i>=1;i--){
26             x+=a.m[i];
27             a.m[i]=x/b;
28             x%=b;
29             x*=10;
30         }
31         while(a.m[a.m[0]]==0&&a.m[0]>1) a.m[0]--;
32     }
33     friend void print(node a){
34         for(int i=a.m[0];i>=1;i--) printf("%d",a.m[i]);
35         puts("");
36     }
37 }ans;
38 int main(){
39     int n,m;
40     scanf("%d",&n);
41     int sum=0;
42     ans.m[0]=ans.m[1]=1;
43     for(int i=1;i<=n;i++){
44         scanf("%d",&d[i]);
45         if(!d[i]&&n!=1){puts("0");return 0;}
46         d[i]--;
47         sum+=d[i];
48     }
49     if(sum!=n-2){puts("0");return 0;}
50     for(int i=1;i<=n-2;i++) ans*=i;
51     //print(ans);
52     for(int i=1;i<=n;i++){
53         if(d[i]>0){
54             for(int j=1;j<=d[i];j++) ans/=j;
55         }
56     }
57     print(ans);
58 }
View Code