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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // uint64_t

#define BUF_SIZE 10

// size: specify sizeof(str)
int get_str(char *str, int size) {
    if(!fgets(str, size, stdin)) return -1;
    return 0;
}

int get_int(void) {
  int num;
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return 0;
  sscanf(line, "%d", &num);
#else
#error
#endif
  return num;
}

#define DIGITS_MAX 100
#define K_MAX 100000

int main(void) {
    static int dp[DIGITS_MAX+1][10] = {0};
    int whole = 0;
    int k = get_int();
    // special case
    if(k <= 12) {
        printf("%d\n", k);
        return 0;
    }
    int i, j;
    for(i = 0; i < 10; i++) {
        dp[0][i] = 1;
    }
    int digit_stack[DIGITS_MAX];
    int sidx = 0;
    whole += 9;
    int size, digit;
    int k_size;
    for(size = 1; size <= DIGITS_MAX; size++) {
        dp[size][0] = dp[size-1][0] + dp[size-1][1];
        int flag = 0;
        for(digit = 1; digit < 10; digit++) {
            int res = 0;
            if(digit+1 <= 9) res += dp[size-1][digit+1];
            res += dp[size-1][digit] + dp[size-1][digit-1];
            dp[size][digit] = res;
            int new = whole + dp[size][digit];
            if(new >= k) {
                k_size = size;
                digit_stack[sidx++] = digit;
                flag = 1; break;
            }
            whole = new;
#ifdef DEBUG
            printf("[%d] dp[%d][%d]\n", whole, size, digit);
#endif
        }
        if(flag) {
#ifdef DEBUG
            printf("whole: %d, %d %d\n", whole, k_size, digit_stack[0]);
#endif
            break;
        }
    }

    int pivot = digit_stack[sidx-1];
    int sum = whole;
    for(size = k_size-1; size >= 0; size--) {
        for(digit = pivot-1; digit <= pivot+1; digit++) {
            if(digit<0 || digit > 9) continue;
            int new = sum + dp[size][digit];
            if(new>=k) {
                digit_stack[sidx++] = digit;
                pivot = digit;
                break;
            }
            sum = new;
#ifdef DEBUG
            printf("[%d] dp[%d][%d]\n", sum, size, digit);
#endif
        }
    }
    for(i = 0; i < sidx; i++) {
        putchar('0' + digit_stack[i]);
    }
    putchar('\n');
    return 0;
}