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 pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template<class T> inline void checkmin(T &a,const T &b){if(b<a) a=b;}
template<class T> inline void checkmax(T &a,const T &b){if(b>a) a=b;}

const int inf=1e9+10;
const int maxn=15;
const int mod=2e5;

int dp[maxn][maxn],k,tot[maxn];

int add(int &a,int b){
	a=(a+b)%mod;
}

int main(){
	cin>>k;
	for(int t=0;t<10;t++) dp[1][t]=1;
	for(int i=1;i<=10;i++){
		for(int t=0;t<10;t++){
			if(t-1>=0) add(dp[i+1][t-1],dp[i][t]);
			if(t+1<=9) add(dp[i+1][t+1],dp[i][t]); 
			add(dp[i+1][t],dp[i][t]);
		}
		for(int t=1;t<10;t++){
			add(tot[i],dp[i][t]);
		}
		add(tot[i],tot[i-1]);
	}
	int len=1,sum=0;
	for(;;){
		for(int t=1;t<10;t++){
			sum+=dp[len][t];
		}
		if(sum>=k) break;
		len++;
	}
//	cout<<len<<endl;
	int mini=0,maxi=9;
	dp[len][0]=tot[len-1];
	string ans="";
	for(int i=len;i>=1;i--){
		for(int t=mini;t<=maxi;t++){
			if(dp[i][t]>=k){
				mini=max(0,t-1);
				maxi=min(9,t+1);
				ans+=char('0'+t);
				break;
			}
			else{
				k-=dp[i][t];
			//	cout<<i<<" "<<t<<" "<<mini<<" "<<maxi<<" "<<dp[i][t]<<" "<<k<<endl;
			}
		}
	}
	cout<<ans;
	return 0;
}