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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define um unordered_map
#define F first
#define S second
#define MOD 1000000007
#define TC                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define count_bits(val) __builtin_popcount(val)

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, k;
  cin >> n >> k;
  cout << min(n % k, (k - (n % k))) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define um unordered_map
#define F first
#define S second
#define MOD 1000000007
#define TC                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define count_bits(val) __builtin_popcount(val)

signed main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt" , "r" , stdin);
  // freopen("output.txt" , "w" , stdout);
  // #endif
  int n, k;
  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 N, K;
  cin >> N >> K;
  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (N % K < K - N % K)
    cout << N % K << endl;
  else
    cout << K - N % K << endl;
}


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

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

Buggy submission (status: 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;
  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;
}


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;
  n %= k;
  cout << min(n, k - n);
}

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

  solve();

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: 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 (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;
}

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 (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;
}
Problem p02719 (C++)

Buggy submission (status: 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;
  while (abs(n - k) < n)
    n = abs(n - k);
  cout << n;
  return 0;
}

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;
  n = n - k * (n / k);
  while (abs(n - k) < n)
    n = abs(n - k);
  cout << n;
  return 0;
}
Problem p02719 (C++)

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

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


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

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

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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> ii;
typedef map<int, int> MPII;
typedef set<int> SETI;

const int mxN = 2e5;
const ld pi = 4.0 * atanl(1.0);
const int iinf = 1e9 + 10;
const int inf = 1e18 + iinf + 10;
const int mod = 1000000007;
const ld prec = .000001;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define rall(c) c.end(), c.begin()

ll n, m, t, a[mxN];
vi v;

void fast() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); }

int main() {
  fast();
  cin >> n >> m;
  bool ok = true;
  if (n % m == 0)
    cout << 0, ok = false;
  if (ok) {
    if (abs(n - m) > n)
      cout << n;
    else {
      while (abs(n - m) < n) {
        n = abs(n - m);
      }
      cout << n;
    }
  }

  return 0;
}


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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> ii;
typedef map<int, int> MPII;
typedef set<int> SETI;

const int mxN = 2e5;
const ld pi = 4.0 * atanl(1.0);
const int iinf = 1e9 + 10;
const int inf = 1e18 + iinf + 10;
const int mod = 1000000007;
const ld prec = .000001;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define rall(c) c.end(), c.begin()

ll n, m, t, a[mxN];
vi v;

void fast() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); }

int main() {
  fast();
  cin >> n >> m;
  bool ok = true;
  if (n % m == 0)
    cout << 0, ok = false;
  if (ok) {
    if (abs(n - m) > n)
      cout << n;
    else {
      cout << abs((n % m) - m);
    }
  }

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using mii = map<int, int>;
const int INF = 1e9;
const ll LINF = 1e18;
#define MP(a, b) make_pair((a), (b))
#define MT(...) make_tuple(__VA_ARGS__)

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)

#define ALL(x) (x).begin(), (x).end()
#define IN(type, a)                                                            \
  type a;                                                                      \
  cin >> a
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define COUT(x) cout << (x) << endl
#define DCOUT(x, n)                                                            \
  cout << fixed << setprecision(n) << (x) << endl // 小数をn桁で出力

int main() {
  IN(int, n);
  IN(int, k);
  n %= k;
  COUT(min(n, abs(k - n)));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using mii = map<int, int>;
const int INF = 1e9;
const ll LINF = 1e18;
#define MP(a, b) make_pair((a), (b))
#define MT(...) make_tuple(__VA_ARGS__)

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)

#define ALL(x) (x).begin(), (x).end()
#define IN(type, a)                                                            \
  type a;                                                                      \
  cin >> a
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define COUT(x) cout << (x) << endl
#define DCOUT(x, n)                                                            \
  cout << fixed << setprecision(n) << (x) << endl // 小数をn桁で出力

int main() {
  IN(ll, n);
  IN(ll, k);
  n %= k;
  COUT(min(n, abs(k - n)));
}
Problem p02719 (C++)

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

