zoukankan      html  css  js  c++  java
  • [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;
        }
    }
    
  • 相关阅读:
    关于IOS6屏幕旋转的问题
    学习备忘录
    关于多种手势问题
    关于 IOS中 绘制图形的问题
    爱的意义
    IOS支付SDK
    IOS开发特别备注
    ios相关教程
    iOS7光标问题
    收藏网站
  • 原文地址:https://www.cnblogs.com/mollnn/p/12300930.html
Copyright © 2011-2022 走看看