//#pragma GCC optimize("O3", "unroll-loops")
//#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
#include <bits/stdc++.h>
#define ldb long double
//#define double ldb
#define db double
#define unomap unordered_map
#define unoset unordered_set
#define endl '\n'
#define str string
#define strstr stringstream
#define sz(a) (int)a.size()
#define ll long long
//#define int ll
#define pii pair <int, int>
#define pll pair <ll, ll>
#define Unique(a) a.resize(unique(all(a)) - a.begin())
#define ull unsigned ll
#define fir first
#define sec second
#define idc cin.ignore()
#define lb lower_bound
#define ub upper_bound
#define all(s) s.begin(), s.end()
#define rev reverse
#define gcd __gcd
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define mul2x(a, x) a << x
#define div2x(a, x) a >> x
#define lcm(a, b) (a / __gcd(a, b) * b)
#define log_base(x, base) log(x) / log(base)
#define debug clog << "No errors!"; exit(0);
#define forw(i, a, b) for (int i = a; i <= b; ++i)
#define forw2(i, a, b) for (ll i = a; i <= b; ++i)
#define fors(i, a, b) for (int i = a; i >= b; --i)
#define fors2(i, a, b) for (ll i = a; i >= b; --i)
#define pqueue priority_queue
#define sqrt sqrtl
#define popcount __builtin_popcountll
#define BIT(x, i) (x >> i) & 1
#define MASK(x) (1LL << x)
#define want_digit(x) cout << fixed << setprecision(x);
#define excuting_time 1000.0 * clock() / CLOCKS_PER_SEC
using namespace std;
const int MOD = 1e9 + 7; // 998244353
const int inf = 1e9;
const ll INF = 1e18;
const int N = 20;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll random(const ll &L, const ll &R)
{
return uniform_int_distribution<ll> (L, R) (rng);
}
bool compare(const str& a, const str& b)
{
if (sz(a) != sz(b))
return sz(a) < sz(b);
return a <= b;
}
str damn(ll k)
{
queue <pair <str, vector <int>>> q;
vector <int> base(10, 0);
for (int d = 1; d <= 9; ++d)
{
++base[d];
q.push(make_pair(to_string(d), base));
--base[d];
}
ll m = 0;
while (!q.empty())
{
pair <str, vector <int>> curr = q.front(); q.pop();
++m;
if (m == k) return curr.fir;
if (sz(curr.fir) == 20) continue;
for (int d = 0; d <= 9; ++d)
{
if (curr.sec[d] < 2)
{
++curr.sec[d];
q.push(make_pair(curr.fir + char('0' + d), curr.sec));
--curr.sec[d];
}
}
}
return "-1";
}
vector <int> operator - (vector <int> v, int x)
{
--v[x];
return v;
}
vector <int> operator + (vector <int> v, int x)
{
++v[x];
return v;
}
ll k;
map <pair <int, vector <int>>, ll> dp;
ll calc(int pos, vector <int> v)
{
if (pos > N) return 1; // Khi đến được vị trí cuối thì mình đã đảm bảo các vị trí trước rồi nên chắc chắn có kết quả
if (dp.find({pos, v}) != dp.end()) return dp[{pos, v}]; // Nếu đã từng tính rồi thì lấy lại kết quả luôn
bool started = false; // Đã bắt đầu có nghĩa hay chưa?
forw (i, 0, 9) if (v[i])
{
started = true;
break;
}
ll ans = 0; // Số lượng số đúng xây được với tiền tố v
forw (i, 0, 9)
{
if (!started && !i) ans += calc(pos + 1, v); // Điền thêm số 0 vô nghĩa
else
{
if (v[i] == 2) continue; // Đạt giới hạn rồi, không thêm nữa
ans += calc(pos + 1, v + i); // Cộng thêm lượng kết quả khác nhau nếu đi hướng này
}
if (ans >= k) // Đã đạt đủ tiêu chuẩn để chọn rồi, không cần thêm làm gì (tràn số)
{
ans = k;
break;
}
}
return dp[{pos, v}] = ans; // Memo lại để tái sử dụng nếu cần
}
bool started; // Biến lưu
vector <int> cnt(10, 0); // Đếm tần số xuất hiện của các chữ số trong kết quả
str res; // Biến kết quả
void solve()
{
cin >> k;
++k;
forw (i, 1, 20) // Xây dựng cho từng vị trí
{
if (!started) // Nếu chưa có ý nghĩa thì có hướng vô nghĩa tiếp
{
ll add = calc(i + 1, cnt); // Không thêm gì cả do thêm một số 0 vô nghĩa
if (add >= k) // Nếu số kết quả đạt được >= lượng kết quả mình cần thì đi theo hướng này luôn
{
res += '0';
continue;
}
else k -= add; // Trừ số kết quả khác nhau của hướng này
forw (j, 1, 9) // Điền các chữ số có nghĩa
{
if (cnt[j] == 2) continue; // Đã đạt tới giới hạn thì không có hướng này
ll add = calc(i + 1, cnt + j); // Số kết quả khi đi theo hướng này
if (add >= k) // Nếu số kết quả đạt được >= lượng kết quả mình cần thì đi theo hướng này luôn
{
res += char(j + '0'); // Thêm vào chữ số của hướng này
started = true; // Đã bắt đầu có nghĩa
++cnt[j]; // Tính thêm 1 chữ số cho chỗ quả
break;
}
else k -= add; // Trừ đi số kết quả không cần
}
}
else // Nếu đã có ý nghĩa từ trước rồi
{
forw (j, 0, 9) // Thử điền số từ 0 đến 9 vì bây giờ chữ số nào cũng có nghĩa
{
if (cnt[j] == 2) continue; // Đã đạt tới giới hạn thì không có hướng này
ll add = calc(i + 1, cnt + j); // Số kết quả khi đi theo hướng này
if (add >= k) // Nếu số kết quả đạt được >= lượng kết quả mình cần thì đi theo hướng này luôn
{
res += char(j + '0'); // Thêm vào chữ số của hướng này
cnt = cnt + j; // Đã bắt đầu có nghĩa
break; // Tính thêm 1 chữ số cho chỗ quả
}
else k -= add; // Trừ đi số kết quả không cần
}
}
}
forw (i, 0, N - 1)
{
if (res[i] != '0') // Tránh những chữ số 0 vô nghĩa
{
forw (j, i, N - 1) cout << res[j];
cout << endl;
break;
}
}
}
signed main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr);
srand(time(NULL));
#define name "test"
/*
if (fopen(name".INP", "r"))
{
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
*/
int numTest = 1;
// cin >> numTest;
while (numTest--)
{
solve();
}
return 0;
}