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

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

int main() {
  long N, K, ans, dif;
  cin >> N >> K;
  ans = N / K;
  N = N - K * ans;
  dif = N - K;
  if (dif < 0)
    dif = -1 * dif;
  while (N >= dif + 1) {
    N = dif;
    dif = N - K;
    if (dif < 0)
      dif = -1 * dif;
  }
  cout << N << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  long N, K, dif;
  cin >> N >> K;
  dif = N - K;
  if (dif < 0)
    dif = -1 * dif;
  while (N >= dif + 1) {
    N = dif;
    dif = N - K;
    if (dif < 0)
      dif = -1 * dif;
  }
  cout << N << 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;
  cout << min(n % k, 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, k - (n % k));
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N, K;
  cin >> N >> K;
  if (K == 0) {
    cout << N << endl;
  } else if (K == 1) {
    cout << 0 << endl;
  } else if (N % K == 0) {
    cout << 0 << endl;
  } else if (N > K) {
    ll a = N % K;
    if (abs(a - K) < abs(a)) {
      cout << abs(a - K) << endl;
    } else {
      cout << abs(a);
    }
  } else {
    ll ab = abs(N - K);
    while (ab < N) {
      N = ab;
      ab = abs(N - K);
    }
    cout << N << endl;
  }
}

Error: TLE

Fixed submission:

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

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

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

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


Error: 0

Fixed submission:

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

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

#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <vector>
typedef long long int ll;
using namespace std;

