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

/**
 *	  Name: Ajay
 *	  Institute: IIITH
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
#define ll long long
#define inf 0x7fffffff
#define mod 1000000007
#define pb push_back
#define ppi pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vii vector<ppi>
#define vll vector<ll>
#define mp make_pair
#define fi first
#define se second
using namespace std;

void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

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

  while (n > k) {
    n = n % k;
  }

  ll m1 = abs(n - k);
  cout << min(n, m1);
  return 0;
}

Error: WA

Fixed submission:

/**
 *	  Name: Ajay
 *	  Institute: IIITH
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
#define ll long long
#define inf 0x7fffffff
#define mod 1000000007
#define pb push_back
#define ppi pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vii vector<ppi>
#define vll vector<ll>
#define mp make_pair
#define fi first
#define se second
using namespace std;

void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

int main() {
  fastio();

  ll n, k;
  cin >> n >> k;
  if (n > k && n % k == 0) {
    cout << 0;
    return 0;
  } else if (n < k && k % n == 0) {
    cout << n;
    return 0;
  }

  while (n > k && n > 0) {
    n = n % k;
  }
  ll m1 = abs(n - k);
  cout << min(n, m1);
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll a, b;
  cin >> a >> b;
  ll c = a / b;
  a = a - b * c;
  a = abs(a - b);
  if (a > b / 2)
    cout << b - a << endl;
  else
    cout << a << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int a, b;
  cin >> a >> b;
  int c = a / b;
  a = a - b * c;
  a = abs(a - b);
  if (a > b / 2)
    cout << b - a << endl;
  else
    cout << a << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long ll;
typedef vector<bool> vecbool;
typedef vector<int> vecint;
typedef vector<ll> vecll;
typedef vector<string> vecstr;
typedef vector<vecint> vec2int;
typedef vector<vecbool> vec2bool;
int dx[4]{-1, +1, 0, 0};
int dy[4]{0, 0, -1, +1};
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  if (a < b)
    return gcd(b, a);
  return gcd(b, a % b);
}
void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
int main() {
  init();
  ll n, k;
  cin >> n >> k;
  ll g = gcd(n, k);
  // cout << g << endl;
  if (n < k) {
    cout << min(n, abs(n - k)) << endl;
  } else if (n % k == 0) {
    cout << 0 << endl;
  } else {
    ll ans = 1e18;
    ans++;
    n /= g;
    k /= g;
    n = n - k * (n / k);
    while (true) {
      n = abs(n - k);
      ans = min(ans, n);
      if (ans < n) {
        cout << ans * g << endl;
        return 0;
      } else if (k % n == 0) {
        cout << min(n, abs(n - k)) * g << endl;
        return 0;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long ll;
typedef vector<bool> vecbool;
typedef vector<int> vecint;
typedef vector<ll> vecll;
typedef vector<string> vecstr;
typedef vector<vecint> vec2int;
typedef vector<vecbool> vec2bool;
int dx[4]{-1, +1, 0, 0};
int dy[4]{0, 0, -1, +1};
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  if (a < b)
    return gcd(b, a);
  return gcd(b, a % b);
}
void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
int main() {
  init();
  ll n, k;
  cin >> n >> k;
  ll g = gcd(n, k);
  // cout << g << endl;
  if (n < k) {
    cout << min(n, abs(n - k)) << endl;
  } else if (n % k == 0) {
    cout << 0 << endl;
  } else {
    ll ans = 1e18;
    ans++;
    while (true) {
      n = abs(n - k);
      ans = min(ans, n);
      if (ans < n) {
        cout << ans << endl;
        return 0;
      } else if (k % n == 0) {
        cout << min(n, abs(n - k)) << endl;
        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++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

  N = N % K;
  ll MIN = N;
  REP(i, 5) {
    N = K - N;
    MIN = min(N, MIN);
  }

  cout << MIN << endl;
}

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++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

  N = N % K;
  ll MIN = N;
  REP(i, K) {
    N = K - N;
    MIN = min(N, MIN);
  }

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

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

inline int read() {
  re int ans = 0;
  re bool f = 1;
  re char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ans = (ans << 3) + (ans << 1) + (ch ^ 48);
    ch = getchar();
  }
  return f ? ans : -ans;
}
int minn(int a, int b) { return a > b ? b : a; }
int aBs(int a) { return a > 0 ? a : -a; }
signed main(void) {
  int n, k;
  n = read();
  k = read();
  printf("%lld\\n", min(aBs(n % k - k), n % k));
  return 0;
}


Error: WA

Fixed submission:

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

inline int read() {
  re int ans = 0;
  re bool f = 1;
  re char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    ans = (ans << 3) + (ans << 1) + (ch ^ 48);
    ch = getchar();
  }
  return f ? ans : ~(ans - 1);
}
int _gcd(int a, int b) { return b == 0 ? a : _gcd(b, a % b); }

signed main(void) {
  int n, k;
  n = read();
  k = read();
  if (k > n)
    swap(k, n);
  printf("%lld\\n", n % k);
  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;

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

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

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

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

  return 0;
}

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;

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

  ll ans = n;
  while (true) {
    if (n == 1) {
      cout << 1 << endl;
      return 0;
    } else if (n == k) {
      cout << 0 << endl;
      return 0;
    } else if (n > k) {
      n %= k;
      if (n == 0) {
        cout << 0 << endl;
        return 0;
      }
      ans = min(ans, n);
    } else {
      n = k % n;
      if (n == 0) {
        cout << ans << endl;
        return 0;
      }

      ans = min(ans, n);
    }
  }

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

#include <iostream>

using namespace std;

int main() {
  long long N, K;

  cin >> N >> K;

  long long min = N;
  long long tmp = 0;
  if (N % K == 0) {
    cout << "0" << endl;
    return 0;
  } else {
    N = N % K;
  }

  while (true) {
    if (N - K > 0) {
      N = N - K;
    } else {
      N = K - N;
    }
    if (min > N) {
      min = N;
      // cout << "Min: " << min << endl;
    } else {
      break;
    }
  }

  cout << min << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  long long N, K;

  cin >> N >> K;

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

  while (true) {
    if (N - K > 0) {
      N = N - K;
    } else {
      N = K - N;
    }
    if (min > N) {
      min = N;
      // cout << "Min: " << min << endl;
    } else {
      break;
    }
  }

  cout << min << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned 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;
int main() {
  int n, k;
  cin >> n >> k;
  cout << min(n % k, 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 p = n % k;
  int64_t q = abs(p - k);
  cout << min(p, q);
}


Error: WA

Fixed submission:

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

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

long long n, k;

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


Error: 0

Fixed submission:

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

#include <iostream>
using namespace std;
int main(void) {
  long long n, k;
  cin >> n >> k;
  long long t = n % k;
  long long t2 = k - t;
  t2 = (t2 < 0) ? -t2 : t2;
  if (t2 < t) {
    cout << t2 << endl;
  } else {
    cout << t << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  int n, k;
  cin >> n >> k;
  int t = n % k;
  int t2 = k - t;
  t2 = (t2 < 0) ? -t2 : t2;
  if (t2 < t) {
    cout << t2 << endl;
  } else {
    cout << t << endl;
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <numeric>
using llong = long long;

int main(void) {
  llong n, k;
  std::cin >> n >> k;
  std::cout << std::min(n % k, k - n % k) << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <numeric>

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

#include <algorithm>
#include <iostream>
#define ULL unsigned long long

using namespace std;

ULL lcm(ULL a, ULL b) {
  if (a % b == 0) {
    return b;
  }
  return lcm(b, a % b);
}

int main() {
  ULL n, m;
  cin >> n >> m;
  cout << min(n - (n / m) * m, (n / m + 1) * m - n);
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int lcm(int a, int b) {
  if (a % b == 0) {
    return b;
  }
  return lcm(b, a % b);
}

int main() {
  int n, k;
  cin >> n >> k;
  cout << lcm(n, k) << endl;

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

#include <iostream>
using namespace std;
#define int unsigned long long

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

  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;

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

  if (k > n)
    cout << n;
  else
    cout << k - (n % k);
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define F first
#define S second
#define f first
#define s second
#define fore(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define forr(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
ll faster_in() {
  ll r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-')
    return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = ll(1e9 + 7);
const lldb PI = acos(-1);
#define tam 320000
const ll MOD = 1e9 + 7;

int main() {
  ll a, b;
  cin >> a >> b;
  if (a % b == 0)
    cout << 0 << endl;
  else {
    a %= b;
    cout << min(b - a, a) << endl;
  }
}
// PLUS ULTRA!

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
typedef int ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define F first
#define S second
#define f first
#define s second
#define fore(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define forr(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
ll faster_in() {
  ll r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-')
    return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = ll(1e9 + 7);
const lldb PI = acos(-1);
#define tam 320000
const ll MOD = 1e9 + 7;

int main() {
  ll a, b;
  cin >> a >> b;
  if (a % b == 0)
    cout << 0 << endl;
  else {
    a %= b;
    cout << min(b - a, a) << endl;
  }
}
// PLUS ULTRA!
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long x, k;
  cin >> x >> k;
  long long remainder = x % k;
  long long ans = min(remainder, k - remainder);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long x, k;
  cin >> x >> k;
  long long remainder = max(x, k) % min(x, k);
  long long ans = min(remainder, k - remainder);
  cout << ans << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

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

  // N %= K;
  // int a = (N > K ? N - K : K - N);
  // if (N > a) {
  //   std::cout << a << '\\n';
  // } else {
  //   std::cout << N << '\\n';
  // }

  long long t = N % K;
  if (t < K - t) {
    std::cout << t << '\\n';
  } else {
    std::cout << K - t << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

// unsigned long abs(long long x) { return x >= 0 ? x : -x; }

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

  // std::cout << N << abs(N - K) << '\\n';

  while (N > (N > K ? N - K : K - N)) {
    // std::cout << (N > K ? N - K : K - N) << '\\n';
    N = (N > K ? N - K : K - N);
  }

  std::cout << N << '\\n';
  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++)

typedef long long ll;
typedef long double ld;

int main() {

  ll N, K;
  cin >> N >> K;

  N = N - (N / K) * K;

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

  cout << N << 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++)

typedef long long ll;
typedef long double ld;

int main() {

  ll N, K;
  cin >> N >> K;

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

  cout << N << endl;

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

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

Error: WA

Fixed submission:

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

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

int main() {
  ll a, b, c, ans;
  cin >> a >> b;
  c = a % b; // cout<<c<<endl;
  if (c == 0) {
    cout << 0 << endl;
    return 0;
  }
  ans = abs(c - b);

  if (ans < a)
    cout << ans << endl;
  else
    cout << a << endl;
}


Error: 0

Fixed submission:

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

int main() {
  ll a, b, c, ans;
  cin >> a >> b;
  c = abs(a - b); // cout<<c<<endl;
  if (c == 0) {
    cout << a << endl;
  }
  ans = a % c;

  if (c > b / 2 && b - c >= 0)
    ans = b - c;
  cout << ans << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
long long min(const long long &a, const long long &b) { return a < b ? a : b; }
using namespace std;
long long n, k;
int main() {
  scanf("%lld %lld", &n, &k);
  long long tmp = n % k;
  printf("%lld\\n", min(tmp, k - tmp));
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
long long k, n, ans;
int main() {
  cin >> n >> k;
  if (n > k) {
    ans = n % k;
    ans = k - ans;
  } else {
    ans = k % n;
    ans = n - ans;
  }
  cout << ans << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y;
  cin >> x >> y;
  x %= y;
  while (2 * x > y) {
    x = abs(x - y);
  }
  cout << x << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, i, d, temp;
  cin >> a >> b;
  d = a;

  if (b == 1) {
    d = 0;
  }

  else {
    while (1) {

      temp = abs(d - b);
      if (temp > d)
        break;
      else if (temp < d)
        d = temp;
    }
  }
  cout << d;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  while (n > abs(n - k)) {
    n = abs(n - k);
  }
  cout << n << 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 (K == 1) {
    cout << 0 << endl;
    return 0;
  }

  N = N % K;

  long long ans = N;
  while (1) {
    long long tmp = llabs(llabs(N) - llabs(K));
    N = tmp;
    if (N < ans) {
      ans = N;
    } else {
      break;
    }
  }
  cout << ans << endl;
}


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 = N;
  while (1) {
    long long tmp = llabs(llabs(N) - llabs(K));
    N = tmp;
    if (N < ans) {
      ans = N;
    } else {
      break;
    }
  }
  cout << ans << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;
  n = n % abs(n - k);
  cout << n % k << 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;       // N7 K4
  long long M = N % K; // M3
  long long O = K - M; // O1

  cout << min(M, O);
}


Error: WA

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  int M = N % K;
  int O = M - K;
  if (O < 0)
    O = K - M;

  if (M < O)
    cout << M;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

unsigned long long int N, K;

int main() {

  cin >> N >> K;

  unsigned long long int q = N % K;
  unsigned long long int answer;

  answer = min(q, K - q);

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

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

unsigned long int N, K;

unsigned long int abs_value(unsigned long int n, unsigned long int m) {

  unsigned long int res;

  if (n >= m)
    res = n - m;
  else
    res = -(n - m);

  return res;
}

int main() {

  cin >> N >> K;

  vector<unsigned long int> result(N);

  unsigned long int i = 0;

  while (true) {
    N = abs_value(N, K);
    result[i] = N;
    i++;
    if (result[i] >= result[i - 1]) {
      printf("%ld\\n", result[i - 1]);
      break;
    }
  }

  return 0;
}