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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define sz(a) int((a).size())
#define int long long
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define bitcnt(n) __builtin_popcountll(n)
#define tr(container, s)                                                       \
  for (auto x : container) {                                                   \
    cout << x << s;                                                            \
  }                                                                            \
  cout << endl;
#define one(x) cerr << #x << " : " << x << endl;
#define two(x, y) cerr << #x << " : " << x << " , " << #y << " : " << y << endl;
#define three(x, y, z)                                                         \
  cerr << #x << " : " << x << " , " << #y << " : " << y << " , " << #z         \
       << " : " << z << endl;
#define PI acos(-1)
const int M = 1000000007;
const int N = 3e5 + 5;
signed main() {
  int tc, m, i, j;
  int n, k;
  cin >> n >> k;
  if (n % j == 0) {
    cout << 0 << endl;
  } else {
    int x = n / k;
    int y = x + 1;
    cout << min(abs(x * k - n), abs(k * y - n)) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sz(a) int((a).size())
#define int long long
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define bitcnt(n) __builtin_popcountll(n)
#define tr(container, s)                                                       \
  for (auto x : container) {                                                   \
    cout << x << s;                                                            \
  }                                                                            \
  cout << endl;
#define one(x) cerr << #x << " : " << x << endl;
#define two(x, y) cerr << #x << " : " << x << " , " << #y << " : " << y << endl;
#define three(x, y, z)                                                         \
  cerr << #x << " : " << x << " , " << #y << " : " << y << " , " << #z         \
       << " : " << z << endl;
#define PI acos(-1)
const int M = 1000000007;
const int N = 3e5 + 5;
signed main() {
  int tc, m, i, j;
  int n, k;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0 << endl;
  } else {
    int x = n / k;
    int y = x + 1;
    cout << min(abs(x * k - n), abs(k * y - n)) << endl;
  }
}
Problem p02719 (C++)

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, m;
int main() {
  cin >> n >> m;
  if (n % m == 0)
    cout << "0";
  else if (n < m) {
    if (n < m - n)
      cout << n;
    if (n >= m - n)
      cout << m - n;
  } else {
    n %= m;
    if (n > m - n) {
      n = m - n;
      // cout<<abs(n-m)<<endl;
    }
    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 int n, k;
  scanf("%lli%lli", &n, &k);
  if (n % k == 0) {
    printf("0\n");
  } else {
    long long int mini;
    mini = n;
    while (1) {
      n = abs(n - k);
      if (n < mini) {
        mini = n;
      } else if (n == mini) {
        printf("%lli", mini);
        break;
      }
    }
  }
}


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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll i, j, k, l, m, n, o, p, q, r, s, t, x, a[500], tt = 0;
  cin >> n >> m;
  p = (n % m);
  q = m % n;
  r = m - p;
  s = n - q;
  cout << min({p, q, r, s}) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll i, j, k, l, m, n, o, p, q, r, s, t, x, a[500], tt = 0;
  cin >> n >> m;
  p = (n % m);
  q = m % n;
  r = m - p;
  s = n - q;
  cout << min({p, r}) << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
const lint mod = 1e9 + 7;

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

  if (k == 1) {
    cout << 0 << "\n";
    return 0;
  }
  lint x = n;
  while (x > abs(x - k)) {
    x = abs(x - k);
  }

  cout << x << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
const lint mod = 1e9 + 7;

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

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

  cout << x << "\n";
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

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

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

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

  return 0;
}


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

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

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

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

  return 0;
}
Problem p02719 (C++)

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


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

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

using namespace std;

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

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

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

  return 0;
}
Problem p02719 (C++)

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

