prithivMLmods/Coder-Stat huggingface dataset:
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
  long long N, K;
  cin >> N >> K;
  bool zero = false;
  long long a = N;
  long long b;
  while (!zero) {
    a = a - K;
    if (a < 0) {
      zero = true;
      b = a;
      a = a + K;
    }
  }
  b = -b;
  if (b > a)
    cout << a << endl;
  else
    cout << b << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
  long long N, K;
  cin >> N >> K;
  bool zero = false;
  long long a = N % K;
  long long b = a - K;
  b = -b;
  if (b > a)
    cout << a << endl;
  else
    cout << b << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
ll strtoint(string str);
int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  while (true) {
    n = abs(n - k);
    if (k == 1)
      break;
    if (abs(n - k) <= n)
      continue;
    else
      break;
  }
  (k == 1) ? cout << 0 << '\n' : cout << n << '\n';
  return 0;
}
ll strtoint(string str) {
  ll x = 0;
  stringstream ss;
  ss << str;
  ss >> x;
  return x;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
ll strtoint(string str);
int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  cout << min(n % k, k - n % k) << '\n';
  return 0;
}
ll strtoint(string str) {
  ll x = 0;
  stringstream ss;
  ss << str;
  ss >> x;
  return x;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

typedef long long unsigned int ll;
typedef pair<ll, ll> P;

int main() {
  int N, K;
  cin >> N >> K;
  cout << min(N % K, K - N % K) << endl;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

typedef long long unsigned int ll;
typedef pair<ll, ll> P;

int main() {
  ll N, K;
  cin >> N >> K;
  cout << min(N % K, K - N % K) << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, K;
  cin >> N >> K;
  if (N % K == 0) {
    N = 0;
  }
  if (N > K) {
    while (N > K) {
      N -= K;
    }
  }
  long Ans = min(N, abs(N - K));
  cout << Ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, K;
  cin >> N >> K;
  N -= (N / K) * K;
  long Ans = min(N, abs(N - K));
  cout << Ans << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define ll long long
#define _for(i, j, k) for (int i = j; i < k; i++)
#define endl '\n'
#define inf 1 << 29 - 1
const int mod = 1e9 + 7;
const int MAX = 1e6 + 5;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, k, t;
  cin >> n >> k;
  if (n < k)
    swap(n, k);
  n = n % k;
  if (n == 0)
    cout << k;
  else {
    cout << min(n, k - n);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define ll long long
#define _for(i, j, k) for (int i = j; i < k; i++)
#define endl '\n'
#define inf 1 << 29 - 1
const int mod = 1e9 + 7;
const int MAX = 1e6 + 5;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, k, t;
  cin >> n >> k;
  ll MIN = n % k;
  cout << min(MIN, k - MIN);
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  if (N > K) {
    N = N - (N - K) / K * K;
  }
  if (N == K) {
    cout << "0\n";
    return 0;
  }
  if (N > K)
    N -= K;
  if (K - N > N) {
    cout << N << "\n";
  } else
    cout << K - N << "\n";
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, K;
  cin >> N >> K;
  if (N > K) {
    N = N - (N - K) / K * K;
  }
  if (N == K) {
    cout << "0\n";
    return 0;
  }
  if (N > K)
    N -= K;
  if (K - N > N) {
    cout << N << "\n";
  } else
    cout << K - N << "\n";
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define ll long long
#define ld long double
#define precision(x, d) cout << fixed << setprecision(d) << x
#define fill(a, b) memset((a), (b), sizeof((a)))
#define FAST                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));
ll power(ll x, ll y, ll m) {
  ll r = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      r = r * x % m;
    x = x * x % m;
  }
  return r;
}
const ll mod = 1e9 + 7;
const ld pi = 3.14159265358979;

int run_test() {
  ll n, k;
  cin >> n >> k;
  while (abs(n - k) < n) {
    n = abs(n - k);
  }
  cout << n;
  return 0;
}

int main() {
  FAST;
  ll t;
  t = 1;

  while (t--) {
    run_test();
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define ll long long
#define ld long double
#define precision(x, d) cout << fixed << setprecision(d) << x
#define fill(a, b) memset((a), (b), sizeof((a)))
#define FAST                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));
ll power(ll x, ll y, ll m) {
  ll r = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      r = r * x % m;
    x = x * x % m;
  }
  return r;
}
const ll mod = 1e9 + 7;
const ld pi = 3.14159265358979;

int run_test() {
  ll n, k;
  cin >> n >> k;
  ll t = n / k;
  n -= k * t;
  if (abs(n - k) < n) {
    n = abs(n - k);
  }
  cout << n;
  return 0;
}

int main() {
  FAST;
  ll t;
  t = 1;

  while (t--) {
    run_test();
  }
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <type_traits>
#include <vector>

typedef long long ll;

using namespace std;

// 素因数分解
void factoring(ll n, map<ll, ll> &mp) {
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      mp[i]++;
      n /= i;
    }
  }
  if (n != 1)
    mp[n]++;
}

// 絶対値
template <class T, class U = std::make_unsigned_t<T>> U SafeAbs(T x) {
  return x < 0 ? -static_cast<uintmax_t>(x) : x;
}

int main() {

  ll n, k;
  cin >> n >> k;

  ll min = n;

  while (true) {
    if (SafeAbs(min - k) < min) {
      min = SafeAbs(min - k);

    } else {
      break;
    }
  }

  cout << min << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <type_traits>
#include <vector>

typedef long long ll;

using namespace std;

// 素因数分解
void factoring(ll n, map<ll, ll> &mp) {
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      mp[i]++;
      n /= i;
    }
  }
  if (n != 1)
    mp[n]++;
}

// 絶対値
template <class T, class U = std::make_unsigned_t<T>> U SafeAbs(T x) {
  return x < 0 ? -static_cast<uintmax_t>(x) : x;
}

int main() {

  ll n, k;
  cin >> n >> k;

  ll min = n;

  if (n % k == 0) {
    min = 0;
  } else {
    min = n % k;
    while (true) {
      if (SafeAbs(k - min) < min) {
        min = SafeAbs(min - k);
      } else {
        break;
      }
    }
  }

  cout << min << endl;

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
using namespace std;

int main() {
  unsigned long long int n, k, n1;
  cin >> n >> k;
  n1 = n;
  while (n >= 0) {
    n = n - k;
  }
  n = llabs(n);
  if (n > n1) {
    cout << n1;
  } else
    cout << n;

  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;

int main() {
  unsigned long long int n, k, n1;
  cin >> n >> k;
  if (n > k)
    n = n % k;
  cout << min(n, k - n);

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long N, K, cnt = 0;
  cin >> N >> K;
  while (N - K > 0) {
    N -= K;
  }
  if (abs(N - K) < N) {
    N = abs(N - K);
  }
  cout << N;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long N, K, cnt = 0;
  cin >> N >> K;
  cnt = N % K;
  if (abs(cnt - K) < cnt) {
    N = abs(cnt - K);
  } else {
    N = cnt;
  }
  cout << N;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll x, y;
ll abs2(ll f) {
  if (f < 0)
    return -f;
  return f;
}
ll min2(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
int main() {
  cin >> x >> y;
  while (x > y) {
    x = x - y;
  }
  cout << min2(abs2(x - y), abs2(x));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll x, y;
ll abs2(ll f) {
  if (f < 0)
    return -f;
  return f;
}
ll min2(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}
int main() {
  cin >> x >> y;
  if (x > y) {
    x = x - (x / y) * y;
  }
  cout << min2(abs2(x - y), abs2(x));
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  a %= b;
  cout << min(a, abs(a - b));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long a, b;
  cin >> a >> b;
  a %= b;
  cout << min(a, abs(a - b));
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int long long a;
  a = n % k;
  int long long b;
  b = k - a;
  if (a <= b) {
    cout << a << endl;
  } else {
    cout << b << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int long long n, k;
  cin >> n >> k;
  int long long a;
  a = n % k;
  int long long b;
  b = k - a;
  if (a <= b) {
    cout << a << endl;
  } else {
    cout << b << endl;
  }
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int main() {
  int a, b, c, d = INT_MAX, e = 0;
  cin >> a >> b;
  cout << min(a % b, (a % b - b) * -1);
}

Fixed submission:
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int main() {
  long long int a, b, c, d = INT_MAX, e = 0;
  cin >> a >> b;
  cout << min(a % b, (a % b - b) * -1);
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int x, k;
  cin >> x >> k;
  if (x > k) {
    x = x % k;
  }
  if (x > k / 2) {
    x = k - x;
  }
  cout << x << endl;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  long x, k;
  cin >> x >> k;
  if (x > k) {
    x = x % k;
  }
  if (x > k / 2) {
    x = k - x;
  }
  cout << x << endl;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;

  if (N % K < (K + 1) / 2)
    cout << N % K << endl;
  else
    cout << K - N % K << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int long long N, K;
  cin >> N >> K;

  if (N % K < (K + 1) / 2)
    cout << N % K << endl;
  else
    cout << K - N % K << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

long long int monmo(long long int n, long long int k, int keta) {
  if (n == 0) {
    return 0;
  } else if (keta == -1) {
    return n;
  } else {
    long long int tmpk;
    tmpk = k;
    for (int i = 0; i < keta; i++) {
      tmpk *= 10;
    }
    while (n >= tmpk) {
      n -= tmpk;
    }
    return monmo(n, k, keta - 1);
  }
}

int main(void) {
  long long int n, k, a, b, na, ka;
  int cn = 0, ck = 0;
  cin >> n >> k;
  na = n;
  ka = k;
  while (na > 9) {
    na /= 10;
    cn++;
  }
  while (ka > 9) {
    ka /= 10;
    ck++;
  }
  a = monmo(n, k, cn - ck);
  b = a - k;
  b *= -1;
  if (a > b)
    cout << b;
  else
    cout << a;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

long long int monmo(long long int n, long long int k, int keta) {
  if (n == 0) {
    return 0;
  } else if (keta < 0) {
    return n;
  } else {
    long long int tmpk;
    tmpk = k;
    for (int i = 0; i < keta; i++) {
      tmpk *= 10;
    }
    while (n >= tmpk) {
      n -= tmpk;
    }
    return monmo(n, k, keta - 1);
  }
}

int main(void) {
  long long int n, k, a, b, na, ka;
  int cn = 0, ck = 0;
  cin >> n >> k;
  na = n;
  ka = k;
  while (na > 9) {
    na /= 10;
    cn++;
  }
  while (ka > 9) {
    ka /= 10;
    ck++;
  }
  a = monmo(n, k, cn - ck);
  b = a - k;
  b *= -1;
  if (a > b)
    cout << b;
  else
    cout << a;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long data = 0;
  long long ans;
  if (k == 1) {
    ans = 0;
  } else {
    if (n > k) {
      data = n % k;
      ans = k - data;
    } else if (n == k) {
      ans = 0;
    } else {
      if (n == 1) {
        ans = 0;
      } else {
        if (k % n == 0) {
          ans = n;
        } else {
          data = k % n;
          ans = n - data;
        }
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;

  long long data = n % k;
  long long ans = min(data, k - data);

  cout << ans << endl;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;

int main() {
  ll N, K;
  cin >> N >> K;

  ll A = abs(N - K);

  ll D = min(K, A);

  cout << N % D << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;

int main() {
  ll N, K;
  cin >> N >> K;

  cout << min(N % K, K - N % K) << endl;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  if (N < abs(N - K)) {
    cout << N;
  }
  if (N % K == 0) {
    cout << '0';
  }
  if (N % K == 1) {
    cout << '1';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t N, M;
  cin >> N >> M;
  long long n = N % M;
  long long ans;
  ans = min(n, M - n);
  cout << ans << endl;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  cout << min(N % K, K - (N % K)) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N, K;
  cin >> N >> K;
  cout << min(N % K, K - (N % K)) << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  long long n, k;
  cin >> n >> k;
  set<long long> st;
  long long n_min = n;
  while (st.count(n) == 0) {
    st.insert(n);
    n = abs(n - k);
    n_min = min(n, n_min);
  }
  cout << n_min << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  long long n, k;
  cin >> n >> k;
  n %= k;
  long long a = abs(n - k);
  long long ans = min(a, n);
  cout << ans << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long min = n;
  if (n % k == 0) {
    cout << 0;
    return 0;
  }
  while (1) {
    n = abs(n - k);
    if (n < min && n != 0) {
      min = n;
    } else {
      cout << min;
      return 0;
    }
  }
  if (n != 0) {
    cout << min;
  } else {
    cout << 0;
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long min = n;
  if (n > k) {
    n = n % k;
  }
  if (n % k == 0) {
    cout << 0;
    return 0;
  }
  while (1) {
    n = abs(n - k);
    if (n < min && n != 0) {
      min = n;
    } else {
      cout << min;
      return 0;
    }
  }
  if (n != 0) {
    cout << min;
  } else {
    cout << 0;
  }
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  cout << min(n % k, k - (n % k)) << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;
  cout << min(n % k, k - (n % k)) << endl;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, K, a;
  cin >> N >> K;
  a = min(N % K, K - (N % K));
  cout << a << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long N, K, a;
  cin >> N >> K;
  a = min(N % K, K - (N % K));
  cout << a << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define ll long long int

int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, m;
  cin >> n >> m;
  while (true) {
    if (n < abs(n - m))
      break;
    n = abs(n - m);
  }

  cout << n << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define ll long long int

int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, m;
  cin >> n >> m;

  n %= m;
  n = (n < abs(n - m)) ? n : abs(n - m);

  cout << n << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  if (n < k) {
    cout << min(n, -(n - k)) << endl;
    return 0;
  }

  long long big = n, small = n;

  while (big % k != 0) {
    big++;
  }
  while (small % k != 0) {
    small--;
  }
  // cout << big << " & " << small << endl;
  cout << min(big % n, n % small) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long t = n % k;
  cout << min(t, k - t) << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;

#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define repd(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()

ll n, k, m, M = 1e9 + 7;

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

  cin >> n >> k;

  while (abs(n - k) < n)
    n = abs(n - k);

  cout << n << endl;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;

#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define repd(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()

ll n, k, m, M = 1e9 + 7;

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

  cin >> n >> k;

  if (n > k)
    n %= k;

  while (abs(k - n) < n)
    n = abs(k - n);

  cout << n << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define T long long

T Abs(T n) { return n > 0 ? n : -n; }

int main() {
  T n, k;
  std::cin >> n >> k;
  while (1) {
    if (Abs(n - k) >= n) {
      std::cout << n << std::endl;
      return 0;
    } else if (n % k == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
    n = Abs(n - k);
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define T long long

T Abs(T n) { return n > 0 ? n : -n; }

int main() {
  T n, k;
  std::cin >> n >> k;
  if (n > k) {
    n = n % k;
  }
  while (1) {
    if (Abs(n - k) >= n) {
      std::cout << n << std::endl;
      return 0;
    } else if (n % k == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
    n = Abs(n - k);
  }
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;

  while (1) {
    if (n <= 1000 * k)
      break;
    n -= 1000 * k;
  }

  long long int x = n;
  while (1) {
    if (abs(x - k) < x) {
      x = abs(x - k);
      // cout << x << endl;
    } else
      break;
  }
  cout << x << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;

  long long int p, q;
  p = n % k;
  q = k - n % k;
  if (p < q)
    cout << p << endl;
  else
    cout << q << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

long long oper(long long n, long long k) { return abs(n - k); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long n, k;
  cin >> n >> k;
  if (k == 1)
    cout << 0 << "\n";
  else {
    while (n >= 0) {
      n = oper(n, k);
      if (oper(n, k) > n) {
        cout << n << "\n";
        break;
      }
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long long oper(long long n, long long k) { return abs(n - k); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  long long n, k;
  cin >> n >> k;
  if (k == 1)
    cout << 0 << "\n";
  else {
    cout << min(n % k, k - (n % k)) << "\n";
  }

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
typedef long long int ll;
#define fr(x, y) for (int x = 0; x < y; x++)
#include <algorithm>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  n = n % k;
  cout << min(k - n, n);
}


Fixed submission:
#include <cmath>
#include <iostream>
typedef long long int ll;
#define fr(x, y) for (int x = 0; x < y; x++)
#include <algorithm>
#include <vector>

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  n = n % k;
  cout << min(k - n, n);
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, k;

void solve() { cout << min(n % k, abs(n % k - k)) << endl; }

int main() {
  cin >> n >> k;
  solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int64_t n, k;

void solve() { cout << min(n % k, abs(n % k - k)) << endl; }

int main() {
  cin >> n >> k;
  solve();
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using p = pair<int, int>;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;
  while (n > 0) {
    n -= k;
  }
  if (n == 0) {
    cout << 0 << endl;
  } else if (n < 0) {
    cout << min(abs(n), abs(n + k)) << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using p = pair<int, int>;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;

  cout << min(n % k, k - (n % k)) << endl;

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
  } else {
    for (int i = 0;; i++) {
      int64_t a = max(N, K), b = min(N, K);
      if (N <= a - N) {
        cout << N << endl;
        break;
      } else {
        N = a - b;
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
  } else {
    int64_t a = N % K, b = K - a;
    int64_t c = min(a, b);
    cout << c << endl;
  }
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> pi;
typedef pair<int, int> vi;

#define F first
#define S second
#define PB push_back
#define MK make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)

int main() {
  ll N, K;
  cin >> N >> K;
  pi visited = {N};
  ll result = N;
  if (N % K == 0) {
    cout << 0 << "\n";
    exit(0);
  }
  while (N > 0) {
    N = abs(N - K);
    if (result < N) {
      N = 0;
    } else {
      result = N;
    }
  }
  cout << result << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> pi;
typedef pair<int, int> vi;

#define F first
#define S second
#define PB push_back
#define MK make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)

int main() {
  ll N, K;
  cin >> N >> K;
  pi visited = {N};
  ll result = N;
  N = N % K;
  if (N % K == 0) {
    cout << 0 << "\n";
    exit(0);
  }
  while (N > 0) {
    N = abs(N - K);
    if (result < N) {
      N = 0;
    } else {
      result = N;
    }
  }
  cout << result << "\n";
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k, x;
  cin >> n >> k;

  if (n < k)
    x = k % n;
  else
    x = n % k;
  if (n != k)
    cout << min(x, k - x) << endl;
  else
    cout << 0 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k, x;
  cin >> n >> k;

  if (n < k)
    x = n;
  else
    x = n % k;
  if (n != k)
    cout << min(x, k - x) << endl;
  else
    cout << 0 << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  long long N, K;
  cin >> N >> K;
  while (N > K) {
    N -= K;
  }
  cout << min(N, K - N);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  long long N, K;
  cin >> N >> K;
  if (N > K) {
    N = N - (N / K) * K;
  }
  cout << min(N, K - N);
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 1; i <= (n); i++)
int main() {
  long long int n, k;
  cin >> n >> k;
  while (n > abs(n - k)) {
    if (n % k == 0)
      n = 0;
    n = abs(n - k);
  }
  cout << n;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 1; i <= (n); i++)
int main() {
  long long int n, k;
  cin >> n >> k;
  n = min(n % k, abs(n % k - k));
  cout << n;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int k;
  long long c[13][10] = {};
  cin >> k;
  rep(i, 10) c[0][i] = 1;
  for (int i = 0;; i++) {
    for (int j = 1; j < 10; j++) {
      if (k > c[i][j]) {
        k -= c[i][j];
      } else {
        long long r = j;
        i--;
        for (; i >= 0; i--) {
          if (j > 0 && c[i][j - 1] >= k) {
            j = j - 1;
            r = r * 10 + j;
            continue;
          }
          if (j > 0)
            k -= c[i][j - 1];
          if (c[i][j] >= k) {
            r = r * 10 + j;
            continue;
          }
          k -= c[i][j];
          j = j + 1;
          r = r * 10 + j;
        }
        cout << r << endl;
        return r;
      }
    }
    for (int j = 0; j < 10; j++) {
      if (j > 0) {
        c[i + 1][j] += c[i][j - 1];
      }
      if (j < 9) {
        c[i + 1][j] += c[i][j + 1];
      }
      c[i + 1][j] += c[i][j];
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int k;
  long long c[13][10] = {};
  cin >> k;
  rep(i, 10) c[0][i] = 1;
  for (int i = 0;; i++) {
    for (int j = 1; j < 10; j++) {
      if (k > c[i][j]) {
        k -= c[i][j];
      } else {
        long long r = j;
        i--;
        for (; i >= 0; i--) {
          if (j > 0 && c[i][j - 1] >= k) {
            j = j - 1;
            r = r * 10 + j;
            continue;
          }
          if (j > 0)
            k -= c[i][j - 1];
          if (c[i][j] >= k) {
            r = r * 10 + j;
            continue;
          }
          k -= c[i][j];
          j = j + 1;
          r = r * 10 + j;
        }
        cout << r << endl;
        return 0;
      }
    }
    for (int j = 0; j < 10; j++) {
      if (j > 0) {
        c[i + 1][j] += c[i][j - 1];
      }
      if (j < 9) {
        c[i + 1][j] += c[i][j + 1];
      }
      c[i + 1][j] += c[i][j];
    }
  }
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>

using namespace std;

long long lunlun_number(int k) {
  int count = 0;
  queue<long long> que;

  for (int i = 1; i <= 9; i++) {
    que.push(i);
  }

  long long ans;

  while (count < k) {
    ans = que.front();
    que.pop();
    count++;
    // minus;
    if (ans % 10 != 0) {
      que.push(10 * ans + (ans % 10) - 1);
    }
    que.push(10 * ans + (ans % 10));

    if (ans % 10 != 9) {
      que.push(10 * ans + (ans % 10) + 1);
    }
  }
  return ans;
}

int main() {
  int k;
  cin >> k;

  return lunlun_number(k);
}

Fixed submission:
#include <iostream>
#include <queue>

using namespace std;

long long lunlun_number(int k) {
  int count = 0;
  queue<long long> que;

  for (int i = 1; i <= 9; i++) {
    que.push(i);
  }

  long long ans;

  while (count < k) {
    ans = que.front();
    que.pop();
    count++;
    // minus;
    if (ans % 10 != 0) {
      que.push(10 * ans + (ans % 10) - 1);
    }
    que.push(10 * ans + (ans % 10));

    if (ans % 10 != 9) {
      que.push(10 * ans + (ans % 10) + 1);
    }
  }
  return ans;
}

int main() {
  int k;
  cin >> k;

  cout << lunlun_number(k);
}
Problem p02720 (C++)

Buggy submission (status: Time Limit Exceeded):
/*                    */
#include <bits/stdc++.h>
using namespace std;
using lint = int64_t;
char to_char(int n) { return n + '0'; };
int to_int(char c) { return c - '0'; };
/*                    */

void solve(string s, int ord, int k);
int cnt = 0;

int main() {
  int k;
  cin >> k;
  int ord = 1;
  cnt = 9;
  ord = 2;
  while (true) {
    for (int i = 1; i <= 9; i++) {
      solve(to_string(i), ord, k);
    }
    ord++;
  }
}

void solve(string s, int ord, int k) {
  if (s.size() == ord) {
    cnt++;
    if (cnt == k) {
      cout << s << endl;
      exit(0);
    }
    return;
  }
  for (int i = -1; i <= 1; i++) {
    int tmp = to_int(s.back()) + i;
    if (tmp < 0 || tmp > 9)
      continue;
    s.push_back(to_char(tmp));
    solve(s, ord, k);
    s.pop_back();
  }
}

Fixed submission:
/*                    */
#include <bits/stdc++.h>
using namespace std;
using lint = int64_t;
char to_char(int n) { return n + '0'; };
int to_int(char c) { return c - '0'; };
/*                    */

void solve(string s, int ord, int k);
int cnt = 0;

int main() {
  int k;
  cin >> k;
  int ord = 1;
  if (k < 10) {
    cout << k << endl;
    return 0;
  }
  cnt = 9;
  ord = 2;
  while (true) {
    for (int i = 1; i <= 9; i++) {
      solve(to_string(i), ord, k);
    }
    ord++;
  }
}

void solve(string s, int ord, int k) {
  if (s.size() == ord) {
    cnt++;
    if (cnt == k) {
      cout << s << endl;
      exit(0);
    }
    return;
  }
  for (int i = -1; i <= 1; i++) {
    int tmp = to_int(s.back()) + i;
    if (tmp < 0 || tmp > 9)
      continue;
    s.push_back(to_char(tmp));
    solve(s, ord, k);
    s.pop_back();
  }
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T, typename cmp=less<T>> using oset =tree<T, null_type,
// cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define inf 100000000000000000
#define ll long long
#define pb push_back
#define f first
#define s second
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
void mahakal() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}
int main() {
  mahakal(), fast;

  ll n;
  cin >> n;
  queue<ll> q;
  for (ll i = 1; i <= 9; i++)
    q.push(i);
  while (n > 1) {
    ll ps = q.front();
    q.pop();
    // cerr<<ps<<" ";
    if (ps % 10 != 0)
      q.push(10 * ps + ps % 10 - 1);
    q.push(10 * ps + ps % 10);
    if (ps % 10 != 9)
      q.push(10 * ps + ps % 10 + 1);
    n--;
  }
  cout << q.front() << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <typename T, typename cmp=less<T>> using oset =tree<T, null_type,
// cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define inf 100000000000000000
#define ll long long
#define pb push_back
#define f first
#define s second
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
void mahakal() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
}
int main() {
  // mahakal(),fast;

  ll n;
  cin >> n;
  queue<ll> q;
  for (ll i = 1; i <= 9; i++)
    q.push(i);
  while (n > 1) {
    ll ps = q.front();
    q.pop();
    // cerr<<ps<<" ";
    if (ps % 10 != 0)
      q.push(10 * ps + ps % 10 - 1);
    q.push(10 * ps + ps % 10);
    if (ps % 10 != 9)
      q.push(10 * ps + ps % 10 + 1);
    n--;
  }
  cout << q.front() << endl;
  return 0;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
//
//  lunlun.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>

int main(int argc, const char *argv[]) {
  int k;
  std::cin >> k;
  long long a[k];
  int i = 0;
  for (i = 0; i < 9; i++) {
    a[i] = i + 1;
  }
  int index = 0;
  while (i < k) {
    // if (a[index]%10 == 9) {
    //   a[i] = a[index]*10 + (a[index]%10 - 1);
    //   a[i+1] = a[index]*10 + (a[index]%10);
    //   i=i+2;
    // }
    // else if (a[index]%10 == 0) {
    //   a[i] = a[index]*10 + (a[index]%10);
    //   a[i+1] = a[index]*10 + (a[index]%10 + 1);
    //   i=i+2;
    // }
    // else {
    //   a[i] = a[index]*10 + (a[index]%10 - 1);
    //   a[i+1] = a[index]*10 + (a[index]%10);
    //   a[i+2] = a[index]*10 + (a[index]%10 + 1);
    //   i=i+3;
    // }
    if (a[index] % 10 != 0) {
      a[i] = a[index] * 10 + (a[index] % 10 - 1);
      i++;
    }
    a[i] = a[index] * 10 + (a[index] % 10);
    i++;
    if (a[index] % 10 != 9) {
      a[i] = a[index] * 10 + (a[index] % 10 + 1);
      i++;
    }
    index++;
  }

  // for (int i=0; i<k; i++) {
  //   std::cout << a[i] << " ";
  // }
  // std::cout << std::endl;
  // std::cout << "index " << index << std::endl;
  std::cout << a[k - 1] << std::endl;
}

Fixed submission:
//
//  lunlun.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>

int main(int argc, const char *argv[]) {
  int k;
  std::cin >> k;
  long long a[100000];
  int i = 0;
  for (i = 0; i < 9; i++) {
    a[i] = i + 1;
  }
  int index = 0;
  while (i < k) {
    // if (a[index]%10 == 9) {
    //   a[i] = a[index]*10 + (a[index]%10 - 1);
    //   a[i+1] = a[index]*10 + (a[index]%10);
    //   i=i+2;
    // }
    // else if (a[index]%10 == 0) {
    //   a[i] = a[index]*10 + (a[index]%10);
    //   a[i+1] = a[index]*10 + (a[index]%10 + 1);
    //   i=i+2;
    // }
    // else {
    //   a[i] = a[index]*10 + (a[index]%10 - 1);
    //   a[i+1] = a[index]*10 + (a[index]%10);
    //   a[i+2] = a[index]*10 + (a[index]%10 + 1);
    //   i=i+3;
    // }
    if (a[index] % 10 != 0) {
      a[i] = a[index] * 10 + (a[index] % 10 - 1);
      i++;
    }
    a[i] = a[index] * 10 + (a[index] % 10);
    i++;
    if (a[index] % 10 != 9) {
      a[i] = a[index] * 10 + (a[index] % 10 + 1);
      i++;
    }
    index++;
  }

  // for (int i=0; i<k; i++) {
  //   std::cout << a[i] << " ";
  // }
  // std::cout << std::endl;
  // std::cout << "index " << index << std::endl;
  std::cout << a[k - 1] << std::endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int k;
  long ans;
  // scanf("%d", &x);
  cin >> k;
  if (k <= 10) {
    cout << k << endl;
    return 1;
  }
  queue<long> que;
  long i = 0;
  rep(j, 9) {
    que.push(i + 1);
    i++;
  }
  long next;
  long prev;
  while (i < k) {
    prev = que.front();
    que.pop();
    long d = prev % 10;
    if (d != 0) {
      next = prev * 10 + d - 1;
      que.push(next);
      i++;
    }
    if (i == k) {
      break;
    }
    next = prev * 10 + d;
    que.push(next);
    i++;
    if (i == k) {
      break;
    }
    if (d != 9) {
      next = prev * 10 + d + 1;
      que.push(next);
      i++;
    }
  }

  // printf("%d\n", x * x * x);
  cout << next << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int k;
  long ans;
  // scanf("%d", &x);
  cin >> k;
  if (k <= 10) {
    cout << k << endl;
    return 0;
  }
  queue<long> que;
  long i = 0;
  rep(j, 9) {
    que.push(i + 1);
    i++;
  }
  long next;
  long prev;
  while (i < k) {
    prev = que.front();
    que.pop();
    long d = prev % 10;
    if (d != 0) {
      next = prev * 10 + d - 1;
      que.push(next);
      i++;
    }
    if (i == k) {
      break;
    }
    next = prev * 10 + d;
    que.push(next);
    i++;
    if (i == k) {
      break;
    }
    if (d != 9) {
      next = prev * 10 + d + 1;
      que.push(next);
      i++;
    }
  }

  // printf("%d\n", x * x * x);
  cout << next << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

int main() {
  fast_io;

  queue<long long> q;

  long long k;
  cin >> k;

  for (int i = 1; i <= 9; i++) {
    q.push(i);
  }

  long long x;
  long long a;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    k--;

    if (k == 0) {
      break;
    }

    a = x % 10;

    for (int i = -1; i < 2; i++) {
      if (a + i >= 0 && a + i < 10) {
        // cout<<(x*10)+a+i<<endl;
        q.push((x * 10) + a + i);
      }
    }
  }
  cout << x << endl;

  return x;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

int main() {
  fast_io;

  queue<long long> q;

  long long k;
  cin >> k;

  for (int i = 1; i <= 9; i++) {
    q.push(i);
  }

  long long x;
  long long a;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    k--;

    if (k == 0) {
      break;
    }

    a = x % 10;

    for (int i = -1; i < 2; i++) {
      if (a + i >= 0 && a + i < 10) {
        // cout<<(x*10)+a+i<<endl;
        q.push((x * 10) + a + i);
      }
    }
  }
  cout << x << endl;
}
Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long LL;

int main() {
  int K;
  cin >> K;

  vector<vector<int>> count(10, vector<int>());
  REP(i, 10) { count[i].push_back(i % 3 == 0 ? 1 : 0); }
  for (int i = 0; i <= 18; ++i) {
    count[0].push_back(count[0][i] + count[1][i]);
    count[9].push_back(count[8][i] + count[9][i]);
    for (int j = 1; j <= 8; ++j) {
      count[j].push_back(count[j - 1][i] + count[j][i] + count[j + 1][i]);
    }
  }
  LL ret = 0;
  for (int keta = 1; keta <= 18; ++keta) {
    for (int n = 1; n <= 9; ++n) {
      if (count[n][keta] < K) {
        K -= count[n][keta];
      } else {
        ret = n;
        while (--keta > 0) {
          if (n >= 1 && K <= count[n - 1][keta]) {
            --n;
          } else {
            K -= count[n - 1][keta];
            if (K > count[n][keta]) {
              K -= count[n][keta];
              ++n;
            }
          }
          ret = ret * 10 + n;
        }
        cout << ret << endl;
        return 0;
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long LL;

int main() {
  int K;
  cin >> K;

  vector<vector<int>> count(10, vector<int>());
  REP(i, 10) { count[i].push_back(i % 3 == 0 ? 1 : 0); }
  for (int i = 0; i <= 18; ++i) {
    count[0].push_back(count[0][i] + count[1][i]);
    count[9].push_back(count[8][i] + count[9][i]);
    for (int j = 1; j <= 8; ++j) {
      count[j].push_back(count[j - 1][i] + count[j][i] + count[j + 1][i]);
    }
  }
  LL ret = 0;
  for (int keta = 1; keta <= 18; ++keta) {
    for (int n = 1; n <= 9; ++n) {
      if (count[n][keta] < K) {
        K -= count[n][keta];
      } else {
        ret = n;
        while (--keta > 0) {
          if (n >= 1 && K <= count[n - 1][keta]) {
            --n;
          } else {
            if (n >= 1) {
              K -= count[n - 1][keta];
            }
            if (K > count[n][keta]) {
              K -= count[n][keta];
              ++n;
            }
          }
          ret = ret * 10 + n;
        }
        cout << ret << endl;
        return 0;
      }
    }
  }
}
Problem p02720 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;

using ll = long long;
using LL = long long;

class Edge {
public:
  int from, to;
  ll value, konzatu;
  Edge(int a, int b, ll c, ll d) {
    from = a;
    to = b;
    value = c;
    konzatu = d;
  }
  Edge(LL a, LL b, LL c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(LL a, LL b) {
    from = a;
    to = b;
  }
};

ll cnt = 0;
int K;
void lunlun(int n, string s) {
  if (n == s.size()) {
    cnt++;
    if (cnt == K) {
      cout << s << endl;
    }
    return;
  }
  if (s.empty()) {
    for (int i = 1; i <= 9; i++) {
      s = to_string(i);
      lunlun(n, s);
    }
  } else {
    char c = s.back();
    for (char i = '0'; i <= '9'; i++) {
      if (abs(c - i) <= 1) {
        s.push_back(i);
        lunlun(n, s);
        s.pop_back();
      }
    }
  }
}

int main() {
  cin >> K;

  for (int i = 1; i <= 15; i++) {
    lunlun(i, string());
  }
}

Fixed submission:
#include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;

using ll = long long;
using LL = long long;

class Edge {
public:
  int from, to;
  ll value, konzatu;
  Edge(int a, int b, ll c, ll d) {
    from = a;
    to = b;
    value = c;
    konzatu = d;
  }
  Edge(LL a, LL b, LL c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(LL a, LL b) {
    from = a;
    to = b;
  }
};

ll cnt = 0;
int K;
void lunlun(int n, string s) {
  if (n == s.size()) {
    cnt++;
    if (cnt == K) {
      cout << s << endl;
    }
    return;
  }
  if (s.empty()) {
    for (int i = 1; i <= 9; i++) {
      s = to_string(i);
      lunlun(n, s);
    }
  } else {
    char c = s.back();
    for (char i = '0'; i <= '9'; i++) {
      if (abs(c - i) <= 1) {
        s.push_back(i);
        lunlun(n, s);
        s.pop_back();
      }
    }
  }
}

int main() {
  cin >> K;

  for (int i = 1; i <= 12; i++) {
    lunlun(i, string());
  }
}