HYSBZ - 1005 明明的烦恼 (挖坑待补)

题目链接点这里

今天好累啊,,脑子一片糊涂的做完这道题,,交了一发wa了,,定眼一看,要写大数,,mdzz,,然而今天感觉身子被掏空,,丝毫不想敲代码了,,留着以后补。。

关于prufe序列的学习强烈推荐这一篇,特别全

树的计数 + prufer序列与Cayley公式 学习笔记


#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"x"<<endl;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<LL,LL> PII;
const double eps=1e-100;
const int MX=1111;
const int P=1e9+7;
int n;
int F[MX],invF[MX];
int quick_pow(int a,int x)
{
    int ans=1;
    while(x)
    {
        if(x&1)ans=(LL)ans*a%P;
        a=(LL)a*a%P;
        x>>=1;
    }
    return ans;
}
void init()
{
    F[0] = 1;
    for(int i = 1; i < MX; i++)  F[i] = ((LL)F[i - 1] * i) % P;
    invF[MX - 1] = quick_pow(F[MX - 1],P - 2);
    for(int i = MX - 2; i >= 0; i--)  invF[i] = (LL)invF[i + 1] * (i + 1) % P;
}
int w[MX];
int tot=0;
int qnum=0;
bool check()
{
    if(tot+n>2*n-2)return 0;
    for(int i=1; i<=n; i++) if(w[i]>n-1||(w[i]==0&&n!=1))return 0;
    return 1;
}
int main()
{
    FIN;
    init();
    while(cin>>n)
    {
        tot=0;
        qnum=0;
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&w[i]);
            if(w[i]==-1)qnum++;
            else tot+=w[i]-1;
        }
        if(check())
        {
            int ans=(LL)quick_pow(qnum,n-2-tot)*F[n-2]%P*invF[n-2-tot]%P;

            for(int i=1; i<=n; i++)if(w[i]!=-1)ans=(LL)ans*invF[w[i]-1];
            if(n==1)ans=1;
            cout<<ans<<endl;
        }
        else cout<<"0"<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值