原题:NOI 生成树计数
{ 预处理矩阵 }
program predo;{start at 18:30}
type state=array[1..6]of byte;
var a:array[1..60]of state;c:state;n,i,j,tot:longint;
mat:array[1..60,1..60]of longint;init:array[1..60]of longint;
procedure make(dep,k:byte);var j:byte;
begin
if dep=n+1 then begin inc(tot);a[tot]:=c;exit;end;
for j:=1 to k do begin c[dep]:=j;make(dep+1,k);end;
c[dep]:=k+1;make(dep+1,k+1);
end;
procedure expand(s:state;cnt:byte);
var f,b:state;i:byte;maxx:byte=0;
function equal(a,b:state):boolean;var j:byte;
begin for j:=1 to n do if a[j]<>b[j] then exit(false);exit(true);end;
procedure remark(var a:state);var j:byte;f:state;max:byte=0;
begin fillchar(f,sizeof(f),0);
for j:=1 to n do if f[a[j]]=0 then begin inc(max);f[a[j]]:=max;end;
for j:=1 to n do a[j]:=f[a[j]];
end;
procedure sol(const x:byte;num:longint);var j:byte;
begin
if x=maxx+1 then begin
for j:=1 to n-1 do c[j]:=s[j+1];c[n]:=maxx+1;
for j:=1 to n-1 do c[j]:=f[c[j]];remark(c);
for j:=1 to tot do if equal(a[j],c) then inc(mat[cnt,j],num); exit;
end;
f[x]:=maxx+1;sol(x+1,num*b[x]);
f[x]:=x;sol(x+1,num);
end;
begin
fillchar(f,sizeof(f),0);fillchar(b,sizeof(b),0);
for i:=1 to n do if s[i]>maxx then maxx:=s[i];
for i:=1 to n do inc(b[s[i]]); init[cnt]:=1;
for i:=1 to maxx do init[cnt]:=init[cnt]*b[i];f[1]:=maxx+1;
if (b[1]=1)and(s[1]=1) then sol(2,b[1]) else sol(1,1);
end;
begin
readln(n);make(1,0);
for i:=1 to tot do begin for j:=1 to n do write(a[i][j],' ');writeln;end;
for i:=1 to tot do expand(a[i],i);
for i:=1 to tot do begin for j:=1 to tot do write(mat[i,j],' ');writeln;end;
end.