1005: [HNOI2008]明明的烦恼

1005: [HNOI2008]明明的烦恼

Time Limit: 1 Sec   Memory Limit: 162 MB
Submit: 4640   Solved: 1831
[ Submit][ Status][ Discuss]

Description

  自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在
任意两点间连线,可产生多少棵度数满足要求的树?

Input

  第一行为N(0 < N < = 1000),
接下来N行,第i+1行给出第i个节点的度数Di,如果对度数不要求,则输入-1

Output

  一个整数,表示不同的满足要求的树的个数,无解输出0

Sample Input

3
1
-1
-1

Sample Output

2

HINT

  两棵树分别为1-2-3;1-3-2

Source

[ Submit][ Status][ Discuss]

首先要知道一种叫无根树的prufer序列的东西,构造方法就不说了
每个prufer序列严格对应一棵无根树,且原树中度数为i的节点会恰好在这个序列中出现i - 1次,最后序列长n - 2
那么对于严格确定次数的点,可以用组合数学统计方案
比如一共k个点,度数分别为d1,d2,d3,...dk
那么方案就是C(tot,d1 - 1) * C(tot - d1 - 1,d2 - 1) * .........这里tot = n - 2
然后还有无限制的点,设一共有m个,要填cnt个位置,答案就上式乘以m^cnt即可
原题没有模数,就需要高精度了,但这么多组合数,需要除法计算
高精度除法不好写,可以考虑质因数分解
质因数分解懒得写,可以像个办法只写加法和乘法
只需要O(1000*1000)预处理所有组合数就行了
不过高精度运算还得乘上一个挺大的常数,取决于高精度的位数,不妨先压位处理
然后显然这么多的高精度数也存不下来,那就滚动地统计,
先记好哪些数字对答案有贡献,算到的时候乘给答案就行了
可惜这样做还是会T,因为bzoj这题时限只有1s......
但是显然并不需要统计出全部的组合数啊,,预处理哪些要算就行了
这样卡卡就跑过去了~
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
 
const int maxn = 1010;
const int N = 334;
typedef long long LL;
const LL mo = 1000000000;
 
struct data{
    LL a[N]; int len; data(){memset(a,0,sizeof(a)); len = 0;}
    data operator + (const data &B)
    {
        data c; int Max = max(len,B.len);
        for (int i = 0; i < Max; i++)
        {
            c.a[i] += a[i] + B.a[i];
            c.a[i + 1] += c.a[i] / mo;
            c.a[i] %= mo;
        }
        c.len = c.a[Max] ? Max + 1 : Max;
        return c;
    }
    data operator * (const data &B)
    {
        data c;
        for (int i = 0; i < len; i++)
            for (int j = 0; j < B.len; j++)
            {
                c.a[i + j] += a[i] * B.a[j];
                c.a[i + j + 1] += c.a[i + j] / mo;
                c.a[i + j] %= mo;
            }
        c.len = c.a[len + B.len - 1] ? len + B.len : len + B.len - 1;
        return c;
    }
}Ans,C[2][maxn];
 
int n,m,tot,res,tp,cur,nex = 1,stk[maxn];
bool vis[maxn][maxn],bo[maxn][maxn];
 
void Print(int x,int k)
{
    if (k == 9) {printf("%d",x); return;}
    Print(x / 10,k + 1); printf("%d",x % 10);
}
 
data ksm()
{
    data ret,x;
    ret.a[0] = ret.len = 1;
    x.a[0] = m; x.len = 1;
    for (; res; res >>= 1)
    {
        if (res & 1) ret = ret * x;
        x = x * x;
    }
    return ret;
}
 
int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
        freopen("2.out","w",stdout);
    #endif
     
    cin >> n; res = tot = n - 2;
    C[0][0].a[0] = Ans.a[0] = Ans.len = C[0][0].len = 1;
    for (int i = 1; i <= n; i++)
    {
        int x; scanf("%d",&x);
        if (x == -1) ++m;
        else stk[++tp] = x - 1;
    }
    sort(stk + 1,stk + tp + 1);
    for (int i = 1; i <= tp; i++)
    {
        vis[res][stk[i]] = bo[res][stk[i]] = 1;
        res -= stk[i]; if (res < 0) {puts("0"); return 0;}
    }
    for (int i = tot; i; i--)
        for (int j = 1; j <= i; j++)
            if (bo[i][j]) bo[i][j] = bo[i-1][j] = bo[i-1][j-1] = 1;
    for (int i = 1; i <= tot; i++,swap(cur,nex))
    {
        C[nex][0].a[0] = C[nex][0].len = 1;
        for (int j = 1; j <= i; j++)
        {
            if (!bo[i][j]) continue;
            C[nex][j] = C[cur][j] + C[cur][j - 1];
            if (vis[i][j]) Ans = Ans * C[nex][j];
        }
    }
    if (m) Ans = Ans * ksm();
    cout << Ans.a[Ans.len - 1];
    for (int i = Ans.len - 2; i >= 0; i--) Print(Ans.a[i],1);
    puts("");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值