Problem p02720 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;
// typedef boost::multiprecision::cpp_int bigint;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  LL K;
  cin >> K;
  VVLL DP;
  DP.resize(10, VLL(12, 0));
  DP[0][1] = 1;
  for (LL a = 1; a <= 9; a++) {
    DP[a][1] = 1;
  }
  for (LL n = 2; n <= 11; n++) {
    for (LL a = 0; a <= 9; a++) {
      if (a > 0)
        DP[a][n] += DP[a - 1][n - 1];
      DP[a][n] += DP[a][n - 1];
      if (a < 9)
        DP[a][n] += DP[a + 1][n - 1];
    }
  }
  VVLL X;
  X.resize(10, VLL(12, 0));
  for (LL n = 1; n <= 11; n++) {
    X[1][n] = X[9][n - 1] + DP[1][n];
    for (LL a = 2; a <= 9; a++) {
      X[a][n] = X[a - 1][n] + DP[a][n];
    }
  }
  LL N = 1, A = 1;
  while (X[A][N] < K) {
    A++;
    if (A == 10) {
      N++;
      A = 1;
    }
  }
  K -= X[A - 1][N];
  cout << A;
  while (N > 1) {
    LL temp = 0;
    if (A > 0) {
      if (0 < K && K <= DP[A - 1][N - 1]) {
        cout << A - 1;
        A = A - 1;
        K -= temp;
        N--;
        continue;
      }
      temp += DP[A - 1][N - 1];
    }
    if (temp < K && K <= temp + DP[A][N - 1]) {
      cout << A;
      K -= temp;
      N--;
      continue;
    }
    temp += DP[A][N - 1];
    if (A < 9) {
      if (temp < K && K <= temp + DP[A + 1][N - 1]) {
        cout << A + 1;
        A = A + 1;
        K -= temp;
        N--;
        continue;
      }
    }
  }
  cout << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;
// typedef boost::multiprecision::cpp_int bigint;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  LL K;
  cin >> K;
  VVLL DP;
  DP.resize(10, VLL(12, 0));
  DP[0][1] = 1;
  for (LL a = 1; a <= 9; a++) {
    DP[a][1] = 1;
  }
  for (LL n = 2; n <= 11; n++) {
    for (LL a = 0; a <= 9; a++) {
      if (a > 0)
        DP[a][n] += DP[a - 1][n - 1];
      DP[a][n] += DP[a][n - 1];
      if (a < 9)
        DP[a][n] += DP[a + 1][n - 1];
    }
  }
  VVLL X;
  X.resize(10, VLL(12, 0));
  for (LL n = 1; n <= 11; n++) {
    X[0][n] = X[9][n - 1];
    X[1][n] = X[9][n - 1] + DP[1][n];
    for (LL a = 2; a <= 9; a++) {
      X[a][n] = X[a - 1][n] + DP[a][n];
    }
  }
  LL N = 1, A = 1;
  while (X[A][N] < K) {
    A++;
    if (A == 10) {
      N++;
      A = 1;
    }
  }
  K -= X[A - 1][N];
  cout << A;
  while (N > 1) {
    LL temp = 0;
    if (A > 0) {
      if (0 < K && K <= DP[A - 1][N - 1]) {
        cout << A - 1;
        A = A - 1;
        K -= temp;
        N--;
        continue;
      }
      temp += DP[A - 1][N - 1];
    }
    if (temp < K && K <= temp + DP[A][N - 1]) {
      cout << A;
      K -= temp;
      N--;
      continue;
    }
    temp += DP[A][N - 1];
    if (A < 9) {
      if (temp < K && K <= temp + DP[A + 1][N - 1]) {
        cout << A + 1;
        A = A + 1;
        K -= temp;
        N--;
        continue;
      }
    }
  }
  cout << "\n";
  return 0;
}