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

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

bool cmp(int a, int b) { return a > b; }

int main() {

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

  long long int n, k;
  cin >> n >> k;

  bool check = 0;
  if (n % k == 0) {
    check = 1;
    cout << "0";
  }

  if (check == 0) {
    set<long long int> s;
    s.insert(n);
    long long int tmp = n;
    bool in = 0;
    while (1) {
      long long int a = abs(tmp - k);
      auto it = s.find(a);

      if (it != s.end())
        break;
      else if (in == 1) {
        s.insert(a);
        break;
      }
      if (a < k) {
        // cout<<"hi"<<endl;
        in = 1;
      }
      s.insert(a);
      tmp = a;
    }
    long long int min = *min_element(s.begin(), s.end());
    cout << min;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool cmp(int a, int b) { return a > b; }

int main() {

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

  long long int n, k;
  cin >> n >> k;

  bool check = 0;
  if (n % k == 0) {
    check = 1;
    cout << "0";
  }

  if (check == 0) {
    long long int a = n / k;
    long long int tmp = n - a * k;
    cout << min(abs(tmp - k), tmp);
  }

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  long long n, k;
  cin >> n >> k;
  long long res = n;
  long long a = n / k;
  res = n - a * k;
  while (res > 0) {
    long long front = res;
    res = min(res, abs(res - k));
    if (front == res)
      break;
  }
  cout << res << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>

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

int main() {
  long long int n, i = 0;
  long long int a, b, c = 0, k = 0;
  cin >> n >> k;
  if (k == 1 || k == n) {
    cout << 0 << endl;
    return 0;
  }
  if (n / k > 10000000000) {
    while (n >= 1000000000) {
      n -= k * 10000000;
    }
    while (1) {
      i++;
      a = n - k;
      // cout << "a"<<a << endl;
      if (a < 0) {
        a = a * -2 + a;
        // cout <<"a2"<<a<< endl;
      }
      b = a - k;
      // cout <<"b"<< b << endl;
      if (b < 0) {
        b = b * -2 + b;
        // cout <<"b2"<< b << endl;
      }

      if (b < a && n != b) {
        n = b;
      } else {
        if (i == 1 && a < n)
          n = a;
        cout << k - n << endl;
        return 0;
      }
    }
  }
  // cout << n << endl;
  while (1) {
    i++;
    a = n - k;
    // cout << "a"<<a << endl;
    if (a < 0) {
      a = a * -2 + a;
      // cout <<"a2"<<a<< endl;
    }
    b = a - k;
    // cout <<"b"<< b << endl;
    if (b < 0) {
      b = b * -2 + b;
      // cout <<"b2"<< b << endl;
    }

    if (b < a && n != b) {
      n = b;
    } else {
      if (i == 1 && a < n)
        n = a;
      if (n - k)
        cout << n << endl;
      break;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>

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

int main() {
  long long int n, i = 0;
  long long int a, b, c = 0, k = 0;
  cin >> n >> k;
  if (k == 1 || k == n) {
    cout << 0 << endl;
    return 0;
  }
  if (n / k > 10000000000) {
    if (k / 10 > 1) {
      while (n >= 1000000000) {
        n -= k * 10000000;
      }
    } else {
      n -= k * 100000000000000000;
      while (n >= 100000000) {
        n -= k * 10000000;
      }
    }
    while (1) {
      i++;
      a = n - k;
      // cout << "a"<<a << endl;
      if (a < 0) {
        a = a * -2 + a;
        // cout <<"a2"<<a<< endl;
      }
      b = a - k;
      // cout <<"b"<< b << endl;
      if (b < 0) {
        b = b * -2 + b;
        // cout <<"b2"<< b << endl;
      }

      if (b < a && n != b) {
        n = b;
      } else {
        if (i == 1 && a < n)
          n = a;
        cout << k - n << endl;
        return 0;
      }
    }
  }
  // cout << n << endl;
  while (1) {
    i++;
    a = n - k;
    // cout << "a"<<a << endl;
    if (a < 0) {
      a = a * -2 + a;
      // cout <<"a2"<<a<< endl;
    }
    b = a - k;
    // cout <<"b"<< b << endl;
    if (b < 0) {
      b = b * -2 + b;
      // cout <<"b2"<< b << endl;
    }

    if (b < a && n != b) {
      n = b;
    } else {
      if (i == 1 && a < n)
        n = a;
      if (n - k)
        cout << n << endl;
      break;
    }
  }

  return 0;
}
Problem p02719 (C++)

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

int main() {

  long long n, k;
  cin >> n >> k;

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

  long long tmp = abs(n - k);

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

  cout << n << endl;

  return 0;
}


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

int main() {

  long long n, k;
  cin >> n >> k;

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

  long long tmp = abs(n - k);

  if (n % k == 0) {
    cout << 0 << endl;
  } else {
    long long dev = n % k;
    if (dev < abs(dev - k)) {
      cout << dev << endl;
    } else {
      cout << abs(dev - k) << endl;
    }
  }

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (int i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a) memset(a, 0, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (int i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a) memset(a, 0, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

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

Buggy submission (status: Time Limit Exceeded):
/// Updated! https://www.youtube.com/watch?v=U07_n9xGIlM
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define dbug printf("I am here\n");
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int maxn = 2e5 + 100;

const ll inf = 1e18;

ll ans(ll a, ll b) {
  if (a <= b)
    return min(a, abs(a - b));
  else
    return ans(abs(a - b), b);
}

int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  if (k == 1) {
    cout << 0 << endl;
  } else
    cout << ans(n, k) << endl;
  return 0;
}
/*



*/


Fixed submission:
/// Updated! https://www.youtube.com/watch?v=U07_n9xGIlM
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define dbug printf("I am here\n");
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int maxn = 2e5 + 100;

const ll inf = 1e18;

ll ans(ll a, ll b) {
  if (a <= b)
    return min(a, abs(a - b));
  else
    return ans(a % b, b);
}

int main() {
  fast;
  ll n, k;
  cin >> n >> k;
  if (k == 1) {
    cout << 0 << endl;
  } else
    cout << ans(n, k) << endl;
  return 0;
}
/*



*/
Problem p02719 (C++)

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

int main() {

  vector<int64_t> N;
  int64_t K;
  cin >> N.at(0) >> K;

  int64_t i = 0;

  while (true) {
    N.at(i + 1) = N.at(i) - K;
    if (N.at(i + 1) < 0) {
      N.at(i + 1) *= (-1);
      if (N.at(i + 1) < N.at(i)) {
        cout << N.at(i + 1) << endl;
      } else {
        cout << N.at(i) << endl;
      }
      break;
    }
  }
}


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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define endl "\n"
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
ll mod = 1e9 + 7;

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define endl "\n"
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
ll mod = 1e9 + 7;

int main() {
  fast
      // #ifndef ONLINE_JUDGE
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      // #endif
      ll n,
      k;
  cin >> n >> k;
  ll ans = n % k;
  ans = min(ans, abs(ans - k));
  cout << ans;

  return 0;
}
Problem p02719 (C++)

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

int main(void) {
  int N, K;
  cin >> N >> K;
  ll a = N % K;
  ll b = abs(K - a);
  cout << min(a, b) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1000000000; // 10^9

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

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ll n, k;
  cin >> n >> k;
  ll p = n / k;
  ll q = n - (k * p);
  n = q;
  ll n1;
  if (n - k < 0)
    n1 = -(n - k);
  else
    n1 = (n - k);
  int flag = 0;
  while (!flag) {
    if (n < n1)
      flag = 1;
    else {
      n = n1;
      if (n - k < 0)
        n1 = -(n - k);
      else
        n1 = (n - k);
    }
  }
  cout << n;
  return 0;
}
Problem p02719 (C++)

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

using namespace std;

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


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

using namespace std;

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

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

using namespace std;

int main() {
  int a, b, c;
  cin >> a >> b;

  c = a % b;
  if (c > b / 2) {
    cout << b - c << endl;
  } else {
    cout << c << endl;
  }
}


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

using namespace std;

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

  c = a % b;
  if (c > b / 2) {
    cout << b - c << endl;
  } else {
    cout << c << endl;
  }
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
long long a, b;
long long out;
int main() {
  cin >> a >> b;
  out = labs(a - b);
  while (out > labs(out - b))
    out = labs(out - b);
  cout << out;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
long long a, b;
long long out;
int main() {
  cin >> a >> b;
  long long x = 0;
  if (a >= b) {
    if (a % b)
      x = min(a % b, llabs(a % b - b));
    else
      x = 0;
  } else
    x = min(a, llabs(a - b));
  cout << x << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

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

// const ll INF = 1e18L + 5;
// const int INF = 1e9 + 5;
// const double pi = 3.14159265358979323846;

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

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

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

// const ll INF = 1e18L + 5;
// const int INF = 1e9 + 5;
// const double pi = 3.14159265358979323846;

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

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

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

typedef long long ll;
const long long LINF = 1LL << 60;
const long long LMINF = 1LL << 63;
const int INF = 1 << 30;

#define REP(i, n) for (int i = 0; i < n; ++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 PB push_back
#define F first
#define S second
#define MP make_pair
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << "\n"

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;

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

  return 0;
}

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

typedef long long ll;
const long long LINF = 1LL << 60;
const long long LMINF = 1LL << 63;
const int INF = 1 << 30;

#define REP(i, n) for (int i = 0; i < n; ++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 PB push_back
#define F first
#define S second
#define MP make_pair
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << "\n"

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;

  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  while (1) {
    if (n < k) {
      if (n < abs(n - k)) {
        cout << n << endl;
      } else {
        cout << abs(n - k) << endl;
      }
      return 0;
    }
    n = abs(n - (k * (n / k)));
  }

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, x, n) for (int i = x; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define INF 1e9
typedef long long ll;
using VI = vector<int>;
using VS = vector<string>;
using graph = vector<vector<int>>;
int main() {
  ll n, k;
  cin >> n >> k;
  if (k == 1 || k == 0) {
    cout << 0 << endl;
    exit(0);
  }

  while (1) {
    ll x = n;
    n = abs(n - k);
    if (n > x) {
      cout << x << endl;
      exit(0);
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, x, n) for (int i = x; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define INF 1e9
typedef long long ll;
using VI = vector<int>;
using VS = vector<string>;
using graph = vector<vector<int>>;
int main() {
  ll n, k;
  cin >> n >> k;
  if (n > k) {
    n %= k;
  }

  if (k == 1 || k == 0) {
    cout << 0 << endl;
    exit(0);
  }

  while (1) {
    ll x = n;
    n = abs(n - k);
    if (n > x) {
      cout << x << endl;
      exit(0);
    }
  }

  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)
using namespace std;
using ll = long long;

int main() {
  ll n, k;
  cin >> n >> k;
  ll temp = abs(n - k);
  while (temp > 0) {
    temp = temp - k;
  }
  ll ans = min(temp + k, abs(temp));
  cout << ans << 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;
  cin >> n >> k;
  // ll temp = abs(n - k);
  // while(temp > 0) {
  //     temp = temp - k;
  // }
  ll temp = n % k;
  // for(int i = 0; i < n; i++){
  //     temp = temp - k;
  //     if(temp < 0)break;
  // }

  ll ans = min(temp, abs(temp - k));
  cout << ans << endl;
  return 0;
}
Problem p02719 (C++)

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

#define ll long long
#define ull unsigned long long
#define vii vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pdd pair<double, double>
#define pldld pair<long long double, long long double>
#define ff first
#define ss second
#define pb push_back
#define read freopen("alu.txt", "r", stdin);
#define write freopen("vorta.txt", "w", stdout);
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);
#define PI 2 * acos(0.0)
#define DEBUG(x) cerr << #x << " = " << x << endl

const int MAX = 2e6 + 5, MOD = 1e9 + 7, MAXLG = log2(MAX) + 1;
const ll inf = 1e18 + 5;

int arr[MAX];
int main() {

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


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

#define ll long long
#define ull unsigned long long
#define vii vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pdd pair<double, double>
#define pldld pair<long long double, long long double>
#define ff first
#define ss second
#define pb push_back
#define read freopen("alu.txt", "r", stdin);
#define write freopen("vorta.txt", "w", stdout);
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);
#define PI 2 * acos(0.0)
#define DEBUG(x) cerr << #x << " = " << x << endl

const int MAX = 2e6 + 5, MOD = 1e9 + 7, MAXLG = log2(MAX) + 1;
const ll inf = 1e18 + 5;

int arr[MAX];
int main() {

  fastio;
  ll n, k;
  cin >> n >> k;
  if (n == k || !n)
    cout << 0 << "\n";
  else {
    if (n > k)
      n %= k;
    cout << min(n, abs(n - k)) << "\n";
  }
}
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 = -a + K;
  cout << min(a, 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 = -a + K;
  cout << min(a, b) << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long n, k, ans;
  cin >> n >> k;
  ans = abs(n - k);
  if (k == 1)
    ans = 0;
  else
    while (2 * ans > k) {
      ans = min(ans, abs(ans - k));
    }
  cout << ans << endl;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long n, k, ans;
  cin >> n >> k;
  ans = n % k;
  if (k == 1)
    ans = 0;
  else
    while (2 * ans > k) {
      ans = min(ans, abs(ans - k));
    }
  cout << ans << endl;
}
Problem p02719 (C++)

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

int main() {
  long long int n, k;
  cin >> n >> k;
  long long int minnumber = (n % k);
  int counter = 0;
  for (int i = 0;; i++) {
    if (minnumber == 0) {
      cout << 0 << endl;
      return 0;
    }
    if (counter == 2) {
      break;
    }
    minnumber = min(minnumber, abs(minnumber - k * i));
    if (n - k * i < 0) {
      counter += 1;
    }
  }
  cout << minnumber << endl;
  return 0;
}

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

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

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


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;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

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

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

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

using namespace std;

int main() {
  // code
  int tc;
  int i, j, k, n, m, p = 0;
  cin >> n >> k;
  cout << min(n % k, abs((n % k) - k)) << endl;

  return 0;
}


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

using namespace std;

int main() {
  // code

  long long i, j, k, n, m, p = 0;
  cin >> n >> k;
  cout << min(n % k, abs((n % k) - k)) << endl;

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define bigger (char)toupper
#define smaller (char)tolower
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<char> vc;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll N, K;
  cin >> N >> K;
  if (N == K)
    cout << 0 << endl;
  else if (N < K) {
    if (N * 2 > K)
      cout << K - N << endl;
    else
      cout << N << endl;
  } else {
    ll a;
    if (N % K == 0)
      cout << 0 << endl;
    else {
      ll a = (N / K) + 1;
      ll answer = 100000000000000100;
      for (ll i = 1; i <= a; i++) {
        answer = min(answer, abs(N - K * a));
      }
      cout << answer << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define bigger (char)toupper
#define smaller (char)tolower
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<char> vc;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll N, K;
  cin >> N >> K;
  if (N == K)
    cout << 0 << endl;
  else if (N < K) {
    if (N * 2 > K)
      cout << K - N << endl;
    else
      cout << N << endl;
  } else {
    ll a;
    if (N % K == 0)
      cout << 0 << endl;
    else {
      ll a = (N / K) + 1;
      cout << abs(N - K * a) << endl;
    }
  }
}
Problem p02719 (C++)

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

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

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

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

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

using namespace std;
typedef unsigned long long llong;

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

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

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

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

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;
typedef unsigned long long llong;

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

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

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

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

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

  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define all(n) (n).begin(), (n).end()
#define ll long long

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

    n = abs(n - k);

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

int main() {

  // Write from here.

  solve();

  return 0;
}

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

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

    n = abs(n - k);

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

int main() {

  // Write from here.

  solve();

  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;

  while (true) {

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

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


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

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

  while (true) {

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i < (int)(n); i++)
using namespace std;
using ll = long long;
int main() {
  ll n, k;
  cin >> n >> k;
  int m = n % k;
  if (m == 0)
    cout << 0 << endl;
  else {
    vector<ll> vec(m + 1);
    vec.at(0) = n;
    vec.at(1) = abs(n - k);
    for (int i = 2; i <= m; i++) {
      vec.at(i) = abs(vec.at(i - 1) - k);
    }
    sort(vec.begin(), vec.end());
    cout << vec.at(0) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i < (int)(n); i++)
using namespace std;
using ll = long long;
int main() {
  ll n, k;
  cin >> n >> k;
  ll m = n % k;

  if (abs(m - k) < m)
    cout << abs(m - k) << endl;
  else
    cout << m << endl;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vii = vector<vi>;
using vs = vector<string>;
using in6 = int64_t;
using ind = double;
using pii = pair<int, int>;
using psi = pair<string, int>;
using vpi = vector<pii>;
using vps = vector<psi>;
#define rep(i, n) for (in6 i = 0; i < (in6)(n); i++)
#define repa(i, a, n) for (in6 i = a; i < (in6)(n); i++)
#define repdown(i, n) for (in6 i = n; i > 0; i--)
#define kyun ios::sync_with_stdio(false)
#define hinata cout << "\n"
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define inf 2147483647
#define dset(n) cout << fixed << setprecision(n)

void yes() { cout << "Yes"; }

void no() { cout << "No"; }

int main() {
  kyun;
  in6 n, k;
  cin >> n >> k;
  in6 ans = n;
  while (1) {
    in6 now = abs(ans - k);
    if (now < ans)
      ans = now;
    else
      break;
  }
  cout << ans;
  hinata;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vii = vector<vi>;
using vs = vector<string>;
using in6 = int64_t;
using ind = double;
using pii = pair<int, int>;
using psi = pair<string, int>;
using vpi = vector<pii>;
using vps = vector<psi>;
#define rep(i, n) for (in6 i = 0; i < (in6)(n); i++)
#define repa(i, a, n) for (in6 i = a; i < (in6)(n); i++)
#define repdown(i, n) for (in6 i = n; i > 0; i--)
#define kyun ios::sync_with_stdio(false)
#define hinata cout << "\n"
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define inf 2147483647
#define dset(n) cout << fixed << setprecision(n)

void yes() { cout << "Yes"; }

void no() { cout << "No"; }

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

Buggy submission (status: Runtime Error):

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

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


Fixed submission:

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < e; i++)
#define for__(i, s, e) for (ull i = s; i < e; i++)
#define SSTR(x)                                                                \
  static_cast<std::ostringstream &>((std::ostringstream() << std::dec << x))   \
      .str()
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

int main() {
#ifndef ONLINE_JUDGE
  freopen("test.in", "r", stdin);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  ll mod = n % k;
  cout << min(mod, k - mod) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < e; i++)
#define for__(i, s, e) for (ull i = s; i < e; i++)
#define SSTR(x)                                                                \
  static_cast<std::ostringstream &>((std::ostringstream() << std::dec << x))   \
      .str()
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  ll mod = n % k;
  cout << min(mod, k - mod) << endl;

  return 0;
}
Problem p02719 (C++)

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

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

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

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

using namespace std;
int main() {
  fio;

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

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

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

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

int main() {
  long long N, K;
  cin >> N >> K;
  if (N > K * 3) {
    N = N - ((N / K) - 3) * K;
  }
  while (true) {
    if (N > abs(N - K)) {
      N = abs(N - K);
    } else {
      break;
    }
  }
  cout << N << endl;
}
Problem p02719 (C++)

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

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

  min = N % K;

  if (min > K - min)
    min = K - min;

  cout << min << endl;

  return 0;
}

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

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

  min = N % K;

  if (min > K - min)
    min = K - min;

  cout << min << 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;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};

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

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

using namespace std;

int main(void) {
  int n, k;

  cin >> n >> k;

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

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

using namespace std;

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

  cin >> n >> k;

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

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

#define ll long long int
#define MOD 1000000007

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

  return 0;
}

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

#define ll long long int
#define MOD 1000000007

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

  return 0;
}
Problem p02719 (C++)

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

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

  while (true) {
    if (K == 1) {
      printf("0\n");
      break;
    }
    if (N < abs(N - K)) {
      printf("%ld\n", N);
      break;
    } else {
      N = abs(N - K);
    }
  }
}

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

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

  if (K == 1) {
    printf("0\n");
  } else if (N > K) {
    long int tmp = N % K;
    printf("%ld", abs(tmp - K));
  } else {
    while (true) {
      if (N < abs(N - K)) {
        printf("%ld\n", N);
        break;
      } else {
        N = abs(N - K);
      }
    }
  }
}
Problem p02719 (C++)

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

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

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

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

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

  if (N > K) {
    N = N % K;
  }

  while ((2 * N) > K) {
    N = abs(N - K);
  }
  cout << N << endl;
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define pb push_back
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;

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

  if (k == 1)
    cout << 0 << endl;
  else {
    set<ll> abc;
    while (true) {
      n = abs(n - k);
      if (abc.find(n) != abc.end())
        break;
      else
        abc.insert(n);
    }
    cout << *(abc.begin()) << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define pb push_back
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;

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

  n = n % k;

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

  return 0;
}
Problem p02719 (C++)

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

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

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

using namespace std;

class Solution {
public:
  void solve() {
    int n, k;
    cin >> n >> k;
    cout << min(n % k, k - n % k) << "\n";

    return;
  };
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}

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

using namespace std;

class Solution {
public:
  void solve() {
    long long n, k;
    cin >> n >> k;
    cout << min(n % k, k - n % k) << "\n";

    return;
  };
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}
Problem p02719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb(x) push_back(x)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
inline int mod(int n, int m) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll exp(ll a, ll b, ll m) {
  if (b == 0LL)
    return 1LL;
  if (b == 1LL)
    return mod(a, m);
  ll k = mod(exp(a, b / 2, m), m);
  if (b & 1LL) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}

int32_t main() {
  fastio;
  int n, k;
  cin >> n >> k;
  n = n % k;
  if (n <= k - n)
    cout << n << endl;
  else
    cout << k - n << endl;
  // Math -> gcd it all // chack MAXN // check ll
  // did you check N=1? Did you mix up N,M?
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb(x) push_back(x)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
inline int mod(int n, int m) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll exp(ll a, ll b, ll m) {
  if (b == 0LL)
    return 1LL;
  if (b == 1LL)
    return mod(a, m);
  ll k = mod(exp(a, b / 2, m), m);
  if (b & 1LL) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}

int32_t main() {
  fastio;
  int n, k;
  cin >> n >> k;
  n = n % k;
  if (n <= k - n)
    cout << n << endl;
  else
    cout << k - n << endl;
  // Math -> gcd it all // chack MAXN // check ll
  // did you check N=1? Did you mix up N,M?
}
Problem p02719 (C++)

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

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

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

int main() {
  long long int n, k;
  cin >> n >> k;
  cout << min(n % k, abs(k - n % k));
}
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;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n, k;
  cin >> n >> k;
  cout << min({n % k, (n + k - 1) / k * k - n, n}) << endl;
  return 0;
}


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

int main() {
  ll n, k;
  cin >> n >> k;
  cout << min({n % k, (n + k - 1) / k * k - n, n}) << endl;
  return 0;
}