int main() {
  long long N, K;
  cin >> N >> K;
  if (K == 1) {
    N = 0;
  }
  while (true) {
    if (2 * N < K) {
      break;
    }
    if (N < K) {
      N = K - N;
      continue;
    }
    N -= K;
  }
  cout << N << endl;
}

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

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

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

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


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

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int testcase = 1;
  // cin>>testcase;
  for (int p = 0; p < testcase; p++) {
    ll n = 0, k = 0;
    cin >> n >> k;
    cout << min((n) % (abs(n - k)), (n % k));
  }

  return 0;
}


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int testcase = 1;
  // cin>>testcase;
  for (int p = 0; p < testcase; p++) {
    ll n = 0, k = 0;
    cin >> n >> k;
    n = n % k;
    cout << min(n, abs(n - k));
  }

  return 0;
}
Problem p02719 (C++)

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

#define debug(x) cerr << #x << ": " << x << "\n"
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define _sort(a) sort(a.begin(), a.end())

using namespace std;

using ll = long long;
using vi = vector<int>;
using vii = vector<int, int>;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  long long int n, k, d, mn = 1e18;
  cin >> n >> k;
  mn = n;
  if (k == 1)
    return cout << 0, 0;
  while (true) {
    d = abs(n - k);
    if (d < mn)
      mn = d;
    else
      break;
    n = mn;
  }
  cout << mn;
}
/*
 *   created: 2020-04-04 17:24:49
 */

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

#define debug(x) cerr << #x << ": " << x << "\n"
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define _sort(a) sort(a.begin(), a.end())

using namespace std;

using ll = long long;
using vi = vector<int>;
using vii = vector<int, int>;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  long long int n, k, d, mn = 1e18;
  cin >> n >> k;
  mn = n = n % k;
  while (true) {
    d = abs(n - k);
    if (d < mn)
      mn = d;
    else
      break;
    n = mn;
  }
  cout << mn;
}
/*
 *   created: 2020-04-04 17:24:49
 */
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
// Hail god Yato

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

#define add(x) accumulate(x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define hs                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//
void solve() {
  ll n, k;
  cin >> n >> k;
  while (true) {
    if (abs(n - k) < n)
      n = abs(n - k);
    else
      break;
  }
  cout << n;
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}


Fixed submission:
// Hail god Yato

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

#define add(x) accumulate(x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define hs                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//
void solve() {
  ll n, k;
  cin >> n >> k;
  ll div = n % k;
  cout << min(div, k - div);
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}
Problem p02719 (C++)

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

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

  while (true) {
    ll tmp = n;
    if (abs(n - k) < n)
      n = abs(n - k);
    if (tmp == n)
      break;
  }

  cout << n << endl;

  return 0;
}


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

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

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

  while (true) {
    ll tmp = n;
    if (abs(n - k) < n)
      n = abs(n - k);
    if (tmp == n)
      break;
  }

  cout << n << endl;

  return 0;
}
Problem p02719 (C++)

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

int main() {
  int N;
  int K;
  int min;

  cin >> N >> K;
  min = N % K;
  if (min > K - min) {
    min = K - min;
  }

  cout << min << endl;
}


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

int main() {
  long N;
  long K;
  long min;

  cin >> N >> K;
  min = N % K;
  if (min > K - min) {
    min = K - min;
  }

  cout << min << 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, K;
  cin >> N >> K;
  ll ans = N;
  while (true) {
    if (abs(ans - K) >= ans) {
      break;
    }
    ans = abs(ans - K);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll N, K;
  cin >> N >> K;
  ll ans;
  ans = min(N % K, abs(N % K - K));
  cout << ans << endl;
}
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) << endl;
}

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

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

using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#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 mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n]
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  // FIO;
  c_p_c();
  int n, k;
  cin >> n >> k;
  cout << min(n % k, k - (n % k)) << endl;
}

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

