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: Accepted)

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define gadd(x,y) x=add(x,y)
#define gmul(x,y) x=mul(x,y)
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;

ll smaller[15];

ll count(ll m, bool q=1){
	ll x = m;
	string s;
	while(x){
		s += '0' + (x%10);
		x/=10;
	}
	reverse(all(s));
	int n = s.length();
	vector<ll> eq(n,0);
	vector<vector<ll>> dp(n, vector<ll>(10,0));
	++eq[0];
	rep(i,1,s[0]-'0')dp[0][i] = 1;
	rep(i,0,n-1){
		rep(j,0,10){
			if(j-1>=0){
				dp[i+1][j-1]+=dp[i][j];
			}
			if(j+1<10){
				dp[i+1][j+1]+=dp[i][j];
			}
			dp[i+1][j]+=dp[i][j];
		}
		if(abs(s[i+1]-s[i]) <= 1)eq[i+1] = eq[i];
		if(eq[i] && s[i+1]>=s[i] ){
			rep(j,max((int)s[i]-'0'-1,0),min((int)s[i]-'0'+2, (int)s[i+1]-'0')){
				++dp[i+1][j];
			}
		}
	}
	ll cnt = eq[n-1] + accumulate(all(dp[n-1]),0LL);
	if(q){
		rep(i,0,n)cnt+=smaller[i];
	}
	return cnt;
}

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	ll mul = 10;
	rep(i,1,15){
		smaller[i] = count(mul-1,0);
		mul*=10;
	}
	ll k;
	cin >> k;
	ll l = 1, r = 1LL << 34;
	ll ans = 0;
	while(l <= r){
		ll m = l + (r-l)/2;
		if(count(m) >= k){
			ans = m;
			r = m - 1;
		}
		else{
			l = m + 1;
		}
	}
	cout << ans << '\n';
}