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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  ll n, k;
  ll left, right;
  cin >> n >> k;

  left = n % k;
  right = left + k;

  cout << min(right - k, k - left) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  int n, k;
  int left, right;
  cin >> n >> k;

  left = n % k;
  right = left + k;

  cout << min(right - k, k - left) << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, k;
  cin >> n >> k;
  n = n % k;
  if (2 * n > k)
    cout << abs(n - k);
  else
    cout << n;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, k;
  cin >> n >> k;
  if (k == 1)
    n = 0;
  while (abs(n - k) < n)
    n = abs(n - k);
  cout << n;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  while (n > k) {
    n = n - k;
  }
  if (n <= k / 2) {
    cout << n;
  } else {
    n = k - n;
    cout << n << endl;
  }

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

#include <bits/stdc++.h>
// #include<math.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << endl;
  else
    cout << min(n % k, abs(n % k - k)) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <map>
using namespace std;
typedef long long ll;
int main() {
  ll n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << endl;
  else {
    ll mmin = n;
    map<ll, ll> mp;
    mp[n]++;
    while (mp[n] < 2) {
      n = abs(n - k);
      mmin = min(mmin, n);
      mp[n]++;
    }
    cout << mmin << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

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

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

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>

using namespace std;

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

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

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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <math.h>    // sqrt
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

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

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <math.h>    // sqrt
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;

int main() {
  long long N, K;
  cin >> N >> K;
  if (K == 1) {
    N = 0;
  }
  while (true) {
    if (2 * N < K) {
      break;
    }
    if (N < K) {
      N = K - N;
      continue;
    }
    N -= K;
  }
  cout << N << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

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

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

    if (N > K) {
      if ((N % K) > (K / 2)) {
        cout << abs((N % K) - K) << endl;
      } else {
        cout << N % K << endl;
      }
    } else {
      if (N <= (K / 2)) {
        cout << N << endl;
      } else {
        cout << abs(N - K) << endl;
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

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

  if (N % K == 0) {
    cout << 0 << endl;
  } else {
    while (1) {
      N = abs(N - K);
      if (N < (K / 2)) {
        break;
      }
    }
    cout << N << endl;
  }

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

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long n, k, ans;
  cin >> n >> k;
  ans = n % k;
  if (k == 1)
    ans = 0;
  else
    while (2 * ans > k) {
      ans = min(ans, abs(ans - k));
    }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long n, k, ans;
  cin >> n >> k;
  ans = abs(n - k);
  if (k == 1)
    ans = 0;
  else
    while (2 * ans > k) {
      ans = min(ans, abs(ans - k));
    }
  cout << ans << endl;
}
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) {
    if (n < abs(k - n))
      cout << n << endl;
    else
      cout << abs(k - n) << endl;
  } else if (n == k)
    cout << 0 << endl;
  else {
    long long rem = n % k;
    if (rem == 0)
      cout << 0 << endl;
    else
      cout << k - rem << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  if (n < k)
    cout << n << endl;
  else {
    // int mul = n / k;
    int rem = n % k;

    if (rem == 0)
      cout << 0 << endl;
    else {
      cout << k - rem << endl;
      /*mul++;
      while (mul > 0) {
          n -= k;
          mul--;
      }
      cout << abs(n) << endl;*/
    }
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>

using namespace std;
#define ll long long

long long N, K;
long long prev1, next1;
int debug = 0;

int main() {
  cin >> N >> K;
  long long i = 0;

  if (N > K) {
    long long tmp;
    tmp = N / K;
    N = N - tmp * K;
    if (debug) {
      cout << "new N=" << N << endl;
    }
  }

  prev1 = N;
  while (true) {
    next1 = abs(prev1 - K);

    if (debug) {
      cout << i << "..."
           << "prev,next=" << prev1 << "," << next1 << endl;
    }

    if (next1 > prev1)
      break;
    prev1 = next1;
    i++;
  }

  cout << prev1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>

using namespace std;
#define ll long long

long long N, K;
long long prev1, next1;
int debug = 0;

int main() {
  cin >> N >> K;
  prev1 = N;
  long long i = 0;
  while (true) {
    next1 = abs(prev1 - K);

    if (debug) {
      cout << i << "..."
           << "prev,next=" << prev1 << "," << next1 << endl;
    }

    if (next1 > prev1)
      break;
    prev1 = next1;
    i++;
  }

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

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

typedef long long ll;
const long long LINF = 1LL << 60;
const long long LMINF = 1LL << 63;
const int INF = 1 << 30;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << "\\n"

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;

  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  while (1) {
    if (n < k) {
      if (n < abs(n - k)) {
        cout << n << endl;
      } else {
        cout << abs(n - k) << endl;
      }
      return 0;
    }
    n = abs(n - (k * (n / k)));
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
const long long LINF = 1LL << 60;
const long long LMINF = 1LL << 63;
const int INF = 1 << 30;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << "\\n"

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;

  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  while (1) {
    if (n < k) {
      if (n < abs(n - k)) {
        cout << n << endl;
      } else {
        cout << abs(n - k) << endl;
      }
      return 0;
    }
    n = abs(n - k);
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
  ll n, k;
  cin >> n >> k;
  ll m;
  bool d = true;
  n = n % k;
  while (d == true) {
    ll n2 = n;
    n = max(n, k) - min(n, k);
    if (n2 <= n) {
      cout << n2 << endl;
      d = false;
    } else {
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std; // N初期値　Kそれから引く
int main() {
  int N, K;
  cin >> N >> K;

  int M = N % K; //+max

  if (K - M < M) {
    cout << K - M << endl;
  } else {
    cout << M << endl;
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: 0

Fixed submission:

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

int main() {
  int n, k, c;
  cin >> n >> k;
  if (n > k) {
    c = n % k;
    cout << min(c, k - c) << endl;
  } else {
    c = k % n;
    cout << min(c, k - c) << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 0;; i++) {
    if (N - K >= 0) {
      N = N - K;
    } else {
      N = K - N;
    }

    if (K <= 2 * N && 2 * N <= 3 * K) {
      break;
    }
  }
  if (N - K >= 0) {
    cout << N - K << endl;
  } else {
    cout << K - N << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

using namespace std;
typedef unsigned long long ull;
ull n, k;
int main() {
  scanf("%lld%lld", &n, &k);
  printf("%lld", (n % k) < (k - (n % k)) ? (n % k) : (k - (n % k)));
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(3, "Ofast", "inline")
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("-fgcse")
%:pragma GCC optimize("-fgcse-lm")
%:pragma GCC optimize("-fipa-sra")
%:pragma GCC optimize("-ftree-pre")
%:pragma GCC optimize("-ftree-vrp")
%:pragma GCC optimize("-fpeephole2")
%:pragma GCC optimize("-ffast-math")
%:pragma GCC optimize("-fsched-spec")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("-falign-jumps")
%:pragma GCC optimize("-falign-loops")
%:pragma GCC optimize("-falign-labels")
%:pragma GCC optimize("-fdevirtualize")
%:pragma GCC optimize("-fcaller-saves")
%:pragma GCC optimize("-fcrossjumping")
%:pragma GCC optimize("-fthread-jumps")
%:pragma GCC optimize("-funroll-loops")
%:pragma GCC optimize("-fwhole-program")
%:pragma GCC optimize("-freorder-blocks")
%:pragma GCC optimize("-fschedule-insns")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("-ftree-tail-merge")
%:pragma GCC optimize("-fschedule-insns2")
%:pragma GCC optimize("-fstrict-aliasing")
%:pragma GCC optimize("-fstrict-overflow")
%:pragma GCC optimize("-falign-functions")
%:pragma GCC optimize("-fcse-skip-blocks")
%:pragma GCC optimize("-fcse-follow-jumps")
%:pragma GCC optimize("-fsched-interblock")
%:pragma GCC optimize("-fpartial-inlining")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("-freorder-functions")
%:pragma GCC optimize("-findirect-inlining")
%:pragma GCC optimize("-fhoist-adjacent-loads")
%:pragma GCC optimize("-frerun-cse-after-loop")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("-finline-small-functions")
%:pragma GCC optimize("-ftree-switch-conversion")
%:pragma GCC optimize("-foptimize-sibling-calls")
%:pragma GCC optimize("-fexpensive-optimizations")
%:pragma GCC optimize("-funsafe-loop-optimizations")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC optimize("-fdelete-null-pointer-checks")
#include <cstdio>

using namespace std;
typedef unsigned long long ULL;
ULL n, k, minn;
ULL t_abs(ULL a, ULL b) { return a > b ? (a - b) : (b - a); }
int main() {
  scanf("%lld%lld", &n, &k);
  if (n % k == 0) {
    printf("0");
    return 0;
  }
  minn = n;
  while (n <= minn) {
    n = t_abs(n, k);
    minn = minn > n ? n : minn;
  }
  printf("%lld", minn);
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

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

  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  if (n < k) {
    ans = min(n, k - n);
  } else {
    ans = min(n % k, k - n % k);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;

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

  ll n, k;
  cin >> n >> k;
  if (n < k) {
    cout << min(n, k % n) << endl;
  } else {
    ll ans = n % k;
    if (ans > k - ans)
      ans = k - ans;
    cout << ans << endl;
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

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

// const ll INF = 1e18L + 5;
// const int INF = 1e9 + 5;
// const double pi = 3.14159265358979323846;

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

  ll n, k;
  cin >> n >> k;
  while (true) {
    if (n % k == 0) {
      cout << 0 << endl;
      return 0;
    } else if (abs(n - k) < (n % k)) {
      cout << min(n, abs(n - k)) << endl;
      return 0;
    } else if ((n % k) < abs(n - k)) {
      cout << min(n % k, abs((n % k) - k)) << endl;
      return 0;
    } else {
      n = abs(n - k);
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

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

// const ll INF = 1e18L + 5;
// const int INF = 1e9 + 5;
// const double pi = 3.14159265358979323846;

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

  ll n, k;
  cin >> n >> k;
  while (true) {
    if (n % k == 0) {
      cout << 0 << endl;
      return 0;
    } else if (abs(n - k) < (n % k)) {
      cout << min(n, abs(n - k)) << endl;
      return 0;
    } else {
      n = abs(n - k);
    }
  }
}
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 repr(i, x, n) for (int i = x; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define INF 1e9
typedef long long ll;
using VI = vector<int>;
using VS = vector<string>;
using graph = vector<vector<int>>;
int main() {
  ll n, k;
  cin >> n >> k;
  if (n > k) {
    n %= k;
  }

  if (k == 1 || k == 0) {
    cout << 0 << endl;
    exit(0);
  }

  while (1) {
    ll x = n;
    n = abs(n - k);
    if (n > x) {
      cout << x << endl;
      exit(0);
    }
  }

  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 repr(i, x, n) for (int i = x; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define INF 1e9
typedef long long ll;
using VI = vector<int>;
using VS = vector<string>;
using graph = vector<vector<int>>;
int main() {
  ll n, k;
  cin >> n >> k;
  if (k == 1 || k == 0) {
    cout << 0 << endl;
    exit(0);
  }

  while (1) {
    ll x = n;
    n = abs(n - k);
    if (n > x) {
      cout << x << endl;
      exit(0);
    }
  }

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

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

#define ll long long
#define ull unsigned long long
#define vii vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pdd pair<double, double>
#define pldld pair<long long double, long long double>
#define ff first
#define ss second
#define pb push_back
#define read freopen("alu.txt", "r", stdin);
#define write freopen("vorta.txt", "w", stdout);
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);
#define PI 2 * acos(0.0)
#define DEBUG(x) cerr << #x << " = " << x << endl

const int MAX = 2e6 + 5, MOD = 1e9 + 7, MAXLG = log2(MAX) + 1;
const ll inf = 1e18 + 5;

int arr[MAX];
int main() {

  fastio;
  ll n, k;
  cin >> n >> k;
  if (n == k || !n)
    cout << 0 << "\\n";
  else {
    if (n > k)
      n %= k;
    cout << min(n, abs(n - k)) << "\\n";
  }
}


Error: 0

Fixed submission:

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

#define ll long long
#define ull unsigned long long
#define vii vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pdd pair<double, double>
#define pldld pair<long long double, long long double>
#define ff first
#define ss second
#define pb push_back
#define read freopen("alu.txt", "r", stdin);
#define write freopen("vorta.txt", "w", stdout);
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);
#define PI 2 * acos(0.0)
#define DEBUG(x) cerr << #x << " = " << x << endl

const int MAX = 2e6 + 5, MOD = 1e9 + 7, MAXLG = log2(MAX) + 1;
const ll inf = 1e18 + 5;

int arr[MAX];
int main() {

  fastio;
  ll n, k;
  cin >> n >> k;
  cout << min(n % k, k % (abs(n - k))) << "\\n";
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int64_t change(int64_t *a, int64_t b) {
  if (*a > b) {
    return *a = (*a - b);
  } else
    return *a = (-1) * (*a - b);
}

int main() {
  int64_t n, k, ans1 = 0, ans2 = 0;
  cin >> n >> k;
  ans1 = n % k;
  ans2 = k - ans1;
  if (ans1 < ans2) {
    cout << ans1;
  } else
    cout << ans2;
}


Error: TLE

Fixed submission:

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

int64_t change(int64_t *a, int64_t b) {
  if (*a > b) {
    return *a = (*a - b);
  } else
    return *a = (-1) * (*a - b);
}

int main() {
  int64_t n, k, ans1 = 0, ans2 = 0, ans3;
  cin >> n >> k;
  if (k == 1) {
    cout << 0;
    return 0;
  }
  ans1 = change(&n, k);
  ans2 = change(&n, k);
  ans3 = change(&n, k);
  while (ans1 > ans2) {
    ans1 = change(&n, k);
    ans2 = change(&n, k);
    if (ans1 == 0 || ans2 == 0)
      break;
  }
  cout << ans1;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k, ans, a, b;
  cin >> n >> k;
  if (n % k == 0)
    cout << "0" << endl;
  else if (n > k) {
    a = n / k;
    b = a + 1;
    ans = min(abs(n - a * k), abs(n - b * k));
    cout << ans << endl;
  } else {
    if (n <= k / 2)
      cout << n << endl;
    else
      cout << k - n << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k, ans, b;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << endl;
  else {
    ans = abs(n - k);
    while (ans <= n) {
      n = ans;
      ans = abs(n - k);
    }
    cout << n << endl;
  }

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

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

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

Error: WA

Fixed submission:

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

int main() {
  int a, b, c, d;
  cin >> a >> b;
  c = a % b;
  d = b - c;
  if (c >= d) {
    cout << d;
  } else {
    cout << c;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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


Error: 0

Fixed submission:

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

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