BZOJ 1211: [HNOI2004]树的计数 prufer序列

直接套用 prufer 序列公式即可,但是要特判不合法的情况. 

code: 

#include <cstdio> 
#include <algorithm>  
#define ll long long 
#define N 200  
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std; 
int bu[N];  
void update(int x,int v) 
{ 
    for(int i=2;i*i<=x;++i)  
        for(;x%i==0;x/=i) bu[i]+=v;                
    if(x>1) bu[x]+=v;   
}
int main() 
{ 
    // setIO("input");     
    int i,j,n,s=0; 
    scanf("%d",&n);   
    for(i=2;i<=n-2;++i) update(i,1);   
    for(i=1;i<=n;++i) 
    {
        int x; 
        scanf("%d",&x),s+=x;     
        if(x==0&&n!=1) 
        {
            printf("0
"); 
            return 0;  
        }
        for(j=2;j<x;++j) update(j,-1);  
    } 
    if(s!=2*(n-1))  printf("0
"); 
    else 
    {
        ll ans=1;   
        for(i=1;i<N;++i) 
        {
            for(j=1;j<=bu[i];++j) ans*=i;  
        }
        printf("%lld
",ans);  
    }  
    return 0; 
}

  

原文地址:https://www.cnblogs.com/guangheli/p/12237645.html