NOIP2016全国信息学分区联赛提高组第二试 组合问题




分析:
组合数可以用杨辉三角形,然后就可以算杨辉三角求解了。
(范围一定要看好)。
代码:

var
 sum,f:array [0..2000,0..2000] of longint;
 x,y:array [0..20000] of longint;
 i,j,n,m,k,t:longint;
function min(x,y:longint):longint;
 begin
  if xthen exit(x)
         else exit(y);
 end;

function max(x,y:longint):longint;
 begin
  if x>y then exit(x)
         else exit(y);
 end;

begin
 readln(t,k);
 for i:=1 to t do
  begin
   read(x[i],y[i]);
   n:=max(n,x[i]);
   m:=max(m,y[i]);
  end;
 f[1,0]:=1; f[1,1]:=1;
 for i:=2 to n do
  begin
   f[i,0]:=1;
   for j:=1 to m do
    begin
     f[i,j]:=(f[i-1,j]+f[i-1,j-1]) mod k;
     if (f[i,j]=0) and (j<=i) then sum[i,j]:=1;
     sum[i,j]:=sum[i-1,j]+sum[i,j]+sum[i,j-1]-sum[i-1,j-1];
    end;
  end;
 for i:=1 to t do
 writeln(sum[x[i],y[i]]);
end.

你可能感兴趣的:(暴力)