[HNOI2004] 树的计数

给定树每个节点的 degree,问满足条件的树的数目。 (nleq 150, ans leq 10^{17})

Solution

注意特判各种坑点

  • (sum d_i - 1 = n-2),否则非法

  • (d_i = 0),非法

#include <bits/stdc++.h>
using namespace std;

#define int long long
int f[155],n,d[155],isp[155],cnt[155],ans=1;

void push(int x) {
    for(int i=2;i<=n;i++) if(isp[i]) {
        while(x%i==0) x/=i,cnt[i]++;
    }
}

void pop(int x) {
    for(int i=2;i<=n;i++) if(isp[i]) {
        while(x%i==0) x/=i,cnt[i]--;
    }
}

signed main() {
    cin>>n;
    int sum=0;
    for(int i=1;i<=n;i++) cin>>d[i], sum+=d[i]-1;
    if(sum!=n-2) {cout<<0; return 0;}
    for(int i=1;i<=n;i++) {
        int flag=1;
        for(int j=2;j<=sqrt(i);j++) {
            if(i%j==0) flag=0;
        }
        if(flag) isp[i]=1;
    }
    if(n==1) {
        if(d[1]==0) cout<<1<<endl;
        else cout<<0<<endl;
    }
    else {
        for(int i=1;i<=n;i++) if(d[i]==0) {cout<<0; return 0;}
        for(int i=2;i<=n-2;i++) push(i);
        for(int i=1;i<=n;i++) for(int j=2;j<=d[i]-1;j++) pop(j);
        for(int i=1;i<=n;i++) while(cnt[i]) ans*=i, --cnt[i];
        cout<<ans;
    }
}
原文地址:https://www.cnblogs.com/mollnn/p/12300930.html