int main() {
  long long n, m;
  cin >> n >> m;
  if (n % m == 0) {
    cout << 0 << endl;
    return 0;
  }
  long long fff = n % m;
  long long ans = n;
  for (int i = 0; i < n % m; i++) {
    fff = fff - m;
    if (fff < 0)
      fff = fff * -1;
    ans = min(ans, fff);
  }
  cout << ans << endl;
}


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

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

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

using namespace std;

int main(int argc, char *argv[]) {
  int N, K;
  cin >> N >> K;

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

  return 0;
}

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

using namespace std;

int main(int argc, char *argv[]) {
  long long N, K;
  cin >> N >> K;

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

  return 0;
}
Problem p02719 (C++)

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

int abs(int a, int b) {
  if (a < b) {
    return b - a;
  } else {
    return a - b;
  }
}

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

  long long f;
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  while (k < n) {
    n = n - k;
  }
  long long mini = min(n, k - n);
  cout << mini << endl;
}

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

int abs(int a, int b) {
  if (a < b) {
    return b - a;
  } else {
    return a - b;
  }
}

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

  long long f;
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  n %= k;
  long long mini = min(n, k - n);
  cout << mini << endl;
}
Problem p02719 (C++)

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

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

  ll n, k;
  cin >> n >> k;
  set<ll> S;

  if (n % k == 0)
    cout << 0 << "\n";
  else {
    int mi = n;
    while (1) {
      n = min(n, abs(n - k));
      if (S.count(n)) {
        cout << n << "\n";
        break;
      }
      S.insert(n);
    }
  }

  return 0;
}

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

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

  ll n, k;
  cin >> n >> k;
  set<ll> S;

  if (n > k) {
    if (n % k == 0) {
      cout << 0 << "\n";
      return 0;
    }
    n = n % k;
    cout << k - n << "\n";

  } else {
    while (1) {
      n = min(n, abs(n - k));
      if (S.count(n)) {
        cout << n << "\n";
        break;
      }
      S.insert(n);
    }
  }

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

Fixed submission:
#include <algorithm>
#include <array>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {

  long long int n, k;
  cin >> n >> k;
  n = n % k;
  if (abs(n - k) >= n) {
    cout << n << endl;
  } else {
    cout << 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, m;
  cin >> n >> m;
  if (n == m || n % m == 0) {
    cout << '0';
  } else {
    n = n % m;
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      n = abs(n - m);
      v.push_back(n);
    }
    sort(v.begin(), v.end());
    cout << v[0];
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  if (n == m || n % m == 0) {
    cout << '0';
  } else {
    n = n % m;
    vector<long long> v;
    for (long long i = 0; i < 2; i++) {
      n = abs(n - m);
      v.push_back(n);
    }
    sort(v.begin(), v.end());
    cout << v[0];
  }
  return 0;
}
Problem p02719 (C++)

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

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


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

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

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

int main() {
  long long N, N_2, K;
  cin >> N >> K;
  if (N % K == 0) {
    cout << 0 << endl;
    return 0;
  }
  while (true) {
    N_2 = abs(N - K);
    if (N < N_2) {
      cout << N << endl;
      break;
    } else {
      N = N_2;
    }
  }
}

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60; // intじゃ扱えないことに注意！
#define mod 1000000007
#define rep(i, n)                                                              \
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！

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

  ll d = abs(N - K);
  while (d <= N) {
    N = d;
    d = abs(N - K);
  }
  cout << N << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60; // intじゃ扱えないことに注意！
#define mod 1000000007
#define rep(i, n)                                                              \
  for (int i = 0; i < (int)(n); i++) // 範囲外参照とループの初期化に注意！

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

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

Buggy submission (status: Time Limit Exceeded):

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

int main() {
  ll n, m, x, y, z, q;
  cin >> n >> m;
  if (n == m || n % m == 0)
    cout << n % m;
  else if (n > m) {
    while (n > m)
      n -= m;
    x = abs(n - m), y = abs(x - m), z = abs(y - m);
    cout << min(min(x, y), z);
  } else {
    x = abs(n - m), y = abs(x - m), z = abs(y - m);
    cout << min(min(x, y), z);
  }
  return 0;
}

