《生成树计数》预处理

原题: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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值