bzoj 1005: [HNOI2008]明明的烦恼
prufer编码的应用:
1 var 2 a:array[1..10000]of longint; 3 su,p:array[1..1000]of longint; 4 b:array[1..10000]of boolean; 5 ans:array[1..10000]of longint; 6 i,j,m,n,s,k,w,sum,c:longint; 7 procedure add(t,d:longint); 8 var 9 i:longint; 10 begin 11 for i:=1 to s do 12 while t mod su[i]=0 do 13 begin 14 inc(p[i],d); 15 t:=t div su[i]; 16 end; 17 end; 18 procedure cheng(t:longint); 19 var 20 i:longint; 21 begin 22 for i:=1 to c do 23 ans[i]:=ans[i]*t; 24 for i:=1 to c-1 do 25 begin 26 inc(ans[i+1],ans[i] div 10); 27 ans[i]:=ans[i] mod 10; 28 end; 29 while ans[c]>=10 do 30 begin 31 ans[c+1]:=ans[c] div 10; 32 ans[c]:=ans[c] mod 10; 33 inc(c); 34 end; 35 end; 36 begin 37 readln(n); 38 for i:=1 to n do 39 read(a[i]); 40 fillchar(b,sizeof(b),true); 41 for i:=2 to n do 42 if b[i] then 43 begin 44 inc(s); 45 su[s]:=i; 46 for j:=1 to n div i do 47 b[i*j]:=false; 48 end; 49 fillchar(p,sizeof(p),0); 50 sum:=n-2; 51 k:=n; 52 for i:=1 to n do 53 if a[i]<>-1 then 54 begin 55 for j:=1 to a[i]-1 do 56 begin 57 add(sum,1); 58 dec(sum); 59 add(j,-1); 60 end; 61 dec(k); 62 end; 63 c:=1; 64 fillchar(ans,sizeof(ans),0); 65 ans[1]:=1; 66 for i:=1 to s do 67 for j:=1 to p[i] do 68 cheng(su[i]); 69 for i:=1 to sum do 70 cheng(k); 71 for i:=c downto 1 do 72 write(ans[i]); 73 end.