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

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

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

  long long int R = N % K;
  if (R > abs(R - K))
    R = abs(R - K);

  cout << R;
}


Error: WA

Fixed submission:

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

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

  int R = N % K;
  if (R > abs(R - K))
    R = abs(R - K);

  cout << R;
}
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;
  long long int r;
  long long int s;
  r = n % k;
  s = abs(n % k - k);
  long long int ans;
  ans = min(r, s);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k;
  cin >> n >> k;
  long long int a;
  long long int x;
  x = n;
  for (int i = 0; i < n + 1; ++i) {
    if (x > abs(x - k)) {
      x = abs(x - k);
    } else {
      cout << x << endl;
      break;
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  rem = n % k;
  t = k - rem;
  cout << min(rem, t);
}


Error: TLE

Fixed submission:

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

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

  long long int now = n, p2 = -1, p3 = -2;
  long long int min = n;
  while (now != p3) {
    p3 = p2;
    p2 = now;
    if (p2 % k == 0) {
      min = 0;
      break;
    } else {
      now = abs(p2 - k);
    }
    if (min > now) {
      min = now;
    }
  }
  cout << min;
}
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++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7, INF = 1e9;

int main() {
  ll n, k;
  cin >> n >> k;
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n == k) {
    cout << 0 << endl;
  } else if (n < k) {
    cout << min(n, abs(n - k)) << endl;
  } else {
    n = n % k;
    cout << min(n, abs(n - k)) << endl;
  }
  system("pause");
}


Error: 0
sh: 1: pause: not found


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7, INF = 1e9;

