1. 程式人生 > >bzoj 1005: [HNOI2008]明明的煩惱

bzoj 1005: [HNOI2008]明明的煩惱

amp prim pro namespace -- ted noi2008 tor ots

題目鏈接:

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; }

bzoj 1005: [HNOI2008]明明的煩惱