1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 typedef long long ll ;
5 char a[20] ;
6 int alp[30] ;
7 char map[20] ;
8 bool vis[30] ;
9 ll k ;
10 int n ;
11
12 ll fact (int n)
13 {
14 ll sum = 1 ;
15 for (int i = 1 ; i <= n ; i ++) {
16 sum *= 1ll * i ;
17 }
18 return sum ;
19 }
20
21 ll calc (int x)
22 {
23 int cnt = 0 ;
24 for (int i = 0 ; i < n ; i++) {
25 if (!vis[i]) cnt ++ ;
26 }
27 ll sum = 1 ;
28 for (int i = 0 ; i < 26; i++) {
29 if (i + ‘A‘ == a[x]) {
30 if ( alp[i] - 1 > 1) sum *= fact (alp[i] - 1) ;
31 }
32 else {
33 if (alp[i] > 1) sum *= fact (alp[i]) ;
34 }
35 }
36 // printf ("cnt=%d, sum=%lld\n" , cnt , sum ) ;
37 return fact (cnt - 1) / sum ;
38 }
39
40 void dfs (int deep)
41 {
42 if (deep == n) return ;
43 for (int i = 0 ; i < n; i ++) {
44 // printf ("deep=%d,%c\n" , deep , a[i]) ;
45 if (!vis[i] ) {
46 if (k - calc (i) > 0) {
47 // printf ("%c , k=%lld , fact=%lld\n" , a[i] , k , calc (i)) ;
48 k -= calc ( i ) ;
49 }
50 else {
51 if (k == 1) {
52
53 }
54 vis[i] = 1 ;
55 alp[a[i] - ‘A‘] -- ;
56 map[deep] = a[i] ;
57 // printf ("k=%d\n" , k ) ;
58 // printf ("deep=%d , %c\n" , deep , map[deep]) ;
59 dfs (deep + 1) ;
60 return ;
61 }
62 while (a[i + 1] == a[i]) i ++ ;
63 }
64 }
65 }
66
67 int main ()
68 {
69 // freopen ("a.txt" , "r" , stdin ) ;
70 while (scanf ("%s" , a) != NULL) {
71 scanf ("%lld" , &k) ;
72 if (a[0] == ‘#‘ && k == 0) break ;
73 memset (alp , 0 , sizeof(alp)) ;
74 for (int i = 0 ; a[i] != ‘\0‘ ; i ++) {
75 alp[a[i] - ‘A‘] ++ ;
76 }
77 n = strlen (a) ;
78 std::sort (a , a + n ) ;
79 /* for (int i = 0 ; i < n ; i++) {
80 printf ("%c" , a[i]) ;
81 } puts ("") ;*/
82 dfs (0) ;
83 memset (vis , 0 , sizeof(vis)) ;
84 for (int i = 0 ; i < n ; i ++) printf ("%c" , map[i]) ; puts ("") ;
85 }
86 return 0 ;
87 }