Cod sursa(job #51116)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 10 aprilie 2007 08:39:36
Problema Farfurii Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.69 kb
var n,k,i:longint;
    f,g:text;
    q,m,p:int64;
begin
 assign(f,'farfurii.in'); reset(f);
 assign(g,'farfurii.out'); rewrite(g);
 read(f,n,k);
 p:=1;
 while p<n do
  p:=p*2;
 m:=0;
 while p>0 do begin
  q:=(m+p)*(m+p-1);
  if (m+p<=n) and (q<=2*k)then
   m:=m+p;
  p:=p div 2;
 end;
 k:=k-m*(m-1) div 2;
 if k=0 then begin
  for i:=1 to n-m do
   write(g,i,' ');
  for i:=n downto n-m+1 do
   write(g,i,' ');
  writeln(g);
 end
 else begin
  k:=m-k;
  inc(m);
  for i:=1 to n-m do
   write(g,i,' ');
  write(g,n-k,' ');
  for i:=n downto n-k+1 do
   write(g,i,' ');
  for i:=n-k-1 downto n-m+1 do
   write(g,i,' ');
  writeln(g);
 end;
 close(f); close(g);
end.