http://acm.pku.edu.cn/JudgeOnline/problem?id=1173

Код:
var
tt,n,k,m,i,j,q,r,l,s:longint;
temp,chr:char;
a,max:array[-50..50,-50..50]of longint;
begin
readln(n,k,m);
a[0,0]:=1;
for i:=1 to k do
for j:=1 to n+i-k do
for q:=1 to m do
inc(a[i,j],a[i-1,j-q]);
writeln(a[k,n]);
readln(r);
for i:=1 to r do
begin
temp:= '1';
j:=0;l:=0;
s:=0;tt:=n;
while not eoln do
begin
read(chr);
if chr<>temp then
begin
inc(j);
if odd(j) then
for q:=2 to s do
inc(l,a[k-j,tt-q+1])
else
for q:=m-1 downto s do
inc(l,a[k-j,tt-q-1]);
dec(tt,s);
s:=0;
end;
inc(s);
temp:=chr;
end;
readln;
writeln(l);
end;
end.