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:
function getOsVersion {
  if [[ -e "/etc/os-release" ]]; then
    os=`cat /etc/os-release | grep "^ID=" | sed \
      -e 's/ID=//' \
      -e 's/"//g'`
    version=`cat /etc/os-release | grep "^VERSION_ID=" | sed \
      -e 's/VERSION_ID=//' \
      -e 's/"//g' \
      -e 's/\.//'`
    os=$os$version

  #The /etc/SuSE-release file will be deprecated in sles11.4 and later release
  elif [[ -e "/etc/SuSE-release" ]]; then
    os='sles'
    version=`cat /etc/SuSE-release | grep "VERSION =" | sed \
      -e 's/^.*VERSION =//' \
      -e 's/\s*$//' \
      -e 's/.//' \
      -e 's/[^0-9]*([0-9]+).*/$1/'`
    os=$os$version

    # Append service level
    level=`echo "/etc/SuSE-release" | grep "LEVEL =" | sed \
      -e 's/^.*LEVEL =//' \
      -e 's/\s*$//' \
      -e 's/.//' \
      -e 's/[^0-9]*([0-9]+).*/$1/'`
    os=$os'sp'$level

  #The /etc/redhat-release file will be deprecated in rhel7 and later release
  elif [[ -e "/etc/redhat-release" ]]; then
    os='rhel'
    version=`cat /etc/redhat-release | grep -i "Red Hat Enterprise Linux Server" | sed \
      -e 's/[A-Za-z\/\.\(\)]//g' \
      -e 's/^ *//g' \
      -e 's/ *$//g' \
      -e 's/\s.*$//'`
    os=$os$version
  fi
  return
}

function onlineDevice {
  device=$1
  local funcName="onlineDevice"
  rc=$(/sbin/chccwdev -e $device > /dev/null; echo $?)
  if (( rc != 0 )); then
    if [[ -e /sbin/cio_ignore ]]; then
      rc=$(/sbin/cio_ignore -r 0.0.$device > /dev/null; echo $?)
      which udevadm &> /dev/null && udevadm settle || udevsettle
    fi
    rc=$(/sbin/chccwdev -e $device > /dev/null; echo $?)
    if (( rc != 0 )); then
      echo "$funcName (Error) Could not activate the virtual device $device"
      return 1
    fi
  fi
  which udevadm &> /dev/null && udevadm settle || udevsettle
  return 0
}

