DEGREE - Số lượng bậc
Tác giả: khuc_tuan
Ngôn ngữ: Pascal
var
xxx, result, na, x, y, k, b : longint;
a : array[1..100] of longint;
procedure them(scs, sb : longint);
var
j, i, r : longint;
begin
r := 1;
j := 2;
for i:=scs downto scs-sb+1 do begin
r := r * i;
while (j<=sb) and (r mod j=0) do begin
r := r div j;
inc(j);
end;
end;
{ for i:=1 to sb do r := r * (b-1);}
{ if sb>scs then r:=0;}
{writeln( scs,' ',sb,' ',r);}
result := result + r;
end;
procedure duyet(i, sb : longint);
begin
if sb>k then exit;
if i=0 then begin
if sb=k then inc(result);
exit;
end;
if a[i]=0 then begin
duyet(i-1,sb);
exit;
end;
if a[i]>1 then begin
them(i,k-sb);
exit;
end;
them(i-1,k-sb);
duyet(i-1,sb+1);
end;
procedure tinh(x : longint);
var
i : integer;
begin
na := 0;
while x<>0 do begin
inc(na);
a[na] := x mod b;
x := x div b;
end;
{ for i:=1 to na do write(a[i],' ');
writeln;}
result := 0;
duyet(na, 0);
end;
begin
read(x, y, k, b);
tinh(y);
xxx := result;
{ writeln(result);}
tinh(x-1);
xxx := xxx - result;
{ writeln(result);}
writeln(xxx);
end.