[HNOI2008]明明的烦恼
2014-12-29 08:20
183 查看
Time
Limit: 1 Sec Memory Limit: 162 MB
Description
自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树?
Input
第一行为N(0 < N < =1000),接下来N行,第i+1行给出第i个节点的度数Di,如果对度数不要求,则输入-1
Output
一个整数,表示不同的满足要求的树的个数,无解输出0Sample Input
31
-1
-1
Sample Output
2题解:这题用Prufer
Code转化为数组,对数组的不同种类进行讨论。
设:know为已知度数节点的个数。tot为已知度数节点的度数减1的和(tot=sigma(d[i]-1)
(1<=i<=know))。
ans=tot!/(d1-1)!*(d2-1)!*(d3-1)!*(d4-1)!*...*(dm-1)!*C(tot,n-2)*(n-m)^n-2-tot
首先,先讨论已知度数节点,我们可以当成单纯的Prufer Code套用公式:
tot!/(d1-1)!*(d2-1)!*(d3-1)!*(d4-1)!*...*(dm-1)!
然后,n-2的数组中剩余空间可以任意放入未知度数的节点。即:
(n-m)^n-2-tot
最后,乘上原先tot个数在n-2长度的数组中的排放方法。即:
C(tot,n-2)
//在n-2个元素中选取tot个的组合序。
最后的最后。。。就是猥琐的高精了。。。
如果单纯高精度计算的要用高精除。我懒。。。。故在zc大神的提醒下用计算质因数的方法,约掉分母,最后再加单精乘就可以了。
程序又臭又长,还巨慢。。。我太弱了。
AC CODE
program
hy_1005;
type ty=array[0..1000] of longint;
var jc:array[0..1000] of ty;
su,d,ans,a,b:array[0..1000] of longint;
i,j,size,n,know,tot:longint;
//============================================================================
procedure prime;
var i,j:longint;
flag:boolean;
begin
for i:=2 to 1000 do
begin
flag:=true;
for j:=2 to
trunc(sqrt(i)) do
if i mod j=0 then
begin
flag:=false; break;
end;
if flag
then
begin
inc(size);
su[size]:=i;
end;
end;
end;
//============================================================================
procedure predone;
var i,j,x:longint;
begin
prime; //打质数表。
for i:=2 to 1000 do
//打阶乘的质因数表。
begin
x:=i;
jc[i]:=jc[i-1];
for j:=1 to
size do
begin
if x=1 then break;
while x mod su[j]=0 do
begin
inc(jc[i][j]); x:=x div su[j];
if j>jc[i][0] then jc[i][0]:=j;
end;
end;
end;
end;
//============================================================================
procedure init;
var i,tmp:longint;
begin
readln(n);
for i:=1 to n do
begin
readln(tmp);
if tmp=-1
then continue;
inc(know);
if tmp=0
then
if n=1 then writeln('1') else
begin
writeln('0');
halt;
end;
d[know]:=tmp; tot:=tot+tmp-1; //要减1!
end;
if (tot+know) div 2>n-1
then //一条边增加两点的度数。要div 2.
begin
writeln('0');
halt;
end;
end;
//============================================================================
4000
procedure devide(x:longint);
var i:longint;
begin
for i:=1 to size do
begin
if
x<2 then break;
while x mod
su[i]=0 do
begin
inc(a[i]); a[0]:=i;
x:=x div su[i];
end;
end;
end;
//============================================================================
procedure jian(t:longint);
var i:longint;
begin
for i:=1 to jc[t][0] do
ans[i]:=ans[i]-jc[t][i];
end;
//============================================================================
procedure cheng(xx:longint); //单精乘。
var x,y,i:longint;
begin
y:=0;
for i:=1 to b[0] do
begin
x:=b[i]*xx+y; b[i]:=x mod 10000;
y:=x div
10000;
end;
if y>0 then
begin
inc(b[0]);
b[b[0]]:=y;
end;
end;
//============================================================================
procedure work;
var i,j:longint;
begin
ans:=jc[tot];
if
jc[n-2][0]>ans[0] then ans[0]:=jc[n-2][0];
for i:=1 to ans[0] do
ans[i]:=ans[i]+jc[n-2][i];
//求tot!
devide(n-know); //分解(n-m)的质因数。
for i:=1 to a[0] do a[i]:=a[i]*(n-2-tot);
//(n-2-tot)次方
if n-2-tot>0 then else
a[0]:=0;
if
ans[0]<a[0] then ans[0]:=a[0];
for i:=1 to ans[0] do
ans[i]:=ans[i]+a[i];
//tot!*(n-m)^(n-2-tot)
jian(n-2-tot);
jian(tot);
for i:=1 to know do if d[i]>2 then
jian(d[i]-1);
//除阶乘
b[0]:=1; b[1]:=1;
for i:=1 to ans[0] do
for j:=1 to
ans[i] do cheng(su[i]);
end;
//============================================================================
procedure print;
var i:longint;
begin
write(b[b[0]]);
for i:=b[0]-1 downto 1 do
begin
if b[i] div
1000=0 then write('0');
if b[i] div
100=0 then write('0');
if b[i] div
10=0 then write('0');
write(b[i]);
end;
end;
//============================================================================
begin
predone;
init;
work;
print;
end.
相关文章推荐
- 1005: [HNOI2008]明明的烦恼 (prufer编码,排列组合,质因数分解,高精度)
- [BZOJ]1005 明明的烦恼(HNOI2008)
- Prufer编码 & [bzoj 1005] [HNOI2008]明明的烦恼:Prufer编码,组合数学,高精度
- bzoj1005: [HNOI2008]明明的烦恼
- bzoj 1005: [HNOI2008]明明的烦恼
- BZOJ 1005 [HNOI2008]明明的烦恼 ★(Prufer数列)
- 【BZOJ 1005】[HNOI2008]明明的烦恼
- 【Prufer编码+组合】BZOJ1005(HNOI2008)[明明的烦恼]题解
- bzoj 1005 [HNOI2008] 明明的烦恼 题解
- bzoj 1005: [HNOI2008]明明的烦恼(prufer数列)
- 【组合数学】【高精度】【prufer数列】【HNOI 2008】【bzoj 1005】明明的烦恼
- 【树】【数论】[BZOJ1005][HNOI2008]明明的烦恼
- 【BZOJ】1005: [HNOI2008]明明的烦恼(prufer编码+特殊的技巧)
- BZOJ 1005 [HNOI2008]明明的烦恼 ★(Prufer数列)
- bzoj 1005: [HNOI2008]明明的烦恼(组合数学 purfer sequence)
- bzoj 1005: [HNOI2008]明明的烦恼 组合数学 + purfer序列
- 【prufer编码+组合数学】BZOJ1005 [HNOI2008]明明的烦恼
- BZOJ1005 [HNOI2008]明明的烦恼
- [HNOI2008] 明明的烦恼
- HYSBZ/BZOJ 1005 [HNOI2008] 明明的烦恼 - Prufer编码&组合数学&高精度 此乃神题!