BZOJ 1005 [HNOI2008]明明的烦恼

题目链接

https://www.lydsy.com/JudgeOnline/problem.php?id=1005

思路

由于这是无根树,因此我们把树转化成prufer序列,由于prufer序列的性质,很容易得到满足条件prufer序列的个数为:

(n2sum)sum!cnti=1(di1)!(ncnt)n2sum ( n − 2 s u m ) s u m ! ∏ i = 1 c n t ( d i − 1 ) ! ⋅ ( n − c n t ) n − 2 − s u m

此处 sum=ni=1[di1]×(di1),cnt=ni=1[di1] s u m = ∑ i = 1 n [ d i ≠ − 1 ] × ( d i − 1 ) , c n t = ∑ i = 1 n [ d i ≠ − 1 ]

化简得到:

(n2)!(n2sum)!cnti=1(di1)!(ncnt)n2sum ( n − 2 ) ! ( n − 2 − s u m ) ! ∏ i = 1 c n t ( d i − 1 ) ! ⋅ ( n − c n t ) n − 2 − s u m

注意这道题要高精度乘法。

代码

#include <cstdio>
#include <cmath>
#include <algorithm>

const int maxn=1000;
const int maxd=100000;

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

int n,d[maxn+10],p[maxn+10][maxn+10],ans[maxn+10];
int bignum[maxd+10],hi,tot,cnt,sum;

inline int times(int v)
{
  int r=0;
  for(int i=1; i<=hi; ++i)
    {
      bignum[i]=bignum[i]*v+r;
      r=bignum[i]/10;
      bignum[i]%=10;
    }
  while(r)
    {
      bignum[++hi]=r%10;
      r/=10;
    }
  return 0;
}

int main()
{
  n=read();
  if(n==1)
    {
      int x=read();
      if(x<=0)
        {
          puts("1");
        }
      else
        {
          puts("0");
        }
      return 0;
    }
  for(int i=1; i<=n; ++i)
    {
      d[i]=read();
      if(!d[i])
        {
          puts("0");
          return 0;
        }
      else if(d[i]==-1)
        {
          tot=-1;
        }
      else
        {
          if(tot!=-1)
            {
              tot+=d[i];
            }
          sum+=d[i]-1;
          ++cnt;
        }
    }
  if((tot!=-1)&&(tot!=2*n-2))
    {
      puts("0");
      return 0;
    }
  for(int i=2; i<=n; ++i)
    {
      int v=i;
      for(int j=2; j<=sqrt(i); ++j)
        {
          while(v%j==0)
            {
              v/=j;
              ++p[i][j];
            }
          if(v==1)
            {
              break;
            }
        }
      if(v!=1)
        {
          ++p[i][v];
        }
    }
  for(int i=2; i<=n; ++i)
    {
      for(int j=2; j<=i; ++j)
        {
          p[i][j]+=p[i-1][j];
        }
    }
  for(int i=2; i<=n-2; ++i)
    {
      ans[i]=p[n-2][i]-p[n-2-sum][i];
    }
  for(int i=1; i<=n; ++i)
    {
      if(d[i]==-1)
        {
          continue;
        }
      for(int j=2; j<=d[i]-1; ++j)
        {
          ans[j]-=p[d[i]-1][j];
        }
    }
  hi=1;
  bignum[1]=1;
  for(int i=2; i<=n; ++i)
    {
      for(int j=1; j<=ans[i]; ++j)
        {
          times(i);
        }
    }
  for(int i=1; i<=n-2-sum; ++i)
    {
      times(n-cnt);
    }
  for(int i=hi; i; --i)
    {
      printf("%d",bignum[i]);
    }
  puts("");
  return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值