BZOJ 1211: [HNOI2004]树的计数

二次联通门 : BZOJ 1211: [HNOI2004]树的计数

 

 

 

 

/*
    BZOJ 1211: [HNOI2004]树的计数

    prufer数列 + Cayley公式 + 组合数学
    其实就是明明的烦恼弱化版
    (n-2)!/((d1 - 1)! * (d2 - 1)! * .... * (dn - 1))
    要分解质因数和特判几种情况
*/
#include <cstdio>
#include <iostream>
#define rg register
typedef long long LL;
#define Max 200
inline void read (LL &n)
{
    rg char c = getchar ();
    for (n = 0; !isdigit (c); c = getchar ());
    for (; isdigit (c); n =    n * 10 + c - '0', c = getchar ());
}
LL d[Max], J[Max];
int main (int argc, char *argv[])
{
    LL Answer = 1, N; rg int i, j; LL s = 0; read (N);
    for (i = 1; i <= N; ++ i)
    {
        read (d[i]), s += d[i] - 1;
        if (d[i] == 0 && N != 1) return printf ("0"), 0;
        for (j = 2, J[i] = 1; j <= d[i] - 1; ++ j) J[i] *= j;
    }
    if (s != N - 2) return printf ("0"), 0; 
    for (i = j = 1; i <= N - 2; ++ i)
    {
        Answer *= i; if (j > N) continue;
        if (Answer % J[j] == 0) Answer /= J[j ++];
    }
    printf ("%lld", Answer); return 0;
}

 

posted @ 2017-09-23 20:54  ZlycerQan  阅读(164)  评论(0编辑  收藏  举报