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

#include <iostream>
using namespace std;
using ll = long long;
int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
int main() {
  ll N, K;
  ll ans;
  cin >> N >> K;
  ans = min(N % K, abs(N % K - K));
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
int main() {
  int N, K;
  int ans;
  cin >> N >> K;
  ans = min(N % K, abs(N % K - K));
  cout << ans << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define mod 1000000007
#define fi first
#define se second
#define endl '\\n'
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

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

  ll p = abs(n - x * k);
  ll q = abs(n - (x + 1) * k);

  cout << min(p, q) << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define mod 1000000007
#define fi first
#define se second
#define endl '\\n'
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

int main() {
  ll a, b, i;
  cin >> a >> b;

  if (a == 1 || b == 1) {
    cout << 0;
  } else {
    ll x = max(a, b);
    ll y = min(a, b);

    ll p = x % y;
    ll ans = abs(y - p);
    cout << ans;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  long long int n, k, x, y, f;

  cin >> n >> k;

  f = (n / k) + 1;

  n = n - (k * f);

  x = -n;

  y = n + k;

  if (x < y) {

    cout << x;

  } else {

    cout << y;
  }
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, k, x, y, f;

  cin >> n >> k;

  f = (n / k) + 1;

  n = n - (k * f);

  x = -n;

  y = n + k;

  if (x < y) {

    cout << x;

  } else {

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

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


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int x, y;
  cin >> x >> y;
  while (x % y == 0) {
    x /= y;
  }
  return x % y == 1;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

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

  ll y = N % K;

  ll x = min(y, K - y);

  cout << x << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

long long smallest_result(long long N, long long K) {

  if (K == 1) {
    return 0;
  }

  else {
    N = N % K;
    N = llabs(N - K);

    if (llabs(N - K) > N) {
      return N;
    } else if (llabs(N - K) == N) {
      return N;
    } else if (N == 0) {
      return 0;
    } else {
      return smallest_result(N, K);
    }
  }
}

int main() {

  long long N, K;

  std::cin >> N >> K;

  std::cout << smallest_result(N, K) << "\\n";
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <queue>
#include <string>

#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

int main(void) {
  long n, k, res, tmp;

  cin >> n >> k;

  res = n % k;
  tmp = k - res;

  if (tmp < 0)
    tmp *= -1;

  if (res < tmp)
    cout << res << endl;
  else
    cout << tmp << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {

  int N, K, tmp, Res, Prev;

  cin >> N >> K;

  Res = N;
  Prev = N;

  while (1) {
    tmp = Res - K;

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

    if ((tmp - Res) > 0) {
      break;
    }

    if (Res > tmp) {
      Prev = Res;
      Res = tmp;
    }
  }

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

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

using namespace std;

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


Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  while (n > abs(n - k)) {
    n = abs(n - k);
  }
  cout << n << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, k;
  cin >> n >> k;
  if (n > k) {
    n = n % k;
    n = min(n, k - n);
  } else {
    n = min(n, k - n);
  }
  cout << n << 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 ll = long long;
using P = pair<int, int>;

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

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

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

int main() {
  // sengen
  long n, k;

  // nyuryoku
  cin >> n >> k;
  // keisan
  if (n % k == abs(n % k - k)) {
    cout << n % k;
  } else {
    cout << min(n % k, abs(n % k - k));
  }
}

Error: WA

Fixed submission:

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

int main() {
  // sengen
  int n, k;

  // nyuryoku
  cin >> n >> k;
  // keisan
  if (n % k == abs(n % k - k)) {
    cout << n % k;
  } else {
    cout << min(n % k, abs(n % k - k));
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int main() {

  ll N, K;
  scanf("%lld %lld", &N, &K);

  if (N == 0) {

    printf("0\\n");
    return 0;
  }

  ll tmp = N % K;
  if (tmp == 0) {

    printf("0\\n");
    return 0;
  }

  printf("%lld\\n", min(tmp, abs(tmp - K)));

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int main() {

  ll N, K;
  scanf("%lld %lld", &N, &K);

  if (N == 0) {

    printf("0\\n");
    return 0;
  }

  ll tmp = N % K;

  printf("%lld\\n", K % tmp);

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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

ll GCD(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }

  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

ll LCM(ll a, ll b) {
  ll g = GCD(a, b);
  return a * b / g;
}

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

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

  N %= K;
  cout << min(N, K - N) << "\\n";
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

ll GCD(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }

  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

ll LCM(ll a, ll b) {
  ll g = GCD(a, b);
  return a * b / g;
}

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

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

  ll ans = GCD(N, K);

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

#include <iostream>

using namespace std;

typedef long long ll;

ll replace(ll &N, ll &K) {
  ll a = abs(N - K);
  if (N < a) {
    return N;
  } else {
    if (N % K == 0)
      return 0;
    return replace(a, K);
  }
}

ll fun(ll N, ll K) {
  ll amari = N % K;
  return min(amari, abs(amari - K));
}

int main() {

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

  // cout << replace(N, K) << endl;

  cout << fun(N, K) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

typedef long long ll;

ll replace(ll &N, ll &K) {
  if (N % K == 0)
    return 0;

  ll a = abs(N - K);
  if (N < a) {
    return N;
  } else {
    return replace(a, K);
  }
}

int main() {

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

  cout << replace(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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {

  ll N, K;
  cin >> N >> K;
  ll t = N % K;
  cout << min(t, K - t) << 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 ll = long long;
using P = pair<int, int>;

int main() {

  long long N, K;
  cin >> N >> K;
  while (llabs(N - K) < N) {
    N = llabs(N - K);
  }
  cout << N << 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;
  while (cin >> N >> k) {
    long long int x, d;
    if (N % k == 0) {
      x = 0;
      goto en;
    }
    if (N < k) {
      d = k - N;
      if (d > N) {
        x = N;
      }
      if (d <= N) {
        x = d;
      }
      goto en;
    }
    if (N > k) {
      d = N / k;
      long long int n = d * k;
      N = N - n;
      while (N >= 0) {
        N = N - k;
      }
      x = -N;
      goto en;
    }
  en:
    cout << x << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  long long n, k, l, i = 0;
  scanf("%lld %lld", &n, &k);
  if (n % k == 0)
    l = 0;
  else {
    while (1) {
      n = abs(n - k);
      if (n > l) {
        if (i != 0)
          break;
        i = 1;
      }
      l = n;
    }
  }
  printf("%lld", l);

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

// vector<vector<int>> data(3,
// vector<int>(4));//int型の2次元配列(3×4要素の)の宣言 int64_t a; 10のi乗pow(10,
// i);

#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() {
  int64_t n, k;
  cin >> n >> k;
  //  int a;

  cout << min(n % k, abs(n % k - k)) << endl;
  //  a = n % k;
  //  if(a<=abs(a-k))cout<< a <<endl;
  //  else cout<<abs(a-k)<<endl;
}


Error: 0

Fixed submission:

// vector<vector<int>> data(3,
// vector<int>(4));//int型の2次元配列(3×4要素の)の宣言 int64_t a; 10のi乗pow(10,
// i);

#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() {
  /* code */
  int N, K;
  cin >> N >> K;
  cout << min(N % K, K - N % K) << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int64_t n, k, m;

  cin >> n >> k;

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

  while (true) {
    m = abs(n - k);
    if (m >= n) {
      break;
    } else {
      n = m;
    }
  }

  cout << n << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int64_t n, k, m;

  cin >> n >> k;

  while (true) {
    m = abs(n - k);
    if (m >= n) {
      break;
    } else {
      n = m;
    }
  }

  cout << n << endl;

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

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

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int a[N];
bool cmp(int x, int y) { return x > y; }
int main() {
  int n, m;
  int sum = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int x = sum / (4 * m);
  sort(a + 1, a + 1 + n, cmp);
  if (a[m] < x)
    puts("No");
  else
    puts("Yes");
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1000000000;

int main() {
  ll n, k;
  cin >> n >> k;
  bool boo = true;
  ll a;
  ll b;
  if (n - k > k) {
    b = (n - k) / k;
    n = n - b * k;
  }

  while (boo) {
    a = n;
    n = abs(n - k);
    if (n >= a) {
      boo = false;
    }
  }
  cout << a << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1000000000;

int main() {
  ll n, k;
  cin >> n >> k;
  bool boo = true;
  ll a;
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (k == 2) {
    if (n % 2 == 0) {
      cout << 0 << endl;
      return 0;
    } else {
      cout << 1 << endl;
      return 0;
    }
  }
  while (boo) {
    a = n;
    n = abs(n - k);
    if (n > a) {
      boo = false;
    }
  }
  cout << a << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;
using ll = long long;

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

  ll ans = n % k;
  ll tmp = k - ans;
  if (tmp < ans)
    ans = tmp;

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

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

  int ans = n % k;
  int tmp = k - ans;
  if (tmp < ans)
    ans = tmp;

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

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

  return 0;
}

Error: TLE

Fixed submission:

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

  return 0;
}
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;
  int64_t x = N / K;
  int64_t s = N - K * (x + 1);
  if (s + K > abs(s))
    cout << abs(s) << endl;
  else
    cout << abs(s + K) << endl;
}


Error: TLE

Fixed submission:

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

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

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

typedef long long ll;
using namespace std;

int pot(ll q, ll w) {
  ll qw = q;
  q = 1;
  for (int i = 0; i < w; i++)
    q *= qw;
  return q;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, k, x, d = 0;

  cin >> n >> k;

  x = n % k;
  ll px = x;
  while (d == 0) {
    x -= k;
    x = abs(x);
    if (x < px)
      px = x;
    else
      d = 1;
  }

  cout << px;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
using namespace std;

int pot(ll q, ll w) {
  ll qw = q;
  q = 1;
  for (int i = 0; i < w; i++)
    q *= qw;
  return q;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, k, x = 9223372036854775807 - 3, d = 0;

  cin >> n >> k;

  if (k != 1) {
    while (d != 1) {
      n -= k;
      n = abs(n);
      if (x > n)
        x = n;
      else
        d = 1;
    }
    cout << x;
  } else
    cout << 0;

  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;
using ll = long long;
using P = pair<int, int>;

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

Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < K / 2 + 1; i++) {
    if (N % K == i || N % K == K - i) {
      cout << i << endl;
      break;
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int x, k;
  cin >> x >> k;
  if (x % k == 0) {
    cout << 0;
  } else {
    long long a, b, c;
    a = x % k;
    b = x / k;
    c = abs(x - k * (b + 1));
    //<<a<<" " <<b<<endl;
    cout << min(a, c);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include<stdio.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  if (k == 1 || n == 0 || k == n)
    n = 0;
  else if (n * 2 <= k)
    ;
  else if (n % k == 0)
    n = 0;
  else if (n < k && n * 2 > k)
    n = k - n;
  else {
    int b;
    do {
      int a = n / k;
      b = abs(n - a * k);
    } while (b < n);
    n = b;
  }
  cout << n << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long t;
  n = n % k;
  //	if(n-k>0){
  //		t=n-k;
  //	}
  //	else t=k-n;
  //	for(;;){
  //		//cout<<t<<"---\\n";
  //		n= abs(n-k);//1
  //		if(n<=t){
  //			t=n;
  //			int s= abs(n-k);
  //			if(s>=t){
  //				break;
  //			}
  //		}
  //
  //	}
  // if(n<k){
  //	n=k%n;
  //}
  if (k - n < n) {
    n = abs(n - k);
  }
  cout << n << endl;
  return 0;
}

Error: TLE

Fixed submission:

// #include<stdio.h>
// #include<string>
// #include <iostream>
// #include<string.h>
// #include <algorithm>
// #include<queue>
// using namespace std;
//
//
// int main(){
//	long n,k;
//	cin>>n>>k;
//	long t;
//	if(n-k>0){
//		t=n-k;
//	}
//	else t=k-n;
//	for(;;){
//		//cout<<t<<"---\\n";
//		n= abs(n-k);//1
//		if(n<=t){
//			t=n;
//			int s= abs(n-k);
//			if(s>=t){
//				break;
//			}
//		}
//
//	}
//	cout<<n<<endl;
//	return 0;
// }
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long t;
  n = n % k;
  if (n - k > 0) {
    t = n - k;
  } else
    t = k - n;
  for (;;) {
    // cout<<t<<"---\\n";
    n = abs(n - k); // 1
    if (n <= t) {
      t = n;
      int s = abs(n - k);
      if (s >= t) {
        break;
      }
    }
  }
  cout << n << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long long n, k, l, m;
  vector<unsigned long long> v;
  cin >> n >> k;
  if (n % k == 0) {
    cout << "0" << endl;
  } else {
    l = n % k;
    m = k - l;
    if (m > l) {
      cout << l << endl;
    } else
      cout << m << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k;
  scanf("%lli%lli", &n, &k);
  if (n % k == 0) {
    printf("0\\n");
  } else {
    long long int mini;
    mini = n;
    while (1) {
      n = abs(n - k);
      if (n >= mini) {
        printf("%lli\\n", mini);
        break;
      }

      else if (n < mini) {
        mini = n;
      }
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
main() {
  unsigned long long n, k, m, ans;
  cin >> n >> k;
  m = n % k;
  if ((k - m) < m)
    cout << k - m << '\\n';
  else
    cout << m << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k;
  scanf("%lli%lli", &n, &k);
  if (n % k == 0) {
    printf("0\\n");
  } else {
    long long int mini;
    mini = n;
    while (1) {
      n = abs(n - k);
      if (n < mini) {
        mini = n;
      } else if (n == mini) {
        printf("%lli", mini);
        break;
      }
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
// cout << fixed << setprecision(8) << a << endl;
// cout << "Case " << ca << ": " << ans << endl;
#define Fast_Input                                                             \\
  ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define MX 100005
#define infinity 20000000
#define pb push_back
int main() {
  long long int n, k;
  cin >> n >> k;
  long long int ans, ans2;
  ans = n % k;
  ans2 = abs(ans - k);
  cout << min(ans, ans2) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

long long m, n, y, x;

int main() {
  scanf("%lld%lld", &m, &n);

  while (1) {

    y = abs(m - n);
    x = abs(y - n);
    if (x == m) {
      printf("%lld", min(x, y));
      break;
    } else {
      m = x;
      continue;
    }
  }

  return 0;
}