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 Rust (1.15.1) (judge verdict: Accepted)

use std::io::{stdin, Read};

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let k: usize = parse!(it);
    let nums = vec![
        vec![0, 1],
        vec![0, 1, 2],
        vec![1, 2, 3],
        vec![2, 3, 4],
        vec![3, 4, 5],
        vec![4, 5, 6],
        vec![5, 6, 7],
        vec![6, 7, 8],
        vec![7, 8, 9],
        vec![8, 9],
    ];
    let decode = |x: &Vec<usize>| {
        let mut ret = vec![x[x.len() - 1]];
        for i in 0..(x.len() - 1) {
            let prev = ret[i];
            ret.push(nums[prev][x[x.len() - i - 2]]);
        }
        ret.reverse();
        ret
    };

    let succ = |x: &mut Vec<usize>| {
        let mut i = 0;
        let dec = decode(x);
        while i < x.len() - 1 {
            let d = &nums[dec[i + 1]];
            if x[i] < d.len() - 1 {
                x[i] += 1;
                return;
            }
            x[i] = 0;
            i += 1;
        }
        if x[i] == 9 {
            x[i] = 0;
            x.push(1);
        } else {
            x[i] += 1;
        }
    };
    let mut x = vec![1];
    let mut k = k - 1;
    while k > 0 {
        succ(&mut x);
        k -= 1;
    }
    let mut ret = 0;
    let dec = decode(&x);
    for i in 0..dec.len() {
        ret += dec[i] * 10usize.pow(i as u32);
    }
    println!("{}", ret);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        100000
        ";
        solve(s);
    }
}