Cod sursa(job #1600442)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 15 februarie 2016 00:25:38
Problema Combinari Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.66 kb
var v:array[0..30] of longint;
    n,p:longint;

{ function valid(k:integer):boolean;
 var i,j:integer;
   begin
       valid:=true;
       for i:=1 to k-1 do
       if v[i]=v[k] then
         begin
             valid:=false;
             exit
         end;

       i:=1;
       j:=i+1;
       while (j<=p) do
        begin
            if (v[j]<>0) and (v[j]<v[i]) then valid:=false;
            inc(i);
            inc(j);
           // if v[j]=0 then break
        end;

      // for i:=1 to k-1 do
       // for j:=1 to k do if v[i]<>v[j] then valid:=true;
   end;

 function solutie(k:integer):boolean;
   begin
       if k=p then solutie:=true
       else solutie:=false;
   end;   }

 procedure afisare(k:integer);
 var i:integer;
   begin
        for i:=1 to k do write(output,v[i],' ');
        writeln(output);
   end;

 {procedure backtrack(k:integer);
 var i:integer;
   begin
       for i:=1 to n do
         begin
             v[k]:=i;
             if valid(k) then
               begin
                  if (solutie(k)) then afisare(k);
                  backtrack(k+1);
               end;

         end;
   end; }

  procedure backtrack2(k:longint);
  var i:longint;
  begin
       if k=p then afisare(k)
       else
          begin
          for i:=v[k]+1 to n do
              begin
              v[k+1]:=i;
              backtrack2(k+1);
              end;
          end;

   end;

 Begin
   assign(input,'combinari.in');  reset(input);
   assign(output,'combinari.out'); rewrite(output);
   fillchar(v,sizeof(v),0);
   readln(input,n,p);
   backtrack2(0);
   close(input);
   close(output);
 end.