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::{self, BufRead};
use std::cmp::{min, max};

fn main() {
  let stdin = io::stdin();
  let mut lines = stdin.lock().lines();
  let line = lines.next().unwrap();
  let vec: Vec<i32> = line.unwrap().split(' ').map(|x| x.parse().unwrap()).collect();
  let input = vec[0];

  let mut counts = vec![vec![1; 10]; 1];
  // counts[0][0] = 0;
  for i in 0..10 {
    let mut vec1 = vec![0; 10];
    for j in 0..10 {
      let beg = if j == 0 { 0 } else { j - 1 };
      let end = min(10, j + 2);
      vec1[j] = counts[i][beg..end].iter().fold(0, |x, y| x + y);
    }
    counts.push(vec1);
  }
  let mut counts2 = vec![0; 11];
  for i in 1..11 {
    counts2[i] = counts[i-1][1..].iter().fold(0, |x, y| x + y) + counts2[i-1];
  }

  let mut output = String::new();
  let mut res = input;
  let mut prev_digit: i32 = -1;
  for i in (0..11).rev() {
    if prev_digit == -1 && res < counts2[i] {
      continue;
    }
    if prev_digit == -1 {
      res -= counts2[i];
    }
    let beg = (if prev_digit == -1 { 1 } else { max(0, prev_digit - 1) }) as usize;
    let end = (if prev_digit == -1 { 10 } else { min(10, prev_digit + 2) }) as usize;
    for j in beg..end {
      if counts[i][j] >= res {
        output.push((j as u8 + 48) as char);
        prev_digit = j as i32;
        break;
      }
      res -= counts[i][j];
    }
  }

  println!("{}", output);
}