记录编号 49246 评测结果 AAAAA
题目名称 三只小猪 最终得分 100
用户昵称 Gravatar张来风飘 是否通过 通过
代码语言 Pascal 运行时间 0.008 s
提交时间 2012-11-07 15:35:20 内存使用 0.88 MiB
显示代码纯文本
program project1;
type arr=array[0..100] of integer;
var f:array[0..60,0..60] of ARR;
    c:arr;
    t:longint;
procedure init;
begin
     assign(input,'piggy.in');reset(input);
     assign(output,'piggy.out');rewrite(output);
     read(t);
end;
procedure print(a:arr);
var i:longint;
begin
     if a[0]=0 then writeln(0) else
     begin
          for i:=a[0] downto 1 do
          write(a[i]);
          writeln;
     end;
end;
function mul(a:arr;b:longint):arr;
var i:longint;
begin
     for i:=1 to a[0] do
         a[i]:=a[i]*b;
     for i:=1 to a[0] do if a[i]>9 then
     begin
          inc(a[i+1],a[i] div 10);
          a[i]:=a[i] mod 10;
     end;
     while a[a[0]+1]>0 do
     begin
          inc(a[0]);
          if a[a[0]]>9 then
          begin
               inc(a[a[0]+1],a[a[0]] div 10);
               a[a[0]]:=a[a[0]] mod 10;
          end;
     end;
     mul:=a;
end;
function add(a,b:arr):arr;
var i:longint;
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
         c[i]:=a[i]+b[i];
     for i:=1 to c[0] do
         if c[i]>9 then
         begin
              inc(c[i+1]);
              dec(c[i],10);
         end;
     if c[c[0]+1]>0 then inc(c[0]);
     add:=c;
end;
procedure add1(var a:arr);
begin
     fillchar(a,sizeof(a),0);
     a[0]:=1;
     a[1]:=1;
end;
function dfs(n,m:longint):arr;
begin
     if f[n,m][0]>0 then exit(f[n,m]);
     if (n=m)or(m=1) then begin add1(f[n,m]);dfs:=f[n,m];exit;end;
     f[n,m]:=mul(dfs(n-1,m),m);
     f[n,m]:=add(f[n,m],dfs(n-1,m-1));
     exit(f[n,m]);
end;

procedure main;
var tt,n,m,i:longint;a,b:arr;
begin
     for tt:=1 to t do
     begin
          read(n,m);
          if (n<m)or(m=0) then writeln(0) else print(dfs(n,m));
     end;
     close(input);
     close(output);
end;
begin
     init;
     main;
end.