BZOJ 1211 [HNOI2004]树的计数

题目链接

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

思路

树转化成prufer序列,每个点在prufer序列的个数就是度数-1,因此答案就是

n!ni=1(di1)!n!∏i=1n(di−1)!

注意这道题需要分解质因数,这样就不需要写高精度了。

代码

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

const int maxn=150;

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],tot;

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;
        }
      tot+=d[i];
    }
  if(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 j=2; j<=n-2; ++j)
    {
      ans[j]=p[n-2][j];
    }
  for(int i=1; i<=n; ++i)
    {
      for(int j=2; j<=d[i]-1; ++j)
        {
          ans[j]-=p[d[i]-1][j];
        }
    }
  long long r=1;
  for(int i=2; i<=n; ++i)
    {
      if(ans[i])
        {
          for(int j=1; j<=ans[i]; ++j)
            {
              r*=i;
            }
        }
    }
  printf("%lld\n",r);
  return 0;
}

转载于:https://www.cnblogs.com/Canopus-wym/p/10376170.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值