int main() {
  ll N, M, a, b;
  cin >> N >> M;
  a = N % M;
  cout << min(a, abs(a - M)) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <vector>
typedef long long int ll;
using namespace std;

int main() {
  ll N, M, a, b;
  cin >> N >> M;
  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  while (1) {
    a = abs(N - M);
    if (N < a) {
      cout << N << endl;
      return 0;
    } else {
      N = a;
    }
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

//...Bismillahir Rahmanir Rahim...
// Code by    Asad Bin Saber
// 04 04 2020

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

using namespace std;

void task() {
  long long n, k, i;

  cin >> n >> k;

  n = n % k;

  while (1) {
    if (n == 0)
      break;
    else if (n < abs(k - n))
      break;
    n = abs(n - k);
  }

  cout << n << endl;
}

int main() {
  task();

  return 0;
}


Error: TLE

Fixed submission:

//...Bismillahir Rahmanir Rahim...
// Code by    Asad Bin Saber
// 04 04 2020

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

using namespace std;

void task() {
  long long n, k, i;

  cin >> n >> k;

  if (n % k == 0) {
    cout << '0' << endl;
    return;
  } else if (k == 1) {
    cout << '0' << endl;
    return;
  } else if (k == 2 && n % 2 == 1) {
    cout << '1' << endl;
  } else if (k == 2 && n % 2 == 0) {
    cout << '0' << endl;
  }
  while (1) {
    if (n == 0)
      break;
    else if (n < abs(k - n))
      break;
    n = abs(n - k);
  }

  cout << n << endl;
}

int main() {
  task();

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

  n = n % k;

  n = min(n, k - n);

  cout << n << endl;
}

Error: TLE

Fixed submission:

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

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

  cout << n << 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 ans1 = N % K;
  long long ans2 = abs(ans1 - K);
  if (ans1 > ans2) {
    cout << ans2 << endl;
  } else
    cout << ans1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {

  int N, K;
  cin >> N >> K;
  int ans1 = N % K;
  int ans2 = abs(ans1 - K);
  if (ans1 > ans2) {
    cout << ans2 << endl;
  } else
    cout << ans1 << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
//	freopen("output.txt", "w", stdout);
int main() {
  unsigned ll n, k;
  scanf("%llu %llu", &n, &k);
  if ((n - k) == 0) {
    cout << 0 << endl;
  } else if (n < k) {
    unsigned ll z = k & n;
    if (z == 0)
      printf("%llu\\n", n);
    else {
      ll v = n * 2;
      if (v < k) {
        printf("%llu\\n", n);
      } else {
        ll x = k - n;
        printf("%lld\\n", x);
      }
    }
    /* unsigned ll x=n-k;
     if(x<0)
         x*=-1;
     if(n>x)
     printf("%llu\\n",x);
     else
          printf("%llu\\n",n);*/
  } else {
    unsigned ll c = n % k;
    if (c == 0) {
      cout << 0 << endl;
    } else {
      ll q;
      if (c > (k - c)) {
        q = k - c;
      } else
        q = c;
      printf("%llu\\n", q);
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
//	freopen("output.txt", "w", stdout);
int main() {
  unsigned ll n, k;
  scanf("%llu %llu", &n, &k);
  if ((n - k) == 0) {
    cout << 0 << endl;
  } else if (n < k) {
    if (k % n == 0)
      printf("%llu\\n", n);
    else {
      if (k / n > 1) {
        printf("%llu\\n", n);
      } else {
        unsigned ll x = k - n;
        printf("%llu\\n", x);
      }
    }
    /* unsigned ll x=n-k;
     if(x<0)
         x*=-1;
     if(n>x)
     printf("%llu\\n",x);
     else
          printf("%llu\\n",n);*/
  } else {
    ll c = n % k;
    if (c == 0) {
      cout << 0 << endl;
    } else {
      unsigned ll q;
      if (c > (k - c)) {
        q = k - c;
      } else
        q = c;
      printf("%llu\\n", q);
    }
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll a = 0, b = 0, n, k;
  cin >> n >> k;
  if (n > k && n % k == 0) {
    cout << 0;
    return 0;
  } else if (n > k && n % k != 0) {
    ll t;
    t = n % k;
    cout << k - t;
    return 0;
  } else {
    while (b <= a) {
      a = n;
      n = abs(n - k);
      b = n;
    }

    cout << a;
    return 0;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll a = 0, b = 0, n, k;
  cin >> n >> k;
  if (n > k && n % k == 0) {
    cout << 0;
    return 0;
  } else {
    while (b <= a) {
      a = n;
      n = abs(n - k);
      b = n;
    }

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

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  long long int n, k;
  cin >> n >> k;
  if (n >= k) {
    if (n % k == 0)
      cout << "0";
    else {
      long long int m;
      m = n % k;
      if (m >= k - m)
        cout << k - m;
      else
        cout << m;
    }
  } else {
    if (n == 0)
      cout << "0";
    else {
      if (k % n == 0)
        cout << n;
      else {
        if (n >= k - n)
          cout << k - n;
        else
          cout << n;
      }
    }
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  long long int n, k;
  cin >> n >> k;
  if (n >= k) {
    if (n % k == 0)
      cout << "0";
    else
      cout << gcd(n, k);
  } else {
    if (k % n == 0)
      cout << n;
    else
      cout << gcd(n, k);
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

  unsigned long int min_num = N % K;

  if (min_num > -1 * (min_num - K))
    min_num = -1 * (min_num - K);
  cout << min_num << endl;
  return 0;
}


Error: 0

Fixed submission:

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

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

  int min_num = N % K;

  if (min_num > -1 * (min_num - K))
    min_num = -1 * (min_num - K);
  cout << min_num << 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;
  cin >> n >> k;
  cout << min(n % k, k - n % k);
}


Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  int X;
  X = N % K;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define repb(bit, n, init) for (ll bit = init; bit < (1 << n); ++bit)

int main() {
  ll N, K;
  cin >> N >> K;
  if (N % K == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (abs(N - K) >= N) {
    cout << N << endl;
    return 0;
  }
  ll cnt = 0;
  N %= K;
  while (true) {
    ll nextN = abs(N - K);
    if (N <= nextN) {
      cout << N << endl;
      return 0;
    }
    N = nextN;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define repb(bit, n, init) for (ll bit = init; bit < (1 << n); ++bit)

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

#include <iostream>
using namespace std;
typedef long long llong;

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

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
typedef long long llong;

int main() {
  int i;
  llong n, k;
  cin >> n >> k;
  int ans = n % k;
  int ans2 = k % ans;
  ans = min(ans, ans2);
  cout << ans << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

long long n, k, a;

int main() {
  cin >> n >> k;
  a = n % k;
  if (k - a >= a)
    cout << a;
  else
    cout << k - a;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

int n, k, a;

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

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

int main() {
  int64_t N, K;
  cin >> N >> K;
  int64_t s = N % K;
  if (N == 0) {
    cout << 0;
  } else if (s < K - s) {
    cout << s;
  } else {
    cout << K - s;
  }
}

Error: WA

Fixed submission:

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

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

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

int main() {
  long N, K;
  cin >> N >> K;
  long x, y;
  x = N % K;

  y = K - x;
  cout << min(x, y) << endl;
}


Error: 0

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(void) {
  ll M, N;
  ll a;
  cin >> N >> M;

  N = N % M;

  while (N > abs(N - M)) {
    N = abs(N - M);
  }
  cout << N;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(void) {
  ll M, N;
  ll a;
  cin >> N >> M;

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

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

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

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

  min = n % k;
  tmp_min = abs(min - k);
  if (tmp_min < min) {
    min = tmp_min;
  }

  cout << min << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long n, k, min, tmp_min = -10;
  cin >> n >> k;

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

  min = abs(n - k);
  while (1) {
    tmp_min = abs(min - k);
    if (tmp_min < min) {
      min = tmp_min;
    } else {
      break;
    }
  }

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

#include <algorithm>
#include <iostream>

int main() {
  long long int n, k;
  std::cin >> n >> k;
  if (n == k || n == 0)
    std::cout << "0\\n";
  else {
    if (n > k) {
      if (n % k == 0)
        std::cout << "0\\n";
      else
        std::cout << std::min(k - n % k, n % k) << '\\n';
    } else
      std::cout << std::min(k - n, n) << '\\n';
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

int main() {
  long long int n, k, aux;
  std::cin >> n >> k;
  if (n == k)
    std::cout << "0\\n";
  else {
    if (n < k) {
      aux = n;
      n = k;
      k = aux;
    }

    std::cout << k - n % k << '\\n';
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  n %= m;
  if (n > m / 2ll)
    n = m - n;
  cout << n;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  n %= m;
  if (n > m / 2)
    n = m - n;
  cout << n;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>

using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long prev_n = n;
  while (true) {
    if (prev_n < n) {
      n = prev_n;
      break;
    } else {
      prev_n = n;
      if (n > k and n / -1 == -n and k / -1 == -k and n % k == 0) {
        n = 0;
        break;
      } else {
        n = abs(k - n);
      }
    }
  }
  cout << n << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

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

int main() {
  ll n, k;
  cin >> n >> k;
  ll a = n % k;
  ll ans = min(a, abs(a - k));
  cout << ans << endl;
  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;
typedef pair<int, int> P;

int main() {
  int n, k;
  cin >> n >> k;
  int a = n % k;
  int ans = min(a, abs(a - k));
  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 int n, k;
  cin >> n >> k;
  long long int m = n % k;
  m = min(m, k - m);
  cout << m << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  long long int n, k;
  cin >> n >> k;
  long long int minnumber = (n % k);
  int counter = 0;
  for (int i = 0;; i++) {
    if (minnumber == 0) {
      cout << 0 << endl;
      return 0;
    }
    if (counter == 2) {
      break;
    }
    minnumber = min(minnumber, abs(minnumber - k * i));
    if (n - k * i < 0) {
      counter += 1;
    }
  }
  cout << minnumber << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
int main() {
  int64 n, k;
  cin >> n >> k;
  int64 r = (int64)n % k;
  cout << min(r, k - r) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int testcase = 1;
  // cin>>testcase;
  for (int p = 0; p < testcase; p++) {
    ll n = 0, k = 0;
    cin >> n >> k;
    n = n % k;
    cout << min(n, abs(n - k));
  }

  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int testcase = 1;
  // cin>>testcase;
  for (int p = 0; p < testcase; p++) {
    ll n = 0, k = 0;
    cin >> n >> k;
    cout << min((n) % (abs(n - k)), (n % k));
  }

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

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

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


Error: 0

Fixed submission:

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

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

#include <bits/stdc++.h>

#define debug(x) cerr << #x << ": " << x << "\\n"
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define _sort(a) sort(a.begin(), a.end())

using namespace std;

using ll = long long;
using vi = vector<int>;
using vii = vector<int, int>;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  long long int n, k, d, mn = 1e18;
  cin >> n >> k;
  mn = n = n % k;
  while (true) {
    d = abs(n - k);
    if (d < mn)
      mn = d;
    else
      break;
    n = mn;
  }
  cout << mn;
}
/*
 *   created: 2020-04-04 17:24:49
 */

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define debug(x) cerr << #x << ": " << x << "\\n"
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define _sort(a) sort(a.begin(), a.end())

using namespace std;

using ll = long long;
using vi = vector<int>;
using vii = vector<int, int>;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  long long int n, k, d, mn = 1e18;
  cin >> n >> k;
  mn = n;
  if (k == 1)
    return cout << 0, 0;
  while (true) {
    d = abs(n - k);
    if (d < mn)
      mn = d;
    else
      break;
    n = mn;
  }
  cout << mn;
}
/*
 *   created: 2020-04-04 17:24:49
 */