function setupDisk {
  local funcName="setupDisk"

  # Parse the parameter from parameter list
  for parameter in $@; do
    keyName=${parameter%\=*}
    value=${parameter#*\=}
    value=$(echo ${value} | sed -e 's/^ *//g')
    newKey='xcat_'$keyName
    eval $newKey=$value
  done

  # Remove the invokeScript.sh file after we have read it
  rm -f invokeScript.sh
  if [[ $xcat_action == "createfilesysnode" ]]; then
    echo "Creating a file system node, source: $xcat_srcFile, target: $xcat_tgtFile"

    if [[ ! -n $xcat_srcFile ]]; then
      echo "$funcName (Error) Source file for creating a file system node was not specified"
      return
    fi

    if [[ ! -n $xcat_tgtFile ]]; then
      echo "$funcName (Error) Target file for creating a file system node was not specified"
      return
    fi
    if [[ -e $xcat_tgtFile ]]; then
      echo "$funcName (Error) Target file for creating a file system node already exists"
      return
    fi

    configFile='/etc/udev/rules.d/56-zfcp.rules'
    # Create udev config file if not exist
    if [[ ! -e $configFile ]]; then
      touch $configFile
      if [[ $os == rhel* ]]; then
        echo "KERNEL==\"zfcp\", RUN+=\"/sbin/zfcpconf.sh\"" >> ${configFile}
        echo "KERNEL==\"zfcp\", RUN+=\"/sbin/multipath -r\"" >> ${configFile}
      fi
    fi

    tgtNode=$(echo ${xcat_tgtFile} | sed -e 's/^\/dev\///')
    if [[ $os == sles* || $os == rhel* ]]; then
      wwpn_lun=$(echo ${xcat_srcFile} | sed -e 's/^\/dev.*-zfcp-//')
      wwpn=$(echo ${wwpn_lun} | sed -e 's/:0x.*//')
      lun=$(echo ${wwpn_lun} | sed -e 's/^0x.*://')
    else
      wwpn_lun=$(echo ${xcat_srcFile} | sed -e 's/^\/dev.*-fc-//')
      wwpn=$(echo ${wwpn_lun} | sed -e 's/-lun-.*//')
      lun=$(echo ${wwpn_lun} | sed -e 's/^0x.*-lun-//')
    fi


    multipath=0
    out=`echo $wwpn | grep ","`
    if [[ -n "$out" ]]; then
      multipath=1
    fi

    if [[ $os == sles* || $os == rhel* ]]; then
      fcp=$(echo ${xcat_srcFile} | sed -e 's/^\/dev.*ccw-0.0.//' | sed -e 's/-zfcp-.*$//')
    else
      fcp=$(echo ${xcat_srcFile} | sed -e 's/^\/dev.*ccw-0.0.//' | sed -e 's/-fc-.*$//')
    fi
    oldIFS=$IFS
    IFS=","
    fcpList=($fcp)
    for fcp in ${fcpList[@]}
    do
      if [[ $multipath == 1 ]]; then
        # Find the name of the multipath device by arbitrary one path in the set
        wwpnList=($wwpn)
        for wwpn in ${wwpnList[@]}
        do
          if [[ ${wwpn:0:2} -ne "0x" ]]; then
            wwpn="0x$wwpn"
          fi
          if [[ $os == sles* || $os == rhel* ]]; then
            cur_wwpn_lun=${wwpn}:${lun}
            srcFile=$(echo ${xcat_srcFile} | sed -e 's/-zfcp-.*//')"-zfcp-"$cur_wwpn_lun
            srcFile=$(echo ${srcFile} | sed -e 's/ccw-.*-zfcp/ccw-0.0.'$fcp'-zfcp/')
          else
            cur_wwpn_lun=${wwpn}-lun-${lun}
            srcFile=$(echo ${xcat_srcFile} | sed -e 's/-fc-.*//')"-fc-"$cur_wwpn_lun
            srcFile=$(echo ${srcFile} | sed -e 's/ccw-.*-fc/ccw-0.0.'$fcp'-fc/')
          fi

          out=`/usr/bin/stat --printf=%n ${srcFile}`
          if (( $? != 0 )); then
            echo "$funcName (Error) Unable to stat the source file: $srcFile"
            continue
          fi

          out=`/sbin/udevadm info --query=all --name=$srcFile | grep ID_SERIAL=`
          devName=$(echo ${out} | sed -e 's/^E:\s//')
          multipathUuid=$(echo $devName | sed -e 's/ID_SERIAL=//')
          if [[ -n "$multipathUuid" ]]; then
            break
          fi
        done

        if [[ -z "$multipathUuid" ]]; then
            echo "$funcName (Error) Building up multipath failed!"
            return
        fi
      else
        if [[ $os == sles* || $os == rhel* ]]; then
          srcFile=$(echo ${xcat_srcFile} | sed -e 's/ccw-.*-zfcp/ccw-0.0.'$fcp'-zfcp/')
        else
          srcFile=$(echo ${xcat_srcFile} | sed -e 's/ccw-.*-zfcp/ccw-0.0.'$fcp'-fc/')
        fi
        out=`/usr/bin/stat --printf=%n ${srcFile}`
        if (( $? != 0 )); then
          echo "$funcName (Error) Unable to stat the source file: $xcat_srcFile"
          return
        fi
      fi
    done
    IFS=$oldIFS

    # Add the entry into udev config file
    if [[ $multipath == 1 ]]; then
      echo "KERNEL==\"dm*\", ENV{DM_UUID}==\"mpath-${multipathUuid}\", SYMLINK+=\"${tgtNode}\"" >> ${configFile}
      udevadm control --reload
      udevadm trigger --sysname-match=dm-*
    else
      echo "KERNEL==\"sd*\", ATTRS{wwpn}==\"${wwpn}\", ATTRS{fcp_lun}==\"${lun}\", SYMLINK+=\"${tgtNode}%n\"" >> ${configFile}
      udevadm control --reload
      udevadm trigger --sysname-match=sd*
    fi

  echo "$funcName successfully create the file system node ${xcat_tgtFile}"

  elif [[ $xcat_action == "removefilesysnode" ]]; then
    echo "Removing a file system node, target: $xcat_tgtFile"
    if [[ ! -n $xcat_tgtFile ]]; then
      echo "$funcName (Error) Target file for creating a file system node was not specified"
      return
    fi

    configFile='/etc/udev/rules.d/56-zfcp.rules'
    tgtNode=$(echo ${xcat_tgtFile} | sed -e 's/^\/dev\///')

    sed -i -e /SYMLINK+=\"${tgtNode}%n\"/d ${configFile}
    sed -i -e /SYMLINK+=\"${tgtNode}\"/d ${configFile}
    udevadm control --reload
    udevadm trigger --sysname-match=sd*
    udevadm trigger --sysname-match=dm-*
    echo "$funcName successfully remove the file system node ${xcat_tgtFile}"
  elif [[ $xcat_action == "addScsiVolume" ]]; then
    echo "Adding a SCSI Volume, FCP addr: $xcat_fcpAddr, WWPN: $xcat_wwpn, LUN: $xcat_lun"

    # Validate the input
    if [[ ! -n $xcat_fcpAddr ]]; then
      echo "$funcName (Error) FCP address was not specified"
      return
    fi
    xcat_fcpAddr=`echo $xcat_fcpAddr | tr '[A-Z]' '[a-z]'`

    if [[ ! -n $xcat_wwpn ]]; then
      echo "$funcName (Error) WWPN was not specified"
      return
    fi
    xcat_wwpn=`echo $xcat_wwpn | tr '[A-Z]' '[a-z]'`

    if [[ ! -n $xcat_lun ]]; then
      echo "$funcName (Error) LUN was not specified"
      return
    fi
    xcat_lun=`echo $xcat_lun | tr '[A-Z]' '[a-z]'`
    decimal_lun=$((16#${xcat_lun:0:4}))

    # Online the device
    oldIFS=$IFS
    IFS=","
    fcp_list=($xcat_fcpAddr)
    for fcp in ${fcp_list[@]}
    do
      rc= onlineDevice $fcp
      if (( rc != 0 )); then
        return
      fi
      if [[ $os == sles12* ]]; then
        out=`cat /boot/zipl/active_devices.txt | grep -i "0.0.$fcp"`
        if [[ -z $out ]]; then
          /sbin/zfcp_host_configure 0.0.$fcp 1
        fi
      elif [[ $os == sles11* ]]; then
        /sbin/zfcp_host_configure 0.0.$fcp 1
      elif [[ $os == ubuntu* ]]; then
        /sbin/chzdev zfcp-host $fcp -e
      fi
    done

    multipath=0
    out=`echo $xcat_wwpn | grep ","`
    if [[ -n "$out" ]]; then
      multipath=1
    fi

    # Start multipathd service
    if [[ $multipath == 1 ]]; then
        if [[ $os == sles* ]]; then
            insserv multipathd
        elif [[ $os == rhel6* ]]; then
            chkconfig multipathd on
        else
            systemctl enable multipathd
        fi
        modprobe dm-multipath
    fi

    for fcp in ${fcp_list[@]}
    do
      wwpn_list=($xcat_wwpn)
      for wwpn in ${wwpn_list[@]}
      do
        # Set WWPN and LUN in sysfs
        echo 0x$xcat_lun > /sys/bus/ccw/drivers/zfcp/0.0.$fcp/0x$wwpn/unit_add

        # Set WWPN and LUN in configuration files
        if [[ $os == sles* ]]; then
          #   SLES: /etc/udev/rules.d/51-zfcp*
          /sbin/zfcp_disk_configure 0.0.$fcp $wwpn $xcat_lun 1

          # Configure zFCP device to be persistent
          touch /etc/udev/rules.d/51-zfcp-0.0.$fcp.rules

          # Check if the file already contains the zFCP channel
          out=`cat "/etc/udev/rules.d/51-zfcp-0.0.$fcp.rules" | egrep -i "ccw/0.0.$fcp]online"`
          if [[ ! $out ]]; then
            echo "ACTION==\"add\", SUBSYSTEM==\"ccw\", KERNEL==\"0.0.$fcp\", IMPORT{program}=\"collect 0.0.$fcp %k 0.0.$fcp zfcp\"" \
              | tee -a /etc/udev/rules.d/51-zfcp-0.0.$fcp.rules
            echo "ACTION==\"add\", SUBSYSTEM==\"drivers\", KERNEL==\"zfcp\", IMPORT{program}=\"collect 0.0.$fcp %k 0.0.$fcp zfcp\"" \
              | tee -a /etc/udev/rules.d/51-zfcp-0.0.$fcp.rules
            echo "ACTION==\"add\", ENV{COLLECT_0.0.$fcp}==\"0\", ATTR{[ccw/0.0.$fcp]online}=\"1\"" \
              | tee -a /etc/udev/rules.d/51-zfcp-0.0.$fcp.rules
          fi

          echo "ACTION==\"add\", KERNEL==\"rport-*\", ATTR{port_name}==\"0x$wwpn\", SUBSYSTEMS==\"ccw\", KERNELS==\"0.0.$fcp\", ATTR{[ccw/0.0.$fcp]0x$wwpn/unit_add}=\"0x$xcat_lun\"" \
            | tee -a /etc/udev/rules.d/51-zfcp-0.0.$fcp.rules
        elif [[ $os == rhel* ]]; then
          #   RHEL: /etc/zfcp.conf
          echo "0.0.$fcp 0x$wwpn 0x$xcat_lun" >> /etc/zfcp.conf
          echo "add" > /sys/bus/ccw/devices/0.0.$fcp/uevent
        elif [[ $os == ubuntu* ]]; then
          #   Ubuntu: chzdev zfcp-lun 0.0.$device:0x$wwpn:0x$lun -e
          /sbin/chzdev zfcp-lun 0.0.$fcp:0x$wwpn:0x$xcat_lun -e
        fi

        # Settle the file system so when we are done the device is fully available
        if [[ $(which udevadm 2> /dev/null) != '' ]]; then
          udevadm settle
        else
          udevsettle
        fi
        if [[ $os == rhel* || $os == sles* ]]; then
          if [[ ! -e "/dev/disk/by-path/ccw-0.0.${fcp}-zfcp-0x${wwpn}:0x${xcat_lun}" ]]; then
            # Sometimes the file takes longer to appear.  We will wait up to 3 minutes.
            maxTime=0
            for time in 1 2 2 5 10 10 30 60 60
            do
              if [[ -e "/dev/disk/by-path/ccw-0.0.${fcp}-zfcp-0x${wwpn}:0x${xcat_lun}" ]]; then
                # Leave the loop now that the file exists
                break
              fi
              maxTime=$maxTime+$time
              echo "Sleeping for $time seconds to allow /dev/disk/by-path/ccw-0.0.${fcp}-zfcp-0x${wwpn}:0x${xcat_lun} to be created"
              sleep $time
            done
          fi
          if [[ ! -e "/dev/disk/by-path/ccw-0.0.${fcp}-zfcp-0x${wwpn}:0x${xcat_lun}" ]]; then
            echo "/dev/disk/by-path/ccw-0.0.${fcp}-zfcp-0x${wwpn}:0x${xcat_lun} did not appear in $maxTime seconds, continuing."
          fi
        elif [[ $os == ubuntu* ]]; then
          if [[ ! -e "/dev/disk/by-path/ccw-0.0.${fcp}-fc-0x${wwpn}-lun-${decimal_lun}" ]]; then
            # Sometimes the file takes longer to appear.  We will wait up to 3 minutes.
            maxTime=0
            for time in 1 2 2 5 10 10 30 60 60
            do
              if [[ -e "/dev/disk/by-path/ccw-0.0.${fcp}-fc-0x${wwpn}-lun-${decimal_lun}" ]]; then
                # Leave the loop now that the file exists
                break
              fi
              maxTime=$maxTime+$time
              echo "Sleeping for $time seconds to allow /dev/disk/by-path/ccw-0.0.${fcp}-fc-0x${wwpn}-lun-${decimal_lun} to be created"
              sleep $time
            done
          fi
          if [[ ! -e "/dev/disk/by-path/ccw-0.0.${fcp}-fc-0x${wwpn}-lun-${decimal_lun}" ]]; then
            echo "/dev/disk/by-path/ccw-0.0.${fcp}-fc-0x${wwpn}-lun-${decimal_lun} did not appear in $maxTime seconds, continuing."
          fi
        fi
      done
    done
    IFS=$oldIFS

    /sbin/multipath -r

  echo "$funcName successfully create the SCSI volume"

  elif [[ $xcat_action == "removeScsiVolume" ]]; then
    echo "Removing a SCSI Volume, FCP addr: $xcat_fcpAddr, WWPN: $xcat_wwpn, LUN: $xcat_lun"

    # Validate the input
    if [[ ! -n $xcat_fcpAddr ]]; then
      echo "$funcName (Error) FCP address was not specified"
      return
    fi
    xcat_fcpAddr=`echo $xcat_fcpAddr | tr '[A-Z]' '[a-z]'`

    if [[ ! -n $xcat_wwpn ]]; then
      echo "$funcName (Error) WWPN was not specified"
      return
    fi
    xcat_wwpn=`echo $xcat_wwpn | tr '[A-Z]' '[a-z]'`

    if [[ ! -n $xcat_lun ]]; then
      echo "$funcName (Error) LUN was not specified"
      return
    fi
    xcat_lun=`echo $xcat_lun | tr '[A-Z]' '[a-z]'`

    oldIFS=$IFS
    IFS=","
    fcp_list=($xcat_fcpAddr)
    for fcp in ${fcp_list[@]}
    do
      wwpn_list=($xcat_wwpn)
      for wwpn in ${wwpn_list[@]}
      do
        # Delete the SCSI device
        scsiDevice=`lszfcp -l 0x$xcat_lun | grep 0x$wwpn | cut -d " " -f2`
        if [[ -n $scsiDevice ]]; then
          echo 1 > "/sys/bus/scsi/devices/$scsiDevice/delete"
        fi

        # Delete WWPN and LUN from sysfs
        if [[ -e /sys/bus/ccw/drivers/zfcp/0.0.$fcp/0x$wwpn/unit_remove ]]; then
          if [[ $(which udevadm 2> /dev/null) != '' ]]; then
            udevadm settle
          else
            udevsettle
          fi
          echo 0x$xcat_lun > /sys/bus/ccw/drivers/zfcp/0.0.$fcp/0x$wwpn/unit_remove
        fi

        # Delete WWPN and LUN from configuration files
        if [[ $os == sles11* || $os == sles12* ]]; then
          #   SLES: /etc/udev/rules.d/51-zfcp*
          expression="/$xcat_lun/d"
          sed --in-place -e $expression /etc/udev/rules.d/51-zfcp-0.0.$fcp.rules
        elif [[ $os == rhel* ]]; then
          #   RHEL: /etc/zfcp.conf
          expression="/$xcat_lun/d"
          sed --in-place -e $expression /etc/zfcp.conf
        elif [[ $os == ubuntu* ]]; then
          #   Ubuntu: chzdev zfcp-lun 0.0.$device:0x$wwpn:0x$lun -d
          /sbin/chzdev zfcp-lun 0.0.$fcp:0x$wwpn:0x$xcat_lun -d
        fi
      done
    done
    IFS=$oldIFS
    /sbin/multipath -W
    /sbin/multipath -r
    echo "$funcName successfully remove the SCSI volume"
  elif [[ $xcat_action == "addMdisk" ]]; then
    echo "Adding a minidisk based ephemeral disk, Vaddr: $xcat_vaddr, Filesystem: $xcat_filesys mountpoint:$xcat_mntdir"

    # Validate the input
    if [[ ! -n $xcat_vaddr ]]; then
      echo "$funcName (Error) Virtual address was not specified"
      return
    fi
    xcat_vaddr=`echo $xcat_vaddr | tr '[A-Z]' '[a-z]'`

    # Online the device
    rc= onlineDevice $xcat_vaddr
    if (( rc != 0 )); then
      echo "$funcName (Error) fail to online the disk $xcat_vaddr"
      return
    fi

    # Configure the added dasd to be persistent
    echo "Permanently online the ephemeral disk"
    if [[ $os == rhel* ]]; then
      out=`cat "/etc/dasd.conf" | egrep -i $xcat_vaddr`
      if [[ ! $out ]]; then
        echo "0.0.$xcat_vaddr" >> /etc/dasd.conf
      fi
    elif [[ $os == sles* ]]; then
      /sbin/dasd_configure 0.0.$xcat_vaddr 1
    elif [[ $os == ubuntu16* ]]; then
      touch /etc/sysconfig/hardware/config-ccw-0.0.$xcat_vaddr
    else
      echo "$funcName (Error) failed to permanently online the disk:$xcat_vaddr on os: $os, please check if $os is in the supported distribution list"
      return
    fi


    # Mount the mdisk to the specified mount point
    echo "Mounting the ephemeral disk $xcat_vaddr to directory $xcat_mntdir"
    if [[ -d $xcat_mntdir ]]; then
      rm -rf $xcat_mntdir
    fi
    mkdir -p $xcat_mntdir

    cp /etc/fstab /etc/fstab.bak
    out=`cat "/etc/fstab" | egrep -i "ccw-0.0.$xcat_vaddr"`
    if [[  $out ]]; then
      sed -i '/ccw-0.0.'"$xcat_vaddr"'/d' /etc/fstab
    fi

    if [[ $os == sles12* ]]; then
      echo "/dev/disk/by-path/ccw-0.0.${xcat_vaddr}-part1 $xcat_mntdir $xcat_filesys defaults,nofail 0 0" >> /etc/fstab
    else
      echo "/dev/disk/by-path/ccw-0.0.${xcat_vaddr}-part1 $xcat_mntdir $xcat_filesys defaults 0 0" >> /etc/fstab
    fi

    out=`mount -a 2>&1`
    if [[ "$out" ]]; then
      echo "Fail to mount the disk $xcat_vaddr with reason $out"
      mv /etc/fstab.bak /etc/fstab
     mount -a
    else
      echo "The disk $xcat_vaddr has been mounted to $xcat_mntdir in format $xcat_filesys successfully"
    fi

  fi

  return
}

Submission status: Runtime Error

Solution code:
import sys
import resource

sys.setrecursionlimit(10 ** 9)
resource.setrlimit(resource.RLIMIT_STACK, ((2 ** 7) * 1000 * 1000, -1))

k = int(input())

ary = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def ans(j, nums):
    nums[j] += 1
    pre = nums[j]
    for i in range(j + 1, len(nums)):
        if pre > 0:
            nums[i] = pre - 1
            pre = nums[i]
        else:
            nums[i] = pre
    return int(''.join(map(str, nums)))

def lunlun(n):
    if n <= 9:
        return ary[n - 1]
    l = lunlun(n - 1)
    nums = list(map(int, list(str(l))))
    first = len(nums) - 1
    last = 0
    for i in range(len(nums)):
        j = len(nums) - i - 1
        c = nums[j] + 1
        # 桁数が変わらないとき
        if j == first:
            # 1桁目
            if nums[j] < 9 and abs(nums[j - 1] - c) <= 1:
                nums[j] += 1
                return int(''.join(map(str, nums)))
        elif j == last:
            # 最終桁
            if nums[j] < 9 and abs(nums[j + 1] - c) <= 1:
                return ans(j, nums)
        elif j != first and j != last:
            # 間の桁
            if nums[j] < 9 and abs(nums[j - 1] - c) <= 1 and abs(nums[j + 1] - c) <= 1:
                return ans(j, nums)

    # 桁が繰り上がるとき
    return int('1' + ''.join(['0'] * len(nums)))

print(lunlun(k))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

n, k = map(int, input().split())

#while n > abs(n-k):
#    n = abs(n-k)
while 2*n > k:
    
    if n > k:
        n = n%k
    elif n <= k:
        n = k-n

print(n)

Submission status: Runtime Error

Solution code:
import numpy as np
K = int(input())
lis = ['1','2','3','4','5','6','7','8','9']
lis = np.array(lis)
if K == 1:
  print(1)
elif K < 10:
  print(K)
else:
  count = 9
  sub_count = 0
  while count < K:
    if lis[sub_count][-1] == '9':
      count += 2
      lis.append(lis[sub_count] + '8')
      lis.append(lis[sub_count] + '9')
      sub_count += 1
    elif  lis[sub_count][-1] == '0':
      count += 2
      lis.append(lis[sub_count] + '0')
      lis.append(lis[sub_count] + '1')
      sub_count += 1
    else:
      count += 3
      for i in range(-1,2):
        lis.append(lis[sub_count] + str(int(lis[sub_count][-1]) + i))
      sub_count += 1
  print(lis[count-1])

Submission status: Runtime Error

Solution code:
import sys
import resource

sys.setrecursionlimit(10 ** 9)
resource.setrlimit(resource.RLIMIT_STACK, ((2 ** 9) * 1000 * 1000, (2 ** 9) * 1000 * 1000))

k = int(input())

ary = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def ans(j, nums):
    nums[j] += 1
    pre = nums[j]
    for i in range(j + 1, len(nums)):
        if pre > 0:
            nums[i] = pre - 1
            pre = nums[i]
        else:
            nums[i] = pre
    return int(''.join(map(str, nums)))

def lunlun(n):
    if n <= 9:
        return ary[n - 1]
    l = lunlun(n - 1)
    nums = list(map(int, list(str(l))))
    first = len(nums) - 1
    last = 0
    for i in range(len(nums)):
        j = len(nums) - i - 1
        c = nums[j] + 1
        # 桁数が変わらないとき
        if j == first:
            # 1桁目
            if nums[j] < 9 and abs(nums[j - 1] - c) <= 1:
                nums[j] += 1
                return int(''.join(map(str, nums)))
        elif j == last:
            # 最終桁
            if nums[j] < 9 and abs(nums[j + 1] - c) <= 1:
                return ans(j, nums)
        elif j != first and j != last:
            # 間の桁
            if nums[j] < 9 and abs(nums[j - 1] - c) <= 1 and abs(nums[j + 1] - c) <= 1:
                return ans(j, nums)

    # 桁が繰り上がるとき
    return int('1' + ''.join(['0'] * len(nums)))

print(lunlun(k))

Submission status: Runtime Error

Solution code:
k = input()
n, m  = divmod(k-9,3)
if k <= 9:
    print(k)
elif m == 1:
    a = str(n+1)
    b = str(n)
    #print(a+b)
elif m == 2:
    a = str(n+1)
    b = str(n)
    #print(a+b)
elif m ==  0:
    a = str(n)
    b = str(n+1)

print(a+b)

Submission status: Runtime Error

Solution code:
K=int(input())
if K<10:
    print(K)
else:
    n=10
    i=9
while i<K:
    S=str(n)
    cond=True
    for j in range(len(S)-1):
        if abs(int(S[j])-int(S[j+1]))>1:
            cond=False
            break
    if cond:
        i+=1
#        print(n)
        n+=1
    else:
        n+=10**(len(S)-j-2)
#        print(n,10**(len(S)-j-2))
        
print(n-1)

Submission status: Runtime Error

Solution code:
import sys
import resource

sys.setrecursionlimit(10 ** 9)
resource.setrlimit(resource.RLIMIT_STACK, (-1, -1))

k = int(input())

ary = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def ans(j, nums):
    nums[j] += 1
    pre = nums[j]
    for i in range(j + 1, len(nums)):
        if pre > 0:
            nums[i] = pre - 1
            pre = nums[i]
        else:
            nums[i] = pre
    return int(''.join(map(str, nums)))

def lunlun(n):
    if n <= 9:
        return ary[n - 1]
    l = lunlun(n - 1)
    nums = list(map(int, list(str(l))))
    first = len(nums) - 1
    last = 0
    for i in range(len(nums)):
        j = len(nums) - i - 1
        c = nums[j] + 1
        # 桁数が変わらないとき
        if j == first:
            # 1桁目
            if nums[j] < 9 and abs(nums[j - 1] - c) <= 1:
                nums[j] += 1
                return int(''.join(map(str, nums)))
        elif j == last:
            # 最終桁
            if nums[j] < 9 and abs(nums[j + 1] - c) <= 1:
                return ans(j, nums)
        elif j != first and j != last:
            # 間の桁
            if nums[j] < 9 and abs(nums[j - 1] - c) <= 1 and abs(nums[j + 1] - c) <= 1:
                return ans(j, nums)

    # 桁が繰り上がるとき
    return int('1' + ''.join(['0'] * len(nums)))

print(lunlun(k))

Submission status: Runtime Error

Solution code:
k = int(input())
cou = 0
while True:
    cou += 1
    t = q.get()
    if cou == k:
        print(t)
        exit()
    if t % 10 == 0:
        q.put(t*10)
        q.put(t*10 + 1)
    elif t % 10 == 9:
        q.put(t*10 + 8)
        q.put(t*10 + 9)
    else:
        s = t % 10
        q.put(t*10 + s -1)
        q.put(t*10 + s)

Submission status: Runtime Error

Solution code:

n = int(input())

a = range(100000)
b = []

for i in a:
  aaa = 0
  bbb = str(i)
  if len(bbb) == 1:
    b.append(i)
    continue
  for ak in range(len(bbb) - 1):
    if abs(int(bbb[ak]) - int(bbb[ak + 1])) > 1:
      aaa += 1
      break
  if aaa == 0:
    b.append(i)
print(b[n])

Submission status: Runtime Error

Solution code:
import sys

K = int(input())

# K = 100000

if K < 10:
    print(K)
    sys.exit(0)

runrun = [1, 2, 3, 4, 5, 6, 7, 8, 9]
count = 9
while True:
    new_runrun = []
    for run in runrun:
        # run=runrun[0]
        run_str = str(run)
        last_num = int(run_str[-1])
        for i in range(3):
            diff = i - 1
            if (last_num == 9 and diff == 1) or (last_num == 0 and diff == -1):
                continue
            ans = int(f'{run_str}{last_num+diff}')
            new_runrun.append(ans)
            count += 1
            if count == K:
                print(ans)
                sys.exit(0)
    runrun = new_runrun

Submission status: Runtime Error

Solution code:
k = int(input())

ans = []


if k < 11:
    print(k)
else:
    l = [i for i in range(1, 10)]
    ans.extend(l)

    while len(ans) <= k:
        n = []
        cnt = 0
        for x in l:
            y = x * 10
            b = x % 10
            if b == 0:
                n.append(y)
                n.append(y+1)
            elif b == 9:
                n.append(y+b-1)
                n.append(y+b)
            else:
                n.append(y+b-1)
                n.append(y+b)
                n.append(y+b+1)
        l = n[:]
        ans.extend(l)

print(ans[k-1])

Submission status: Runtime Error

Solution code:
k=input()

ln=[1,2,3,4,5,6,7,8,9]
old=0
new=9
while len(ln)<100:
    for i in xrange(old, new):
        if ln[i]%10!=9:
            ln.append(ln[i]*10+ln[i]%10-1)
            ln.append(ln[i]*10+ln[i]%10)
            ln.append(ln[i]*10+ln[i]%10+1)
        else:
            ln.append(ln[i]*10+ln[i]%10-1)
            ln.append(ln[i]*10+ln[i]%10)
    old=new
    new=len(ln)

ln.sort()
print ln[k-1]

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

def neighbor(a):
    if a == 0:
        return [0,1]
    if a == 9:
        return [8,9]
    return [a-1,a,a+1]

def make_lunlun(digit, start):
    if digit == 1:
        return [start]
    res = []
    for start_neighbor in neighbor(start):
        b = make_lunlun(digit-1, start_neighbor)
        c = list(map(lambda x:x+(start*(10**(digit-1))), b))
        res.extend(c)
    return res
lunlun = []
for i in range(1,11):
    for j in range(1,10):
        lunlun.extend(make_lunlun(i, j))

k = int(input())
print(lunlun[k-1])


import sys
sys.setrecursionlimit(10**9)

def neighbor(a):
    if a == 0:
        return [0,1]
    if a == 9:
        return [8,9]
    return [a-1,a,a+1]

def make_lunlun(digit, start):
    if digit == 1:
        return [start]
    res = []
    for start_neighbor in neighbor(start):
        b = make_lunlun(digit-1, start_neighbor)
        c = list(map(lambda x:x+(start*(10**(digit-1))), b))
        res.extend(c)
    return res
lunlun = []
for i in range(1,11):
    for j in range(1,10):
        lunlun.extend(make_lunlun(i, j))

k = int(input())
print(lunlun[k-1])

Submission status: Runtime Error

Solution code:

from sys import stdin
import sys
sys.setrecursionlimit(10**8)

def nextLine(): return next(stdin)
def nextStrList(): return nextLine().split()
def nextIntList(): return [int(_) for _ in nextStrList()]


def main():
	k=nextIntList()[0]
	if k<=9:
		print(k)
		sys.exit(0)
	num=[0,1]
	for i in range(10, k):
# 		print(num)
		if all([n==9 for n in num]):
			num=[*[0 for d in range(len(num))], 1]
			continue
		
		for di in range(len(num)):
			if di==0:
				if num[di]<=num[di+1] and num[di]<9:
					num[di]+=1
					break
			elif di<len(num)-1:
				if num[di]<=num[di+1] and num[di]<9 and num[di]<=num[di-1]:
					num[di]+=1
					break
			else: #di==len(num)-1
				num[di]+=1
	
		for dii in range(di-1, -1,-1):
			if num[dii+1]==0:
				num[dii]=0
			else:
				num[dii]=num[dii+1]-1 
	
	print("".join(str(n) for n in reversed(num)))

if __name__=="__main__":
	main()

Submission status: Runtime Error

Solution code:
k = int(input())

if k < 11:
    print(k)
else:
    l = [i for i in range(1, 10)]
    cntotal = 9
    while cntotal < k:
        n = []
        cnt = 0
        for x in l:
            y = x * 10
            b = x % 10
            if b == 0:
                n.append(y)
                n.append(y+1)
                cnt += 2
            elif b == 9:
                n.append(y+b-1)
                n.append(y+b)
                cnt += 2
            else:
                n.append(y+b-1)
                n.append(y+b)
                n.append(y+b+1)
                cnt += 3
        cntotal += cnt
        l = n[:]

print(l[k - (cntotal - cnt) - 1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())

a = N - (N // K)*K

if N < K:
        print(N)
else:
        for i in range(100):
                a = min(a, abs(a - K)) 
        print(a)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

List = [str(i) for i in range(0, 10)]
prev_num = 10
K = int(input())
if K <= 9:
    print(List[K])
else:
    while True:
        List2 = []
        for i in List:
            if i != "0":
                List2.append(i + str(i[-1]))
                if i[-1] != "9":
                    List2.append(i + str(int(i[-1]) + 1))
                if i[-1] != "0":
                    List2.append(i + str(int(i[-1]) - 1))
        List = List2
        if prev_num + len(List) >= K:
            List = list(map(int, List))
            List.sort()
            print(List[K - prev_num])
            sys.exit()
        prev_num += len(List)