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

#include <bits/stdc++.h>
#define ll long long
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

using namespace std;
int main() {
  fio;

  ll n, k, min;
  cin >> n >> k;
  if (n % k > k - n % k)
    cout << (k - n % k) << "\\n";
  else
    cout << n % k << "\\n";
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

using namespace std;
int main() {
  fio;
  int t;
  cin >> t;

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

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll n, k;
  cin >> n >> k;

  while (n > abs(n - k)) {
    if (n % k == 0) {
      n = 0;
    } else
      n = abs(n - k);
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define _h cout << "here" << endl;
#define fill(a, v) memset(a, v, sizeof(a));
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; i += 1)
#define prarr(a, n)                                                            \\
  rep(i, n) cout << a[i] << " \\n"[i == n - 1]; // printf("%s\\n"," " );
#define ff first
#define ss second
#define pu push_back
const int inf = 1e9 + 10;
const ll INF = 1e16 + 10;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 10;

int main() {
  ll n, k;
  cin >> n >> k;
  ll min = 0;
  if (n % k == 0) {
    cout << 0 << endl;
    exit(0);
  }

  n = n - (n / k) * k;
  min = n;
  n = n - k;

  if (abs(n) < min) {
    cout << abs(n) << endl;
  } else {
    cout << min << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define _h cout << "here" << endl;
#define fill(a, v) memset(a, v, sizeof(a));
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; i += 1)
#define prarr(a, n)                                                            \\
  rep(i, n) cout << a[i] << " \\n"[i == n - 1]; // printf("%s\\n"," " );
#define ff first
#define ss second
#define pu push_back
const int inf = 1e9 + 10;
const ll INF = 1e16 + 10;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 10;

int main() {
  ll n, k;
  cin >> n >> k;
  ll min = n;
  if (n % k == 0) {
    cout << 0 << endl;
    exit(0);
  }
  while (true) {
    if (n < 0) {
      if (abs(n) < min) {
        cout << abs(n) << endl;

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k;
  long long int inf = 5000000000000000000;
  long long int ans = 5000000000000000000;
  cin >> n >> k;
  while (true) {
    n -= k;
    if (n < 0)
      n = -n;
    if (n >= ans)
      break;
    ans = min(n, inf);
  }
  cout << ans << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

  cout << min(tmp, K - tmp) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> P;
const int INF = 100000000;

int gcd(int a, int b) {

  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  int tmp = N % K;

  cout << min(tmp, K - tmp) << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

// 2020 0404 161 C Replacing integer
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;

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

  ll x;
  if (N == K) {
    cout << "0" << endl;
    return 0;
  } else if (N > K) {
    x = N % K;
  } else {
    x = N;
  }

  ll ans;
  ll tmp;
  if (x >= K) {
    tmp = x - K;
  } else {
    tmp = K - x;
  }

  ans = min(x, tmp);

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

// 2020 0404 161 C Replacing integer
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;

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

  int x;
  if (N > K) {
    x = N % K;
  } else {
    x = K % N;
  }

  int ans;
  int tmp;
  if (x >= K) {
    tmp = x - K;
  } else {
    tmp = K - x;
  }

  ans = min(x, tmp);

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
signed main() {
  ll n, k, f, b;
  cin >> n >> k;
  n %= k;
  cout << min(n, -n + k) << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
signed main() {
  ll n, k, f;
  cin >> n >> k;
  f = abs(n - k);
  if (n == 0 || n % k == 0) {
    cout << "0\\n";
  } else {
    while (n > f) {
      n = f;
      f = abs(f - k);
    }
    cout << n << "\\n";
  }
  return (0);
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

  while (true) {

    if (N < abs(N - K)) {
      break;
    }

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

Error: 0

Fixed submission:

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

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

  while (true) {

    if (N < abs(N - K)) {
      break;
    }

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

#include <iostream>
using namespace std;

int main() {
  unsigned long long N, K;
  cin >> N >> K;
  unsigned long long M;
  if (N == 0) {
    cout << 0;
    return 0;
  }
  if (K == 0) {
    cout << N;
    return 0;
  }
  M = N / K;
  N = N - (M * K);
  while (1) {
    M = N;
    if (N >= K)
      N = N - K;
    if (N < K)
      N = K - N;
    if (N >= M)
      break;
  }
  cout << M;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  unsigned long long N, K;
  cin >> N >> K;
  unsigned long long M;
  if (N % K == 0) {
    cout << 0;
    return 0;
  }
  while (1) {
    M = N;
    if (N >= K)
      N = N - K;
    if (N < K)
      N = K - N;
    if (N >= M)
      break;
  }
  cout << M;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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() {
  long long int N, K;
  cin >> N >> K;

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

    temp = abs(N - K);
    if (N - K <= 0) {
      if (N >= temp) {
        N = temp;
      }

      break;
    } else {
      N %= K;
    }
  }

  cout << N << endl;
}


Error: TLE

Fixed submission:

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

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() {
  long long int N, K;
  cin >> N >> K;

  long long int temp;
  int temp1 = -1, temp2 = -1;
  for (int i = 0;; i++) {
    temp = abs(N - K);
    N = (N > temp) ? temp : N;

    if (N == temp1 && temp1 == temp2) {
      break;
    }

    temp2 = temp1;
    temp1 = N;
  }

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

#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;
}


Error: TLE

Fixed submission:

#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;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <x86intrin.h>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag,
                         tree_order_statistics_node_update>;

#define pb push_back
#define mp make_pair
#define in insert
#define f first
#define s second
#define ll long long
#define dd double

const int mod = 1e9 + 7;
const int N = 2e5 + 7;
const int inf = 2e9;
const ll INF = 1e18;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("*.in", "r", stdin);freopen("*.out", "w", stdout);
  ll n, k;
  cin >> n >> k;
  cout << min(n % k, abs(n % k - k));
}

Error: WA

Fixed submission:

#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <x86intrin.h>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag,
                         tree_order_statistics_node_update>;

#define pb push_back
#define mp make_pair
#define in insert
#define f first
#define s second
#define ll long long
#define dd double

const int mod = 1e9 + 7;
const int N = 2e5 + 7;
const int inf = 2e9;
const ll INF = 1e18;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("*.in", "r", stdin);freopen("*.out", "w", stdout);
  int n, k;
  cin >> n >> k;
  cout << n % k;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <set>
#include <string.h>
#include <utility>

typedef long long ll;

using namespace std;

int main() {

  ll n, k;
  cin >> n >> k;
  ll prev = abs(n - k);
  if (k == 1) {
    cout << "0";
    return 0;
  }
  if (n == 0) {
    cout << "0";
    return 0;
  }
  if (n % k == 0) {
    cout << "0";
    return 0;
  }
  if (n < k) {
    if (abs(n - k) < n) {
      cout << abs(n - k);
      return 0;
    } else {
      cout << n;
      return 0;
    }
  } else {
    if (abs((n % k) - k) < (n % k)) {
      cout << abs((n % k) - k);
      return 0;
    } else {
      cout << n % k;
      return 0;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <set>
#include <string.h>
#include <utility>

typedef long long ll;

using namespace std;

int main() {

  ll n, k;
  cin >> n >> k;
  ll prev = abs(n - k);
  if (k == 1) {
    cout << "0";
    return 0;
  }
  if (n == 0) {
    cout << "0";
    return 0;
  }
  if (n % k == 0) {
    cout << "0";
    return 0;
  }

  while (true) {
    n = abs(n - k);

    if (prev < n) {
      cout << prev;
      break;
    }
    prev = n;
  }

  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;
  a %= b;
  cout << min(a, abs(a - b));
}

Error: WA

Fixed submission:

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

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

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

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

  a = N % K;
  b = K - a;

  if (a <= b)
    cout << a << endl;
  else
    cout << b << endl;

  return 0;
}

// REとは?

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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

  a = N % K;
  b = K - a;

  if (a <= b)
    cout << a << endl;
  else
    cout << b << endl;

  return 0;
}

// REとは?
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin, (x).end()
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return b / gcd(a, b) * a; }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin, (x).end()
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return b / gcd(a, b) * a; }

int main() {
  ll n, nn, k;
  cin >> n >> k;
  while (1) {
    nn = abs(n - k);
    if (nn >= n)
      break;
    else
      n = nn;
  }
  cout << n << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#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";
}

Error: 0

Fixed submission:

#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";
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a, b;
  cin >> a >> b;
  long long int curr = a, prev;
  while (true) {
    prev = curr;
    if (abs(curr - b) > prev) {
      break;
    }
    curr = abs(curr - b);
  }
  cout << prev << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const long long INF = (long long)1e18 + 1;
#define DIV 1000000007
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long idx = 0;
  ll N, K;
  cin >> N >> K;
  ll q = N / K;
  ll smaller = min(abs(N - q * K), abs(N - (q + 1) * K));
  cout << smaller << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const long long INF = (long long)1e18 + 1;
#define DIV 1000000007
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long idx = 0;
  ll N, K;
  cin >> N >> K;
  bool small = true;
  ll num = N;
  ll prev_num = N;
  while (small) {
    num = abs(num - K);
    // cerr << num << "," << prev_num << "," << K << endl;
    if (num >= prev_num) {
      small = false;
    } else
      prev_num = num;
  }
  cout << prev_num << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

signed main() {
  int n, k;
  cin >> n >> k;
  if (n < k)
    cout << min(n, k - n);
  else if (!(n % k))
    cout << 0;
  else
    cout << k - n % k;
  return 0;
}

Error: WA

Fixed submission:

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

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

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;
typedef unsigned long long llong;

#define MAX 1e18
const llong mod = 1000000000 + 7;

int main() {
  llong N, K;
  llong prevN = MAX;
  scanf("%llu%llu", &N, &K);

  llong sub;
  if (N > K)
    N -= N / K * K;
  do {
    prevN = min(prevN, N);
    if (N > K)
      sub = N - K;
    else
      sub = K - N;

    N = sub;
  } while (N != prevN);

  printf("%llu\\n", N);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;
typedef unsigned long long llong;

#define MAX 1e18
const llong mod = 1000000000 + 7;

int main() {
  llong N, K;
  llong prevN = mod;
  scanf("%llu%llu", &N, &K);

  llong sub;
  if (N > K)
    N -= N / K * K;
  do {
    prevN = min(prevN, N);
    if (N > K)
      sub = N - K;
    else
      sub = K - N;

    N = sub;
  } while (N != prevN);

  printf("%llu\\n", N);

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

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define all(n) (n).begin(), (n).end()
#define ll long long

void solve() {
  ll n, k;
  cin >> n >> k;
  ll ans = n;
  while (1) {
    if (ans % k == 0) {
      ans = 0;
      break;
    }

    n = abs(n - k);

    if (ans > n) {
      ans = n;
    } else {
      break;
    }
    n = n % k;
  }
  cout << ans << endl;
}

int main() {

  // Write from here.

  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define all(n) (n).begin(), (n).end()
#define ll long long

void solve() {
  ll n, k;
  cin >> n >> k;
  ll ans = n;
  while (1) {
    if (ans % k == 0) {
      ans = 0;
      break;
    }

    n = abs(n - k);

    if (ans > n) {
      ans = n;
    } else {
      break;
    }
  }
  cout << ans << endl;
}

int main() {

  // Write from here.

  solve();

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: 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;
  ll MIN = n % k;
  cout << min(MIN, k - MIN);
}

Error: WA

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;
  if (n < k)
    swap(n, k);
  n = n % k;
  if (n == 0)
    cout << k;
  else {
    cout << min(n, k - n);
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

//
//  main.cpp
//  CoderProj
//
//  Created by Minoru Hayashi on 2020/04/04.
//  Copyright © 2020 Minoru Hayashi. All rights reserved.
//
//  ABC161
//
//

#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)

using ll = long long;

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

const ll INF = 1LL << 60;
// const int INF = 1<<29;
// const ll MOD = 1000000007;
// #define __DBG__

void solve(void) {

  ll N, K;

  cin >> N >> K;

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

  ll dif = abs(N - K);

  N = dif;

  while (abs(N - K) < dif) {
    N = abs(N - K);
    if (N == 0)
      break;
  }

  cout << N << endl;

  return;
}

#define TESTCASES 1

int main(int argc, const char *argv[]) {

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

  REP(i, TESTCASES) { solve(); }

  return 0;
}


Error: TLE

Fixed submission:

//
//  main.cpp
//  CoderProj
//
//  Created by Minoru Hayashi on 2020/04/04.
//  Copyright © 2020 Minoru Hayashi. All rights reserved.
//
//  ABC161
//
//

#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)

using ll = long long;

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

const ll INF = 1LL << 60;
// const int INF = 1<<29;
// const ll MOD = 1000000007;
// #define __DBG__

void solve(void) {

  ll N, K;

  cin >> N >> K;

  ll dif = abs(N - K);

  N = dif;

  if (N % K == 0) {
    cout << "0" << endl;
    return;
  }
  while (abs(N - K) < dif) {
    N = abs(N - K);
    if (N == 0)
      break;
  }

  cout << N << endl;

  return;
}

#define TESTCASES 1

int main(int argc, const char *argv[]) {

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

  REP(i, TESTCASES) { solve(); }

  return 0;
}