int main() {
  ll n, k;
  cin >> n >> k;
  if (n < k) {
    if (k % n == 0) {
      cout << n << endl;
    } else {
      cout << k - abs(n - k) << endl;
    }
  } else {
    if (n == 0 || n == 1 || k == 1) {
      cout << 0 << endl;
    } else
      cout << n % abs(n - k) << endl;
  }
  system("pause");
}
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;
  long long int output = n % k;
  cout << min(output, k - output);
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  ll n, k;
  ll output = n % k;
  cout << min(output, n - k);
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long unsigned x, y, anti_rem;
  cin >> x >> y;
  if (x % y == 0) {
    cout << 0 << endl;
  } else if (x < y) {
    anti_rem = labs(x - y);
    cout << min(x, anti_rem) << endl;
  } else {
    x = x % y;
    long long unsigned xx = labs(x - y);
    cout << min(x, xx) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long unsigned x, y, anti_rem;
  cin >> x >> y;
  if (x % y == 0) {
    cout << 0 << endl;
  } else if (x < y) {
    anti_rem = labs(x - y);
    cout << min(x, anti_rem) << endl;
  } else {
    while (x > y) {
      x = x - y;
    }
    long long unsigned xx = labs(x - y);
    cout << min(x, xx) << endl;
  }

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

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

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

Error: 0

Fixed submission:

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

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  else {
    return gcd(b, a % b);
  }
}
int main() {
  int N, K, ans;
  cin >> N >> K;
  int r = N % K;
  //   else ans = gcd(K,r);
  if (r > K / 2)
    ans = K - r;
  else
    ans = r;
  //   if(K==1){
  //     ans = 0;
  //   }else{
  //     int a = max(abs(N-K),K);
  //     int b = min(abs(N-K),K);
  //   	ans = gcd(a,b);
  //   }
  cout << ans << 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 x = n % k;
  if (x < 0) {
    x = -x;
  }
  long long y = x - k;
  if (y < 0) {
    y = -y;
  }
  if (x < y) {
    cout << x << endl;
  } else {
    cout << y << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a = n % k;
  cout << min(a, k - 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 long N, K;

  cin >> N >> K;

  long long a = N % K;
  cout << min(a, K - a) << endl;

  return 0;
}

Error: 0

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 a = N % K;
  cout << min(a, K - a) << endl;

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

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

ll a, b, c, n, m;
ll ans = 100000000000;
string s;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  if (n % m == 0)
    n = 0;
  else {
    if (n < m && n * 2 > m)
      n = abs(n - m);
    if (n > abs(n - m * (n / m + 1)) && n > abs(n - m * (n / m))) {
      n = min(abs(n - m * (n / m + 1)), abs(n - m * (n / m)));
    }
  }
  cout << n;

  return 0;
}


Error: TLE

Fixed submission:

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

ll a, b, c, n, m;
string s;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  while (1) {
    if (n % m == 0) {
      n = 0;
      break;
    }
    if (abs(n - m) > n)
      break;
    n = abs(n - m);
  }
  cout << n;

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define R(i, n) for (int i = 0; i < n; i++)
#define S(a) scanf("%d", &a)
#define S2(a, b) scanf("%d%d", &a, &b)
#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SL(a) scanf("%lld", &a)
#define P(a) printf("%d", a)
#define PL(a) printf("%lld", a);
#define PY printf("%s", "Yes");
#define PN printf("%s", "No");
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> IR;

int main() {
  ll n, k;
  SL(n);
  SL(k);
  ll mod = n % k;
  ll diff = k - mod;
  PL(min(mod, diff));
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define R(i, n) for (int i = 0; i < n; i++)
#define S(a) scanf("%d", &a)
#define S2(a, b) scanf("%d%d", &a, &b)
#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SL(a) scanf("%lld", &a)
#define P(a) printf("%d", a)
#define PL(a) printf("%lld", a);
#define PY printf("%s", "Yes");
#define PN printf("%s", "No");
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef pair<int, int> IR;

int main() {
  ll n, k;
  SL(n);
  SL(k);
  ll now = n;
  while (true) {
    ll tmp = abs(k - n);
    if (tmp >= now)
      break;
    else {
      n = tmp;
      now = tmp;
    }
  }
  PL(n);
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;
typedef long long ll;
int main() {
  ll x, p;
  cin >> x >> p;
  cout << min(x % p, (p - x % p)) << endl;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;
const int N = 110;
int a[N];
int main() {
  int n, m;
  cin >> n >> m;
  int sum = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i], sum += a[i];
  sum = (sum + 4 * m - 1) / (4 * m);
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] >= sum)
      cnt++;
  if (cnt >= m)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  ll flag = n / k;
  n -= flag * k;
  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = 0; i < 1e17; ++i) {
    if ((abs(n - k)) < n) {
      n = abs(n - k);
    } else {
      break;
    }
  }
  cout << abs(n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
int main() {
  ll n, k;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = 0; i < 1e17; ++i) {
    if ((abs(n - k)) < n) {
      n = abs(n - k);
    } else {
      break;
    }
  }
  cout << abs(n) << endl;

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

#include <bits/stdc++.h>

using namespace std;

// 計算量の限界 10^8
// 最大値 int i=2147483647;//2^31-1

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

  // 考え方メモ
  // 具体例
  // (N, K) = (13, 4)
  // N = 13, 9, 5, 1, -3
  // r = 13 % 4 = 1
  // 抽象化
  // final N - K >= 0,  N == r
  // final N - K < 0, r - K == N -> N == r - K -> N == |r - K|

  int64_t r = N % K;
  int64_t res;

  res = min(r, abs(r - K));

  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long n; // 整数の初期値
  long long k; // 引く数
  cin >> n >> k;

  long long target = n; // 求める値

  while (target >= llabs(target - k)) {
    target = llabs(target - k);
  }

  cout << target << endl;

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

using namespace std;
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define input()                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
void solve() {}
int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = res * x;
    x = x * x;
    y = y / 2;
  }
  return res;
}
int main() {
  input() ll n, k;
  cin >> n >> k;
  ll ans = 1e18;
  if (n >= k) {
    ll p = n % k;
    ans = min(ans, p);
    ans = min(ans, k - p);
  } else {
    ans = min(ans, n);
    ans = min(ans, k - n);
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

using namespace std;
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define input()                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
void solve() {}
int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = res * x;
    x = x * x;
    y = y / 2;
  }
  return res;
}
int main() {
  input() ll n, k;
  cin >> n >> k;
  ll ans = 1e18;
  if (n > k) {
    ll p = n % k;
    ans = min(ans, p);
    ans = min(ans, abs(k - p));
  } else {
    ll p = k % n;
    ans = min(ans, p);
    ans = min(ans, abs(k - p));
  }
  cout << ans;
  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 temp;
  if (N % K == 0) {
    cout << 0 << endl;
    return 0;
  }

  temp = N % K;
  long long ans = min(temp, K - temp);
  cout << setprecision(20) << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int gcd(long long a, long long b) {
  if (a < b)
    return gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

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

  long long ans = gcd(N, K);
  cout << ans << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

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

  if (n > k)
    n = n - (n / k) * k;
  while (n > abs(n - k)) {
    n = abs(n - k);
  }

  cout << n << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

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

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

  cout << n << 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 t = N % K;

  cout << (K - t > t ? t : K - t) << endl;
}


Error: WA

Fixed submission:

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

int main() {
  long long N, K;

  cin >> N >> K;

  long long x = abs(N - K) >= N ? N : abs(N - K);

  if (N % x == 1) {
    cout << 0 << endl;
  } else {
    cout << (N > x ? N % x : N) << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

  a = n % k;
  if (a - k >= 0) {
    b = a - k;
  } else {
    b = k - a;
  }

  if (a >= b) {
    cout << b << endl;
  } else {
    cout << a << endl;
  }
}

Error: 0

Fixed submission:

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

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

  a = n % k;
  if (a - k >= 0) {
    b = a - k;
  } else {
    b = k - a;
  }

  if (a >= b) {
    cout << b << endl;
  } else {
    cout << a << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void) {
  long N, K, t, answer;
  cin >> N >> K;
  t = N % K; // tは正の値での最小値,|t-K|に操作をしたらtに戻る
  // tと|t-K|の大小比較
  if (t <= abs(t - K)) {
    answer = t;
  } else {
    answer = abs(t - K);
  }
  cout << answer;
}

Error: WA

Fixed submission:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void) {
  int N, K, t, answer;
  cin >> N >> K;
  t = N % K;
  if (t <= abs(t - K)) {
    answer = t;
  } else {
    answer = abs(t - K);
  }
  cout << answer;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << "0" << endl;
    return 0;
  } else {
    int x = N % K;
    cout << min(x, abs(K - x)) << 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;
  if (N == 0 || K == 0) {
    cout << N << endl;
  } else {
    long long amari = N % K;
    long long over = amari - K > 0 ? amari - K : -(amari - K);
    long long result = min(amari, N);
    result = min(result, over);
    cout << result << endl;
  }
}

Error: 0

Fixed submission:

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

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

#include <iostream>
using namespace std;

int main() {
  long long int n, k;
  long long int rem;
  long long int x;

  cin >> n >> k;

  rem = n % k;

  if (rem > k) {
    x = rem - k;
  } else {
    x = k - rem;
  }

  if (x > rem)
    cout << rem << endl;
  else
    cout << x << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  cin >> N >> K;
  ans = N;

  do {
    min = ans;
    ans = 0;

    if (K == 1 || N % K == 0 || N == K) {
      min = ans;
      break;
    }

    if (min > K) {
      ans = min - K;
    } else {
      ans = K - min;
    }

  } while (ans < min);

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

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  cout << min(n % k, abs(k - n % k));
  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++)   // 普通のループ
#define REPR(i, n) for (int i = n; i >= 0; i--) // 逆ループ
#define FOR(i, m, n) for (int i = m; i < n; i++) // 最初の数値を指定したループ

// 型名省略
typedef long long ll;

using namespace std;

// #include "./lib/string.h"

int main() {
  // string s;
  // bool ans;
  ll n, k, ans;

  cin >> n >> k;

  if (0 > n || n > 1000000000000000000) {
    return 0;
  }
  if (1 > k || k > 1000000000000000000) {
    return 0;
  }

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

  tmp = ans - k;
  if (ans > abs(tmp)) {
    ans = abs(tmp);
  }
  cout << ans;

  // ans = checkCharacterMatching(s, 2, 3);
  // cout << (ans ? "Yes" : "No") << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> //全てのヘッダファイルをインクルード

// ループ
#define REP(i, n) for (int i = 0; i < n; i++)   // 普通のループ
#define REPR(i, n) for (int i = n; i >= 0; i--) // 逆ループ
#define FOR(i, m, n) for (int i = m; i < n; i++) // 最初の数値を指定したループ

// 型名省略
typedef long long ll;

using namespace std;

// #include "./lib/string.h"

int main() {
  // string s;
  // bool ans;
  ll n, k, ans;

  cin >> n >> k;

  if (0 > n || n > 1000000000000000000) {
    return 0;
  }
  if (1 > k || k > 1000000000000000000) {
    return 0;
  }

  ans = n;
  ll tmp;
  while (1) {
    tmp = ans - k;
    // cout << tmp << " " << abs(tmp) << " " << ans << "\\n";
    if (ans > abs(tmp)) {
      ans = abs(tmp);
    } else {
      break;
    }
  }

  cout << ans;

  // ans = checkCharacterMatching(s, 2, 3);
  // cout << (ans ? "Yes" : "No") << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <math.h>
#include <numeric>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

long long combi(long long n, long long k) {
  if (n == k || k == 0)
    return 1;
  else {
    return combi(n - 1, k - 1) + combi(n - 1, k);
  }
}

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

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <math.h>
#include <numeric>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

long long combi(long long n, long long k) {
  if (n == k || k == 0)
    return 1;
  else {
    return combi(n - 1, k - 1) + combi(n - 1, k);
  }
}

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

  if (N < abs(N - K)) {
    cout << N << endl;
    return 0;
  } else {
    M = abs(N - K);
    while (M > 2) {
      M = abs(M - K);
      // cout << M << endl;
    }
  }
  cout << M << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long int ll;

ll minCheck(ll min, ll K) {
  ll m = abs(min - K);

  if (min > m)
    return minCheck(m, K);

  return min;
}

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

  cut = (N / K);
  if (cut > 0)
    N = N - (K * cut);
  cout << minCheck(N, K) << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long int ll;

ll minCheck(ll min, ll K) {
  ll m = abs(min - K);

  if (min > m)
    return minCheck(m, K);

  return min;
}

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

  cout << minCheck(N, K) << 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, sur, ans;

  cin >> N >> K;

  sur = N % K;

  ans = min(sur, K - sur);

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N, K, sur, ans;

  cin >> N >> K;

  sur = N % K;

  ans = min(sur, K - sur);

  cout << ans << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

ll K;
ll d;
ll N;

int main() {

  ll ans;
  ll keep;
  cin >> N >> K;
  N %= K;
  ans = min(N, abs(N - K));
  // cout << N%K << endl;
  cout << ans << endl;
}


Error: WA

Fixed submission:

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

ll K;
ll d;
ll N;

ll aaa(int a) {
  ll b = a % K;
  ll c = abs(b - K);
  return min(b, c);
}

ll bbb(int a) {

  if (a > d) {
    return abs(a - K);
  }
  d = abs(a - K);
  return min(N, bbb(d));
}

int main() {

  ll ans;
  ll keep;
  cin >> N >> K;
  if (N > K) {
    ans = bbb(N);

  } else {
    ans = aaa(N);
  }

  cout << ans << endl;
}