Description
Input
N与K(n<=1800,k<=10)
Output
答案
Sample Input
2 10
Sample Output
90
Solution
很显然的数位DP。
f[1..1800][0..1]表示好就行了。
注意高精度,其实这题算是高精度模板
Code
type arr=array[0..1000] of longint;
var
f:array[0..1801,0..1] of arr;
i,n,k:longint;
mo:longint=1000000;
function cheng(a:arr;b:longint):arr;
var
i:longint;
c:arr;
begin
c[0]:=0;c[1]:=0;
for i:=1 to a[0] do
begin
a[i]:=c[i]+a[i]*b;c[i+1]:=0;
if(a[i]>=mo)then begin c[i+1]:=a[i] div mo;a[i]:=a[i]mod mo;end;
end;
a[i+1]:=c[i+1];if(a[a[0]+1]>0) then inc(a[0]);exit(a);
end;
function jia(a,b:arr):arr;
var
i:longint;
c:arr;
begin
fillchar(c,sizeof(c),0);
if a[0]>b[0] then c[0]:=a[0] else c[0]:=b[0];
for i:=1 to c[0] do
begin
c[i]:=a[i]+b[i]+c[i];
if c[i]>=mo then begin c[i+1]:=c[i+1]+c[i]div mo;c[i]:=c[i] mod mo;end;
end;
if c[c[0]+1]>0 then inc(c[0]);exit(c);
end;
procedure printf(a:arr);
var
i,j:longint;
s:ansistring;
begin
write(a[a[0]]);
for i:=a[0]-1 downto 1 do
begin
str(a[i],s);
for j:=length(s)+1 to 6 do write('0');
write(a[i]);
end;
writeln;
end;
begin
read(n,k);
if n=0 then begin writeln(0);exit;end;
f[0,0][0]:=1;f[0,0][1]:=1;
for i:=1 to n do
begin
f[i,0]:=f[i-1,1];
f[i,1]:=jia(cheng(f[i-1,0],k-1),cheng(f[i-1,1],k-1));
end;
printf(jia(f[n,0],f[n,1]));
end.