BZOJ1211 [HNOI2004]树的计数

这个是BZOJ1005的简化版

因为不会爆long long所以不需要高精度了,但是如果直接计算,中间过程会爆long long

一个很好的方法就是分解质因数了,最后算完,把所有因子都乘起来

 1 /**************************************************************
 2     Problem: 1211
 3     User: round_0
 4     Language: C++
 5     Result: Accepted
 6     Time:0 ms
 7     Memory:1276 kb
 8 ****************************************************************/
 9  
10 #include <iostream>
11 #include <cstdio>
12 using namespace std;
13 const int maxn = 305;
14 typedef long long LL;
15 int a[maxn] = {0};
16 int fac[maxn] = {0};
17 int n;
18 void f(int x,int b){
19     while(x>1){
20         for(int j = 2;j<=n;++j)if(x%j==0){
21             fac[j]+=b;
22             x/=j;
23             break;
24         }
25     }
26 }
27 int main()
28 {
29    // freopen("in.txt","r",stdin);
30     scanf("%d",&n);
31     int s = 0;
32     for(int i = 1;i<=n;++i)scanf("%d",&a[i]);
33     if(n==1){
34         if(a[1]!=0)printf("0\n");
35         else printf("1\n");
36         return 0;
37     }
38     for(int i = 1;i<=n;++i){
39         if(a[i]<1){printf("0\n");return 0;}
40         a[i]--;s+=a[i];
41     }
42     if(s!=n-2){printf("0\n");return 0;}
43     for(int i = 2;i<=n-2;++i)f(i,1);
44     for(int i = 1;i<=n;++i)
45         for(int k = 2;k<=a[i];++k)
46             f(k,-1);
47     LL ans = 1;
48     for(int i = 1;i<=n;++i)
49         for(int j = 1;j<=fac[i];++j)
50             ans*=i;
51     printf("%lld\n",ans);
52     return 0;
53 }

 

posted on 2015-08-15 14:24  round_0  阅读(139)  评论(0编辑  收藏  举报

导航