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

#line 2 "../../library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pl = pair<ll, ll>;
using vl = vector<ll>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define rep3(i, l, r) for (ll i = l; i < (ll)r; i++)
#define per(i, n) for (ll i = (ll)n - 1; i >= 0; i--)
#define per3(i, l, r) for (ll i = (ll)r - 1; i >= (ll)l; i--)
#define all(v) begin(v), end(v)
template <class T, class U> inline void cmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> inline void cmin(T &a, U b) {
  if (a > b)
    a = b;
}

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(15);
  }
} io_setup;

constexpr char el = '\\n';

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  rep(i, v.size()) os << v[i] << (i + 1 != v.size() ? " " : "");
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}
#line 2 "c.cpp"

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

  if (n > k)
    n %= k;
  ll ans = n;
  ll d = abs(n - k);
  cmin(ans, d);
  cmin(ans, abs(k - d));
  if (k == 1)
    ans = 0;
  cout << ans << el;
}


Error: 0

Fixed submission:

#line 2 "../../library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pl = pair<ll, ll>;
using vl = vector<ll>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define rep3(i, l, r) for (ll i = l; i < (ll)r; i++)
#define per(i, n) for (ll i = (ll)n - 1; i >= 0; i--)
#define per3(i, l, r) for (ll i = (ll)r - 1; i >= (ll)l; i--)
#define all(v) begin(v), end(v)
template <class T, class U> inline void cmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> inline void cmin(T &a, U b) {
  if (a > b)
    a = b;
}

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(15);
  }
} io_setup;

constexpr char el = '\\n';

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  rep(i, v.size()) os << v[i] << (i + 1 != v.size() ? " " : "");
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}
#line 2 "c.cpp"

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

  ll d = n - k;
  cout << k % d << el;
}
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;
#include <algorithm>
using ll = long long;

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;
#include <algorithm>

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

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

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

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

  if (n >= 0) {
    cout << n << endl;
  } else {
    cout << n * (-1) << endl;
  }

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

#include <iostream>

unsigned long long aoki(unsigned long long x, unsigned long long K) {
  return x > K ? x - K : K - x;
}

