Cod sursa(job #2376)

Utilizator bigsarpeadrian bigsarpe Data 17 decembrie 2006 09:03:12
Problema Zebughil Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.05 kb
{$q-,r-,s-,d-,i-,o+}
const maxn=17;
var t,tout:text;
    A:Array[0..maxn]of longint;
    Sol,V:array[0..1 shl maxn]of longint;
    cat,i,n,g,tmp,go,lg,j,test:longint;
    procedure back(config,sum,niv:longint);
    begin
       if niv=n then begin if sum<=G then Sol[config]:=1;end else
       begin
          back(config,sum,niv+1);
          tmp:=sum+a[niv];if tmp<=G then back(config or (1 shl niv),tmp,niv+1);
       end;
    end;
    procedure testare;
    begin
       readln(t,n,g);for i:=0 to N-1 do read(t,A[i]);readln(t);go:=(1 shl n)-1;
       for i:=0 to go do sol[i]:=maxn;back(0,0,0);lg:=0;
       for i:=0 to go do if Sol[i]=1 then begin inc(lg);V[lg]:=i;end;
       for i:=1 to go do
       begin
          j:=lg;cat:=sol[i]+1;
          while V[j]>i do
          begin tmp:=v[j]or i;if Sol[tmp]>cat then sol[tmp]:=cat;dec(j);end;
       end;
       writeln(tout,sol[go]);
    end;
begin
   assign(t,'zebughil.in');reset(T);assign(Tout,'zebughil.out');rewrite(tout);
   for test:=1 to 3 do testare;close(T);close(tout);
end.