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 <stdbool.h>

bool is_lunlun(unsigned long int x) {
    bool ans = true;
    if (x / 10 > 0) {
        unsigned long int temp = x % 10;
        x = x /10;
        while (x > 0) {
            if (x % 10 != temp && x % 10 + 1 != temp && temp + 1 != x % 10) {
                ans = false;
                break;
            }
            temp = x % 10;
            x = x / 10;
        }
    }
    return ans;
}

int lb(int x) {
    if (x == 0) return 0;
    else return x-1;
}

int ub(int x) {
    if (x == 0 || x == 9) return 9;
    else return x+1;
}

unsigned long int answer(int i[]) {
    unsigned long int ans = 0;
    int j;
    for (j=0; j<10; j++) {
        ans = ans * 10 + (unsigned long int)i[j];
    }
    return ans;
}

bool judge(int i[]) {
    int j;
    for (j=0; i[j] == 0; j++) ;
    bool ans = true;
    int temp = i[j];
    for (j++; j < 10; j++) {
        if (temp-i[j] > 1 || temp-i[j] < -1) {
            ans = false;
            break;
        }
        temp = i[j];
    }
    return ans;
}

int main(void) {
    int k;
    scanf("%d", &k);

    int count = -1;
    int i[10] = {0};
    unsigned long int ans;

    for (i[0] = 0; i[0] <= 9; i[0]++) {
        for (i[1] = lb(i[0]); i[1] <= ub(i[0]); i[1]++) {
            for (i[2] = lb(i[1]); i[2] <= ub(i[1]); i[2]++) {
                for (i[3] = lb(i[2]); i[3] <= ub(i[2]); i[3]++) {
                    for (i[4] = lb(i[3]); i[4] <= ub(i[3]); i[4]++) {
                        for (i[5] = lb(i[4]); i[5] <= ub(i[4]); i[5]++) {
                            for (i[6] = lb(i[5]); i[6] <= ub(i[5]); i[6]++) {
                                for (i[7] = lb(i[6]); i[7] <= ub(i[6]); i[7]++) {
                                    for (i[8] = lb(i[7]); i[8] <= ub(i[7]); i[8]++) {
                                        for (i[9] = lb(i[8]); i[9] <= ub(i[8]); i[9]++) {
                                            if (judge(i)) count += 1;
                                            if (count == k) goto ANS;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    ANS:
        ans = answer(i);
        printf("%ld\n", ans);

    return 0;
}