int main() {
  unsigned long long N, K, x, min, tmp;
  std::cin >> N >> K;

  tmp = N / K;
  x = N - tmp * K;
  while (x > K) {
    x = x > K ? x - K : K - x;
  }

  min = x;
  min = aoki(x, K) < x ? aoki(x, K) : x;

  std::cout << min << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

unsigned long long aoki(unsigned long long x, unsigned long long K) {
  return x > K ? x - K : K - x;
}

int main() {
  unsigned long long N, K, x, min;
  std::cin >> N >> K;

  x = N;
  while (x > K / 2) {
    x = aoki(x, K);
  }

  min = x;
  min = aoki(x, K) < x ? aoki(x, K) : x;

  std::cout << x << std::endl;

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

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

/*ll small(ll n,ll k){
    if(min(n,k) == 1) return 0;
    else if(n % k == 0 || k % n == 0) return min(n,k);
    ll temp1,temp2;
    temp1 = max(n,k);
    temp2 = min(n,k);
    small(temp1 % temp2, temp2)
}*/
int main() {
  ll n, k;
  cin >> n >> k;
  if (n > k)
    n = n % k;
  while (n > abs(n - k)) {
    n = min(n, (abs(n - k)));
  }

  cout << n << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long ll;
int main() {
  ll n, k;
  cin >> n >> k;
  while (n > (ll)abs(n - k)) {
    n = min(n, (ll)(abs(n - k)));
  }
  cout << n << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: 0

Fixed submission:

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

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

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

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


Error: 0

Fixed submission:

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

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long n = 0;
  long long k = 0;
  cin >> n >> k;

  cout << min(n % k, k - n % k) << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long n = 0;
  long long k = 0;
  cin >> n >> k;

  long long checker = 1000000000000000000;

  while (1) {
    checker = n;
    n = abs(n - k);
    if (n >= checker) {
      break;
    }
  }

  cout << checker << "\\n";
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

/* int sub(int x,int y) {
   if ( x > y ) {
     return x-y;
   }
   else {
     return y-x;
   }
 }*/

int main() {
  int64_t n, k;
  cin >> n >> k;
  int64_t m = n % k;
  int64_t l = m - k;

  if (l < 0) {
    l = l * (-1);
  }

  cout << min(l, m) << endl;
}

Error: TLE

Fixed submission:

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

/* int sub(int x,int y) {
   if ( x > y ) {
     return x-y;
   }
   else {
     return y-x;
   }
 }*/

int main() {
  int64_t n, k;
  cin >> n >> k;
  int64_t m = k / 2;

  for (int i = 0; i <= m; i++) {
    if ((n + i) % k == 0 || (n - i) % k == 0) {
      cout << i << endl;
      break;
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long a, b, cpy, i;
  std::cin >> a >> b;
  cpy = b;
  if (a % b == 0)
    std::cout << 0;
  else {
    if (a > b) {
      i = a / b;
      b = b * i;
      a = a - b;
      if (abs(a - cpy) < a)
        a = abs(a - cpy);
    } else if (abs(a - cpy) < a)
      a = abs(a - cpy);
    std::cout << a;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  long long a, b, cpy;
  std::cin >> a >> b;
  cpy = a;
  if (a % b == 0)
    std::cout << 0;
  else {
    while (a % b != 0) {
      a--;
    }
    a = cpy - a;
    if (abs(a - b) < a)
      a = abs(a - b);
    std::cout << a;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long int N;
  long int K;
  cin >> N >> K;
  long int J = N % K;
  if (J <= K - J) {
    cout << J << endl;
  } else {
    cout << K - J << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  int K;
  cin >> N >> K;
  int J = N % K;
  cout << min(J, K - J) << 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;

  if (N == 0 or K == 1) {
    cout << 0 << endl;
    return 0;
  } else {
    cout << min(abs(N % K), abs(N - K * ((N / K) + 1))) << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  long long ans = 0;
  while (1) {
    ans = min(N, abs(N - K));
    N = ans;
    if (ans == min(N, abs(N - K))) {
      break;
    }
  }

  cout << N << endl;

  return 0;
}
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++)
#define ll long long

int main() {
  ll n, k;
  ll a, b, ans;

  cin >> n >> k;

  a = n / k;
  b = n - a * k;
  if (b == 0)
    ans = b;
  else {
    b -= k;
    if (b < 0)
      b *= -1;
    if (n < b)
      b = n;
    if (b == 0)
      ans = 0;
    else
      ans = b;
  }

  std::cout << ans << endl;

  return (0);
}


Error: TLE

Fixed submission:

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

int main() {
  ll n, k;
  ll sa, ans;

  cin >> n >> k;

  ans = n;
  while (true) {
    sa = ans - k;
    if (sa < 0)
      sa *= -1;
    if (ans > sa)
      ans = sa;
    else
      break;
  }

  std::cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k, t, ans;
  cin >> n >> k;

  t = n % k;
  if (t > k - t) {
    ans = k - t;
  } else {
    ans = t;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, t;
  cin >> n >> k;
  t = n % k;

  int ans = min(t, k - t);
  cout << ans << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>

int main() {
  using namespace std;
  long long int N, K;
  cin >> N >> K;
  long long int t = N % K;
  // cout << t;
  long long int tmp = abs(t - K);
  if (tmp <= t)
    cout << tmp;
  else
    cout << t;

  return 0;
}

Error: WA

Fixed submission:

#include <cmath>
#include <iostream>

int main() {
  using namespace std;
  int N, K;
  cin >> N >> K;
  int t = N % K;
  // cout << t;
  int tmp = abs(t - K);
  if (tmp <= t)
    cout << tmp;
  else
    cout << t;

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

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

using lint = long long;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int64_t n, k;
  cin >> n >> k;
  if (k == 1) {
    cout << "0\\n";
    exit(0);
  }
  int64_t d = n / k;
  int64_t res = n - d * k;
  cout << min(abs(res), abs(res - k)) << '\\n';
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast") // enable all O3 optimizations
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2")                                        // Enable AVX
#include <bits/stdc++.h>
using namespace std;

using lint = long long;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int64_t n, k;
  cin >> n >> k;
  if (k == 1) {
    cout << "0\\n";
    exit(0);
  }
  int64_t res = LLONG_MAX;
  while (n >= k)
    n = abs(n - k);

  cout << min(n, abs(n - k)) << '\\n';
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, k, s, ans;
  cin >> n >> k;
  s = n % k;
  if (s > k / 2)
    ans = abs(s - k);
  else
    ans = s;
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

#include <bits/stdc++.h>

using namespace std;

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

    return;
  };
};

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

    return;
  };
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: 0

Fixed submission:

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

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

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

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

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));
}
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 q = N % K;
  int64_t p = (N - q) / K;

  if (q <= K / 2) {
    cout << q << endl;
  } else {
    cout << K * (p + 1) - N << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  int q = N % K;
  int p = (N - q) / K;

  if (q < K / 2) {
    cout << q << endl;
  } else {
    cout << K * (p + 1) - N << endl;
  }
}
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;
  ll s = N % K;
  cout << min(s, K - s) << endl;
}

Error: 0

Fixed submission:

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

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

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

#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  fast_io;
  vector<long long> arr;

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

  long long ans = abs(n - k);
  bool loop = false;
  while (loop == false) {
    if (find(arr.begin(), arr.end(), n) != arr.end()) {
      loop = true;
    }
    if (n > k) {
      arr.push_back(n % k);
      n = n % k;
    } else {
      arr.push_back(abs(n - k));
      n = abs(n - k);
    }
    for (int i = 0; i < arr.size(); i++) {
    }
    ans = min(ans, n);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  fast_io;
  vector<int> arr;

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

  long long ans = abs(n - k);
  bool loop = false;
  while (loop == false) {
    if (find(arr.begin(), arr.end(), n) != arr.end()) {
      loop = true;
    }
    if (n > k) {
      arr.push_back(n % k);
      n = n % k;
    } else {
      arr.push_back(abs(n - k));
      n = abs(n - k);
    }
    ans = min(ans, n);
  }

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

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

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
  long long N, K;
  cin >> N >> K;
  bool zero = false;
  long long a = N % K;
  long long b = a - K;
  b = -b;
  if (b > a)
    cout << a << endl;
  else
    cout << b << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

int main() {
  long long N, K;
  cin >> N >> K;
  bool zero = false;
  long long a = N;
  long long b;
  while (!zero) {
    a = a - K;
    if (a < 0) {
      zero = true;
      b = a;
      a = a + K;
    }
  }
  b = -b;
  if (b > a)
    cout << a << endl;
  else
    cout << b << endl;
}
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 (N % K == 0) {

    cout << 0 << endl;

  } else {

    N = N % K;

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


Error: TLE

Fixed submission:

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

int main() {

  long long int N, K;

  cin >> N >> K;

  if (N % K == 0) {

    cout << 0 << endl;

  } else {
    while (N - K > 0) {

      N = N - K;
    }

    if (K - N > N) {
      cout << N << endl;
    } else {
      cout << K - N << endl;
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;

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

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
long long gcd(long long a, long long b) {
  if (a < b)
    return gcd(b, a);
  long long r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
int main() {
  long long N, K;
  cin >> N >> K;
  long long t = gcd(N, K);
  cout << t % K;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

  /*   while(1){
          N=abs(N-K);
          a=min(a,N);
          if(a!=N){
              cout<<a<<endl;
              exit(0);
          }
      }*/
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

  while (1) {
    N = abs(N - K);
    a = min(a, N);
    if (a != N) {
      cout << a << endl;
      exit(0);
    }
  }
}