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.



Sample input:
15


Sample output:
23

Submission status: Runtime Error

Solution code:
from collections import deque

K=int(input())
q=deque()
A=[]
q.append('0')
q.append('1')
q.append('2')
q.append('3')
q.append('4')
q.append('5')
q.append('6')
q.append('7')
q.append('8')
q.append('9')
q.append('10')
q.append('11')
q.append('12')

while len(A)<=K+10:
    snum=q.popleft()
    inum=int(snum)
    if snum[0]!='0':
        A.append(snum)
    
    if inum!=10 or inum!=11 or inum!=12: 
        if 1<=int(snum[0])<=8:
            q.append(str(int(snum[0])-1)+snum)
            q.append(str(int(snum[0])+1)+snum)

        elif int(snum[0])==0:
            q.append(str(int(snum[0])+1)+snum)
        
        elif int(snum[0])==9:
            q.append(str(int(snum[0])+1)+snum)
print(list(set(A))[K])

Submission status: Runtime Error

Solution code:
import sys

K = int(input())

NLUNLUN = [[-1 for _ in range(11)] for _ in range(10)]
def num_lunlun(n, digit):
    if digit == 1:
        NLUNLUN[n][digit] = 1
        return NLUNLUN[n][digit]

    if digit == 2:
        if 0 < n < 9:
            NLUNLUN[n][digit] = 3
        else:
            NLUNLUN[n][digit] = 2
        return NLUNLUN[n][digit]

    n2 = 0
    if n - 1 >= 0:
        n2 += NLUNLUN[n-1][digit-1]
    n2 += NLUNLUN[n][digit-1]
    if n + 1 <= 9:
        n2 += NLUNLUN[n+1][digit-1]
    NLUNLUN[n][digit] = n2
    return NLUNLUN[n][digit]


if K < 10:
    print(K)

k = 0
for digit in range(1, 11):
    for n in range(10):
        tmp = num_lunlun(n, digit)
        if n > 0:
            if k + tmp >= K:
                print(n, digit)
                ans = str(n)
                n2 = n
                for digit2 in range(digit-1, 1, -1):
                    if n2 - 1 >= 0:
                        tmp = NLUNLUN[n2 - 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 - 1)
                            n2 -= 1
                            continue
                        k += tmp

                    tmp = NLUNLUN[n2][digit2]
                    if k + tmp >= K:
                        ans += str(n2)
                        continue
                    k += tmp

                    if n2 + 1 <= 9:
                        tmp = NLUNLUN[n2 + 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 + 1)
                            n2 += 1
                            continue
                        k += tmp

                ans = int(ans + str(n2 + K - k - 2))
                print(ans)

                sys.exit(0)
            k += tmp

Submission status: Runtime Error

Solution code:
K=int(input())
# one=9
if K<=9:
    print(K)
# two=26
elif K<=35: 
    ans=[]
    for i in range(1,10):
        a=str(i)
        for j in range(len(graph[i])):
            b = a + str(graph[i][j])
            ans.append(b)
    print(int(ans[K-10]))
# three=69
elif K<=104:
    ans=[]
    for i in range(1,10):
        a=str(i)
        for j in range(len(graph[i])):
            for p in range(len(graph[j])):
                b = a + str(graph[i][j]) + str(graph[j][p])
                ans.append(b)
    print(int(ans[K-36]))
# four=181
elif K<=285:
    ans=[]
    for i in range(1,10):
        a=str(i)
        for j in range(len(graph[i])):
            for p in range(len(graph[j])):
                for q in range(len(graph[p])):
                    b = a + str(graph[i][j]) + str(graph[j][p]) + str(graph[p][q])
                    ans.append(b)
    print(int(ans[K-105]))
# five=474
elif K<=759:
    ans=[]
    for i in range(1,10):
        a=str(i)
        for j in range(len(graph[i])):
            for p in range(len(graph[j])):
                for q in range(len(graph[p])):
                    for r in range(len(graph[q])):
                        b = a + str(graph[i][j]) + str(graph[j][p]) + str(graph[p][q]) + str(graph[q][r])
                        ans.append(b)
    print(int(ans[K-286]))

Submission status: Runtime Error

Solution code:
K = int(input())
lis = []

def search(s):
  lis.append(s)
  
  if int(s) > 3234566667:
    return
  
  it = int(s[-1])
  if it == 0:
    search(s + '1')
  elif it == 9:
    search(s + '8')
  else:
    search(s + str(it - 1))
    search(s + str(it + 1))
  

for i in (1, 10):
  search(str(i))
  
ans = sorted(map(int,lis))
print(ans[K-1])

Submission status: Runtime Error

Solution code:
K = int(input())

