{
    Die Hausaufgabe 2.0.0
    Copyright (C) 2003 Andrej Krutak

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
}
{
zadane: n, k (0<=k<n!)

testy:

n=4, k=13	n=1, k=0
abcd		a <=
abdc
acbd		n=2, k=1
acdb		ab
adbc		ba <=
adcb
bacd-		n=3, k=3
badc		abc
bcad		acb
bcda		bac
bdac		bca <=
bdca		cab
cabd-		cba
cadb  <=
cbad
cbda
cdab
cdba
dabc-
dacb
dbac
dbca
dcab
dcba

kazda postupnost je rozdelena na n-podpostupnosti...
...rekurzivne je to podobne pre podpostupnosti :)
...treba si zapamatat pouzite pismena a potom len tlacit pismena s nejakym indexom (vid program)
}

var n, k, i, x, y, l: longint;
    used: array[0..30] of boolean;

{OPTIMALIZACIA: Na kieho certa tu pouzivame rekurziu ked sa to da urobit for cyklom? :-) }
function fakt(n:longint):longint;
begin
     if (n=0) then fakt:=1
     else fakt:=(n)*fakt(n-1);
end;

begin
     writeln('N, K (0<=K<N!)');
     readln(n, k);
     
     if (0<=K) AND (k<fakt(n)) then begin
	     for i:=0 to 30 do used[i]:=false;
	     while n>0 do begin
		   x:=fakt(n-1);
		   y:=k div x;
	
		   {vybrat pismeno s indexom y -> index do l (&oznacit, aby sa uz nepouzilo)}
		   i:=0; l:=0;
		   while (i<>y) or (used[l]=true) do begin
			 if (used[l]<>true) then i:=i+1;
			 l:=l+1;
		   end;
		   used[l]:=true;
	
		   {vypisat ten znak a pokracujeme dalsim znakom permutacie...}
		   write(chr(ord('a')+l));
		   k:=k mod x;
		   n:=n-1;
	     end;
	     writeln;
     end else writeln('# Vazne si myslis ze 0 <= ', k, ' < ', n, '! ? Smutne ;-)');
end.
