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

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

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

  LL d = abs(n - k);
  LL x = n / d;
  LL ans = min(n - x * d, abs(n - x * (d + 1)));

  cout << ans << endl;
}


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

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

  cout << min(n % k, abs(n % k - k)) << endl;
}
Problem p02719 (C++)

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define foi(i, k, n) for (int i = (int)k; i < (int)n; ++i)

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

constexpr int MAXN = 5 + 1000;
int a[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll a, b;
  cin >> a >> b;
  cout << a % abs(a - b) << '\n';
  return 0;
}


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

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

constexpr int MAXN = 5 + 1000;
int a[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  ll ans = n;
  for (int i = 0; i < 100; ++i) {
    if (n >= k) {
      n %= k;
    } else {
      n = abs(n - k);
    }
    ans = min(ans, n);
  }
  cout << ans << '\n';
  return 0;
}
Problem p02719 (C++)

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

using namespace std;

int n, k;

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


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

using namespace std;

long long n, k;

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

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

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
using namespace std;
long long n, k, ans;
map<int, int> mp;
int main() {
  scanf("%lld%lld", &n, &k);
  if (n % k == 0)
    ans = 0;
  else {
    while (n > k) {
      n -= k;
    }
    if (k - n < n)
      ans = k - n;
    else
      ans = n;
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
using namespace std;
long long n, k, ans;
map<int, int> mp;
int main() {
  scanf("%lld%lld", &n, &k);
  if (n % k == 0)
    ans = 0;
  else {
    if (n > k) {
      n %= k;
    }
    if (k - n < n)
      ans = k - n;
    else
      ans = n;
  }
  printf("%lld\n", ans);
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, k, mn;
  cin >> n >> k;
  mn = n;
  if (n % k > 0) {
    while (n > 0) {
      n = n - k;
      mn = min(abs(n), mn);
    }
  } else {
    mn = 0;
  }

  cout << mn << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, k, mn;
  cin >> n >> k;
  mn = n;
  if (n % k > 0) {
    mn = min(n - (n / k * k), abs(n - (n / k * k + k)));
  } else {
    mn = 0;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  cout << min(n % m, m - (n % m)) << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  cout << min(n % m, m - (n % m)) << '\n';
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  cout << min(n % k, abs(n % k - k)) << endl;
  return 0;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  cout << min(n % k, abs(k - n % k));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  cout << min(n % k, abs(k - n % k));
  return 0;
}
Problem p02719 (C++)

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

int main() {
  int N, K, sur, ans;

  cin >> N >> K;

  sur = N % K;

  ans = min(sur, K - sur);

  cout << ans << endl;
}

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

int main() {
  long long int N, K, sur, ans;

  cin >> N >> K;

  sur = N % K;

  ans = min(sur, K - sur);

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

Buggy submission (status: 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(ans % n, ans);
  ans = mini(ans, absc(m, n % m));
  ans = mini(absc(m, absc((n % m + n), m)), ans);
  cout << ans;
}


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(n % m, 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 (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

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

  a = n % k;
  if (a - k >= 0) {
    b = a - k;
  } else {
    b = k - a;
  }

  if (a >= b) {
    cout << b << endl;
  } else {
    cout << a << endl;
  }
}

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

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

  a = n % k;
  if (a - k >= 0) {
    b = a - k;
  } else {
    b = k - a;
  }

  if (a >= b) {
    cout << b << endl;
  } else {
    cout << a << endl;
  }
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

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

  while (n > abs(n - k)) {
    n = abs(n - k);
  }

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


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

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

  if (n > k)
    n = n - (n / k) * k;
  while (n > abs(n - k)) {
    n = abs(n - k);
  }

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

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

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long unsigned x, y, anti_rem;
  cin >> x >> y;
  if (x % y == 0) {
    cout << 0 << endl;
  } else if (x < y) {
    anti_rem = labs(x - y);
    cout << min(x, anti_rem) << endl;
  } else {
    while (x > y) {
      x = x - y;
    }
    long long unsigned xx = labs(x - y);
    cout << min(x, xx) << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long unsigned x, y, anti_rem;
  cin >> x >> y;
  if (x % y == 0) {
    cout << 0 << endl;
  } else if (x < y) {
    anti_rem = labs(x - y);
    cout << min(x, anti_rem) << endl;
  } else {
    x = x % y;
    long long unsigned xx = labs(x - y);
    cout << min(x, xx) << endl;
  }

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define R(i, n) for (int i = 0; i < n; i++)
#define S(a) scanf("%d", &a)
#define S2(a, b) scanf("%d%d", &a, &b)
#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SL(a) scanf("%lld", &a)
#define P(a) printf("%d", a)
#define PL(a) printf("%lld", a);
#define PY printf("%s", "Yes");
#define PN printf("%s", "No");
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> IR;

int main() {
  ll n, k;
  SL(n);
  SL(k);
  ll now = n;
  while (true) {
    ll tmp = abs(k - n);
    if (tmp >= now)
      break;
    else {
      n = tmp;
      now = tmp;
    }
  }
  PL(n);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define R(i, n) for (int i = 0; i < n; i++)
#define S(a) scanf("%d", &a)
#define S2(a, b) scanf("%d%d", &a, &b)
#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SL(a) scanf("%lld", &a)
#define P(a) printf("%d", a)
#define PL(a) printf("%lld", a);
#define PY printf("%s", "Yes");
#define PN printf("%s", "No");
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> IR;

int main() {
  ll n, k;
  SL(n);
  SL(k);
  ll mod = n % k;
  ll diff = k - mod;
  PL(min(mod, diff));
}
Problem p02719 (C++)

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

  cout << N << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  N = N % K;
  if (K / 2 < N) {
    N = abs(N - K);
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, x, n) for (int i = x; i < n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
const int mod = 1'000'000'007;

ll factor(ll n, ll k) {
  ll x = abs(n - k);
  if (x > n) {
    return n;
  }
  return factor(x, k);
}

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, x, n) for (int i = x; i < n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
const int mod = 1'000'000'007;

ll factor(ll n, ll k) {
  ll x = abs(n - k);
  if (x > n) {
    return n;
  }
  return factor(x, k);
}

int main() {
  ll n, k;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  ll x = n % k;
  ll y = abs(x - k);
  ll z = min(x, y);
  cout << z << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  ll N, K;
  cin >> N >> K;
  ll x;
  int i = 2;
  while (i) {
    if (N - K < 0) {
      i--;
    }
    x = N;
    N = labs(N - K);
  }
  x <= N ? cout << x : cout << N;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  ll N, K;
  cin >> N >> K;
  ll x;
  int i = 2;
  N = N % K;
  while (i) {
    if (N - K < 0) {
      i--;
    }
    x = N;
    N = labs(N - K);
  }
  x <= N ? cout << x : cout << N;
}
Problem p02719 (C++)

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

int main() {
  long long N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
    return 0;
  }

  while (1) {
    if (N < abs(N - K))
      break;
    N = abs(N - K);
  }
  cout << N << endl;
}

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

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

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;

  long long amari = N % K;

  long long ans = min(amari, K - amari);
  cout << ans;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define BIGINF (int)4e18 + 7
#define MOD7 1000000007
#define MOD9 1e9 + 9
#define INF (int)2e9 + 7
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define MEM(a, b) memset(a, (b), sizeof(a))
typedef long long ll;
typedef long double ld;
typedef string str;
typedef double d;
typedef unsigned u;
typedef unsigned long long ull;

int main() {
#ifndef ONLINE_JUDGE
  freopen("inputf.txt", "r", stdin);
  freopen("outputf.txt", "w", stdout);
#endif
  long long n, k;
  SCLLD(n);
  SCLLD(k);
  if (n == 0) {
    printf("0");
  } else {
    long long ans = n % k;
    ans = min(ans, abs(ans - k));
    printf("%lld", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define BIGINF (int)4e18 + 7
#define MOD7 1000000007
#define MOD9 1e9 + 9
#define INF (int)2e9 + 7
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define MEM(a, b) memset(a, (b), sizeof(a))
typedef long long ll;
typedef long double ld;
typedef string str;
typedef double d;
typedef unsigned u;
typedef unsigned long long ull;

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("inputf.txt", "r", stdin);
  // freopen("outputf.txt", "w", stdout);
  // #endif
  ll n, k;
  SCLLD(n);
  SCLLD(k);
  if (n == 0) {
    printf("0");
  } else {
    long long ans = n % k;
    ans = min(ans, abs(ans - k));
    printf("%lld", ans);
  }
  return 0;
}
Problem p02719 (C++)

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

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


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

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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <time.h>
using namespace std;

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

Fixed submission:
#include <iostream>
#include <map>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <time.h>
using namespace std;

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

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

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

  cout << min(N % K, K - (N % K));
}

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

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

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

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

#define ll long long

int main() {
  ios::sync_with_stdio(0);

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

  if (k == 1 or n == k)
    cout << 0;
  else {
    if (n < k)
      cout << min(n, k - n);
    else if (n % k == 0)
      cout << 0;
    else {
      set<ll> xs;
      while (true) {
        if (xs.count(n)) {
          cout << *xs.begin();
          return 0;
        }
        xs.insert(n);
        n = abs(n - k);
      }
    }
  }

  cout << endl;

  return 0;
}


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

#define ll long long

int main() {
  ios::sync_with_stdio(0);

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

  if (k == 1 or n == k)
    cout << 0;
  else {
    if (n < k)
      cout << min(n, k - n);
    else if (n % k == 0)
      cout << 0;
    else
      cout << min(n % k, k - (n % k));
  }

  cout << endl;

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long tmp = abs(n - k);
  if (n % k == 0)
    n = 0;
  while (n > tmp) {
    n = tmp;
    tmp = abs(n - k);
  }
  cout << n << endl;
  return 0;
}

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

using namespace std;

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;           // 10^19 < 9223372036854775807
typedef unsigned long long ull; // 10^20 < 18446744073709551615

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto &i : I)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define MOD 1000000007

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

  while (N >= abs(N - K)) {
    N = abs(N - K);
  }

  cout << N << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;           // 10^19 < 9223372036854775807
typedef unsigned long long ull; // 10^20 < 18446744073709551615

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto &i : I)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define MOD 1000000007

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

  return 0;
}
Problem p02719 (C++)

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

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

  long long ans = N;
  while (1) {
    if (ans >= K) {
      ans = ans - K;
    } else {
      break;
    }
  }

  ans = min(ans, K - ans);
  cout << ans << endl;

  return 0;
}

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

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

  long long ans = N % K;

  ans = min(ans, K - ans);
  cout << ans << endl;

  return 0;
}
Problem p02719 (C++)

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

using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  if (n % k == 0 || n == k) {
    cout << "0";
    return 0;
  } else {
    if (k > 2 * n) {
      cout << n;
    } else {
      while (1) {
        long long m = n;
        n = abs(n - k);
        if (m < n) {
          cout << m;
          return 0;
        }
      }
    }
  }
}

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

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

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

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, int(n) + 1)
#define RREP(i, n) RFOR(i, 0, n)
#define RREP1(i, n) RFOR(i, 1, int(n) + 1)
#define ALL(c) begin(c), end(c)

int _ = (
#ifndef LOCAL
    std::cin.tie(nullptr), std::ios::sync_with_stdio(false),
#endif
    std::cout.precision(10), std::cout.setf(std::ios::fixed));

using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T> using vec = std::vector<T>;
using namespace std;

int main() {
  ll N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll x = N;
  while (K < 2 * x) {
    x = abs(x - K);
  }
  cout << x << endl;
  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, int(n) + 1)
#define RREP(i, n) RFOR(i, 0, n)
#define RREP1(i, n) RFOR(i, 1, int(n) + 1)
#define ALL(c) begin(c), end(c)

int _ = (
#ifndef LOCAL
    std::cin.tie(nullptr), std::ios::sync_with_stdio(false),
#endif
    std::cout.precision(10), std::cout.setf(std::ios::fixed));

using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T> using vec = std::vector<T>;
using namespace std;

int main() {
  ll N, K;
  cin >> N >> K;
  const ll r = N % K;
  const ll s = K - r;
  cout << (r < s ? r : s) << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < int(n); ++i)
using namespace std;
template <class A> void pr(A a) { cout << a << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  pr(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  pr(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  pr(b, c, d);
}
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int INF = numeric_limits<int>::max();
// for(int j=0;j<N;++j) {}

int main(void) {
  ll n, k;
  cin >> n >> k;
  ll res = n;
  while (abs(res - k) < res) {
    res = abs(res - k);
  }
  pr(res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < int(n); ++i)
using namespace std;
template <class A> void pr(A a) { cout << a << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  pr(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  pr(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  pr(b, c, d);
}
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int INF = numeric_limits<int>::max();
// for(int j=0;j<N;++j) {}

int main(void) {
  ll n, k;
  cin >> n >> k;
  ll res;
  ll cnt = n / k;
  res = n - k * cnt;
  res = min(res, abs(res - k));
  pr(res);
  return 0;
}
Problem p02719 (C++)

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

using namespace std;

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

  n = n % k;
  if (n == 0 || (n < k && n < abs(n - k))) {

    cout << n << endl;
    return 0;
  } else
    cout << abs(n - k) << endl;
  return 0;
}


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

using namespace std;

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

  n = n % k;
  if (n == 0 || (n < k && n < abs(n - k))) {

    cout << n << endl;
    return 0;
  } else
    cout << abs(n - k) << endl;
  return 0;
}
Problem p02719 (C++)

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

int main() {
  long long a, b;
  cin >> a >> b;
  long long m = abs(a - b);
  long long i = 1;
  for (;;) {
    if (m < abs(a - i * b))
      break;
    else {
      m = abs(a - i * b);
      i++;
      continue;
    }
  }
  cout << min(a, m) << endl;
  return 0;
}


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

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

Buggy submission (status: Runtime Error):
// vector<vector<int>> data(3,
// vector<int>(4));//int型の2次元配列(3×4要素の)の宣言 int64_t a; 10のi乗pow(10,
// i);

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

int main() {
  /* code */
  int N, K;
  cin >> N >> K;
  cout << min(N % K, K - N % K) << endl;
  return 0;
}


Fixed submission:
// vector<vector<int>> data(3,
// vector<int>(4));//int型の2次元配列(3×4要素の)の宣言 int64_t a; 10のi乗pow(10,
// i);

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

int main() {
  int64_t n, k;
  cin >> n >> k;
  //  int a;

  cout << min(n % k, abs(n % k - k)) << endl;
  //  a = n % k;
  //  if(a<=abs(a-k))cout<< a <<endl;
  //  else cout<<abs(a-k)<<endl;
}
Problem p02719 (C++)

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

using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;
  int t = 0;
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  while (t == 0) {
    if (n > abs(n - k)) {
      n = abs(n - k);
    } else {
      break;
    }
  }
  cout << n << endl;
}


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

using namespace std;

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

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

using namespace std;

int main() {
  int64_t n, k, m;

  cin >> n >> k;

  while (true) {
    m = abs(n - k);
    if (m >= n) {
      break;
    } else {
      n = m;
    }
  }

  cout << n << endl;

  return 0;
}

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

using namespace std;

int main() {
  int64_t n, k, m;

  cin >> n >> k;

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

  while (true) {
    m = abs(n - k);
    if (m >= n) {
      break;
    } else {
      n = m;
    }
  }

  cout << n << endl;

  return 0;
}
Problem p02719 (C++)

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

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

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

  long long ans = N;
  while (1) {
    long long tmp = llabs(llabs(N) - llabs(K));
    N = tmp;
    if (N < ans) {
      ans = N;
    } else {
      break;
    }
  }
  cout << ans << endl;
}


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

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

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

  N = N % K;

  long long ans = N;
  while (1) {
    long long tmp = llabs(llabs(N) - llabs(K));
    N = tmp;
    if (N < ans) {
      ans = N;
    } else {
      break;
    }
  }
  cout << ans << endl;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
unsigned long long k, n;
int main() {
  cin >> n >> k;
  if (max(n, k) % min(n, k) != 0)
    if (n > k)
      cout << k - n % k;
    else
      cout << max(n, k) - min(n, k);
  if (max(n, k) % min(n, k) == 0)
    if (n < k)
      cout << n;
    else
      cout << 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
unsigned long long k, n;
int main() {
  cin >> n >> k;
  if (n == 0)
    cout << 0, exit(0);
  n %= k;
  cout << min(n, k - n);
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  cout << min(n % k, k - n % k);
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, k;
int main() {
  cin >> n >> k;
  cout << min(n % k, k - (n % k)) << endl;
  return 0;
}


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

long long n, k;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  cin >> a >> b;
  if (a % b == 0) {
    cout << 0 << endl;
  } else if (a % b <= b - a % b) {
    cout << a % b << endl;
  } else
    cout << b - a % b << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b;
  cin >> a >> b;
  if (a % b == 0) {
    cout << 0 << endl;
  } else if (a % b <= b - a % b) {
    cout << a % b << endl;
  } else
    cout << b - a % b << endl;
}
Problem p02719 (C++)

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

using namespace std;

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


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

using namespace std;

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long n, k;
  vector<long long> v;
  cin >> n >> k;
  long long m = n;
  v.push_back(n);
  bool flag = 1;
  while (1) {
    n = abs(n - k);
    m = min(n, m);
    for (int i = 0; i < v.size(); i++) {
      if (n == v[i])
        flag = 0;
    }
    if (flag == 0)
      break;
    else
      v.push_back(n);
  }
  cout << m << endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long n, k;
  vector<long long> v;
  cin >> n >> k;
  long long m = n;
  v.push_back(n);
  bool flag = 1;
  while (1) {
    if (n > k)
      n = n % k;
    else
      n = abs(n - k);
    m = min(n, m);
    for (int i = 0; i < v.size(); i++) {
      if (n == v[i])
        flag = 0;
    }
    if (flag == 0)
      break;
    else
      v.push_back(n);
  }
  cout << m << endl;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

#define MOD (1000000007)
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  ll maxv = max(a, b);
  ll minv = min(a, b);

  if (maxv % minv == 0)
    return minv;
  else
    return gcd(maxv - minv, minv);
}

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

  cout << min(N % K, K - (N % K)) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

#define MOD (1000000007)
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  ll maxv = max(a, b);
  ll minv = min(a, b);

  if (maxv % minv == 0)
    return minv;
  else
    return gcd(maxv - minv, minv);
}

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

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

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

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

  long long int n, k, min;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0;
  } else {
    do {
      min = n;
      n = abs(n - k);
    } while (n < min);
    cout << min;
  }

  // system("pause");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

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

  long long int n, k, min;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0;
  } else {
    n %= k;
    do {
      min = n;
      n = abs(n - k);
    } while (n < min);
    cout << min;
  }

  // system("pause");
  return 0;
}
Problem p02719 (C++)

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

