引用第2楼bluetear于2006-08-03 21:20发表的“”:
I got Compilation error or Crash (at #test 5 access violation),but my program is so quickly!
偶的弱程序:
用的类似背包问题的算法。
var tt,top,k,b:int64;
i,j,x,y:longint;
q:int64;
a:array[0..32]of longint;
f:packed array[0..1000000000]of boolean;
total:packed array[0..1000000000]of byte;
begin
read(x,y,k,b);
q:=1;
top:=0;
repeat
a[top]:=q;
inc(top);
q:=q*b;
until q>y;
dec(top);
f[0]:=true;
total[0]:=0;
tt:=0;
if k<>1 then
for i:=0 to top do
begin
for j:=y downto a
do
if total[j]<k then
if ((f[j]=false)and(f[j-a]=true))
or((f[j]=true)and(f[j-a]=true)
and(total[j-a]+1<total[j])) then
begin
f[j]:=true;
total[j]:=total[j-a]+1;
if (total[j]=k)and(j>=x) then inc(tt);
end;
end
else
begin
for i:=0 to top do
if a in [x..y] then inc(tt);
end;
writeln(tt);
end.