Problem:
Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

Note that the answer may not fit into the 32-bit signed integer type.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

#ifdef __LOCAL
    #include <filesystem>
    namespace fs = std::filesystem;
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);
static mt19937 _g(time(nullptr));
 
inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); }
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

int K;
void input(){
    fast_io();
    #ifdef __LOCAL
        fs::path p = __FILE__;
        fs::path input,output;
        input = output = p.parent_path();
        input += string("/input/") + string(p.stem()) + string(".txt");
        output += string("/output/") + string(p.stem()) + string(".txt");
        freopen(input.c_str(), "r", stdin);
        freopen(output.c_str(), "w", stdout);
    #endif
    cin >> K;
}

// x以下のるんるん数の個数
ll numOfLunlun(ll x){
    string z = to_string(x);
    int n = z.length();
    ll dp[100][2][10];
    memset(dp,0,sizeof(dp));

    dp[0][0][z[0]-'0'] = 1;
    for (int i = 1; i < (z[0]-'0'); i++)
    {
        dp[0][1][i] = 1;
    }
    

    for (int i = 0; i < n-1; i++)
    {
        for (int k = 0; k < 10; k++)
        {
            dp[i+1][1][k] += dp[i][1][k];
            if(k!=0) dp[i+1][1][k] += 1;
            if(k!=0) dp[i+1][1][k] += dp[i][1][k-1];
            if(k!=9) dp[i+1][1][k] += dp[i][1][k+1];
            // if(k<(z[i+1]-'0')) dp[i+1][1][k] += dp[i][0][k];
            // if(k<=(z[i+1]-'0') && k!=0) dp[i+1][1][k] += dp[i][0][k-1];
            // if(k+1<(z[i+1]-'0') && k!=9) dp[i+1][1][k] += dp[i][0][k+1];
        }
        
        if((z[i]-'0'-1)<(z[i+1]-'0')) dp[i+1][1][z[i]-'0'-1] += dp[i][0][z[i]-'0'];
        if((z[i]-'0'+0)<(z[i+1]-'0')) dp[i+1][1][z[i]-'0'+0] += dp[i][0][z[i]-'0'];
        if((z[i]-'0'+1)<(z[i+1]-'0')) dp[i+1][1][z[i]-'0'+1] += dp[i][0][z[i]-'0'];
        
        if(abs(z[i+1]-z[i])<=1) dp[i+1][0][z[i+1]-'0'] = dp[i+1][0][z[i]-'0'];            

        // dp[i+1][0][z[i+1]-'0'] += dp[i][0][z[i+1]-'0'];            
        // if((z[i+1]-'0'+1)<10) dp[i+1][0][z[i+1]-'0'] += dp[i][0][z[i+1]-'0'+1];            
        // if((z[i+1]-'0'-1)>=0) dp[i+1][0][z[i+1]-'0'] += dp[i][0][z[i+1]-'0'-1];                    
    }
    ll ret=0;
    for (int k = 0; k < 10; k++)
    {
        ret += dp[n-1][0][k]+dp[n-1][1][k];
    }
    return ret-1;
}

int solve(){
    ll ng=0;
    ll ok=2e18;
    while(ok-ng>1){
        ll mid=(ok+ng)/2;
        if(numOfLunlun(mid)>=K) ok=mid;
        else ng = mid;
    }
    cout << ok << endl;
    return 0;
}   

int main()
{
    input();
    solve();
    return 0;
}