Fixed submission:

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

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0;
  else if (abs(n % k - k) <= abs(n % k))
    cout << abs(n % k - k);
  else
    cout << (abs(n % k));
}


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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long int n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0;
  else if (abs(n % k - k) <= abs(n % k))
    cout << abs(n % k - k);
  else
    cout << (abs(n % k));
}
Problem p02719 (C++)

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

int main(void) {
  long long n, k;
  cin >> n >> k;
  long long min = n;
  long long tmp = 0;
  while (true) {
    tmp = abs(min - k);
    if (min > tmp)
      min = tmp;
    else
      break;
  }
  cout << min << endl;
}


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

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

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

int main() {
  ll m, n;
  cin >> n >> m;
  ll t = n;
  // cout << n%m;
  n = n % m;
  m = m % t;
  set<ll> st;
  while (max(n, m)) {
    if (st.find(n) != st.end())
      break;
    st.insert(n);
    n = abs(n - m);
  }
  ll min = 1000000000000000000;
  for (auto it : st)
    if (it < min)
      min = it;
  cout << min;
}

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

int main() {
  ll m, n;
  cin >> n >> m;
  ll a = n / m;
  ll b = a + 1;
  cout << min(abs(n - (a * m)), abs(n - (b * m)));
}
Problem p02719 (C++)

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

int main() {
  int64_t N, K;
  cin >> N >> K;
  if (N % K == 0)
    N = 0;
  while (N > abs(N - K)) {
    N = abs(N - K);
  }
  cout << N << endl;
}


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

int main() {
  int64_t 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: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int INF = 1e9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;
  n -= (n / k) * k;
  cout << min(n, k - n) << '\n';
  return 0;
}

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

typedef long long ll;
const int INF = 1e9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k;
  cin >> n >> k;
  n -= (n / k) * k;
  cout << min(n, k - n) << '\n';
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }

int main() {
  long long int N;
  long long int K;
  cin >> N >> K;
  long long int res = 1000000000000000001;
  while (N < res) {
    res = N;
    N = labs(N - K);
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }

int main() {
  long long int N;
  long long int K;
  cin >> N >> K;
  long long int res = 1000000000000000001;
  if (N > K)
    N = N % K + K;
  while (N < res) {
    res = N;
    N = labs(N - K);
  }
  cout << res << endl;
}
Problem p02719 (C++)

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

int main() {
  ll N, K;
  cin >> N >> K;
  ll x = min(K, abs(N - K));
  ll y = max(K, abs(N - K));
  cout << y % x << endl;
}


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

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

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

int main() {
  long long n, k, m, temp;
  int cnt = 3;
  cin >> n >> k;
  m = n;
  if (m > 0) {
    while (1) {
      temp = n - k;
      if (temp < 0) {
        --cnt;
        if (cnt < 0) {
          break;
        }
        m = min(m, abs(temp));
      }
    }
  }
  cout << m << endl;
  return 0;
}


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

int main() {
  long long n, k, m, temp;
  int cnt = 3;
  cin >> n >> k;
  m = n;
  if (m > 0) {
    if (n > k) {
      n = n % k;
      m = min(m, n);
    }
    for (int i = 0; i < 2; ++i) {
      n = abs(n - k);
      m = min(m, n);
    }
  }
  cout << m << endl;
  return 0;
}
Problem p02719 (C++)

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

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


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

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

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

int main(void) {
  long n, k;
  long b, a, temp = 0;
  cin >> n >> k;
  b = n;
  while (1) {
    a = b - k;
    // cout<<a<< " " << b << endl;
    if (a < 0) {
      break;
    }
    b = a;
  }
  // cout << a << " "<< b<<endl;
  a = a * -1;
  temp = min(a, b);
  cout << temp << "\n";
  return 0;
}

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

int main(void) {
  long n, k;
  long b, a, temp = 0;
  cin >> n >> k;
  temp = n / k;
  b = n - k * temp;
  a = b - k;
  // cout << a << " "<< b<<endl;
  a = a * -1;
  temp = min(a, b);
  cout << temp << "\n";
  return 0;
}
Problem p02719 (C++)

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

// 素数判定
bool isPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}

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

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

  ll ans = n;
  bool flag = false; // 処理終了フラグ
  for (;;) {
    ll tmp = abs(ans - k);
    if (ans < tmp)
      break;
    ans = tmp;
  }
  cout << ans << endl;
}

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

