alexjercan/bugnet huggingface dataset:
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;

int main() {
  long int n, k;
  cin >> n >> k;
  long int nt = n;
  if (k == 1)
    nt = 0;
  else {
    nt %= k;
    if (abs(nt - k) < nt)
      nt = abs(nt - k);
  }
  cout << nt << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;

int main() {
  long int n, k;
  cin >> n >> k;
  long int nt = n;
  if (k == 1)
    nt = 0;
  else {
    long int min = n;
    do {
      if (min > nt)
        min = nt;
      nt = abs(nt - k);
    } while (min != nt);
  }
  cout << nt << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main(void) {
  long long n, k;
  cin >> n >> k;
  long long abs_Data = 0;
  while (1) {
    if (n > k)
      abs_Data = n % k;
    else
      abs_Data = abs(n - k);
    if (abs_Data >= n)
      break;
    n = abs_Data;
  }

  cout << n;
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

int main(void) {
  long long n, k;
  cin >> n >> k;
  long long abs_Data = 0;
  while (1) {
    abs_Data = abs(n - k);
    // cout << n << "," << k << "," << abs_Data << endl;
    if (abs_Data > n)
      break;
    n = abs_Data;
  }
  cout << n;
  return 0;
}
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;
  set<int64_t> x;
  for (; !x.count(N);) {
    x.insert(N);
    if (N > K)
      N %= K;
    else
      N = K - N;
  }
  cout << *begin(x) << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t N, K;
  cin >> N >> K;
  set<int64_t> x;
  for (; !x.count(N);) {
    x.insert(N);
    if (N > K)
      N -= K;
    else
      N = K - N;
  }
  cout << *begin(x) << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <cstdint>
#include <iostream>
using namespace std;

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

  uint64_t first = N % K;
  uint64_t second = K - first;

  cout << (first < second ? first : second) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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

  int first = N % K;
  int second = K - first;

  cout << (first < second ? first : second) << endl;

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

#include <iostream>
using namespace std;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {

  int n, k;
  cin >> n >> k;
  if (n >= k) {
    {
      // while(n==0||n>0)
      while (n > 0) {

        n = n - k;

        //
      }
      //
    }
    cout << -n << endl;
  }
  // if(n<k)
  else {
    cout << n << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded


/*Bismillahir Rahmanir Rahim*/

#include <bits/stdc++.h>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#define ll long long int
#define dd double
using namespace std;

int main() {
  ll a, b;
  cin >> a >> b;
  ll x = a % b;
  ll y = abs(x - b);
  if (x < y)
    cout << x << endl;
  else {
    cout << y << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

int main() {

  long long int n, k, c = 0, x;

  cin >> n >> k; /// 7 4
  while (1) {
    x = abs(n - k); /// 3
    if (x == 0) {
      n = 0;
      break;
    } else if (x <= n) {
      n = x;

    } else if (x > n) {
      break;
    }
  }
  cout << n << endl;
  return 0;
}
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;
  long long int m = n - (n / k) * k;
  long long int ans = abs(m - k);
  cout << min(ans, abs(ans - k));
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int m = n - (n / k) * k;
  int ans = abs(m - k);
  cout << min(ans, abs(ans - k));
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()

typedef long long ll;

void solve() {
  ll n, k;
  cin >> n >> k;
  n %= k;
  cout << min(n, k - n);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()

typedef long long ll;

void solve() {
  ll n, k;
  cin >> n >> k;
  if (k % n != 0) {
    ll q = (n + k - 1) / k;
    n = abs(n - k * q);
  }
  cout << n;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();

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

#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {

  long long N, K;
  long long ans, now1, now2;
  cin >> N >> K;

  now1 = abs(N - (N / K + 1) * K);
  now2 = abs(N - (N / K) * K);

  cout << min(now1, now2) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  int N, K, ans, now, now2;
  cin >> N >> K;
  if (N == 0)
    ans = 0;
  else {
    now = N % K;
    now2 = K - now;
    if (now >= now2)
      ans = now2;
    else
      ans = now;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

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

  N = (N > K) ? N % K : N;
  ll ret = (K - N > N) ? N : K - N;
  cout << ret;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll GCD(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return GCD(a % b, b);
}

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

  if (K == 1) {
    cout << 0;
    return 0;
  }

  ll gcd = GCD(N, K);
  if (gcd == K) {
    cout << 0;
    return 0;
  }

  cout << gcd;

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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define el endl
#define ll long long int
int main() {
  ll a, k, i, p, q;
  cin >> a >> k;
  p = a % k;
  q = (((a / k) + 1) * k) - a;
  if (p > q)
    cout << q << el;
  else
    cout << p << el;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k;
  scanf("%lli%lli", &n, &k);
  if (n % k == 0) {
    printf("0\\n");
  } else {
    long long int mini;
    mini = n;
    while (1) {
      n = abs(n - k);
      if (n == mini) {
        printf("%lli\\n", mini);
        break;
      } else if (n < mini) {
        mini = n;
      }
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

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

  ll r = n % k;

  if (k - r < r) {
    r = k - r;
  }

  cout << r;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

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

  ll temp;

  bool flag = true;

  if (k == 1) {
    n = 0;
    flag = false;
  }

  if (k == 2) {
    if (n % 2 == 0) {
      n = 0;
    } else {
      n = 1;
    }
    flag = false;
  }

  while (flag) {
    temp = abs(n - k);

    if (temp < k && temp > n) {
      break;
    }
    if (temp == 0) {
      n = 0;
      break;
    }
    n = temp;
  }

  cout << n;

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

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl

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

  if (n > k * 2) {
    n = n % k;
  }

  while (1) {
    if (n - k > 0) {
      n -= k;
    } else {
      break;
    }
  }

  if (n > abs(n - k)) {
    cout << abs(n - k) << endl;
  } else {
    cout << n << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl

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

  if (k == 1) {
    cout << "0" << endl;
    return 0;
  }

  while (1) {
    if (n - k > 0) {
      n -= k;
    } else {
      break;
    }
  }

  if (n > abs(n - k)) {
    cout << abs(n - k) << endl;
  } else {
    cout << n << endl;
  }

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

#include <algorithm>
#include <iostream>
using namespace std;

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

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  int a, b;
  a = N % K;
  b = K - a;
  if (a < b) {
    cout << a << endl;
  } else {
    cout << b << endl;
  }
}
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 < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()

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

  /*
  if(n%k==0){
      cout << "0" << endl;
      return 0;
  }
  */

  while (true) {
    if (abs(x - k) < x)
      x = abs(x - k);
    else
      break;
  }

  cout << x << endl;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()

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

  if (n % k == 0) {
    cout << "0" << endl;
    return 0;
  }

  while (true) {
    if (abs(x - k) < x)
      x = abs(x - k);
    else
      break;
  }

  cout << x << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define vec(a, y, x) vector<vector<char>> a(y, vector<char>(x))
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};

in kansu(in x, in k) {
  if (x % k == 0)
    return 0;
  if (x > k)
    return kansu(x % k, k);
  in next_x = k - x;
  if (next_x > x)
    return x;
  return kansu(next_x, k);
}

int main() {
  int64_t n;
  int64_t k;
  cin >> n >> k;
  cout << kansu(n, k) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define vec(a, y, x) vector<vector<char>> a(y, vector<char>(x))
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};

int64_t gcd(int64_t n, int64_t k) {
  if (n == k)
    return n;
  if (n > k) {
    int64_t a = n % k;
    if (a == 0)
      return k;
    return gcd(a, k);
  } else {
    int64_t a = k % n;
    if (a == 0)
      return n;
    return gcd(n, a);
  }
}

int main() {
  int64_t n;
  int64_t k;
  cin >> n >> k;
  // cout<<gcd(n,k);
  int64_t g = gcd(n, k);
  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (g == k)
    cout << k << endl;
  else
    cout << g << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>

using namespace std;

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

  long long int new_n1, new_n2, best = n;
  while (true) {
    if (best == 0)
      break;

    new_n1 = abs(best - (best / k) * k);
    new_n2 = abs(best - (k / best) * k);
    if (best > min(new_n1, new_n2))
      best = min(new_n1, new_n2);
    else
      break;
  }

  cout << best << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>

using namespace std;

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

  long long new_n1, new_n2, best = n;
  while (true) {
    new_n1 = abs(best - (n / k) * k);
    new_n2 = abs(best - (k / n) * k);
    if (best > min(new_n1, new_n2))
      best = min(new_n1, new_n2);
    else
      break;
  }

  cout << best << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  while (abs(n - k) < n)
    n = abs(n - k);
  cout << n;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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


Error: WA

Fixed submission:

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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define l1(i, n) for (ll i = 1; i <= n; i++)
#define l0(i, n) for (ll i = 0; i < n; i++)
#define lo(i, s, n) for (ll i = s; i <= n; i++)
#define pb push_back
#define nl cout << "\\n";

const ll sz = 2e5;

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

  ll n, k;
  cin >> n >> k;
  cout << min((n % k), abs((n % k) - k));
  return 0;
}


Error: TLE

Fixed submission:

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

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

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

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


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
using namespace std;
int main() {
  long long a, b;
  scanf("%lld%lld", &a, &b);
  while (true) {
    if (abs(a - b) < a) {
      a = abs(a - b);
    } else
      break;
  }
  printf("%lld\\n", a);
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long n, k, i, t, tt;
  scanf("%lld %lld", &n, &k);
  if (n >= k) {
    t = n % k;
    printf("%lld", min(t, k - t));
  } else
    printf("%lld", min(n, k - n));
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
  long long int n, k;
  cin >> n >> k;
  while (1) {
    n = abs(n - k);
    if (n > abs(n - k))
      continue;
    else {
      cout << n << endl;
      break;
    }
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded



#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {

  long long N, K, temp, x;
  cin >> N >> K;

  for (int i = 0; true; i++) {

    x = 1;

    if (N > K) {
      x = N / K;
    }

    temp = abs(N - x * K);
    if (N >= temp) {
      N = temp;
    } else
      break;
  }

  cout << N << endl;

  return 0;
}


Error: TLE

Fixed submission:



#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {

  long long N, K, temp;
  cin >> N >> K;

  for (int i = 0; true; i++) {
    temp = abs(N - K);
    if (N >= temp) {
      N = temp;
    } else
      break;
  }

  cout << N << endl;

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

#include <iostream>

using namespace std;

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

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

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

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

#include <bits/stdc++.h>

using namespace std;

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define LL long long
#define inf INT_MAX / 3
#define mod 1000000007ll
#define PI acos(-1.0)
#define linf (1ll << 60) - 1
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define REP(I, N) FOR(I, 0, N)
#define For(I, N) for (int I = 1; I <= (N); I++)
#define ALL(A) ((A).begin(), (A).end())
#define set0(ar) memset(ar, 0, sizeof ar)
#define vsort(v) sort(v.begin(), v.end())
#define setinf(ar) memset(ar, 126, sizeof ar)
#define SZ(X) ((int)(X.size()))
#define LG(X) ((int)(X.length()))
#define CL(X) (X).clear()
#define MAXEL(A, B) (*max_element(A, B))
#define MINEL(A, B) (*min_element(A, B))
#define ROPEN(X) freopen(X, "r", stdin)
#define WOPEN(X) freopen(X, "w", stdout)
#define BG(X) (X.begin())
#define EN(X) (X.end())
#define VI vector<int>
#define PII pair<int, int>

// cout << fixed << setprecision(20) << p << endl;

template <class T> inline T bigmod(T p, T e, T M) {
  LL ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  LL n, m;
  cin >> n >> m;
  n %= m;
  cout << min(n, m - n) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define LL long long
#define inf INT_MAX / 3
#define mod 1000000007ll
#define PI acos(-1.0)
#define linf (1ll << 60) - 1
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define REP(I, N) FOR(I, 0, N)
#define For(I, N) for (int I = 1; I <= (N); I++)
#define ALL(A) ((A).begin(), (A).end())
#define set0(ar) memset(ar, 0, sizeof ar)
#define vsort(v) sort(v.begin(), v.end())
#define setinf(ar) memset(ar, 126, sizeof ar)
#define SZ(X) ((int)(X.size()))
#define LG(X) ((int)(X.length()))
#define CL(X) (X).clear()
#define MAXEL(A, B) (*max_element(A, B))
#define MINEL(A, B) (*min_element(A, B))
#define ROPEN(X) freopen(X, "r", stdin)
#define WOPEN(X) freopen(X, "w", stdout)
#define BG(X) (X.begin())
#define EN(X) (X.end())
#define VI vector<int>
#define PII pair<int, int>

// cout << fixed << setprecision(20) << p << endl;

template <class T> inline T bigmod(T p, T e, T M) {
  LL ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  n %= m;
  cout << min(n, m - n) << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define INF LLONG_MAX
#define mod 1000000007
typedef long long ll;

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define INF LLONG_MAX
#define mod 1000000007
typedef long long ll;

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

/**
 *  author: mahfuzz
 *  created: 01.05.2020
 **/

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  ll n, x;
  cin >> n >> x;

  cout << min(n, min(n % x, abs(x - (n % x)))) << "\\n";

  return 0;
}


Error: 0

Fixed submission:

/**
 *  author: mahfuzz
 *  created: 02.05.2020
 **/

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  int n, x;
  cin >> n >> x;

  cout << min(n, min(n % x, abs(x - (n % x)))) << "\\n";

  return 0;
}