I and OI
Past...

题意:给出M,S,和M对ai,bi.选出最少对的ai,bi,使得(∑ai)^2+(∑bi)^2=s^2.

分析:背包.f[i,j]表示构成∑ai=i,∑bi=j的最少对数.(很少人做的水题)

code:

const oo=16843009;
var   f:array[0..310,0..310] of longint;
      a,b:array[0..50] of longint;
      n,m,s,d,o,i,j,k,ans:longint;

      function min(a,b:longint):longint;
      begin
            if a>b then exit(b); exit(a);
      end;

begin
      readln(n);
      for D:=1 to n do
      begin
            readln(m,s);
            for o:=1 to m do
               readln(a[o],b[o]);
            readln;

            fillchar(f,sizeof(f),1);
            f[0][0]:=0;

            for i:=1 to m do
               for j:=a[i] to s do
                  for k:=b[i] to s do
                  f[j][k]:=min(f[j][k],f[j-a[i]][k-b[i]]+1);

            ans:=oo;
            for i:=0 to s do
               for j:=0 to s do
               if i*i+j*j=s*s then
               ans:=min(ans,f[i][j]);
            if ans=oo then writeln('not possible')
            else writeln(ans);
      end;
end.
posted on 2011-08-13 13:59  exponent  阅读(217)  评论(0编辑  收藏  举报