p01984 Tanka Number_36603
Number of tanka

Wishing to die in the spring under the flowers

This is one of the famous tanka poems that Saigyo Hoshi wrote. Tanka is a type of waka poem that has been popular in Japan for a long time, and most of it consists of five phrases and thirty-one sounds of 5, 7, 5, 7, and 7.

By the way, the number 57577 consists of two types, 5 and 7. Such a positive integer whose decimal notation consists of exactly two types of numbers is called a tanka number. For example, 10, 12, 57577, 25252 are tanka numbers, but 5, 11, 123, 20180701 are not tanka songs.

A positive integer N is given. Find the Nth smallest tanka number.

Input

The input consists of up to 100 datasets. Each dataset is represented in the following format.

> N

The integer N satisfies 1 ≤ N ≤ 1018.

The end of the input is represented by a single zero line.

Output

For each dataset, output the Nth smallest tanka number on one line.

Sample Input


1
2
3
390
1124
1546
314159265358979323
0


Output for the Sample Input


Ten
12
13
2020
25252
57577
7744444777744474777777774774744777747477444774744744






Example

Input

1
2
3
390
1124
1546
314159265358979323
0


Output

10
12
13
2020
25252
57577
7744444777744474777777774774744777747477444774744744
#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr8,pr7,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl
#define pr1(a) (#a)<<"="<<(a)<<" "
#define pr2(a,b) pr1(a)<<pr1(b)
#define pr3(a,b,c) pr1(a)<<pr2(b,c)
#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)
#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)
#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)
#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)
#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)
#define prArr(a) {cerr<<(#a)<<"={";int i=0;for(auto t:(a))cerr<<(i++?", ":"")<<t;cerr<<"}"<<endl;}
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL<<60)+1e9; // ~ 1.15 * 1e18
const Int mod = (1e9)+7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int,Int>;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
template<class T1, class T2> ostream& operator<<(ostream& o,pair<T1,T2> p){return o<<"("<<p.first<<","<<p.second<<")";}
template<class T1, class T2, class T3> ostream& operator<<(ostream& o,tuple<T1,T2,T3> t){
  return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";}
template<class T1, class T2> istream& operator>>(istream& i,pair<T1,T2> &p){return i>>p.first>>p.second;}
template<class T> ostream& operator<<(ostream& o,vector<T> a){Int i=0;for(T t:a)o<<(i++?" ":"")<<t;return o;}
template<class T> istream& operator>>(istream& i,vector<T> &a){for(T &t:a)i>>t;return i;}

Int count(Int n){
  if(n == 1) return 0;
  Int a = (9 * 8 / 2) * ((1LL<<n) - 2);
  Int b = 9 * ((1LL<<(n-1)) - 1);
  Int res = a + b;
  return res;
}

Int count2(Int n){
  Int a = 8 * ((1LL<<n) - 1);
  Int b = (1LL<<n) - 1;
  Int res = a + b;
  return res;
}

Int count3(Int n){
  return (1LL<<n) - 1;
}


Int used[11][1000], mem[11][1000];
Int dfs(Int first, Int second,Int len){
  if(len == 0) return second != 10;
  if(used[second][len]++) return mem[second][len];
  
  Int res = 0;
  for(Int i=0;i<10;i++){
    if(second == 10 && i == first) res += dfs(first, second, len - 1);
    else if(second == 10 && i != first) res += dfs(first, i, len - 1);
    else if(i == first || i == second) res += dfs(first, second, len - 1);
    Min(res, INF);
  }
  return mem[second][len] = res;
}

string dfs2(Int first, Int second, Int len, Int K){
  if(len == 0) return "";
  string res;
  for(Int i=0;i<10;i++){
    Int num = -1;
    Int nsecond = second;
    if(second == 10 && i == first) num = dfs(first, second, len - 1);
    else if(second == 10 && i != first) num = dfs(first, i, len - 1), nsecond = i;
    else if(i == first || i == second) num = dfs(first, second, len - 1);
    if(num == -1) continue;
    if(K - num <= 0) return res + to_string(i) + dfs2(first, nsecond, len - 1, K);
    K -= num;
  }
  return res;
}

string solve(Int n){
  Int len = 2;
  while(n - count(len) > 0) n -= count(len), len++;
  Int a = 1;
  while(n - count2(len-1) > 0) n -= count2(len-1), a++;
  return to_string(a) + dfs2(a, 10, len-1, n);
}


Int isTanka(Int n){
  string s = to_string(n);
  sort(s.begin(), s.end());
  s.erase(unique(s.begin(), s.end()),s.end());
  return s.size() == 2;
}

signed main(){
  srand((unsigned)time(NULL));
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  /*  Int cnt = 0;
  for(Int i=0;i<=25252;i++)
    if(to_string(i).size() == 5 && to_string(i)[0] == '2' && isTanka(i)) cnt++;
  pr(cnt);
  */
  
  while(1){
    Int n;
    cin>>n;
    if(n == 0) break;
    string ans = solve(n);
    cout<<ans<<endl;
    
  }
  
  
  return 0;
}


1
2
3
390
1124
1546
314159265358979323
0 1
2
3
390
216
1546
314159265358979323
0 1
2
2
390
1124
1546
314159265358979323
0 1
2
3
390
216
1546
31125328462643205
0 1
2
2
390
1124
1546
211760204581026131
0 1
2
3
104
216
1546
31125328462643205
0 1
0
2
390
1124
1546
211760204581026131
0 1
3
3
104
216
1546
31125328462643205
0 1
3
5
104
216
1546
31125328462643205
0 1
3
5
104
216
1546
35211924413826153
0 1
4
5
104
216
1546
35211924413826153
0 1
4
5
104
216
1546
14282246706276917
0 1
2
5
104
216
1546
14282246706276917
0 1
2
5
104
216
1546
10789390619882127
0 1
2
3
20
1124
1546
314159265358979323
0 1
2
3
390
142
1546
314159265358979323
0 1
2
2
765
1124
1546
314159265358979323
0 1
2
5
390
216
1546
31125328462643205
0 1
2
2
575
1124
1546
211760204581026131
0 1
4
3
104
216
1546
31125328462643205
0 1
3
5
104
216
1546
30412936253375597
0 1
4
5
120
216
1546
35211924413826153
0 1
2
5
104
48
1546
10789390619882127
0 1
2
3
20
1050
1546
314159265358979323
0 1
2
5
390
216
247
31125328462643205
0 1
2
2
444
1124
1546
211760204581026131
0 1
4
3
104
356
1546
31125328462643205
0 1
3
9
104
216
1546
30412936253375597
0 1
4
5
120
233
1546
35211924413826153
0 1
2
7
104
48
1546
10789390619882127
0 1
2
3
11
1050
1546
314159265358979323
0 1
2
5
390
216
284
31125328462643205
0 1
1
2
444
1124
1546
211760204581026131
0 1
4
3
141
356
1546
31125328462643205
0 1
3
17
104
216
1546
30412936253375597
0 1
4
5
120
276
1546
35211924413826153
0 1
2
7
104
48
1546
14537068688664322
0 1
2
3
11
1050
1546
132147252238502491
0 1
2
5
390
216
442
31125328462643205
0 1
1
2
444
1978
1546
211760204581026131
0 1
4
3
115
356
1546
31125328462643205
0 1
3
17
104
216
1546
34091156012094062
0 1
4
1
120
276
1546
35211924413826153
0 1
2
3
11
1050
1546
168569390037112973
0 1
2
5
390
216
442
3724086920287233
0 1
1
2
444
1978
1534
211760204581026131
0 1
4
0
115
356
1546
31125328462643205
0 1
3
17
104
216
1200
34091156012094062
0 1
4
1
120
276
2174
35211924413826153
0 1
2
1
11
1050
1546
168569390037112973
0 1
2
5
390
216
442
877438609014957
0 1
1
2
444
1978
1534
360294546918145334
0 1
7
0
115
356
1546
31125328462643205
0 1
3
17
104
216
291
34091156012094062
0 1
4
1
120
276
2457
35211924413826153
0 1
2
2
11
1050
1546
168569390037112973
0 1
2
5
390
216
763
877438609014957
0 1
3
11
104
216
291
34091156012094062
0 1
4
1
168
276
2457
35211924413826153
0 1
3
2
11
1050
1546
168569390037112973
0 1
2
5
390
216
763
1563679537664695
0 1
3
6
104
216
291
34091156012094062
0 1
4
1
185
276
2457
35211924413826153
0 1
3
2
11
564
1546
168569390037112973
0 1
3
5
390
216
763
1563679537664695
0 1
2
0
152
356
1546
31125328462643205
-1 1
3
6
19
216
291
34091156012094062
0 1
3
2
11
319
1546
168569390037112973
0 1
3
6
19
216
291
45420024717148297
0 1
3
2
20
319
1546
168569390037112973
0 1
6
6
19
216
291
45420024717148297
0 1
3
0
20
319
1546
168569390037112973
0 1
6
6
19
216
291
22590470820161213
0 1
5
6
19
216
291
22590470820161213
0 1
3
6
19
216
291
22590470820161213
0 1
1
3
30
3175
2423
268313004515723671
0 1
1
3
30
3057
2423
268313004515723671
0 1
1
3
0
3057
2423
268313004515723671
0 1
1
3
1531
2317
714
225725068861419081
0 1
1
4
1531
2317
714
225725068861419081
0 1
2
3
0
2780
119
131765678390808916
0 1
2
2
0
2780
119
131765678390808916
0 1
4
2
0
2780
119
131765678390808916
-1 1
1
0
0
2780
13
131765678390808916
-3 1
1
1
0
2780
3
131765678390808916
-3 1
1
2
0
4231
3
131765678390808916
-3 1
2
3
390
1124
1546
298901390280203360
0 1
2
3
390
216
2203
314159265358979323
0 1
2
2
390
1124
2149
314159265358979323
0 1
2
3
390
216
374
31125328462643205
0 1
2
1
390
1124
1546
211760204581026131
0 1
4
3
5
216
1546
31125328462643205
0 1
1
2
390
1124
1546
211760204581026131
0 1
3
3
104
216
1831
31125328462643205
0 1
3
5
150
216
1546
35211924413826153
0 1
4
4
104
216
1546
35211924413826153
0 1
4
5
104
122
1546
14282246706276917
0 1
2
5
104
216
1546
24308699306116919
0 1
2
4
104
216
1546
10789390619882127
0 1
2
3
20
607
1546
314159265358979323
0 1
2
3
390
184
1546
314159265358979323
0
10
12
13
2020
25252
57577
7744444777744474777777774774744777747477444774744744 10
12
13
2020
599
57577
7744444777744474777777774774744777747477444774744744
 10
12
12
2020
25252
57577
7744444777744474777777774774744777747477444774744744
 10
12
13
2020
599
57577
4422424222424224224222242422242224222444442422222
 10
12
12
2020
25252
57577
2242242422424422422242444244422444244422422444244222
 10
12
13
177
599
57577
4422424222424224224222242422242224222444442422222
 10
 10
13
13
177
599
57577
4422424222424224224222242422242224222444442422222
 10
13
15
177
599
57577
4422424222424224224222242422242224222444442422222
 10
13
15
177
599
57577
5855885585555888855555885855585885858585555585885
 10
14
15
177
599
57577
5855885585555888855555885855585885858585555585885
 10
14
15
177
599
57577
333133133131111131331113311113133133311313331331
 10
12
15
177
599
57577
333133133131111131331113311113133133311313331331
 10
12
15
177
599
57577
90909009990090099000099009999099900000990990909
 10
12
13
31
25252
57577
7744444777744474777777774774744777747477444774744744
 10
12
13
2020
330
57577
7744444777744474777777774774744777747477444774744744
 10
12
12
7999
25252
57577
7744444777744474777777774774744777747477444774744744
 10
12
15
2020
599
57577
4422424222424224224222242422242224222444442422222
 10
12
12
4994
25252
57577
2242242422424422422242444244422444244422422444244222
 10
14
13
177
599
57577
4422424222424224224222242422242224222444442422222
 10
13
15
177
599
57577
4000044000440044440000404444404044004040000004000
 10
14
15
228
599
57577
5855885585555888855555885855585885858585555585885
 10
12
15
177
62
57577
90909009990090099000099009999099900000990990909
 10
12
13
31
22122
57577
7744444777744474777777774774744777747477444774744744
 10
12
15
2020
599
717
4422424222424224224222242422242224222444442422222
 10
12
12
2828
25252
57577
2242242422424422422242444244422444244422422444244222
 10
14
13
177
1211
57577
4422424222424224224222242422242224222444442422222
 10
13
19
177
599
57577
4000044000440044440000404444404044004040000004000
 10
14
15
228
664
57577
5855885585555888855555885855585885858585555585885
 10
12
17
177
62
57577
90909009990090099000099009999099900000990990909
 10
12
13
21
22122
57577
7744444777744474777777774774744777747477444774744744
 10
12
15
2020
599
868
4422424222424224224222242422242224222444442422222
 10
10
12
2828
25252
57577
2242242422424422422242444244422444244422422444244222
 10
14
13
323
1211
57577
4422424222424224224222242422242224222444442422222
 10
13
28
177
599
57577
4000044000440044440000404444404044004040000004000
 10
14
15
228
828
57577
5855885585555888855555885855585885858585555585885
 10
12
17
177
62
57577
333838383883338833338888383333883888388338338833
 10
12
13
21
22122
57577
505055550550055500555005055505550000505505050500005
 10
12
15
2020
599
2777
4422424222424224224222242422242224222444442422222
 10
10
12
2828
90990
57577
2242242422424422422242444244422444244422422444244222
 10
14
13
223
1211
57577
4422424222424224224222242422242224222444442422222
 10
13
28
177
599
57577
5552555255255522555225522222525252552225222252252
 10
14
10
228
828
57577
5855885585555888855555885855585885858585555585885
 10
12
13
21
22122
57577
886888666668888688886686868668686868686866688868886
 10
12
15
2020
599
2777
3553553333533553335535555555553353335353535335
 10
10
12
2828
90990
55999
2242242422424422422242444244422444244422422444244222
 10
14
 10
13
28
177
599
33311
5552555255255522555225522222525252552225222252252
 10
14
10
228
828
111511
5855885585555888855555885855585885858585555585885
 10
12
10
21
22122
57577
886888666668888688886686868668686868686866688868886
 10
12
15
2020
599
2777
31311111133131133111311331311311333131313131
 10
10
12
2828
90990
55999
9992222229299999929929222299922922292222992999992229
 10
17
 10
13
28
177
599
884
5552555255255522555225522222525252552225222252252
 10
14
10
228
828
222242
5855885585555888855555885855585885858585555585885
 10
12
12
21
22122
57577
886888666668888688886686868668686868686866688868886
 10
12
15
2020
599
7979
31311111133131133111311331311311333131313131
 10
13
21
177
599
884
5552555255255522555225522222525252552225222252252
 10
14
10
424
828
222242
5855885585555888855555885855585885858585555585885
 10
13
12
21
22122
57577
886888666668888688886686868668686868686866688868886
 10
12
15
2020
599
7979
177711171711171111177177111717171111177777711
 10
13
16
177
599
884
5552555255255522555225522222525252552225222252252
 10
14
10
477
828
222242
5855885585555888855555885855585885858585555585885
 10
13
12
21
4666
57577
886888666668888688886686868668686868686866688868886
 10
13
15
2020
599
7979
177711171711171111177177111717171111177777711
 10
12
 10
13
16
30
599
884
5552555255255522555225522222525252552225222252252
 10
13
12
21
993
57577
886888666668888688886686868668686868686866688868886
 10
13
16
30
599
884
9999799797977797997797997999979779779777779797779
 10
13
12
31
993
57577
886888666668888688886686868668686868686866688868886
 10
16
16
30
599
884
9999799797977797997797997999979779779777779797779
 10
13
 10
16
16
30
599
884
999666699996966999699966966696996999699666996966
 10
15
16
30
599
884
999666699996966999699966966696996999699666996966
 10
13
16
30
599
884
999666699996966999699966966696996999699666996966
 10
10
13
42
474444
220202
5445445555454544444554445454554445554445545444545554
 10
10
13
42
444404
220202
5445445555454544444554445454554445554445545444545554
 10
10
13
 10
10
13
55955
155155
7277
3223333233333322222322232232233222323233333233223332
 10
10
14
55955
155155
7277
3223333233333322222322232232233222323233333233223332
 10
12
13
 10
12
12
 10
14
12
 10
10
 10
10
10
 10
10
12
 10
12
13
2020
25252
57577
6669999696696699969966696996966669699696996966669669
 10
12
13
2020
599
113113
7744444777744474777777774774744777747477444774744744
 10
12
12
2020
25252
111133
7744444777744474777777774774744777747477444774744744
 10
12
13
2020
599
1661
4422424222424224224222242422242224222444442422222
 10
12
10
2020
25252
57577
2242242422424422422242444244422444244422422444244222
 10
14
13
15
599
57577
4422424222424224224222242422242224222444442422222
 10
10
12
2020
25252
57577
2242242422424422422242444244422444244422422444244222
 10
13
13
177
599
79797
4422424222424224224222242422242224222444442422222
 10
13
15
339
599
57577
5855885585555888855555885855585885858585555585885
 10
14
14
177
599
57577
5855885585555888855555885855585885858585555585885
 10
14
15
177
232
57577
333133133131111131331113311113133133311313331331
 10
12
15
177
599
57577
1221222112121112111122122111212222212122122111111
 10
12
14
177
599
57577
90909009990090099000099009999099900000990990909
 10
12
13
31
5550
57577
7744444777744474777777774774744777747477444774744744
 10
12
13
2020
474
57577
7744444777744474777777774774744777747477444774744744
