tkj
文章143
标签102
分类0
bzoj 1005: [HNOI2008]明明的烦恼

bzoj 1005: [HNOI2008]明明的烦恼

.

题意:给出标号为1到N的点,以及某些点最终的度数,允许在
任意两点间连线,可产生多少棵度数满足要求的树?
题解:Prüfer序列+组合数学+高精度
和1211差不多。
代码:

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

int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997};
int n,d[1010],s[200],k=0,sum=0,base=10000;
struct num
{
    int len,a[10010];
    num()
    {
        len=1;
        memset(a,0,sizeof(a));
        a[0]=1;
    }
    int& operator[](int x)
    {
        return a[x];
    }
    void print()
    {
        printf("%d",a[len-1]);
        for(int i=len-2;i>=0;i--)
        printf("%04d",a[i]);
        puts("");
    }
};

num operator*(num x,int y)
{
    for(int i=0;i<x.len;i++)
    x[i]*=y;
    for(int i=0;i<x.len;i++)
    {
        x[i+1]+=x[i]/base;
        x[i]%=base;
    }
    while(x[x.len])
    {
        x[x.len]+=x[x.len-1]/base;
        x[x.len-1]%=base;
        x.len++;
    }
    return x;
}
void wk(int x,int y)
{
    for(int j=0;j<168;j++)
    {
        while(x%prime[j]==0)
        {
            x/=prime[j];
            s[j]+=y;
        }
    }
}
int main()
{
    scanf("%d",&n);
    if(n==1)
    {
        int x;
        scanf("%d",&x);
        if(x<1)
        puts("1");
        else
        puts("0");
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&d[i]);
        if(d[i]==-1)
        k++;
        else if(d[i]==0||d[i]>=n)
        {
            puts("0");
            return 0;
        }
        else
        sum+=d[i]-1;
    }
    if(n==2)
    {
        if((d[1]==-1||d[1]==1)&&(d[2]==-1||d[2]==1))
        puts("1");
        else
        puts("0");
        return 0;
    }
    if(sum>n-2)
    {
        puts("0");
        return 0;
    }
//    printf("%d\n",sum);
    for(int i=0;i<sum;i++)
    wk(n-2-i,1);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<d[i];j++)
        wk(j,-1);
    }
    wk(k,n-sum-2);
    num ans;
    for(int i=0;i<200;i++)
    {
        for(int j=0;j<s[i];j++)
        ans=ans*prime[i];
    }
    ans.print();
}
本文作者:tkj
本文链接:https://tkj666.github.io/1/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可