void Main() {
  long long N, K;
  cin >> N >> K;
  long long x = N;
  long long tmp = abs(N - K);
  bool isExist = (x > tmp);
  while (isExist) {
    x = tmp;
    tmp = abs(tmp - K);
    if (x <= tmp) {
      break;
    }
  }
  cout << x << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

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

void Main() {
  long long N, K;
  cin >> N >> K;

  cout << min(N % K, abs(K - N % K)) << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
}
Problem p02719 (C++)

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

int main() {
  const int INF = 1e9 + 10;
  long long N, K;
  cin >> N >> K;
  int chk = 0;
  long long min = INF;
  N = N - K * (N / K);

  while (chk == 0) {
    N = abs(N - K);
    if (N == min)
      chk++;
    if (N < min)
      min = N;
  }
  cout << min << endl;
}


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

int main() {
  const long long INF = 1e18 + 10;
  long long N, K;
  cin >> N >> K;
  int chk = 0;
  long long min = INF;
  N = N - K * (N / K);

  while (chk == 0) {
    N = abs(N - K);
    if (N == min)
      chk++;
    if (N < min)
      min = N;
  }
  cout << min << endl;
}
Problem p02719 (C++)

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

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

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

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

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

int main() {
  long long n, k;
  cin >> n >> k;
  map<long long, bool> mp;
  long long ans = n;
  while (!mp[abs(n - k)]) {
    mp[abs(n - k)] = 1;
    ans = min(ans, abs(n - k));
    n = abs(n - k);
  }
  cout << ans;
}


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

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

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

#define FastIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define nl "\n"
#define sp " "
#define int long long

int32_t main() {
  FastIO;
  int n, k;
  cin >> n >> k;
  if (k > n / 2)
    k = abs(n - k);
  cout << n % k << nl;
  return 0;
}


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

#define FastIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define nl "\n"
#define sp " "
#define int long long

int32_t main() {
  FastIO;
  int n, k;
  cin >> n >> k;
  int x = n / k;
  int y = x + 1;
  x *= k;
  y *= k;
  int ans = min(abs(x - n), abs(y - n));
  cout << ans << nl;
  return 0;
}
Problem p02719 (C++)

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

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

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

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

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

  ll n, k;
  cin >> n >> k;
  ll mod = n % k;
  ll modEdit = k - mod;
  cout << min(mod, modEdit) << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const ll maxn = 1005;
const ll mod = 1e9 + 7;
const ll inf = 1 << 30;
int main() {
  int n, k;
  cin >> n >> k;
  int x = n % k;
  printf("%d", min(x, abs(x - k)));
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const ll maxn = 1005;
const ll mod = 1e9 + 7;
const ll inf = 1 << 30;
int main() {
  ll n, k;
  scanf("%lld%lld", &n, &k);
  ll x = n % k;
  printf("%lld", min(x, abs(x - k)));
}
Problem p02719 (C++)

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

int main() {
  ll n, k;
  cin >> n >> k;
  if (n == k) {
    cout << 0 << endl;
  } else if (n < k) {
    cout << min(n, k - n) << endl;
  } else {
    bool f = true;
    while (f) {
      n = n - k;
      if (n <= k) {
        f = false;
      }
    }
    cout << min(n, k - n) << endl;
  }
}

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
typedef int ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define F first
#define S second
#define f first
#define s second
#define fore(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define forr(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
ll faster_in() {
  ll r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-')
    return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = ll(1e9 + 7);
const lldb PI = acos(-1);
#define tam 320000
const ll MOD = 1e9 + 7;

int main() {
  ll a, b;
  cin >> a >> b;
  if (a % b == 0)
    cout << 0 << endl;
  else {
    a %= b;
    cout << min(b - a, a) << endl;
  }
}
// PLUS ULTRA!

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define F first
#define S second
#define f first
#define s second
#define fore(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define forr(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
ll faster_in() {
  ll r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-')
    return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = ll(1e9 + 7);
const lldb PI = acos(-1);
#define tam 320000
const ll MOD = 1e9 + 7;

int main() {
  ll a, b;
  cin >> a >> b;
  if (a % b == 0)
    cout << 0 << endl;
  else {
    a %= b;
    cout << min(b - a, a) << endl;
  }
}
// PLUS ULTRA!
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
/* Author - thiolhub */

#include <bits/stdc++.h>

using namespace std;

#define ll long long int
#define ld long double
#define db double
#define pi 3.1415926535897932384626
#define PI acos(-1.0)
#define endl "\n"
#define mod 1000000007
#define mk make_pair
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define sc(a) scanf("%lld", &a);
#define pr(a) printf("%lld\n", a)
#define f(i, a, n) for (ll i = a; i < n; i++)
#define m(a) memset(a, 0, sizeof(a))
#define FastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0), cout.tie(0)

int main() {
  FastIO;

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

  return 0;
}

Fixed submission:
/* Author - thiolhub */

#include <bits/stdc++.h>

using namespace std;

#define ll long long int
#define ld long double
#define db double
#define pi 3.1415926535897932384626
#define PI acos(-1.0)
#define endl "\n"
#define mod 1000000007
#define mk make_pair
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define sc(a) scanf("%lld", &a);
#define pr(a) printf("%lld\n", a)
#define f(i, a, n) for (ll i = a; i < n; i++)
#define m(a) memset(a, 0, sizeof(a))
#define FastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0), cout.tie(0)

int main() {
  FastIO;

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

  return 0;
}
Problem p02719 (C++)

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

typedef long long ll;
typedef long double ld;

int main() {

  ll N, K;
  cin >> N >> K;

  while (1) {
    if (N > abs(N - K)) {
      N = abs(N - K);
    } else {
      break;
    }
  }

  cout << N << endl;

  return 0;
}


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

typedef long long ll;
typedef long double ld;

int main() {

  ll N, K;
  cin >> N >> K;

  N = N - (N / K) * K;

  while (1) {
    if (N > abs(N - K)) {
      N = abs(N - K);
    } else {
      break;
    }
  }

  cout << N << endl;

  return 0;
}
Problem p02719 (C++)

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

using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  while (n > abs(n - k)) {
    n = abs(n - k);
  }
  cout << n << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  n = n % k;
  while (n > abs(n - k)) {
    n = abs(n - k);
  }
  cout << n << endl;
}