bzoj 1005: [HNOI2008]明明的烦恼
题目链接:
题解:
首先要了解prufer序列
对于每个prufer序列都对应唯一的一棵树,对于该规定了度数的点也就规定了该店在prufer序列中出现的次数,那么就是求prufer序列的方案数也就是可重复序列的全排列。
首先只考虑规定度数得点设其度数为d[i],有k个,那么他在prufer中出现的次数就是d[i]-1
设$$tot=\sum_i^{k}d[i]-1$$
那么可重排列方案数为
\[C_{tot}^{d[1]-1}*C_{tot-d[1]-1}^{d[2]-1}*\ldots C_{d[k]-1}^{d[k]-1}
\]
然后在乘上\(C_{n-2}^{tot}\)选点方案
考虑非规定度数点的选取方案
对于没有限制的点,直接有 \((n-k)^{n-2-tot}\)种可选。
答案为无限制与有限制向乘
然后,高精,为了不超时,用质数表来优化。
/**************************************************************
Problem: 1005
为了不超时,用质数表来优化。
Language: C++
Result: Accepted
Time:640 ms
Memory:876 kb
****************************************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int prime[1000]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997};
int n,d[1010],s[200],k=0,sum=0,base=10000;
struct BigInt {
int len,a[10007];
BigInt() {
len=1; memset(a,0,sizeof(a));a[0]=1;
}
int & operator[](int x) {
return a[x];
}
void print() {
printf("%d",a[len-1]);
for(int i=len-2;i>=0;i--) printf("%04d",a[i]);
puts("");
}
};
BigInt operator*(BigInt x,int y) {
for(int i=0;i<x.len;i++)
x[i]*=y;
for(int i=0;i<x.len;i++)
x[i+1]+=x[i]/base,x[i]%=base;
while(x[x.len])
x[x.len]+=x[x.len-1]/base, x[x.len-1]%=base, x.len++;
return x;
}
void mul(int x,int y){
for(int j=0;j<168;j++){
while(x%prime[j]==0) {
x/=prime[j];
s[j]+=y;
}
}
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&d[i]);
if(d[i]==-1) k++;
else sum+=d[i]-1;
}
if(sum>n-2) {
puts("0"); return 0;
}
for(int i=0;i<sum;i++)
mul(n-2-i,1);
for(int i=1;i<=n;i++)
for(int j=1;j<d[i];j++)
mul(j,-1);
mul(k,n-sum-2);
BigInt ans;
for(int i=0;i<200;i++)
for(int j=0;j<s[i];j++)
ans=ans*prime[i];
ans.print();
return 0;
}