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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;

#[allow(unused_macros)]
macro_rules! multi_vec {
    ( $elem:expr; $num:expr ) => (vec![$elem; $num]);
    ( $elem:expr; $num:expr, $($rest:expr),* ) => (vec![multi_vec![$elem; $($rest),*]; $num]);
}

fn main() {
    input! {
        k: u64,
    }

    let mut mx = 1;
    while calc(mx) < k {
        mx *= 10;
    }
    // dbg!(mx, calc(mx));
    let mut l = 0;
    let mut r = mx;
    while l + 1 < r {
        let mid = (l + r) / 2;
        if calc(mid) < k {
            l = mid
        } else {
            r = mid
        }
    }
    let ans = r;
    println!("{}", ans);
}

fn calc(m: u64) -> u64 {
    let s: Vec<u8> = m.to_string().chars().map(|x| x as u8 - b'0').collect();
    let l = s.len();
    // dp[i][j] = i桁目まで、jは超えないことが確定したか、kは今の桁数字
    let mut dp = multi_vec![0; l + 1, 2, 10];
    // TODO 要検討
    // dp[0][0][0] = 1;
    // for j in 1..=s[0] {
    //     dp[0][j as usize] = 1;
    // }
    for i in 1..=l {
        let d = s[i - 1] as usize;
        for k in 0..=9 {
            // 超えない確定
            if k > 0 {
                dp[i][1][k] += dp[i - 1][1][k - 1];
            }
            dp[i][1][k] += dp[i - 1][1][k];
            if k < 9 {
                dp[i][1][k] += dp[i - 1][1][k + 1];
            }

            // dbg!(dp[1][1][1]);
            // dbg!(i, k, dp[2][1][2]);

            // 超えるかわからない
            if k > 0 {
                if k < d {
                    dp[i][1][k] += dp[i - 1][0][k - 1];
                } else if k == d {
                    dp[i][0][k] += dp[i - 1][0][k - 1];
                }
            }
            if k < d {
                dp[i][1][k] += dp[i - 1][0][k];
            } else if k == d {
                dp[i][0][k] += dp[i - 1][0][k];
            }
            if k < 9 {
                if k < d {
                    dp[i][1][k] += dp[i - 1][0][k + 1];
                } else if k == d {
                    dp[i][0][k] += dp[i - 1][0][k + 1];
                }
            }

            // dbg!(i, k, dp[2][1][2]);

            // この桁から始まる数字
            if k > 0 {
                if i == 1 {
                    if k < d {
                        dp[i][1][k] += 1;
                    } else if k == d {
                        dp[i][0][k] += 1;
                    }
                } else if i > 1 {
                    dp[i][1][k] += 1;
                }
            }

            // dbg!(i, k, dp[2][1][2]);
        }
    }

    // dbg!(m, &dp);

    let mut ret = 0;
    for i in 0..=9 {
        ret += dp[l][0][i] + dp[l][1][i]
    }

    // if l > 1 {
    //     let next = 10u64.pow(l as u32 - 1) - 1;
    //     ret += calc(next);
    // }

    ret
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn calc_test() {
        assert_eq!(calc(1), 1);
        assert_eq!(calc(2), 2);
        assert_eq!(calc(9), 9);
        assert_eq!(calc(10), 10);
        assert_eq!(calc(11), 11);
        assert_eq!(calc(12), 12);
        assert_eq!(calc(21), 13);
        assert_eq!(calc(23), 15);
    }
}
*/

fn main() {
    let exe = "/tmp/bin5B17A9E3";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";