Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())

using namespace std;
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1001001001;
const long long INFL = (1LL << 60);
const double eps = (1e-9);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int k;
  cin >> k;
  vector<ll> a;
  for (int i = 1; i <= 9; i++)
    a.push_back(i);
  while (true) {
    if (k <= a.size()) {
      cout << a[k - 1] << endl;
      return 0;
    }
    k -= a.size();
    vector<ll> old;
    swap(old, a);
    for (ll x : old) {
      for (int i = -1; i <= 1; i++) {
        int d = x % 10 + i;
        if (d < 0 || d > 9)
          continue;
        ll nx = x * 10 + d;
        a.push_back(nx);
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())

using namespace std;
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1001001001;
const long long INFL = (1LL << 60);
const double eps = (1e-9);

ll dp[33][10];

// m 桁目 最上位の数は n, 下から取って x になる
void get_val(ll m, ll n, ll x, vector<int> &val) {
  //  cerr << "get_val: " << m << " " << n << " " << x << endl;
  ll tmp = 0;
  if (n != 0) {
    tmp += dp[m - 1][n - 1];
    if (tmp == x) {
      val[m - 1] = n - 1;
      return;
    }
    if (tmp > x) {
      val[m - 1] = n - 1;
      get_val(m - 1, n - 1, x, val);
      return;
    }
  }

  x -= tmp;
  //  cerr << "x: " << x << endl;

  tmp = dp[m - 1][n];
  if (tmp == x) {
    val[m - 1] = n;
    return;
  }
  if (tmp > x) {
    val[m - 1] = n;
    get_val(m - 1, n, x, val);
    return;
  }

  x -= tmp;
  //  cerr << "x: " << x << endl;

  if (n != 9) {
    tmp = dp[m - 1][n + 1];
    if (tmp == x) {
      val[m - 1] = n + 1;
      return;
    }
    if (tmp > x) {
      val[m - 1] = n + 1;
      get_val(m - 1, n + 1, x, val);
      return;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll k;
  cin >> k;

  ll num = 0;
  ll m = 1;
  ll n = 1;

  bool end = false;
  for (int i = 0; i <= 9; i++)
    dp[1][i] = 1;
  num += 9;
  for (int i = 2; i <= 32; i++) {
    for (int j = 0; j <= 9; j++) {
      if (j == 0) {
        dp[i][j] += dp[i - 1][j];
        dp[i][j] += dp[i - 1][j + 1];
      } else if (j == 9) {
        dp[i][j] += dp[i - 1][j - 1];
        dp[i][j] += dp[i - 1][j];
      } else {
        dp[i][j] += dp[i - 1][j - 1];
        dp[i][j] += dp[i - 1][j];
        dp[i][j] += dp[i - 1][j + 1];
      }
      if (j != 0) {
        if (num + dp[i][j] > k) {
          end = true;
          break;
        } else if (num + dp[i][j] == k) {
          m = i;
          n = j;
          end = true;
          break;
        } else {
          m = i;
          n = j;
          num += dp[i][j];
        }
      }
    }
    if (end)
      break;
  }

  // cerr << m << " " << n << endl;
  // cerr << num << endl;
  vector<int> val(33, 0);
  val[m] = n;
  if (k != num)
    get_val(m, n, k - num, val);

  string s;
  bool flag = false;
  for (int i = 32; i >= 1; i--) {
    if (val[i] != 0)
      flag = true;
    if (!flag)
      continue;
    char c = '0' + val[i];
    s.append(1, c);
  }

  cout << s << endl;

  return 0;
}