Luogu P2290 [HNOI2004]树的计数 Prufer序列+组合数

最近碰了$prufer$ 序列和组合数。。于是老师留了一道题:P2624 [HNOI2008]明明的烦恼

qwq要用高精。。。

于是我们有了弱化版:P2290 [HNOI2004]树的计数(考一样的可还行OvO)


 

首先前置知识:$Prufer序列$

然后,因为对于一个$ Prufer $序列有$n-2$ 项,而每个点的度数-1是这个点在$ Prufer$ 序列中出现的次数

所以。。。这不是多重集的排列吗(不懂多重集?

所以我们成功了一半(雾)

在计算时会爆$ long \space long $ 所以要拆分成质因数,然后去删除(反正不会T)

#include<cstdio>
#include<iostream>
#define ll long long
#define R register ll
using namespace std;
const int N=155;
inline int g() {
    R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
    do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
}
int n,m,tot,cnt;
int r[N],num[N],pri[N];
ll fac[25],ans=1;
bool v[N];
inline void PRI() {
    for(R i=2;i<=N-5;++i) {
        if(!v[i]) pri[++cnt]=i;
        for(R j=1;j<=cnt&&i*pri[j]<=N-5;++j) {
            v[i*pri[j]]=true; if(i%pri[j]==0) break;
        }
    }
}
inline void calc(ll x,int vl) {
    for(R i=1;i<=cnt;++i) {
        if(x==1) return ;
        while(x%pri[i]==0) num[i]+=vl,x/=pri[i];
    }
}
signed main() { PRI(); fac[0]=fac[1]=1;
    for(R i=2;i<=22;++i) fac[i]=fac[i-1]*i;
    n=g(); if(n==1) {
        R x=g(); if(!x) printf("1\n");
        else printf("0\n");
        return 0;
    } for(R i=1;i<=n;++i) {
        r[i]=g(); if(!r[i]) {printf("0\n"); return 0;}
        --r[i]; tot+=r[i];
    } if(tot!=n-2) {printf("0\n"); return 0;}
    calc(fac[n-2],1); for(R i=1;i<=n;++i) calc(fac[r[i]],-1);
    for(R i=1;i<=cnt;++i) while(--num[i]>=0) ans*=pri[i]; 
    printf("%lld\n",ans); 
} 

2019.05.16

 

转载于:https://www.cnblogs.com/Jackpei/p/10877348.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值