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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
constexpr int mod = 1e9 + 7;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const mint &a) { return (os << a.x); }
};
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

int main() {
  int k;
  cin >> k;
  queue<ll> q;
  for (int i = 1; i < 10; ++i)
    q.push(i);

  ll x = 0;
  int c = 0;
  while (c < k) {
    x = q.front();
    q.pop();
    ++c;
    // printf("%d %d\\n", c, x);
    if (x % 10 != 0) {
      q.push(10 * x + x % 10 - 1);
    }
    q.push(10 * x + x % 10);
    if (x % 10 != 9) {
      q.push(10 * x + x % 10 + 1);
    }
  }
  // while (!q.empty()) {
  //   cout << q.front() << endl;
  //   q.pop();
  // }
  cout << x << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
constexpr int mod = 1e9 + 7;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const mint &a) { return (os << a.x); }
};
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

int main() {
  ll num = 0;
  int k;
  cin >> k;

  vector<bool> dp = vector<bool>(10, true);
  int count = 10;

  for (ll i = dp.size(); count <= k + 1; ++i) {

    ll d = log10(i);
    ll d1 = i / static_cast<int>(pow(10, d));
    ll d2 = i % static_cast<int>(pow(10, d));

    // cout << d1 << " " << d2 << endl;

    if (!dp[d2]) {
      dp[i] = false;
      continue;
    }

    ll d2d1 = d2 / static_cast<int>(
                       pow(10, (d2 == 0 ? 0 : static_cast<int>(log10(d2)))));
    // cout << d2 << " " << d2d1 << endl;

    if (abs(d1 - d2d1) <= 1) {
      dp[i] = true;
      ++count;
    } else {
      dp[i] = false;
    }

    if (count == k + 1) {
      k = i;
      break;
    }

    // dp[i] = true or false
  }
  cout << k << endl;

  // vector<string> l;
  // vector<string> _l = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

  // vector<ll> _l = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

  // for (signed j = 1; _l.size() <= k; ++j) {
  //   vector<ll> __l;
  //   for (int i = 1; i < 10; ++i) {
  //     for (auto e : _l) {
  //       signed p = (e == 0 ? 0 : log10(e));
  //       int f = e / static_cast<ll>(pow(10, j - 1));
  //       if (abs(i - f) <= 1) { __l.push_back(i * pow(10, j) + f); }
  //     }
  //   }

  //   for (auto e : __l) { _l.push_back(e); }
  // }

  // while (l.size() <= k) {
  //   vector<string> __l;
  //   // cout << " A" << endl;
  //   if (_l.front() != "0") { __l.push_back("0"); }
  //   // cout << "C" << endl;
  //   for (int i = 1; i < 10; ++i) {
  //     for (auto e : _l) {
  //       char c = e[0] - '0';
  //       if (abs(i - c) <= 1) {
  //         string s = to_string(i) + e;
  //         // cout << e << " " << c << " " << s << endl;
  //         __l.push_back(s);
  //       }
  //     }
  //   }

  //   for (auto e : __l) { _l.push_back(e); }
  // }

  // for (int i = 0; i < _l.size(); ++i) cout << _l[i] << endl;
  // cout << _l[k] << endl;
}