Problem p02720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <stdio.h>
#include <utility>
#include <vector>

using namespace std;

long read_int() {
  long a;
  scanf("%ld", &a);
  return a;
}

unsigned int get_digit(long num) {
  unsigned int digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
}

inline bool is_lunlun(long num) {
  // if(num == 0){
  //     return false;
  // }
  unsigned int dig = get_digit(num);
  if (dig < 2) {
    return true;
  }
  long n[10] = {};
  n[0] = num % 10;
  n[1] = num / 10 % 10;
  n[2] = num / 100 % 10;
  n[3] = num / 1000 % 10;
  n[4] = num / 10000 % 10;
  n[5] = num / 100000 % 10;
  n[6] = num / 1000000 % 10;
  n[7] = num / 10000000 % 10;
  n[8] = num / 100000000 % 10;
  n[9] = num / 1000000000 % 10;
  for (int k = 0; k < dig - 1; k++) {
    if (n[dig - 1 - k] - n[dig - k] < -1 || n[dig - 1 - k] - n[dig - k] > 1) {
      return false;
    }
  }
  return true;
}

long count_branch(int number, int depth) {
  if (depth < 0) {
    return 0;
    // fprintf(stdout, "error\\n");
  }
  if (number < 0 || number > 9) {
    return 0;
  }
  if (depth == 1) {
    // return the number of my decsendent branch
    return 1;
  } else {
    // return
    int ret = 0;
    ret += count_branch(number - 1, depth - 1);
    ret += count_branch(number, depth - 1);
    ret += count_branch(number + 1, depth - 1);
    return ret;
  }
}

long count_lunlun_of_digit(int digit) {
  int num = 0;
  for (int i = 1; i < 10; i++) {
    num += count_branch(i, digit);
  }
  return num;
}

long count_lunlun(int digit) {
  int num = 0;
  for (int i = 1; i <= digit; i++) {
    num += count_lunlun_of_digit(i);
  }
  return num;
}

long get_lunlun(int digit) { return 1; }

long convert_array_to_lunlun(array<int, 10> arr) {
  long ret = 0;
  for (int i = 0; i < arr.size(); i++) {
    long factor = 1;
    for (int k = 1; k <= i; k++) {
      factor *= 10;
    }
    ret += factor * arr[i];
  }
  return ret;
}

long search_lunlun(long target) {
  int digit = 1;
  while (count_lunlun(digit) < target) {
    digit++;
  }
  target -= count_lunlun(digit - 1);
  if (target == 0) {
    return get_lunlun(digit);
  }
  array<int, 10> lunlun;
  lunlun.fill(0);
  lunlun[digit - 1] = 1;
  while (digit > 0) {
    while (count_branch(lunlun[digit - 1], digit) < target) {
      target -= count_branch(lunlun[digit - 1], digit);
      lunlun[digit - 1]++;
    }
    digit--;
    if (digit > 0) {
      lunlun[digit - 1] = lunlun[digit] - 1;
    }
  }
  if (target != 0) {
    // printf("fail\\n");
  }
  return convert_array_to_lunlun(lunlun);
}

long D(long K) {
  long num = 0;
  // for(int i=1;i<10;i++){
  //     printf("%d %ld\\n", i, count_branch(i,4));
  // }
  // printf("count %ld\\n", count_lunlun(10));
  // array<int, 10> lunlun;
  // lunlun.fill(0);
  // for(int i=0;i<lunlun.size();i++){
  //     lunlun[i]=i;
  //     // lunlun[9]=8;
  // }
  // printf("convert %ld\\n", convert_array_to_lunlun(lunlun));
  long ans = search_lunlun(K);
  // printf("searched %ld\\n", ans);
  return ans;
}

int main() {
  long K = read_int();

  // D(13);
  // for(int i=1;i<20;i++){
  //     printf("%d %ld\\n",i, D(i));
  // }
  printf("%ld\\n", D(K));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <utility>

using namespace std;

long read_int() {
  long a;
  scanf("%ld", &a);
  return a;
}

unsigned int get_digit(long num) {
  unsigned int digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
}

inline bool is_lunlun(long num) {
  // if(num == 0){
  //     return false;
  // }
  unsigned int dig = get_digit(num);
  if (dig < 2) {
    return true;
  }
  for (int k = 0; k < dig - 1; k++) {
    long num1 = num;
    long num2 = num;
    for (int i = k; i < dig - 2; i++) {
      num1 /= 10;
    }
    num1 %= 10;
    for (int i = k; i < dig - 1; i++) {
      num2 /= 10;
    }
    num2 %= 10;
    if (num1 - num2 < -1 || num1 - num2 > 1) {
      return false;
    }
  }
  return true;
}

long D(long K) {
  long num = 0;
  for (int i = 0; i < K; i++) {
    num++;
    while (!is_lunlun(num)) {
      num++;
    }
    // printf("%ld\\n", num);
  }
  return num;
}

int main() {
  long K = read_int();

  printf("%ld\\n", D(K));
  return 0;
}