using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#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 mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n]
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int32_t main() {
  FIO;
  // c_p_c();
  int n, k;
  cin >> n >> k;
  cout << min(n % k, k - (n % k)) << endl;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
typedef int64_t Int;

using namespace std;
using Graph = vector<vector<Int>>;

const double pi = M_PI;
const Int MOD = 1000000007;

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

  Int ans = 0;
  if (n < k) {
    ans = n;
  } else {
    ans = n % (n - k);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
typedef int64_t Int;

using namespace std;
using Graph = vector<vector<Int>>;

const double pi = M_PI;
const Int MOD = 1000000007;

int main() {
  Int n, k;
  cin >> n >> k;
  n = n - (n / k * k);
  Int ans = min(n, abs(n - k));

  cout << ans << 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) << endl;
}

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

int main() {
  long long int 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;
#define int long long
const int INF = 1e16;

signed main() {
  int N, K;
  cin >> N >> K;
  int ans;
  if (K == 1)
    ans = 0;
  if (N > K) {
    while (N > K) {
      N -= K;
      ans = min(N, K - N);
    }
  }
  if (N < K)
    ans = min(N, K - N);
  if (N == K)
    ans = 0;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e16;

signed main() {
  int N, K;
  cin >> N >> K;
  int ans;
  if (K == 1)
    ans = 0;
  if (N > K) {
    int x = N / K;
    N -= x * K;
    ans = min(N, K - N);
  }
  if (N < K)
    ans = min(N, K - N);
  if (N == K)
    ans = 0;
  cout << ans << endl;
}
Problem p02719 (C++)

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

int main() {
  long int N, K;

  cin >> N >> K;

  long int num1, num2;

  if (N > K) {
    if (N % K == 0) {
      num1 = 0;
      num2 = 0;
    } else {
      while (N > K) {
        N = N - K;
      }
      num1 = N;
      N = K - N;
      num2 = N;
    }
  } else if (N < K) {
    num1 = N;
    N = K - N;
    num2 = N;
  } else {
    num1 = N;
    num2 = N - K;
  }

  if (num1 <= num2) {
    cout << num1 << endl;
  } else {
    cout << num2 << endl;
  }
}


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

int main() {
  long int N, K;

  cin >> N >> K;

  long int num1, num2;

  if (N > K) {
    num1 = N % K;
    num2 = K - num1;
  } else if (N < K) {
    num1 = N;
    N = K - N;
    num2 = N;
  } else {
    num1 = N;
    num2 = N - K;
  }

  if (num1 <= num2) {
    cout << num1 << endl;
  } else {
    cout << num2 << endl;
  }
}
Problem p02719 (C++)

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

int main() {
  long long int a, k, q = -1;
  for (long long int i = 1; i <= 1000000000000000000; i++) {
    a = abs(a - k);
    q = min(q, a);
  }
  cout << q << endl;
}

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

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

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  long long int n, t, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << "0" << endl;
  else {
    do {
      t = n;
      n = n - k;
      if (n < 0)
        n = -n;
    } while (n < t);
    cout << t << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long long int n, t, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << "0" << endl;
  else {
    n = n % k;
    do {
      t = n;
      n = n - k;
      if (n < 0)
        n = -n;
    } while (n < t);
    cout << t << endl;
  }
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int n, k, a;

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

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

long long n, k, a;

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

ll GCD(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }

  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

ll LCM(ll a, ll b) {
  ll g = GCD(a, b);
  return a * b / g;
}

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

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

  ll ans = GCD(N, K);

  if (ans == K)
    ans = 0;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

ll GCD(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }

  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

ll LCM(ll a, ll b) {
  ll g = GCD(a, b);
  return a * b / g;
}

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

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

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, k, x, y, f;

  cin >> n >> k;

  f = (n / k) + 1;

  n = n - (k * f);

  x = -n;

  y = n + k;

  if (x < y) {

    cout << x;

  } else {

    cout << y;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long long int n, k, x, y, f;

  cin >> n >> k;

  f = (n / k) + 1;

  n = n - (k * f);

  x = -n;

  y = n + k;

  if (x < y) {

    cout << x;

  } else {

    cout << y;
  }
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int x, y;
  cin >> x >> y;
  while (x % y == 0) {
    x /= y;
  }
  return x % y == 1;
}


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

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

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

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

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

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

using namespace std;

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


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

using namespace std;

int main() {
  long long 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>
#include <math.h>
using namespace std;

int main() {
  // sengen
  int n, k;

  // nyuryoku
  cin >> n >> k;
  // keisan
  if (n % k == abs(n % k - k)) {
    cout << n % k;
  } else {
    cout << min(n % k, abs(n % k - k));
  }
}

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

int main() {
  // sengen
  long n, k;

  // nyuryoku
  cin >> n >> k;
  // keisan
  if (n % k == abs(n % k - k)) {
    cout << n % k;
  } else {
    cout << min(n % k, abs(n % k - k));
  }
}
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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

  N = N % K;
  ll MIN = N;
  REP(i, K) {
    N = K - N;
    MIN = min(N, MIN);
  }

  cout << MIN << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

  N = N % K;
  ll MIN = N;
  REP(i, 5) {
    N = K - N;
    MIN = min(N, MIN);
  }

  cout << MIN << endl;
}
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() {
  long long 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;

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

  cout << n << endl;
}

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

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

  n = n % k;

  n = min(n, k - n);

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

Buggy submission (status: Time Limit Exceeded):
//...Bismillahir Rahmanir Rahim...
// Code by    Asad Bin Saber
// 04 04 2020

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

void task() {
  long long n, k, i;

  cin >> n >> k;

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

  cout << n << endl;
}

int main() {
  task();

  return 0;
}


Fixed submission:
//...Bismillahir Rahmanir Rahim...
// Code by    Asad Bin Saber
// 04 04 2020

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

void task() {
  long long n, k, i;

  cin >> n >> k;

  n = n % k;

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

  cout << n << endl;
}

int main() {
  task();

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <vector>
typedef long long int ll;
using namespace std;

int main() {
  ll N, M, a, b;
  cin >> N >> M;
  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  while (1) {
    a = abs(N - M);
    if (N < a) {
      cout << N << endl;
      return 0;
    } else {
      N = a;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <vector>
typedef long long int ll;
using namespace std;

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

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

int main() {
  ll N, K;
  cin >> N >> K;
  if (K == 0) {
    cout << N << endl;
  } else if (K == 1) {
    cout << 0 << endl;
  } else {
    ll ab = abs(N - K);
    while (ab < N) {
      N = ab;
      ab = abs(N - K);
    }
    cout << N << endl;
  }
}

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

int main() {
  ll N, K;
  cin >> N >> K;
  if (K == 0) {
    cout << N << endl;
  } else if (K == 1) {
    cout << 0 << endl;
  } else if (N % K == 0) {
    cout << 0 << endl;
  } else if (N > K) {
    ll a = N % K;
    if (abs(a - K) < abs(a)) {
      cout << abs(a - K) << endl;
    } else {
      cout << abs(a);
    }
  } else {
    ll ab = abs(N - K);
    while (ab < N) {
      N = ab;
      ab = abs(N - K);
    }
    cout << N << endl;
  }
}
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));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  cout << min(n % k, k - (n % k));
  return 0;
}
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 <cmath>
#include <iostream>
using namespace std;

int main() {
  int x, k, min;
  cin >> x >> k;
  int a = x % k;
  min = fabs(a - k);
  if (min > a)
    min = a;
  cout << min;
}


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

int main() {
  unsigned long int x, k, min, a;
  cin >> x;
  cin >> k;
  a = x % k;
  min = k - a;
  if (min > a)
    min = a;
  cout << min;
}
Problem p02719 (C++)

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

    cout << a;
    return 0;
  }
}


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

    cout << a;
    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;
  int X;
  X = N % K;

  X = K % X;
  cout << X << endl;
}


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

int main() {
  long long int 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;
long long n, k;

int main(void) {
  long long ans, t;
  scanf("%lld%lld", &n, &k);
  if (n % k == 0) {
    cout << "0";
    return 0;
  }
  ans = min(n, k);
  t = n / k;
  t++;
  for (int i = 0; i < t; i++) {
    n = abs(n - k);
    ans = min(ans, n);
  }
  cout << ans;
}

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

int main(void) {
  long long ans, t;
  scanf("%lld%lld", &n, &k);
  if (n % k == 0) {
    cout << "0";
    return 0;
  }
  if (n > k)
    n %= k;
  n = min(n, abs(n - k));
  cout << n;
  return 0;
}
Problem p02719 (C++)

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

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

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

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

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

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

  ll n, k;
  cin >> n >> k;
  ll rem = n % k;
  cout << min(rem, abs(rem - k));
}
Problem p02719 (C++)

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

int N, K, mod, n, a, b;

int main() {
  cin >> N >> K;
  mod = N % K;
  n = (N - mod) / K;
  a = abs(N - n * K);
  b = abs(N - (n + 1) * K);
  if (a < b) {
    cout << a;
  } else {
    cout << b;
  }
}

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

long long int N, K, mod, n, a, b;

int main() {
  cin >> N >> K;
  mod = N % K;
  n = (N - mod) / K;
  a = abs(N - n * K);
  b = abs(N - (n + 1) * K);
  if (a < b) {
    cout << a;
  } else {
    cout << b;
  }
}
Problem p02719 (C++)

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

int main() {
  long long n, k, sol;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0 << '\n';
  } else {
    sol = abs(n - k);
    while (true) {
      if (abs(sol - k) < sol) {
        sol = abs(sol - k);
        continue;
      }
      break;
    }
    cout << sol << '\n';
  }
}


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

int main() {
  long long n, k, sol;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0 << '\n';
  } else {
    if (n > k) {
      n -= n / k * k;
    }
    sol = abs(n - k);
    while (true) {
      if (abs(sol - k) < sol) {
        sol = abs(sol - k);
        continue;
      }
      break;
    }
    cout << sol << '\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;
  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;
  long long a = n % k;
  long long ans = min(a, k - a);
  cout << ans << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
using namespace std;
int main() {

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

  while (true) {
    N = N - K;
    if (N <= 0) {
      if (0 - N < N + K) {
        cout << 0 - N << endl;
        break;
      } else {
        cout << N + K << endl;
        break;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
using namespace std;
int main() {

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

  while (true) {
    N = N % K - K;
    if (N <= 0) {
      if (0 - N < N + K) {
        cout << 0 - N << endl;
        break;
      } else {
        cout << N + K << endl;
        break;
      }
    }
  }
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ill long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ill long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fo(i, n) for (i = 0; i < n; i++)
#define fok(i, k, n) for (i = k; i < n; i++)
#define ci(x) cin >> x;
#define ci2(x, y) cin >> x >> y;
#define co(x) cout << x << "\n";
#define co2(x, y) cout << x << " " << y << "\n";
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
const int mod = 1000000007;

int main() {
  ll n, k;
  ci2(n, k);
  if (abs(n - k) > n) {
    co(n);
  } else {
    ll x = n % abs(n - k);
    x = min(x, k - x);
    co(x);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fo(i, n) for (i = 0; i < n; i++)
#define fok(i, k, n) for (i = k; i < n; i++)
#define ci(x) cin >> x;
#define ci2(x, y) cin >> x >> y;
#define co(x) cout << x << "\n";
#define co2(x, y) cout << x << " " << y << "\n";
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
const int mod = 1000000007;

int main() {
  ll n, k;
  ci2(n, k);
  cout << min(n % k, abs(n % k - k)) << "\n";
  return 0;
}
Problem p02719 (C++)

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

using namespace std;

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

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

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

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


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

using namespace std;

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

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

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

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

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

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


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

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