• 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
    ");
            else printf("0
    ");
            return 0;
        } for(R i=1;i<=n;++i) {
            r[i]=g(); if(!r[i]) {printf("0
    "); return 0;}
            --r[i]; tot+=r[i];
        } if(tot!=n-2) {printf("0
    "); 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
    ",ans); 
    } 

    2019.05.16

  • 相关阅读:
    《数字图像处理原理与实践(MATLAB版)》一书之代码Part5
    编程算法
    SVM 输出分类概率(python)
    mysql 数据库通过拷贝文件恢复方法
    ubuntu取消自动登录
    迁移mysql数据位置
    No module named 'lsb_release'
    python3 ssl导入失败
    Linux tar: Cannot change ownership to [..]: Permission denied
    树莓派蓝牙
  • 原文地址:https://www.cnblogs.com/Jackpei/p/10877348.html
Copyright © 2020-2023  润新知