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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

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

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

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

    long long int cnt = n / diff;
    n = n - (cnt * diff);
    // cout <<  n << endl;
    if (n < -(n - k)) {
      cout << n << endl;
    } else {
      cout << -(n - k) << endl;
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  long long n, k, t, i;
  bool check = true;
  cin >> n >> k;
  t = n;
  i = n / k;
  n = n - k * i;
  if (n == 0) {
    cout << 0;
  } else {
    while (check) {
      n = abs(n - k);
      if (t <= n) {
        check = false;
      }

      t = min(t, n);
    }
    cout << t;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  long long n, k, t;
  bool check = true;
  cin >> n >> k;
  t = n;
  long long i = 0;
  if (n % k == 0) {
    cout << 0;
  } else {
    while (check) {
      n = abs(n - k);
      if (t <= n) {
        check = false;
      }

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

#include <iostream>
using namespace std;
int min(int n, int m) {
  if (n > m)
    n = m;
  return n;
}
int main() {
  long long n, k;
  cin >> n >> k;
  n = n % k;
  cout << min(n, k - n);
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
void a(int n, int k);
int main() {
  int n, k;
  cin >> n >> k;
  a(n, k);
  return 0;
}
void a(int n, int k) {
  int x;
  while (1) {
    x = n;
    n = abs(n - k);
    if (n >= x)
      cout << x;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

  if (k == 0) {
    cout << n << endl;
  } else {
    long long int a = max(k - n % k, n % k - k);
    cout << min(n % k, a) << endl;
  }
}

Error: 0

Fixed submission:

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

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

  int a = max(k - n % k, n % k - k);

  cout << min(n % k, a) << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, k;
  scanf("%lld%lld", &n, &k);

  if (n > k) {
    n = n % k;
  }
  printf("%lld", min(n, abs(k - n)));
  return 0;
}


Error: WA

Fixed submission:

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

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

int main() {
  ll n, k;
  scanf("%lld%lld", &n, &k);
  if (n < k) {
    printf("%lld", k % n == 0 ? n : k % n);
    return 0;
  }
  if (n == 1 || k == 1 || n == k) {
    printf("0");
    return 0;
  }

  printf("%lld", __gcd(n, k));

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N, K;
  cin >> N >> K;
  cout << min(N % K, abs((N % K) - K)) << "\\n";
}


Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  cout << (N % abs(N - K)) << "\\n";
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

using ll = long long;

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

  n %= k;
  if (k - n < n)
    cout << k - n << '\\n';
  else
    cout << n << '\\n';

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

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

  n %= k;
  if (k - n < n)
    cout << k - n << '\\n';
  cout << n << '\\n';

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

//
//  main.cpp
//  Study
//
//  Created by 佐々木勇星 on 2020/03/12.
//  Copyright © 2020 佐々木勇星. All rights reserved.
//

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

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

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

  ans = n;

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

    if (n % k == 0) {
      ans = 0;
      break;
    } else if (n / k == 0) {
      n = abs(n - k);
      if (ans > n) {
        ans = n;
      } else if (ans == n) {
        break;
      } else if (ans < n) {
        break;
      }
    } else {
      n = n % k;
      // ans = n;
    }

    // cout << ans << " " << n << endl;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

//
//  main.cpp
//  Study
//
//  Created by 佐々木勇星 on 2020/03/12.
//  Copyright © 2020 佐々木勇星. All rights reserved.
//

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

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

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

  ans = n;

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

    if (k == 1) {
      ans = 0;
      break;
    }

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

    // cout << ans << " " << n << endl;
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long int N, K;
  cin >> N >> K;
  if (K < N)
    N = (N % K);
  long long int prevN = N + 1;
  while (prevN > N) {
    prevN = N;
    N = abs(N - K);
  }
  cout << prevN;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  cin >> N >> K;
  long long int prevN = N + 1;
  while (prevN > N) {
    prevN = N;
    N = abs(N - K);
  }

  cout << prevN;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: 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;
  n = n - (n / k * k);
  Int ans = min(n, abs(n - k));

  cout << ans << endl;

  return 0;
}


Error: 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;

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

  cout << ans << endl;

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

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

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

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

typedef unsigned long long ULL;
typedef long long LL;

#define pb push_back
#define mp make_pair
#define reset(a, b) memset(a, b, sizeof(a))
#define rep(i, b) for (int i = 0; i < b; i++)
#define F first
#define S second

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

  int T;
  //    cin>>T;
  T = 1;
  while (T--) {

    ULL n, k;
    cin >> n >> k;

    ULL r = n % k;
    ULL p;
    if (r > k)
      p = r - k;
    else
      p = k - r;
    if (r < p)
      cout << r;
    else
      cout << p;
  }

  return 0;
}

Error: WA

Fixed submission:

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

typedef unsigned long long ULL;
typedef long long LL;

#define pb push_back
#define mp make_pair
#define reset(a, b) memset(a, b, sizeof(a))
#define rep(i, b) for (int i = 0; i < b; i++)
#define F first
#define S second

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

  int T;
  //    cin>>T;
  T = 1;
  while (T--) {

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

    int r = n % k;

    int x = min(r, abs(r - k));

    cout << x;
  }

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

// omajinai c++ atcoder
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define itn int
typedef long long ll;
constexpr auto mod = 1000000007;
using namespace std;
int main() {

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

Error: WA

Fixed submission:

// omajinai c++ atcoder
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define itn int
typedef long long ll;
constexpr auto mod = 1000000007;
using namespace std;
int main() {

  int n, k;
  cin >> n >> k;
  int ans = min(n % k, k - n % k);
  cout << ans;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int main() {

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

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

  long long ans = N;
  while (N / K != 0) {
    N = N % K;
    ans = N;
  }

  long long sum = abs(N - K);
  if (sum < N) {
    cout << sum << endl;
  } else {
    cout << N << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int main() {

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

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

  long long ans = N;
  while (ans >= 0) {
    ans = ans - K;
  }

  long long A = abs(ans);
  if (N < A) {
    cout << N << endl;
  } else {
    cout << A << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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

Error: TLE

Fixed submission:

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

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

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

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

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

  if (n < k) {
    long long temp;
    temp = k - n;
    if (n < temp)
      temp = n;
    cout << temp << '\\n';
    return 0;
  }

  long long tmp1, tmp2;
  tmp1 = llabs(n - (k * (n / k)));
  tmp2 = llabs(n - (k * ((n / k) + 1)));

  if (tmp1 <= tmp2) {
    cout << tmp1 << '\\n';
  } else {
    cout << tmp2 << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

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

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

  long long temp = 0;
  if (k == 1) {
    cout << "0" << '\\n';
    return 0;
  }
  long long ans = n;
  long long k2;
  while (temp >= 0) {
    k2 = k;
    temp = n - k;
    if (n < k) {
      long long i = n;
      n = k;
      k = i;
    }
    n = n - k;
    if (n < ans)
      ans = n;
    k = k2;
  }

  cout << ans << '\\n';

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long

signed main() {
  int i = 0, j = 0, ans = 0;

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

  int a;
  a = n % k;
  int b;
  b = abs(a - k);

  if (a > b)
    cout << b << endl;
  else
    cout << a << endl;
  return 0;
}

Error: WA

Fixed submission:

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

#define int long long

signed main() {
  int i = 0, j = 0, ans = 0;

  int k, n, a[200000];
  cin >> k >> n;
  for (i; i < n; i++) {
    cin >> a[i];
    if (i > 0) {
      if (a[i] - a[i - 1] > j)
        j = a[i] - a[i - 1];
    }
  }
  if (a[0] + (k - a[n - 1]) > j)
    j = a[0] + (k - a[n - 1]);

  cout << k - j << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: WA

Fixed submission:

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

int main() {
  ll n, k;
  cin >> n >> k;
  if (k - (k % n) < k % n) {
    cout << (k - (k % n)) << endl;
  } else {
    cout << k % n << endl;
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: 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 % 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;
  }
}


Error: 0

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

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


Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, n - 1)
using namespace std;
typedef long long ll;
template <class T> inline bool dpmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool dpmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve(long long N, long long K) {
  cout << min(N % K, K - (N % K)) << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  cin >> N;
  long long K;
  cin >> K;
  solve(N, K);
  return 0;
}


Error: TLE

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, n - 1)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve(long long N, long long K) {
  if (N % K == 0) {
    cout << 0 << endl;
  } else if (K % N == 0) {
    cout << N << endl;
  } else {
    ll min = 1e18;
    ll x = N % K;
    rep(i, N % K + 1) {
      x = abs(x - K);
      if (x <= min) {
        min = x;
      }
    }
    cout << min << endl;
  }
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  cin >> N;
  long long K;
  cin >> K;
  solve(N, K);
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long n, k, diff, x;
  cin >> n >> k;
  diff = n % k;
  x = k - diff;
  cout << min(x, diff);

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  long long n, k, diff;
  cin >> n >> k;
  while (true) {
    diff = llabs(n - k);
    if (diff < n)
      n = diff;
    else
      break;
  }
  cout << n;

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

#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;
class func {
private:
  long long int c;
  long long int d;

public:
  long long int kei(long long int a, long long int b);
  long long int ans;
  func();
};
inline func::func() {
  c = 0;
  d = 0;
  ans = 0;
}

inline long long int func::kei(long long int a, long long int b) {

  c = a % b;
  d = abs(b - c);

  ans = min(c, d);
  return ans;
}

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

  func mei;
  mei.kei(n, k);
  cout << mei.ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;
class func {
private:
  long long int c;
  long long int d;

public:
  long long int kei(long long int a, long long int b);
  long long int ans;
  func();
};
inline func::func() {
  c = 0;
  d = 0;
  ans = 0;
}

inline long long int func::kei(long long int a, long long int b) {
  c = a;
  do {
    d = c;
    c = abs(c - b);
  } while ((c <= d));
  ans = d;
  return ans;
}

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

  func mei;
  mei.kei(n, k);
  cout << mei.ans << endl;

  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: 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) {
    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;
}

Error: TLE

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

#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <utility>

using namespace std;

long read_int() {
  long a;
  scanf("%ld", &a);
  return a;
}

long C(long N, long K) {
  if (N > K) {
    N = N % K;
  }
  long tmp = abs(N - K);
  long ret = min(N, tmp);
  return ret;
}

int main() {
  long N = read_int();
  long K = read_int();

  printf("%ld\\n", C(N, K));
  return 0;
}

Error: 1

Fixed submission:

#include <cmath>
#include <stdio.h>
#include <utility>

using namespace std;

long read_int() {
  long a;
  scanf("%ld", &a);
  return a;
}

long C(long N, long K) {
  long ret = N;
  long tmp = abs(ret - K);
  while (ret > tmp) {
    ret = tmp;
    tmp = abs(ret - K);
  }
  return ret;
}

int main() {
  long N = read_int();
  long K = read_int();

  return C(N, K);
}