alexjercan/bugnet huggingface dataset:
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 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;
}

Error: TLE

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 ans = min(temp + k, abs(temp));
  cout << ans << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#define mm(a, x) memset(a, x, sizeof(a))
#define sync                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0)
#define ll long long
using namespace std;
int main(void) {
  sync;
  ll n, k;
  cin >> n >> k;
  n %= k;
  cout << min(n, k - n) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#define mm(a, x) memset(a, x, sizeof(a))
#define sync                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0)
#define ll long long
using namespace std;
int main(void) {
  sync;
  int n, k;
  cin >> n >> k;
  if (n != 0)
    n %= k;
  else
    n = k;
  cout << min(n, k - n) << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long int n, k, a;
  cin >> n >> k;
  while (1) {
    if (n % k == 0) {
      cout << n % k;
      break;
    } else if (n > k) {
      n = n % k;
      continue;
    } else if (n <= k) {
      a = k - n;
      if (a <= n) {
        cout << a;
        break;
      } else {
        cout << n;
        break;
      }
    }
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, a;
  cin >> n >> k;
  while (1) {
    if (n % k == 0) {
      cout << n;
      break;
    } else if (n > k) {
      n = n % k;
      continue;
    } else if (n <= k) {
      a = k - n;
      if (a <= n) {
        cout << a;
        break;
      } else {
        cout << n;
        break;
      }
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
const ll MOD = 1000000007;
long long modinv(long long a, long long m = MOD) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
  /*MODの割り算の際に
  a/＝b;
  を
  a *= modinv(b,MOD);
  とする。
      */
}
ll N, K;
int main() {
  cin >> N >> K;
  if (N < K)
    cout << min(K - N, N) << endl;
  else if (N > K)
    cout << min(N % K, K - (N % K)) << endl;
  else
    cout << 0 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, n) for (int i = 1; i <= n; i++)
const ll MOD = 1000000007;
long long modinv(long long a, long long m = MOD) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
  /*MODの割り算の際に
  a/＝b;
  を
  a *= modinv(b,MOD);
  とする。
      */
}
ll N, K;
int main() {
  cin >> N >> K;
  if (N < K)
    cout << min(K - N, N) << endl;
  else
    cout << min(N % (N - K), (N % K)) << endl;
  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;
  long long ans = N % K;
  if (ans > K - (N % K))
    ans = K - (N % K);
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    if (n < k) {
      a.at(i) = k - n;
      n = k - n;
    } else {
      a.at(i) = n - k;
      n = n - k;
    }
  }
  sort(a.begin(), a.end());
  //  cout<<a[0]<<a[5]<<a[6]<<endl;
  for (int j = 0; j < n; j++) {
    if (a.at(j) != 0) {
      cout << a.at(j) << endl;
      break;
    } else {
      continue;
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: 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;

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


Error: TLE

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;

  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, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using p = pair<int, int>;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
int dx[4]{-1, 0, 1, 0};
int dy[4]{0, 1, 0, -1};

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using p = pair<int, int>;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; i++)
int dx[4]{-1, 0, 1, 0};
int dy[4]{0, 1, 0, -1};

ll K;
ll trial(ll current, ll t) {
  if (current <= t) {
    return current;
  }
  current = t;
  return trial(current, abs(current - K));
}

int main() {
  ll N;
  cin >> N >> K;
  ll a = abs(N - K);
  ll ans = trial(N, a);
  cout << ans << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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


Error: 0

Fixed submission:

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

#include <algorithm>
#include <iostream>
using namespace std;

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

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  if (n >= k) {
    long long diff = n / k;
    cout << min(abs(n - diff * k), abs(n - 2 * diff * k));
  } else {
    abort();
    cout << min(k - n, n);
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: WA

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  if (n == k) {
    cout << "0";
  } else {
    int ans = min(n % (k - n), abs(n % k - (n - k)));
    cout << ans;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N, K, sa;
  cin >> N >> K;
  N %= K;
  sa = abs(N - K);
  cout << min(N, sa) << endl;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int N, K, sa;
  cin >> N >> K;
  K %= N;
  sa = abs(N - K);
  cout << min(N, sa) << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  if (n >= k) {
    ll a = n % k;
    ll b = k - a;
    ll c = min(a, b);
    cout << c << endl;
  } else {
    cout << min(n, k - n) << endl;
  }
}

Error: 0

Fixed submission:


#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  if (n >= k) {
    int a = n % k;
    int b = k - a;
    int c = min(a, b);
    cout << c << endl;
  } else {
    cout << min(n, k - n) << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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


Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  if (N < K) {
    cout << N << endl;
    return 0;
  } else {
    if (N % K == 0) {
      cout << 0 << endl;
      return 0;
    }
    int x = N / K;
    int ans = min(N - K * x, abs(N - K * (x + 1)));
    cout << ans << endl;
    return 0;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

  long long int N, K;
  cin >> N >> K;
  N %= K;
  if (N <= K / 2)
    cout << N << endl;
  else
    cout << K - N << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int issum(int n, int k) {
  if (k == 0) {
    if (n == 0)
      return 1;
    else
      return 0;
  }
  int ret = 0;
  for (int i = 0; i <= n; i++) {
    ret += issum(n - i, k - 1);
  }
  return ret;
}

int main() {
  int n, k;
  cin >> n >> k;
  int p;
  p = issum(n, k);
  cout << p;
}
Problem p02719 (C++) — buggy submission, verdict: 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;
  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;
}


Error: TLE

Fixed submission:

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

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


Error: 0

Fixed submission:

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

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

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

Error: WA

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  int a = N / K;
  int A = N - K * a;
  if (A > N / 2) {
    cout << N - A << endl;
  } else {
    cout << A << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: WA

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  int t = n % k;
  cout << min(n, t);
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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


Error: WA

Fixed submission:

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

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

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

Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  int a;
  a = N % K;
  cout << min(a, K - a) << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Error: TLE

Fixed submission:

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

int main() {
  long long int n, k;
  cin >> n >> k;
  string num = to_string(n);
  int x = num.size();
  if (k == 1) {
    cout << 0 << endl;
  } else if (k == 2) {
    int t = num[x - 1] - 0;
    if (t % 2 == 0) {
      cout << 0 << endl;
    } else {
      cout << 1 << endl;
    }

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

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

int main(void) {
  long long int n = 0, k = 0, x = 0, oldx = 0, a = 0;

  scanf("%lld%lld", &n, &k);

  a = n / k;

  n = n - a * k;

  x = n;
  while (x != oldx) {
    oldx = x;
    x = abs(x - k);
    //        printf("x=%lld oldx=%lld\\n", x, oldx);
    if (x > oldx) {
      x = oldx;
      break;
    }
  }

  printf("%lld\\n", x);

  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  long long int n = 0, k = 0, x = 0, oldx = 0;

  scanf("%lld%lld", &n, &k);

  if (k == 1) {
    printf("0\\n");
    return 0;
  }

  x = n;
  while (x != oldx) {
    oldx = x;
    x = abs(x - k);
    //        printf("x=%lld oldx=%lld\\n", x, oldx);
    if (x > oldx) {
      x = oldx;
      break;
    }
  }

  printf("%lld\\n", x);

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

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

int main() {
  std::cin.sync_with_stdio(false);

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

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

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  std::cin.sync_with_stdio(false);

  long long int n, k;
  scanf("%lld %lld", &n, &k);

  if (n % k != 0) {
    while (abs(n - k) < n) {
      n = abs(n - k);
    }
    printf("%lld\\n", n);
  } else {
    printf("0\\n");
  }

  return 0;
}
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 namespace std;
using ll = long long;
using P = pair<int, int>;

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

Error: TLE

Fixed submission:

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

int main() {
  ll n, k, ans;
  ll m = 1e18;
  cin >> n >> k;
  if (n >= m)
    ans = 0;
  else {
    ans = min(n, abs(n - k));
    rep(i, n) {
      n = abs(ans - k);
      ans = min(ans, n);
    }
  }
  cout << ans << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

  long long ans = N % K;

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef pair<ll, ll> P;

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

  ll ans = n - n / k * k;
  ll tmp = ans - k;

  cout << min(ans, abs(tmp)) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef vector<ll> vec;
typedef pair<ll, ll> P;

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

  ll a = n, b = k;
  if (k != 1 && n != k) {
    while (1) {
      ll tmp = a;
      a = b % a;
      b = tmp;
      if (a == 0) {
        if (b - k >= 0)
          b = min(b, b - k);
        break;
      }
    }
  } else
    b = 0;
  cout << b << 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;

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

  return 0;
}

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;

  if (n % k == 0) {
    cout << 0;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    ans = min(ans, abs(ans - k));
  }

  cout << ans;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void solve(long long N, long long K) {
  if (K == 1) {
    cout << 0 << endl;
    return;
  }

  long long q = N / K;
  long long m = N - q * K;
  long long mp = abs(m - K);

  cout << min(m, mp) << 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;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  solve(N, K);
  return 0;
}


Error: TLE

Fixed submission:

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

void solve(long long N, long long K) {
  if (K == 1) {
    cout << 0 << endl;
    return;
  }

  set<long long> seen;
  while (true) {
    seen.insert(N);
    N = abs(N - K);
    if (seen.count(N) >= 1)
      break;
  }

  cout << *min_element(seen.begin(), seen.end()) << 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;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  solve(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;
  long long t = n % k;
  cout << min(t, k - t) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int t = n % k;
  cout << min(t, k - t) << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {

  // 標準入力
  ll N, K;
  cin >> N >> K;

  // 最小値が0の場合
  if (N % K == 0) {
    cout << 0 << endl;
    return 0;
  }

  // 最小値が0以外の場合
  ll output = N % K;
  output = min(abs(output - K), output);

  // 標準出力
  cout << output << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
typedef long long ll;

int main(void) {

  // 標準入力
  int N, K;
  std::cin >> N >> K;

  // 最小値が0の場合
  if (N % K == 0) {
    std::cout << 0 << std::endl;
    return 0;
  }

  // 最小値が0以外の場合
  int output = N % K;
  output = std::min(abs(output - K), output);

  // 標準出力
  std::cout << output << std::endl;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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