// 素数判定
bool isPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}

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

  n = n % k;
  ll ans = n;
  bool flag = false; // 処理終了フラグ
  for (;;) {
    ll tmp = abs(ans - k);
    if (ans < tmp)
      break;
    ans = tmp;
  }
  cout << ans << endl;
}
Problem p02719 (C++)

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

long long m, n, y, x;

int main() {
  scanf("%lld%lld", &m, &n);
  x %= y;
  printf("%lld", min(y - x, x));

  return 0;
}

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

long long m, n, y, x;

int main() {
  scanf("%lld%lld", &x, &y);

  if (y == 0)
    printf("%lld", x);

  else {
    x %= y;
    printf("%lld", min(y - x, x));
  }

  return 0;
}
Problem p02719 (C++)

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

void printVec(std::vector<int> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

int main() {
  ll n, k;

  cin >> n >> k;
  ll min = n;
  ll diff = abs(n - k);
  if (diff > min) {
    cout << min << endl;
  } else {
    min = diff;
    bool flag = true;
    while (flag) {
      diff = abs(diff - k);
      if (diff > min) {
        cout << min << endl;
        break;
      } else {
        min = diff;
      }
    }
  }
  return 0;
}

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

void printVec(std::vector<int> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

int main() {
  ll n, k;

  cin >> n >> k;
  ll min = n;
  ll diff = abs(n - k);
  if (diff > min) {
    cout << min << endl;
  } else {
    min = diff;
    bool flag = true;
    ll div = n / k;
    // cout << "diff = " << diff << endl;
    // cout << "div =  " << div << endl;
    diff = abs(n - (div * k));
    if (diff > abs(diff - k)) {
      cout << abs(diff - k) << endl;
    } else {
      cout << diff << endl;
    }
  }
  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++)
using namespace std;
using ll = long long;

int main() {

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


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

int main() {

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

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

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

  return 0;
}

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

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

  return 0;
}
Problem p02719 (C++)

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long

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

  if (n % k == 0)
    cout << 0;
  else {
    set<int> s;
    s.insert(abs(n - k));
    n = abs(n - k);
    while (1) {
      n = abs(n - k);
      if (s.find(n) != s.end()) {
        cout << min(n, abs(n - k));
        break;
      } else
        s.insert(n);
    }
  }
  return 0;
}

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long

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

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

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define endl '\n'

using namespace std;

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

  long long n, k;

  cin >> n >> k;

  cout << n % (abs(n - k)) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define endl '\n'

using namespace std;

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

  long long n, k;

  cin >> n >> k;

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

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
/******************************************
 * Author : sinon.su96@gmail.com
 * Or : ahafid96@gmail.com
 * Username : SINON
 * Date : April 2020
 * Civil and Structural engineering student
 * at Ecole des Ponts ParisTech
 * ***************************************/

// Import usefull library :
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

// usefull abreviation
#define fi first
#define se second
#define pb push_back
#define sqr(x) (x) * (x)
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef vector<int> vi;

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


Fixed submission:
/******************************************
 * Author : sinon.su96@gmail.com
 * Or : ahafid96@gmail.com
 * Username : SINON
 * Date : April 2020
 * Civil and Structural engineering student
 * at Ecole des Ponts ParisTech
 * ***************************************/

// Import usefull library :
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

// usefull abreviation
#define fi first
#define se second
#define pb push_back
#define sqr(x) (x) * (x)
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef vector<int> vi;

int main() {
  long long n, k;
  long long x;
  cin >> n >> k;
  x = n % k;
  while (abs(x - k) < x) {
    x = abs(x - k);
  }
  cout << x << "\n";
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>

using namespace std;

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


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long option1, option2;
  option1 = n - n / k * k;
  option2 = abs(option1 - k);
  if (option1 > option2)
    cout << option2 << endl;
  else
    cout << option1 << endl;

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef complex<int> ci;
typedef complex<double> cd;
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define pf push_front
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ge(a, b) get<(b)>((a))
#define SZ(x) (x).size()
#define ft front()
#define bk back()
#define ins insert
#define endl '\n'
#define FOR(a, b, c) for (auto(a) = (b); (a) < (c); ++(a))
#define ROF(a, b, c) for (auto(a) = (b); (a) > (c); --(a))
#define F0R(a, b) FOR((a), 0, (b))
#define R0F(a, b) ROF((a), (b), -1)
#define FORI(a, b) for (auto(a) = (b).begin(); (a) != (b).end(); ++(a))
#define ROFI(a, b) for (auto(a) = (b).rbegin(); (a) != (b).rend(); ++(a))
#define CEIL(a, b) ((a) + (b)-1) / (b)
const int xd[4] = {0, 1, 0, -1}, yd[4] = {1, 0, -1, 0};
template <typename t> bool ckmin(t &a, const t &b) {
  return a > b ? a = b, true : false;
}
template <typename t> bool ckmax(t &a, const t &b) {
  return a < b ? a = b, true : false;
}

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

  ll n, k;
  cin >> n >> k;
  vector<ll> sols;
  sols.pb(n);
  if (n > k) {
    n -= (n - k) / k * k;
    sols.pb(n);
    n -= k;
  }
  assert(n < k);
  sols.pb(n);
  sols.pb(k - n);

  cout << *min_element(ALL(sols)) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef complex<int> ci;
typedef complex<double> cd;
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define pf push_front
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ge(a, b) get<(b)>((a))
#define SZ(x) (x).size()
#define ft front()
#define bk back()
#define ins insert
#define endl '\n'
#define FOR(a, b, c) for (auto(a) = (b); (a) < (c); ++(a))
#define ROF(a, b, c) for (auto(a) = (b); (a) > (c); --(a))
#define F0R(a, b) FOR((a), 0, (b))
#define R0F(a, b) ROF((a), (b), -1)
#define FORI(a, b) for (auto(a) = (b).begin(); (a) != (b).end(); ++(a))
#define ROFI(a, b) for (auto(a) = (b).rbegin(); (a) != (b).rend(); ++(a))
#define CEIL(a, b) ((a) + (b)-1) / (b)
const int xd[4] = {0, 1, 0, -1}, yd[4] = {1, 0, -1, 0};
template <typename t> bool ckmin(t &a, const t &b) {
  return a > b ? a = b, true : false;
}
template <typename t> bool ckmax(t &a, const t &b) {
  return a < b ? a = b, true : false;
}

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

  ll n, k;
  cin >> n >> k;
  vector<ll> sols;
  sols.pb(n);
  if (n >= k) {
    n -= (n - k) / k * k;
    sols.pb(n);
    n -= k;
  }
  assert(n < k);
  sols.pb(n);
  sols.pb(k - n);

  cout << *min_element(ALL(sols)) << endl;

  return 0;
}
Problem p02719 (C++)

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

#define pb push_back
#define ff first
#define ss second
#define N 200005
#define mod 1000000007
#define fo(n) for (int i = 1; i <= n; i++)
typedef pair<int, int> pii;
typedef long long int ll;
typedef long int li;
int mod1 = 998244353;

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

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

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

#define pb push_back
#define ff first
#define ss second
#define N 200005
#define mod 1000000007
#define fo(n) for (int i = 1; i <= n; i++)
typedef pair<int, int> pii;
typedef long long int ll;
typedef long int li;
int mod1 = 998244353;

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

  ll n, k;
  cin >> n >> k;
  n = 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;

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

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

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

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

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

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

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

  if (N % K < K - (N % K)) {
    cout << N % K << endl;
  }
}
Problem p02719 (C++)

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

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