dp = [-1] * (10 ** 5 + 10)
dp[1] = 1
runrun = []
for i in range(1, len(dp) - 1):
    if len(str(i)) == 1:
        dp[i + 1] = i
        runrun += [i]
        continue
    s = str(i)
    last = int(s[-1])
    upper = s[:-1]
    upper = int(upper)
    if dp[upper + 1] != -1 and (
        dp[upper + 1] == last - 1 or dp[upper + 1] == last or dp[upper + 1] == last + 1
    ):
        dp[i + 1] = last
        runrun += [i]

# print(runrun[:30])
print(runrun[K - 1])

Submission status: Runtime Error

Solution code:
k = int(input())
l = ['1','2','3','4','5','6','7','8','9']
lf = ['1','2','3','4','5','6','7','8','9']
sum = 9
while sum < k:
    lff = []
    for i in lf:
        if i[-1] == [0]:
            lff.append(i+'0')
            lff.append(i+'1')
        elif i[-1] == [9]:
            lff.append(i+'8')
            lff.append(i+'9')
        else:
            lff.append(i+str(int(i[-1]-1)))
            lff.append(i+str(int(i[-1])))
            lff.append(i+str(int(i[-1]+1)))
    lf = lff
    l += lff
print(l[k-1])

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>



#define select(_1, _2, x, ...) x



#define repn(i, n) for (int i = 0; i < (n); i++)

#define repr(i, a, b) for (int i = (a); i < (b); i++)

#define rep(i, ...) select(__VA_ARGS__, repr, repn)(i, __VA_ARGS__)



#define rrepn(i, n) for (int i = (n - 1); i >= 0; i--)

#define rrepr(i, a, b) for (int i = (b - 1); i >= (a); i--)

#define rrep(i, ...) select(__VA_ARGS__, rrepr, rrepn)(i, __VA_ARGS__)



#define umax(m, x) (m = max(m, x));

#define umin(m, x) (m = min(m, x));



#define rng(v) v.begin(), v.end()

#define rrng(v) v.rbegin(), v.rend()



#define debug(x) cerr << #x << " = " << x << endl



using namespace std;



using ll = long long int;



using vi = vector<int>;

using vl = vector<ll>;

using vs = vector<string>;

using vb = vector<bool>;



using vvi = vector<vector<int>>;

using vvl = vector<vector<ll>>;

using vvs = vector<vector<string>>;

using vvb = vector<vector<bool>>;



const int INF = 1e9;

const ll LINF = 1e18;



inline void yn(bool f) { cout << (f ? "Yes" : "No") << endl; }



struct node {

    ll num;

    string all;

};



int main() {

    ll k;

    cin >> k;



    if (k <= 9) {

        cout << k << endl;

        return 0;

    }



    queue<struct node> q;

    rep(i, 1, 10) {

        struct node temp = {i, to_string(i)};

        q.push(temp);

    }



    ll cnt = 0;



    struct node t;

    while (cnt < k) {

        t = q.front(); q.pop();

        rep(i, t.num - 1, t.num + 2) {

            if (i < 0 || i > 9) continue;

            struct node temp = {i, t.all + to_string(i)};

            q.push(temp);

        }

        cnt++;

    }



    cout << t.all << endl;



    return 0;

}

Submission status: Runtime Error

Solution code:
i = int(input())
p = 11
count = 10
if i < 11:
  print(i)
else:
  i = 1 - 10
  while i != count:
    x = p.split()
    l = len(x) - 2
    j = 0
    for f in range(l):
      if abs(int(x[f]) - int(x[f+1])) > 1:
        j = 1
    if j == 0:
      count += 1
  print(count)

Submission status: Runtime Error

Solution code:
k = int(input())
table = [1,2,3,4,5,6,7,8,9,10,11,12,21,22,23,32,33,34,43,44,45,54,55,56,65,66,67,76,77,78,87,88,89,98]
for i in range(1,9):
    key1=int(str(i)+str(i-1)+str(i-1))
    key2=int(str(i)+str(i)+str(i))
    key3=int(str(i)+str(i+1)+str(i+1))
    table.append(key1-1)
    table.append(key1)
    table.append(key1+1)
    table.append(key2-1)
    table.append(key2)
    table.append(key2+1)
    table.append(key3-1)
    table.append(key3)
    table.append(key3+1)
