[HNOI2008]明明的烦恼

来源:互联网 发布:信贷抢单软件 编辑:程序博客网 时间:2024/04/30 05:35

TimeLimit: 1 Sec  Memory Limit: 162 MB

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
 
 
题解:这题用PruferCode转化为数组,对数组的不同种类进行讨论。
 
设: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
programhy_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 totrunc(sqrt(i)) do
     if i mod j=0 then
     begin
       flag:=false; break;
     end;
    if flagthen
    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 tosize 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=-1then continue;
   inc(know);
    if tmp=0then
     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-1then  //一条边增加两点的度数。要div 2.
  begin
   writeln('0');
    halt;
  end;
end;
//============================================================================
procedure devide(x:longint);
var i:longint;
begin
  for i:=1 to size do
  begin
    ifx<2 then break;
    while x modsu[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] doans[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 div10000;
  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];
  ifjc[n-2][0]>ans[0] then ans[0]:=jc[n-2][0];
  for i:=1 to ans[0] doans[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 elsea[0]:=0;
 
  ifans[0]<a[0] then ans[0]:=a[0];
  for i:=1 to ans[0] doans[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 thenjian(d[i]-1);
 //除阶乘
  b[0]:=1; b[1]:=1;
 for i:=1 to ans[0] do
    for j:=1 toans[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] div1000=0 then write('0');
    if b[i] div100=0 then write('0');
    if b[i] div10=0 then write('0');
   write(b[i]);
  end;
end;
//============================================================================
begin
  predone;
  init;
  work;
  print;
end.
0 0
原创粉丝点击