alexjercan/bugnet huggingface dataset:
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

typedef long long ll;

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

  if (!(N % K)) {
    cout << 0 << "\\n";
    return 0;
  } else {
    prev = N % K;
    curr_abs = abs(prev - K);
    if (prev < curr_abs) {
      cout << prev << "\\n";
    } else {
      cout << curr_abs << "\\n";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

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

  long int prev = N;
  long int curr = N - K;
  long int curr_abs = abs(curr);

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

  while (1) {
    if (!prev) {
      cout << prev << endl;
      return 0;
    } else if (!curr) {
      cout << curr << endl;
      return 0;
    } else if (prev < curr_abs && curr < 0) {
      cout << prev << endl;
      return 0;
    } else if (curr_abs < prev && curr <= 0) {
      cout << curr_abs << endl;
      return 0;
    }
    prev = curr_abs;
    curr -= K;
    curr_abs = abs(curr);
  }

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

#include <bits/stdc++.h>
using namespace std;
long long n, m;
int main() {
  cin >> n >> m;
  if (n <= m) {
    cout << min(n, m - n);
  } else {
    long long x = n % m;
    cout << min(x, m - x);
  }
}

Error: WA

Fixed submission:

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

#include <iostream>
using namespace std;

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

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int x, k;
  cin >> x >> k;
  if (x > k) {
    x = x % k;
  }
  if (x > k / 2) {
    x = k - x;
  }
  cout << x << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long oper(long long n, long long k) { return abs(n - k); }

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

  long long n, k;
  cin >> n >> k;
  if (k == 1)
    cout << 0 << "\\n";
  else {
    cout << min(n % k, k - (n % k)) << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

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

long long oper(long long n, long long k) { return abs(n - k); }

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

  long long n, k;
  cin >> n >> k;
  if (k == 1)
    cout << 0 << "\\n";
  else {
    while (n >= 0) {
      n = oper(n, k);
      if (oper(n, k) > n) {
        cout << n << "\\n";
        break;
      }
    }
  }

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

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

int main() {
  long long N, K;

  cin >> N >> K;

  long long NmodK = N % K;

  if (NmodK > (abs(NmodK - K))) {
    cout << (abs(NmodK - K)) << endl;
  } else {
    cout << NmodK << endl;
  }

  // while(1){
  //
  //  tmp = N;
  //  N = abs(N-K);
  //
  //  cout << "tmp:" << tmp;
  //  cout << " N:" << N;
  //  cout << " K:" << K;
  //  cout << endl;
  //
  //  if(tmp < N){
  //    cout << tmp << endl;
  //    return 0;
  //  }
  //}
}


Error: TLE

Fixed submission:

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

int main() {
  long long N, K;

  cin >> N >> K;

  long long tmp = 0;
  long long tmp2 = 0;

  if ((N % K == 0) || (N == 0)) {
    cout << tmp << endl;
    return 0;
  }
  while (1) {

    tmp = N;
    N = abs(N - K);

    // cout << "tmp:" << tmp;
    // cout << " N:" << N;
    // cout << " K:" << K;
    // cout << endl;

    if (tmp < N) {
      cout << tmp << endl;
      return 0;
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
typedef long long int ll;
#define fr(x, y) for (int x = 0; x < y; x++)
#include <algorithm>
#include <vector>

using namespace std;

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


Error: WA

Fixed submission:

#include <cmath>
#include <iostream>
typedef long long int ll;
#define fr(x, y) for (int x = 0; x < y; x++)
#include <algorithm>
#include <vector>

using namespace std;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  long long n, k;
  cin >> n >> k;

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

  return 0;
}

Error: TLE

Fixed submission:

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

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

  cout << n;
  return 0;
}
int main() {
  long long n;
  long long k;

  cin >> n >> k;

  check(n, k);

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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
#define bay_bay return 0;
#define fori(n) for (int i = 0; i < n; ++i)
#define for1(n) for (int i = 1; i <= n; i += 3)
#define all(v) ((v).begin()), ((v).end())
#define sz(v) v.size()
#define endl "\\n"
constexpr auto EPS = 1e-9;
#define ff fflush(stdout);
const ll Mod = 1e9 + 7;
clock_t T;
#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl
void GOAT() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#else
#endif
}

int main() {
  //	GOAT();
  ll n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << endl;
  else
    cout << min(n % k, k - (n - (n / k) * k)) << endl;
}

Error: WA

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
#define bay_bay return 0;
#define fori(n) for (int i = 0; i < n; ++i)
#define for1(n) for (int i = 1; i <= n; i += 3)
#define all(v) ((v).begin()), ((v).end())
#define sz(v) v.size()
#define endl "\\n"
constexpr auto EPS = 1e-9;
#define ff fflush(stdout);
const ll Mod = 1e9 + 7;
clock_t T;
#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl
void GOAT() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#else
#endif
}

int main() {
  GOAT();
  ll n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << endl;
  else if (n < k)
    cout << n << endl;
  else
    cout << min(n % k, k - (n - (n / k) * k)) << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll last_check(ll n, ll k) {
  if (n > k - n) {
    return k - n;
  } else {
    return n;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (N == K) {
    cout << 0 << endl;
    return 0;
  } else if (N < K) {
    cout << last_check(N, K) << endl;
    return 0;
  }
  ll tmp = N / K;
  cout << last_check(N - (tmp * K), K) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll last_check(ll &n, ll &k) {
  if (n > k - n) {
    return k - n;
  } else {
    return n;
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
    return 0;
  }
  while (true) {
    if (N == K) {
      cout << 0 << endl;
      break;
    } else if (N < K) {
      cout << last_check(N, K) << endl;
      break;
    }
    N = N - K;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

int64_t n, k;

void solve() { cout << min(n % k, abs(n % k - k)) << endl; }

int main() {
  cin >> n >> k;
  solve();
}

Error: 0

Fixed submission:

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

int n, k;

void solve() { cout << min(n % k, abs(n % k - k)) << endl; }

int main() {
  cin >> n >> k;
  solve();
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int64_t N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
  } else {
    int64_t a = N % K, b = K - a;
    int64_t c = min(a, b);
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t N, K;
  cin >> N >> K;
  if (K == 1) {
    cout << 0 << endl;
  } else {
    for (int i = 0;; i++) {
      int64_t a = max(N, K), b = min(N, K);
      if (N <= a - N) {
        cout << N << endl;
        break;
      } else {
        N = a - b;
      }
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> pi;
typedef pair<int, int> vi;

#define F first
#define S second
#define PB push_back
#define MK make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)

int main() {
  ll N, K;
  cin >> N >> K;
  pi visited = {N};
  ll result = N;
  N = N % K;
  if (N % K == 0) {
    cout << 0 << "\\n";
    exit(0);
  }
  while (N > 0) {
    N = abs(N - K);
    if (result < N) {
      N = 0;
    } else {
      result = N;
    }
  }
  cout << result << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> pi;
typedef pair<int, int> vi;

#define F first
#define S second
#define PB push_back
#define MK make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)

int main() {
  ll N, K;
  cin >> N >> K;
  pi visited = {N};
  ll result = N;
  if (N % K == 0) {
    cout << 0 << "\\n";
    exit(0);
  }
  while (N > 0) {
    N = abs(N - K);
    if (result < N) {
      N = 0;
    } else {
      result = N;
    }
  }
  cout << result << "\\n";
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 1; i <= (n); i++)
int main() {
  long long int n, k;
  cin >> n >> k;
  n = min(n % k, abs(n % k - k));
  cout << n;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 1; i <= (n); i++)
int main() {
  long long int n, k;
  cin >> n >> k;
  while (n > abs(n - k)) {
    if (n % k == 0)
      n = 0;
    n = abs(n - k);
  }
  cout << n;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
#define endl "\\n"
#define deb(x) cerr << #x << ":" << x << "\\n"

void solve() {
  ll n, k, ans;
  cin >> n >> k;
  if (n >= k)
    ans = min(abs(k - n % k), n % k);
  else {
    ans = min(n, abs(n - k));
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}

Error: WA

Fixed submission:

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

typedef long long ll;
#define endl "\\n"
#define deb(x) cerr << #x << ":" << x << "\\n"

void solve() {
  ll n, k;
  cin >> n >> k;
  ll ans = n % k;
  cout << min(ans, n % abs(n - k));
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;

#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define repd(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()

ll n, k, m, M = 1e9 + 7;

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

  cin >> n >> k;

  if (n > k)
    n %= k;

  while (abs(k - n) < n)
    n = abs(k - n);

  cout << n << endl;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;

#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define repd(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()

ll n, k, m, M = 1e9 + 7;

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

  cin >> n >> k;

  while (abs(n - k) < n)
    n = abs(n - k);

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define T long long

T Abs(T n) { return n > 0 ? n : -n; }

int main() {
  T n, k;
  std::cin >> n >> k;
  if (n > k) {
    n = n % k;
  }
  while (1) {
    if (Abs(n - k) >= n) {
      std::cout << n << std::endl;
      return 0;
    } else if (n % k == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
    n = Abs(n - k);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define T long long

T Abs(T n) { return n > 0 ? n : -n; }

int main() {
  T n, k;
  std::cin >> n >> k;
  while (1) {
    if (Abs(n - k) >= n) {
      std::cout << n << std::endl;
      return 0;
    } else if (n % k == 0) {
      std::cout << 0 << std::endl;
      return 0;
    }
    n = Abs(n - k);
  }
}
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 p = pair<int, int>;
typedef long long ll;

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

  cout << min(n % k, k - (n % k)) << 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 p = pair<int, int>;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;
  while (n > 0) {
    n -= k;
  }
  if (n == 0) {
    cout << 0 << endl;
  } else if (n < 0) {
    cout << min(abs(n), abs(n + k)) << endl;
  }

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

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

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

  long long amari = N % K;
  if (amari == 0) {
    cout << "0" << endl;
    exit(0);
  }
  long long tmp = abs(K - amari);
  if (amari < tmp) {
    cout << amari << endl;
  } else {
    cout << tmp << endl;
  }
}


Error: TLE

Fixed submission:

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

long N, K;

void dfs(long long a, long long old) {
  long long ans = abs(a - K);
  if (old != 0) {

    if (ans > a) {
      cout << a << endl;
      exit(0);
    }
  }

  dfs(ans, 1);
}

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

  if (N >= K) {
    long long amari = N % K;
    if (amari == 0) {
      cout << "0" << endl;
      exit(0);
    }
  }

  long long ans = abs(N - K);

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

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

int main() {
  ios_base::sync_with_stdio(false);
  int64 n, k;
  cin >> n >> k;
  int64 min1;
  min1 = min(n % k, abs((n % k) - k));
  cout << min1 << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int64 n, k;
  cin >> n >> k;
  int64 now = abs(n - k);
  int64 min = n;
  if (min >= now) {
    while (1) {
      now = abs(now - k);
      if (min > now)
        min = now;
      else if (min < now)
        break;
    }
  }
  cout << min << endl;
}
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;
  long long t = n % k;
  cout << min(t, k - t) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  long long big = n, small = n;

  while (big % k != 0) {
    big++;
  }
  while (small % k != 0) {
    small--;
  }
  // cout << big << " & " << small << endl;
  cout << min(big % n, n % small) << 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;
  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;
}

Error: TLE

Fixed submission:

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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, n) for (int i = 0; i <= (n); ++i)
#define repe1(i, n) for (int i = 0; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define eb(k, v) emplace_back(k, v)
#define ct(res) cout << res << "\\n";
#define vi vector<int>
#define vl vector<ll>
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const ll MOD = 1000000007;
using Graph = vector<vector<int>>;
ll gcd(ll a, ll b) { return (a % b) ? gcd(b, a % b) : b; }
ll modPow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = modPow(x * x % MOD, n / 2);
  if (n & 1)
    res = res * x % MOD;
  return res;
}
ll mul(ll l, ll r) {
  ll ret = 1;
  for (int i = 0; i < l - r + 1; ++i) {
    ret *= i + r;
    ret %= MOD;
  }
  return ret;
}
ll com(ll n, ll k) {
  ll ret1 = mul(n, n - k + 1);
  ll ret2 = mul(k, 1);
  ll ret3 = modPow(ret2, MOD - 2);
  return ret1 * ret3 % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  ll ans = min(n % k, k - n % k);
  ct(ans);
}


Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repe(i, n) for (int i = 0; i <= (n); ++i)
#define repe1(i, n) for (int i = 0; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define eb(k, v) emplace_back(k, v)
#define ct(res) cout << res << "\\n";
#define vi vector<int>
#define vl vector<ll>
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const ll MOD = 1000000007;
using Graph = vector<vector<int>>;
ll gcd(ll a, ll b) { return (a % b) ? gcd(b, a % b) : b; }
ll modPow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = modPow(x * x % MOD, n / 2);
  if (n & 1)
    res = res * x % MOD;
  return res;
}
ll mul(ll l, ll r) {
  ll ret = 1;
  for (int i = 0; i < l - r + 1; ++i) {
    ret *= i + r;
    ret %= MOD;
  }
  return ret;
}
ll com(ll n, ll k) {
  ll ret1 = mul(n, n - k + 1);
  ll ret2 = mul(k, 1);
  ll ret3 = modPow(ret2, MOD - 2);
  return ret1 * ret3 % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  int ans = min(n % k, k - n % k);
  ct(ans);
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dunk(a) cout << (a) << endl
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using namespace std;
typedef long long ll;

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

  ll r = n % k;
  ll s = abs(n % k - k);

  ll m = min(r, s);
  dunk(min(m, n));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dunk(a) cout << (a) << endl
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using namespace std;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;
  bool a = true;
  ll m = n;

  if (n % k == 0)
    dunk(0);
  else {
    while (a) {
      n -= k;
      if (n < 0)
        break;
    }
    if (m < abs(n))
      dunk(m);
    else
      dunk(abs(n));
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: WA

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  float total = 0.0;
  rep(i, n) {
    cin >> a[i];
    total += a[i];
  }
  int count = 0;
  rep(i, n) {
    if (a[i] * 4 * m >= total) {
      count += 1;
    }
  }
  if (count < m) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: 0

Fixed submission:

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

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

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

Error: 0

Fixed submission:

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


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