table.pop()
table.append(987)
table.append(988)
table.append(989)
table.append(998)
table.append(999)
table.append(1000)
table.append(1001)
table.append(1010)
table.append(1011)
table.append(1012)
table.append(1100)
table.append(1101)
table.append(1110)
table.append(1111)
table.append(1112)
table.append(1121)
table.append(1122)
table.append(1123)
table.append(1210)
table.append(1211)
table.append(1212)
table.append(1221)
table.append(1222)
table.append(1223)
table.append(1232)
table.append(1233)
table.append(1234)
for j in range(2,9):
        key1=int(str(j)+str(j-1)+str(j-1)+str(j-1))
        key2=int(str(j)+str(j)+str(j)+str(j))
        key3=int(str(j)+str(j+1)+str(j+1)+str(j+1))    
    table.append(key1-1)
    table.append(key1)
    table.append(key1+1)
    table.append(key2-1)
    table.append(key2)
    table.append(key2+1)
    table.append(key3-1)
    table.append(key3)
    table.append(key3+1)
table.pop()
table.append(9887)
table.append(9888)
table.append(9889)
table.append(9998)
table.append(9999)
print(table[k-1])

Submission status: Runtime Error

Solution code:
K=int(input())
i=0
current_num=0
while (i<K) :
  current_string=str(current_num)
  flag=True
  for k in range(len(current_string)-1) :
    if abs(int(current_string[k])-int(current_string[k+1]))>1 :
      flag=False
      break
  if flag=True :
    i+=1
  current_num+=1
  
print(current_num)

Submission status: Runtime Error

Solution code:
from collections import deque
a=int(input())
q=deque([1,2,3,4,5,6,7,8,9])
def dfs(k):
  while k=a:
    s=q.popleft()
    if s-1>=0:
      q.aapend(s*10+s-1)
      n=n+1
    q.append(s*10+s)
    k=k+1
    if s+1<=9:
      q.append(s*10+s+1)
      k=k+1
  print(q.pop())

if a<=9:
  print(a)
else:
  dfs(9)

Submission status: Runtime Error

Solution code:
N, count, i, lists = input(), 0, 0, []
while (len(lists) - 9) != N:
    for i in range(9, 10000000):
        i = str(i)
            for x in range(len(i) - 1):
                if abs(int(i[x]) - int(i[x + 1])) == 1:
                    count += 1
            if count == (len(i) - 1):
                    lists.append(i)
            if len(lists) = N:
                break
print(int(lists[len(lists) - 9]))

Submission status: Runtime Error

Solution code:
K=int(input())
A=[0]*10
indicator=[0]*10
for i in range(0,K):
    for j in range(0,10):
        if A[j]==9:
            indicator[j]=1
    for j in range(0,10):
        if indicator[9-j]==1:
            continue
        break
    A[9-j]=A[9-j]+1
    for k in range(9-j,9):
        if A[k]==0:
            A[k+1]=0
        else:
            A[k+1]=A[k]-1
    for k in range(0,9):
        indicator[k+1]=A[k+1]-A[k]
        if A[k+1]==1 and A[k]==0:
            indicator[k+1]=0
for t in range(0,10):
  answer=answer+A[9-t]*10^(9-t)

Submission status: Runtime Error

Solution code:
K = int(input());
result = 0;
if(len(str(K)) == 1):
    print(K);
elif(len(str(K)) == 2):
    for i in range(10,K):
        while(abs(int(str(i)[0]) - int(str(i)[1])) <= 1):
            result = i;
            count +=1;
        if(count + 10 > K):
            break;
    print(result);
else:
    #わからん
    print(K);

Submission status: Runtime Error

Solution code:
a=int(input())
List=[]
for i in range (1,1000000):
    x=str(i)
    b=True
    for j in range(len(x)-1):
        if abs(int(x[j])-int(x[j+1]))<=1:
            continue
        else:
            b=False
            break
    if b:
        List+=[i]
print(List[a-1])

Submission status: Runtime Error

Solution code:
%load_ext Cython
li = list(map(int, input().split()))
k = li[0]
a = 9
r = [1, 2, 3, 4, 5, 6, 7, 8, 9]

i = 10
while True:
    if k < 10:
        break
    j = 0
    if a == k:
        break
    l = list(str(i))
    l_i = [int(s) for s in l]
    for x in range(0, len(l_i)-1):
        if abs(l_i[x] - l_i[x + 1]) <= 1:
            j += 1
    if j + 1 == len(l_i):
        r.append(i)
        a+=1
    i += 1
print(r[k-1])

Submission status: Runtime Error

Solution code:
import itertools
k=int(input())
l=[[1,2,3,4,5,6,7,8,9]]
for _ in range(10):
  a=[]
  for i in range(len(l[_])):
    L=l[_][i]
    if L%10==9:
      a.append(L*10+8)
      a.append(L*10+9)
    elif L%10==0:
      a.append(L*10+0)
      a.append(L*10+1)
    else:
      a.append(L*10+L%10-1)
      a.append(L*10+L%10)
      a.append(L*10+L%10+1)
    l.append(a)
print(sorted(set(list(itertools.chain.from_iterable(l))))[k-1])