Fixed submission:
#include <cmath>
#include <iostream>
typedef long long ll;
using namespace std;

int main(void) {
  ll N, K;
  cin >> N >> K;
  if (N >= K) {
    N = N % K;
  }
  while (true) {
    ll N_tmp = abs(N - K);
    if (N_tmp > N) {
      cout << N << endl;
      return 0;
    }
    N = N_tmp;
  }
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
typedef long double ld;
#define PI 3.14159265358979323846

#define REP(i, s, n) for (ll i = s; i < (n); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = N % (abs(N - K));
  while (true) {
    if (abs(ans - K) < ans) {
      ans = abs(ans - K);
    } else {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
typedef long double ld;
#define PI 3.14159265358979323846

#define REP(i, s, n) for (ll i = s; i < (n); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = N % (K);
  while (true) {
    if (abs(ans - K) < ans) {
      ans = abs(ans - K);
    } else {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
N, K = map(int, input().split())

while abs(N - K) < N:
    N = abs(N - K)

print(N)


Fixed submission:
N, K = map(int, input().split())

N %= K

while abs(N - K) < N:
    N = abs(N - K)

print(N)
Problem p02719 (Python)

Buggy submission (status: Runtime Error):
import sys

# 未解決(最後だけREになる)


def get():
    return list(map(int, input().split()))


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - a))
        sys.exit(0)
    else:
        print(b)
        sys.exit(0)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
        sys.exit(0)
    else:
        if abs((a[0] % a[1]) - a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
            if abs(a[0] % a[1]) < abs((a[0] % a[1]) - a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs((a[0] % a[1]) - a[1]))
                sys.exit(0)
        else:
            if abs(a[0] % a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs(((a[0] % a[1]) - a[1]) % a[1]))
                sys.exit(0)


Fixed submission:
import sys

# 未解決(最後だけREになる)


def get():
    return list(map(int, input().split()))


a = get()
b = a[0]
c = a[1]
if b < c:
    if abs(b - c) < b:
        print(abs(b - c))
        sys.exit(0)
    else:
        print(b)
        sys.exit(0)
else:
    if a[0] % a[1] == a[1] or a[0] % a[1] == 0:
        print(0)
        sys.exit(0)
    else:
        if abs((a[0] % a[1]) - a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
            if abs(a[0] % a[1]) < abs((a[0] % a[1]) - a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs((a[0] % a[1]) - a[1]))
                sys.exit(0)
        else:
            if abs(a[0] % a[1]) < abs(((a[0] % a[1]) - a[1]) % a[1]):
                print(abs(a[0] % a[1]))
                sys.exit(0)
            else:
                print(abs(((a[0] % a[1]) - a[1]) % a[1]))
                sys.exit(0)
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())

r = []
if n % k == 0:
    print(0)
    exit()
while True:
    n = abs(n - k)
    r.append(n)
    if r.count(n) >= 2:
        break

print(min(r))


Fixed submission:
n, k = map(int, input().split())

r = []
n = n % k
if n == 0:
    print(0)
    exit()
while True:
    n = abs(n - k)
    r.append(n)
    if r.count(n) >= 2:
        break

print(min(r))
Problem p02719 (Python)

Buggy submission (status: Runtime Error):
n, k = map(int, input().split())

print(min(n % k), (k - (n % k)))


Fixed submission:
n, k = map(int, input().split())

print(min((n % k), (k - (n % k))))
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
def resolve():
    N, K = list(map(int, input().split()))
    if K == 1:
        print(0)
        return
    if N % K == 0:
        print(0)
        return
    x = N
    memo = [N]
    while True:
        x = min(abs(x - K), x)
        if x in memo:
            print(min(memo))
            return
        memo.append(x)
    return


resolve()


Fixed submission:
def resolve():
    N, K = list(map(int, input().split()))
    print(min(N % K, K - (N % K)))
    return


resolve()
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
list_input = [int(n) for n in input().split()]

N = list_input[0]
K = list_input[1]

while N > abs(N - K):
    N = abs(N - K)

print(N)


Fixed submission:
list_input = [int(n) for n in input().split()]

N = list_input[0]
K = list_input[1]

while N > abs(N - K):
    if N >= K:
        N = N % K
    else:
        N = K - N
print(N)
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

a = [int(i) for i in input().split()]
N = a[0]
K = a[1]

if K == 1:
    print(0)
    sys.exit()


def resZ(i, K):
    return abs(i - K)


while True:
    ab = resZ(N, K)
    # print(ab)
    if N < ab:
        print(N)
        # print('end')
        sys.exit()

    N = ab
# def


Fixed submission:
import sys

a = [int(i) for i in input().split()]
N = a[0]
K = a[1]

if K == 1:
    print(0)
    sys.exit()


def resZ(i, K):
    return abs(i - K)


ab = resZ(N, K)
# print(ab)
if N < ab:
    print(N)
    # print('end')
    sys.exit()

N = N % K
while True:
    ab = resZ(N, K)
    # print(ab)
    if N < ab:
        print(N)
        # print('end')
        sys.exit()

    N = ab
# def
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = list(map(int, input().split()))

n_prev = n

while True:
    n = abs(n - k)
    if n > n_prev:
        print(n_prev)
        exit()
    else:
        n_prev = n


Fixed submission:
n, k = list(map(int, input().split()))

print(min(n % k, abs(k - n % k)))
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
N, K = map(int, input().split())

tmp1 = abs(N - K)

while True:
    if K == 1:
        print(0)
        break
    tmp2 = abs(tmp1 - K)
    if tmp1 < tmp2:
        print(tmp1)
        break
    else:
        tmp1 = tmp2


Fixed submission:
N, K = map(int, input().split())
print(min(N % K, K - (N % K)))
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
N, K = map(int, input().split())
Nj = N
while True:
    Ni = Nj
    Nj = abs(Ni - K)
    if Ni > N:
        print(N)
        break
    elif Nj > Ni:
        print(Ni)
        break
    elif K == 1 or Nj == K:
        print(0)
        break
    else:
        continue


Fixed submission:
n, k = map(int, input().split())
if n % k == 0:
    print(0)
else:
    l = [n]
    p = n // k
    c = n - (p * k)
    half = k // 2
    while c > half:
        c = abs(c - k)
    l.append(c)
    print(min(l))
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
#!python3

# input
N, K = list(map(int, input().split()))


def main():
    x = N
    while True:
        y = max(x - K, K - x)
        if y < x:
            x = y
        else:
            break
    print(x)


if __name__ == "__main__":
    main()


Fixed submission:
#!python3

# input
N, K = list(map(int, input().split()))


def main():
    s = N % K
    ans = min(s, K - s)
    print(ans)


if __name__ == "__main__":
    main()
Problem p02719 (Python)

Buggy submission (status: Runtime Error):
N, K = list(map(int, input().split()))

if N >= K:
    print(N % K)
else:
    print(K % N)


Fixed submission:
N, K = list(map(int, input().split()))

print(min(N % K, K - (N % K)))
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

N, K = map(int, input().split())
first = N
list_a = []

if N % K == 0:
    print(0)
    sys.exit()
else:
    while N > 0:
        N = N - K

second = N + K

N = abs(N)

list_a.append(N)
list_a.append(second)
list_a.append(first)

list_a = sorted(list_a)

print(list_a[0])


Fixed submission:
import sys

N, K = map(int, input().split())
first = N
list_a = []

if N % K == 0:
    print(0)
    sys.exit()
else:
    X = N // K
    N = N - (K * (X + 1))

second = N + K

N = abs(N)

list_a.append(N)
list_a.append(second)
list_a.append(first)

list_a = sorted(list_a)

print(list_a[0])
Problem p02719 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

N, K = map(int, input().split())

if K == 1:
    print(0)
    sys.exit()

current = N

while True:
    prev = current
    current = abs(current - K)
    next = abs(current - K)
    if prev == next:
        print(min(current, next))
        break


Fixed submission:
import sys

N, K = map(int, input().split())

if K == 1:
    print(0)
    sys.exit()

a = abs(N - K * (N // K + 1))
print(min(a, abs(a - K)))
Problem p02719 (C++)

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
int main() {
  ull n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << endl;
  else {
    n = n - ((n / k) * k);
    while (abs((ll)(n - k)) <= n && n != 0) {
      n = abs((ll)(n - k));
    }
    cout << n << endl;
  }
}