alexjercan/bugnet huggingface dataset:
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define PI 3.14159265358979
typedef long long int ll;
const int inf = 1001001001;
ll absc(ll a, ll b) { return (a - b > 0 ? a - b : b - a); }
ll mini(ll a, ll b) { return (a > b ? b : a); }
int main() {
  ll m, b, c, n, sum = 0, ans;
  string s, t;
  cin >> n >> m;
  ans = n;
  ans = mini(n % m, ans);
  ans = mini(ans, absc(m, n % m));
  ans = mini(absc(m, absc((n % m + n), m)), ans);
  cout << ans;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define PI 3.14159265358979
typedef long long int ll;
const int inf = 1001001001;
ll absc(ll a, ll b) { return (a - b > 0 ? a - b : b - a); }
ll mini(ll a, ll b) { return (a > b ? b : a); }
int main() {
  ll m, b, c, n, sum = 0, ans;
  string s, t;
  cin >> n >> m;
  ans = n;
  ans = mini(ans % n, ans);
  ans = mini(ans, absc(m, n % m));
  ans = mini(absc(m, absc((n % m + n), m)), ans);
  cout << ans;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t n, k;
  cin >> n >> k;
  int64_t t = n % k;
  int64_t ans;
  ans = min(t, abs(k - t));
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t n, k;
  cin >> n >> k;
  int64_t ans = abs(n - k);
  while (ans > abs(ans - k)) {

    ans = abs(ans - k);
  }
  cout << ans << endl;

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;
int MOD = 1000000007;

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

  if (K == 1) {
    cout << 0 << endl;
    return 0;
  } else if (N < K) {
    if (N <= abs(N - K))
      cout << N << endl;
    else
      cout << abs(N - K) << endl;
    return 0;
  }

  int tmp = 0, MIN = 9223372036854775807;

  tmp = (N + K) / K;

  cout << min(abs((tmp * K) - N), abs((tmp - 1) * K - N)) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int MOD = 1000000007;

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

  if (K == 1) {
    cout << 0 << endl;
    return 0;
  } else if (N < K) {
    if (N <= abs(N - K))
      cout << N << endl;
    else
      cout << abs(N - K) << endl;
    return 0;
  }

  int tmp = 0, MIN = 9223372036854775807;
  while (N) {
    tmp += K;

    MIN = min(MIN, abs(N - tmp));

    if (MIN < abs(N - tmp)) {
      cout << MIN << endl;
      return 0;
    }
  }

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

// x,yを常にx>yにするfunction
/*
void func(int x, int y){
  int z;
  if(x<y){
    z = x;
    x = y;
    y = z;
  }
}
*/

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

  // func(N,K);
  int64_t z;

  bool q = true;
  while (q) {

    N = N % K; // N<K
    if (K - N > N) {
      break;
    }

    z = N;
    N = K;
    K = z;

    // N = N % K;

    // func(N,K);
    /*
    if(N<K){
      z = N;
      N = K;
      K = z;
    }
    */
  }
  cout << N << endl;
}


Error: 0

Fixed submission:

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

// x,yを常にx>yにするfunction
/*
void func(int x, int y){
  int z;
  if(x<y){
    z = x;
    x = y;
    y = z;
  }
}
*/

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

  // func(N,K);
  int64_t z;
  if (N < K) {
    z = N;
    N = K;
    K = z;
  }

  bool q = true;
  while (q) {
    N = N % K;

    // func(N,K);
    if (N < K) {
      z = N;
      N = K;
      K = z;
    }

    if (K == 0) {
      break;
    }

    if (N - K >= K) {
      q = false;
    }
  }
  cout << K << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define PI 3.14159265358979323846264338327950L

int res;

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

  long long int N, K;
  cin >> N >> K;

  if (N % K < K - N % K) {
    cout << N % K << endl;
  } else {
    cout << K - N % K << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define PI 3.14159265358979323846264338327950L

int res;

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

  long long int N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
    return 0;
  }
  while (abs(N - K) <= N) {
    N = abs(N - K);
  }
  cout << N << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define endl "\\n"
#define mod 1000000007
#define mem(a, val) memset(a, val, sizeof(a))
#define ps(x, noOfDecimal) fixed << setprecision(noOfDecimal) << x

using namespace std;
int main() {

  std::ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  if (n == 0 || k == 1 || n % k == 0) {
    cout << 0 << endl;
  } else {
    if (n < k) {
      ll ans = min(n, abs(n - k));
      cout << ans << endl;
    } else {
      ll rem = n % k;
      rem = min(abs(rem - k), rem);
      cout << rem << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define endl "\\n"
#define mod 1000000007
#define mem(a, val) memset(a, val, sizeof(a))
#define ps(x, noOfDecimal) fixed << setprecision(noOfDecimal) << x

using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  if (n == 0 || k == 1 || n == k) {
    cout << 0 << endl;
  } else {

    ll rem = n % k;
    if (n & 1) {

      rem = min(abs(rem - k), rem);
    }

    cout << rem << endl;
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = int64_t;
using Float = double;

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

  auto X = N;
  if (X == K) {
    cout << 0 << endl;
  } else if (X > K) {
    X = X - (Int((Float)(X - K) / K) + 10) * K;
    while (X < K) {
      X += K;
    }
    cout << min({X, X - K, K - (X - K)}) << endl;
  } else {
    cout << min(X, K - X) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = int64_t;

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

  auto X = N;
  while (abs(X - K) < X) {
    X = abs(X - K);
  }
  cout << X << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
#define sz(x) int(x.size())
using ll = long long;
const ll LLINF = numeric_limits<long long>::max();
const int mod = 1000000007;
const int INF = 1001001001;

bool has_key_using_find(map<ll, ll> &m, ll n) {
  if (m.find(n) == m.end()) {

    return false;
  } else {

    return true;
  }
}

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

  ll mi = n;
  ll b, c;
  if (n > k) {
    b = n;
    c = k;
  } else {
    b = k;
    c = n;
  }
  ll d = b - c;

  if (n < k) {
    cout << (n < d ? n : d);
    return 0;
  } else if (n == k) {
    cout << 0;
    return 0;
  }

  ll a = d % k;

  cout << (a < abs(k - a) ? a : abs(k - a));

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
#define sz(x) int(x.size())
using ll = long long;
const ll LLINF = numeric_limits<long long>::max();
const int mod = 1000000007;
const int INF = 1001001001;

bool has_key_using_find(map<ll, ll> &m, ll n) {
  if (m.find(n) == m.end()) {

    return false;
  } else {

    return true;
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  map<ll, ll> a;
  ll mi = n;
  while (true) {
    ll b, c;
    if (n > k) {
      b = n;
      c = k;
    } else {
      b = k;
      c = n;
    }
    ll d = b - c;

    if (d < mi) {
      mi = d;
    }

    if (has_key_using_find(a, d)) {
      break;
    } else {
      a[d] = 1;
    }
    n = d;
  }

  cout << mi;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  if (n >= k)
    n = n % k;
  cout << min(n, k - n);

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  if (n >= k)
    n = n % k;
  cout << min(n, k - n);

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll N, K, M;
  cin >> N >> K;
  M = min(N % K, K - N % K);
  cout << M << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N, K, M;
  cin >> N >> K;
  M = min(N % K, K - N % K);
  cout << M << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  if (N > K) {
    N = N % K;
  }
  N = min(N, K - N);
  cout << N << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  if (N > K) {
    N = N % K;
  }
  N = min(N, K - N);
  cout << N << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long a, b;
  cin >> a >> b;
  if (a % b == 0)
    cout << 0;
  else if (a < b)
    cout << min(b - a, a);
  else
    cout << min(abs(b - (a % b)), a % b);

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int a, b;
  cin >> a >> b;
  if (a % b == 0)
    cout << 0;
  else if (a < b)
    cout << min(b - a, a);
  else
    cout << min(abs(b - (a % b)), a % b);

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  long long N, K;
  cin >> N >> K;
  long long x = N % K;
  if (x > (K / 2)) {
    x = K - x;
  }
  cout << x;
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  int x = N % K;
  if (x > (K / 2)) {
    x = K - x;
  }
  cout << x;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main(void) {
  long N, K;
  cin >> N >> K;
  long ans = min(N % K, abs(N % K - K));
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main(void) {
  int N, K;
  cin >> N >> K;
  int ans = min(N % K, abs(N % K - K));
  cout << ans << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

// -----------------------------------
// Author     : MatsuTaku
// Affiliation: Tokushima University
// Country    : Japan
// Date       : 04/04/2020
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

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

  return 0;
}


Error: 0

Fixed submission:

// -----------------------------------
// Author     : MatsuTaku
// Affiliation: Tokushima University
// Country    : Japan
// Date       : 04/04/2020
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

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

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

  long long result;
  if (n <= k) {
    result = min(n, k - n);
  } else {
    // result = n % min(k, n - k);
    result = n % min(k, n - k);
    if (result < k)
      result = min(result, k - result);
  }

  cout << result << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

  long long result;
  if (n < k) {
    result = min(n, k - n);
  } else {
    result = n % min(k, n - k);
  }

  cout << result << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  //    if(abs(N - K) >= N)
  //    {
  //        cout << N << endl;
  //        return 0;
  //    }
  //
  //    else
  //    {
  //        N = abs(N - K);
  //        cout <<  N << endl;
  //
  //        goto a;
  //    }
  if (N >= K)
    N = N % K;

  if (N > abs(N - K))
    cout << abs(N - K) << endl;
  else
    cout << N << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

a:
  if (abs(N - K) >= N) {
    cout << N << endl;
    return 0;
  }

  else {
    N = abs(N - K);
    goto a;
  }

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = (int)(a); (i) < (int)(b); i++)

typedef unsigned long long ull;
typedef long long ll;

const int INF = 1001001001;

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

  ull ans = 0;
  if (n % k == 0) {
    ans = 0;
    cout << ans << endl;
    return 0;
  }
  if (n <= abs(n - k)) {
    ans = n;
    cout << ans << endl;
    return 0;
  }

  n = (n % k > k / 2) ? k - (n % k) : n % k;
  cout << n << endl;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = (int)(a); (i) < (int)(b); i++)

typedef unsigned long long ull;
typedef long long ll;

const int INF = 1001001001;

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

  ull ans = 0;
  if (n % k == 0) {
    ans = 0;
    cout << ans << endl;
    return 0;
  }
  if (n <= abs(n - k)) {
    ans = n;
    cout << ans << endl;
    return 0;
  }

  ull bef_n = 1000000000000000001;
  while (n < bef_n) {
    bef_n = n;
    n = (ull)abs(n - k);
    // cout << n << " " << bef_n << endl;
  }
  cout << bef_n << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, min, max) for (int i = (min); i < (max); ++i)
#define FORE(i, min, max) for (int i = (min); i <= (max); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPV(vec, i) for (int i = 0; i < (int)(vec.size()); ++i)
#define FOR_EACH(vec, it)                                                      \\
  for (typeof((vec).begin()) it = (vec).begin(); it != (vec).end(); ++it)

using namespace std;
static const double EPS = 1e-12;
typedef long long ll;

int main(void) {
  ll N, K;
  cin >> N >> K;
  ll ret = N;
  set<ll> visited;
  while (visited.count(N) == 0) {
    visited.insert(N);
    if (N >= 2 * K) {
      ll a = N / K - 1;
      N = N - a * K;
      while (N >= K) {
        N -= K;
      }
      N += K;
    } else if (N >= K) {
      N -= K;
    } else {
      assert(N < K);
      N = (K - N);
    }
    ret = min(N, ret);
  }
  cout << ret << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, min, max) for (int i = (min); i < (max); ++i)
#define FORE(i, min, max) for (int i = (min); i <= (max); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPV(vec, i) for (int i = 0; i < (int)(vec.size()); ++i)
#define FOR_EACH(vec, it)                                                      \\
  for (typeof((vec).begin()) it = (vec).begin(); it != (vec).end(); ++it)

using namespace std;
static const double EPS = 1e-12;
typedef long long ll;

int main(void) {
  ll N, K;
  cin >> N >> K;
  ll ret = N;
  set<ll> visited;
  while (visited.count(N) == 0) {
    if (N > K) {
      ll a = N / K - 1;
      N = N - a * K;
      while (N - K >= K) {
        N -= K;
      }
      N += K;
    } else {
    }
    ret = min(N, ret);
  }
  cout << ret << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
int mx4[] = {0, 1, 0, -1};
int my4[] = {1, 0, -1, 0};
int mx2[] = {1, 0};
int my2[] = {0, 1};

class CReplacingInteger {
public:
  void solve(istream &in, ostream &out) {
    ll n, k;
    in >> n >> k;
    ll a = n % k;
    ll ans = min(a, abs(k - a));
    out << ans << endl;
  }
};

int main() {
  CReplacingInteger solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
int mx4[] = {0, 1, 0, -1};
int my4[] = {1, 0, -1, 0};
int mx2[] = {1, 0};
int my2[] = {0, 1};

class CReplacingInteger {
public:
  void solve(istream &in, ostream &out) {
    ll n, k;
    in >> n >> k;
    ll a = n % k;
    ll ans = min(a, k % a);
    out << ans << endl;
  }
};

int main() {
  CReplacingInteger solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;

#define mem(dp, a) memset(dp, a, sizeof dp)
#define ninja                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(c) c.begin(), c.end()
#define ff first
#define ss second
#define nl '\\n'
#define inf 1e9 + 7
int main() {
  ninja ll n, k;
  cin >> n >> k;

  ll ans = abs(n - k);
  ans = min(ans, abs(ans - k));
  if (n <= k) {
    cout << ans;
    return 0;
  } else {
    ll x = n / k;
    ans = n - x * k;
    ans = abs(ans - k);
    ans = min(ans, abs(ans - k));
    cout << ans;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;

#define mem(dp, a) memset(dp, a, sizeof dp)
#define ninja                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(c) c.begin(), c.end()
#define ff first
#define ss second
#define nl '\\n'
#define inf 1e9 + 7
ll mod(string num, ll a) {
  // Initialize result
  ll res = 0;

  // One by one process all digits of 'num'
  for (ll i = 0; i < num.length(); i++)
    res = (res * 10 + (ll)num[i] - '0') % a;

  return res;
}

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

  int ans = abs(n - k);
  ans = min(ans, abs(ans - k));
  if (n <= k) {
    cout << ans;
    return 0;
  } else {
    int x = n / k;
    ans = n - x * k;
    ans = abs(ans - k);
    ans = min(ans, abs(ans - k));
    cout << ans;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int MAX_N = 1000;

int main() {
  // スペース区切りの整数の入力
  long long N, K;
  cin >> N >> K;

  long long x, pre_x;
  x = N;
  /*
      if (K == 1 || N == 0)
      {
          cout << 0 << endl;
          return 0;
      }
  */
  /*
      while (x > 0)
      {
          x = x - K;
      }
      */

  x = N % K;
  pre_x = abs(x - K);
  /*
      pre_x = x + K;
      x = abs(x);
  */

  if (x >= pre_x) {
    cout << pre_x << endl;
  } else {
    cout << x << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int MAX_N = 1000;

int main() {
  // スペース区切りの整数の入力
  long long N, K;
  cin >> N >> K;

  long long x, pre_x;
  x = N;

  if (K == 1 || N == 0 || N % K == 0) {
    cout << 0 << endl;
    return 0;
  }

  while (x > 0) {
    pre_x = x;
    x = pre_x - K;
  }

  x = abs(x);

  if (x >= pre_x) {
    cout << pre_x << endl;
  } else {
    cout << x << endl;
  }

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define _GLIBCXX_DEBUG
using ll = long long;
using namespace std;

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;

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