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

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

int main() {
  int64_t a, b;
  cin >> a >> b;
  int64_t x, y;
  y = a % b;
  x = b - y;
  cout << min(x, y) << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int a, b;
  cin >> a >> b;
  int x, y;
  y = a % b;
  x = b - y;
  cout << min(x, y) << endl;
}
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 ull = unsigned long long;

int main() {
  ll n, k;
  cin >> n >> k;
  n = min(n % k, n);
  while (true) {
    ll pre = n;
    n = min(abs(n - k), n);
    if (n == pre)
      break;
  }
  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 ull = unsigned long long;

int main() {
  ll n, k;
  cin >> n >> k;
  n = min(n % k, n);
  while (true) {
    int pre = n;
    n = min(abs(n - k), n);
    if (n == pre)
      break;
  }
  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, min;
  cin >> N >> K;
  min = N;

  if (N >= K) {
    min = N % K;
    N = min;
  }
  if (abs(N - K) < min) {
    min = abs(N - K);
  }

  cout << min << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long int N, K, min;
  cin >> N >> K;
  min = N;
  while (N - K > 0) {
    N -= K;
    min = N;
  }

  if (abs(N - K) < min) {
    min = abs(N - K);
  }

  cout << min << 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 a = n % k, b = n % k - k;
  if (b >= 0)
    b = n % k - k;
  else
    b = k - n % k;
  cout << min(a, b) << endl;
}

Error: 0

Fixed submission:

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

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long x;
  long res = 1000000000000000001;
  long t = n % k;
  long mmm = min(n % k, k - (n % k));
  cout << mmm << endl;
  return 0;
  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  // if ( n % k )

  x = n;
  while (true) {
    x = abs(x - k);
    // cout << x << endl;
    if (x >= res)
      break;
    res = x;
  }

  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long x;
  long res = 1000000000000000001;
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }

  x = n;
  while (true) {
    x = abs(x - k);
    if (x >= res)
      break;
    res = x;
  }

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

#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
#define rep(i, n) for (int i = 1; i <= (int)(n); i++)
using vi = vector<int>;
using vs = vector<string>;
using gi = greater<int>;
#define soao(c) sort((c).begin(), (c).end())       // 昇順
#define sodo(c) sort((c).begin(), (c).end(), gi()) // 降順
#define rev(c) reverse((c).begin(), (c).end())

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
#define rep(i, n) for (int i = 1; i <= (int)(n); i++)
using vi = vector<int>;
using vs = vector<string>;
using gi = greater<int>;
#define soao(c) sort((c).begin(), (c).end())       // 昇順
#define sodo(c) sort((c).begin(), (c).end(), gi()) // 降順
#define rev(c) reverse((c).begin(), (c).end())

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

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(0);
  long long n, k;
  cin >> n >> k;
  n %= k;
  cout << min(n, abs(n - k));
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(0);
  long long n, k;
  cin >> n >> k;
  if (k == n % abs(n - k))
    cout << "0";
  else
    cout << n % abs(n - k);
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
  long n, k;
  cin >> n >> k;
  cout << min(n % k, abs(k - n % k));
  return 0;
}


Error: WA

Fixed submission:

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

#include <bits/stdc++.h>

using namespace std;

long long n, k;

int main() {
  cin >> n >> k;
  if (k >= n) {
    cout << min(n, k - n) << '\\n';
  } else {
    cout << min(n % k, k - (n % k)) << '\\n';
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n, k;

int main() {
  cin >> n >> k;
  if (k >= n) {
    cout << min(n, k - n) << '\\n';
  } else {
    cout << min(n % k, k - (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;
  cin >> n >> k;
  ll a = n % k;
  ll b = abs(a - k);
  cout << min(a, b) << endl;
}


Error: 0

Fixed submission:

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

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

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

typedef long long ll;
typedef pair<ll, ll> P;

const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;

#define rep(i, a, b) for (ll(i) = (a); (i) < (b); ++i)

int main() {
  ll N, M;
  cin >> N >> M;
  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (N % M < M / 2) {
    cout << N % M << endl;
  } else {
    cout << M - N % M << endl;
  }
  // int A[N];
  // int sum = 0;
  // for (int i = 0;i < N;i++) {
  // 	cin >> A[i];
  // 	sum += A[i];
  // }
  // int sup = sum / (4 * M);
  // int cnt = 0;
  // if (sum == 0) {
  // 	cout << "Yes" << endl;
  // 	return 0;
  // }
  // rep(i,0,N) {
  // 	if (A[i] > sup) cnt++;
  // }
  // if (cnt >= M) cout << "Yes" << endl;
  // else cout << "No" << endl;
  // cout << sup << " " << cnt << endl;
  return 0;
}


Error: WA

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> P;

const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;

#define rep(i, a, b) for (ll(i) = (a); (i) < (b); ++i)

int main() {
  int N, M;
  cin >> N >> M;
  cout << N % M << endl;
  // int A[N];
  // int sum = 0;
  // for (int i = 0;i < N;i++) {
  // 	cin >> A[i];
  // 	sum += A[i];
  // }
  // int sup = sum / (4 * M);
  // int cnt = 0;
  // if (sum == 0) {
  // 	cout << "Yes" << endl;
  // 	return 0;
  // }
  // rep(i,0,N) {
  // 	if (A[i] > sup) cnt++;
  // }
  // if (cnt >= M) cout << "Yes" << endl;
  // else cout << "No" << endl;
  // cout << sup << " " << cnt << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main(void) {
  long long x;
  cin >> x;
  long long K;
  cin >> K;
  long long ans1 = x % K;
  long long ans2 = abs(ans1 - K);
  cout << min(ans1, ans2) << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  int x;
  cin >> x;
  int K;
  cin >> K;
  int ans1 = x % K;
  int ans2 = abs(ans1 - K);
  cout << min(ans1, ans2) << endl;
  // Your code here!
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll N, K, c, m1;
  int i = 1;

  cin >> N >> K;

  c = N % K;

  if (c == 0) {
    cout << 0;
  } else {
    if (N >= K) {
      cout << K - c;
    } else {
      m1 = abs(N - K);
      if (N > m1) {
        cout << m1;
      } else {
        cout << N;
      }
    }
  }
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll N, K, m1, c;
  int i = 1;

  cin >> N >> K;

  m1 = N;

  while (i == 1) {
    if (K == 1) {
      cout << 0;
      i = 0;
    }
    c = abs(m1 - K);
    if (c >= m1) {
      cout << m1;
      i = 0;
    } else {
      m1 = c;
    }
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

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

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int main() {
  ll n, k;
  ll mn = 1000000000000000000;
  cin >> n >> k;
  if (k >= n) {
    cout << min(n, k - n) << endl;
  } else {
    cout << min(n % k, k - n % k) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int main() {
  ll n, k;
  ll mn = 1000000000000000000;
  cin >> n >> k;
  while (1) {
    if (n % k == 0) {
      mn = 0;
      break;
    }
    ll h = abs(n - k);
    if (mn <= h) {
      if (mn == h) {
        mn = 0;
      }
      break;
    } else {
      mn = h;
      n = mn;
    }
  }
  cout << mn << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
/*
n<|k-n|までwhileで計算量ダメそう(サンプル3)
10e18な時点でO(N)は不可

n==1 =>0 これ偽じゃんどうやって真であること証明したお前！？
n%k==0 =>0 これも証明済

*/
int main() {
  int64_t n, k;
  cin >> n >> k;
  if (k == 1 || n == k || n % k == 0) {
    cout << 0 << endl;
  } else {
    int64_t b = n / k + 1;
    if (k < n) {
      cout << min(abs(n - b * k), abs(n - b * k + k)) << endl;
    } else {
      cout << min(n, k - n) << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*
n<|k-n|までwhileで計算量ダメそう(サンプル3)
10e18な時点でO(N)は不可

どちらかが1=>0 これは証明可

その他=>min(an-k,bk-n)
ここでa=k/n+1,b=n/k+1(どちらもfloor)
理由はわからんがそんな気がする
*/
int main() {
  int64_t n, k;
  cin >> n >> k;
  if (n == 1 || k == 1) {
    cout << 0 << endl;
  } else {
    int64_t a = k / n + 1, b = n / k + 1;
    cout << min(a * n - k, b * k - n) << endl;
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define X first
#define Y second
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
const int maxn = 1e4 + 10;
int a[maxn];

ll ABS(ll a, ll b) {
  if (a > b) {
    return a - b;
  } else
    return b - a;
}

int main() {
  ll n, k;
  cin >> n >> k;
  if (n < k / 2) {
    cout << n << endl;
    return 0;
  }
  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  cout << min(n % k, ABS(n % k, k)) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define X first
#define Y second
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
const int maxn = 1e4 + 10;
int a[maxn];

ll ABS(ll a, ll b) {
  if (a > b) {
    return a - b;
  } else
    return b - a;
}

int main() {
  ll n, k;
  cin >> n >> k;
  if (n < k / 2) {
    cout << n << endl;
    return 0;
  }
  if (n % k == 0) {
    cout << 0 << endl;
    return 0;
  }
  ll num = ABS(n, k);
  while (num < n) {
    n = num;

    num = ABS(n, k);
  }
  cout << n << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

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

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

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

  LL d = abs(n - k);
  LL x = n / d;
  LL ans = min(n - x * d, abs(n - x * (d + 1)));

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

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

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

Error: WA

Fixed submission:

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

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

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

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

Error: TLE

Fixed submission:

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

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }

int main() {
  ll n, k;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0;
  // else if(n<k) cout<<gcd(n,k);
  else {
    ll m = n;
    for (ll i = 0;; i++) {
      n = abs(n - k);
      if (m > n)
        m = n;
      else
        break;
    }
    cout << m;
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

int main() {
  ll N, K;
  cin >> N >> K;
  ll min_value = N;
  if (N >= K) {
    N = N % K;
    min_value = min(min_value, N);
  }
  if (N <= K) {
    N = K - N;
    min_value = min(min_value, N);
  }
  cout << min_value << endl;
}

Error: WA

Fixed submission:

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

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

  cout << min(N % K, N % abs(N - K));
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

  cout << mn << endl;
}

Error: TLE

Fixed submission:

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

  cout << mn << endl;
}
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;

void Main() {
  ll n, k;
  cin >> n >> k;

  ll sho = n % k;
  cout << min(sho, abs(sho - k)) << endl;
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

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;

void Main() {
  ll n, k;
  cin >> n >> k;
  ll mi = n;
  if (k == 1) {
    cout << 0 << endl;
  } else if (n == k) {
    cout << 0 << endl;
  } else if (n < k) {
    cout << min(n, abs(n - k)) << endl;
  } else {
    for (ll i = 0; i < n; i++) {
      ll a = n - k;

      if (mi >= abs(a)) {
        mi = abs(a);
      } else if (n < k) {
        cout << min(n, abs(n - k)) << endl;
      } else {
        break;
      }
      n = abs(a);
    }
    cout << mi << endl;
  }
}

int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

void func() {

  ll n, k;
  cin >> n >> k;

  ll ans = min(n % k, k - n % k);
  cout << ans << endl;
}

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

  /*   #ifndef ONLINE_JUDGE
     freopen("input.txt", "r" , stdin);
     freopen("output.txt", "w" , stdout);
     #endif*/

  ll t = 1;
  // cin>>t;
  while (t--)
    func();
}

Error: TLE

Fixed submission:

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

#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

void func() {

  ll n, k;
  cin >> n >> k;
  ll min = n;

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

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

  /* #ifndef ONLINE_JUDGE
   freopen("input.txt", "r" , stdin);
   freopen("output.txt", "w" , stdout);
   #endif*/

  ll t = 1;
  // cin>>t;
  while (t--)
    func();
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

long long n, k;

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

Error: 0

Fixed submission:

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

int n, k;

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

#include <bits/stdc++.h>

#define int long long

using namespace std;

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

  n %= k;

  cout << min(n, k - n) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int a[100010];

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

  n %= k;

  cout << min(n, k - 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 (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
const ll INF = 1LL << 60;

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

  if (n >= k) {
    ans = n % k;
  }
  ans = min(ans, k - ans);

  cout << ans << endl;
}

Error: WA

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
const ll INF = 1LL << 60;

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

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
using namespace std;
long long n, k, ans;
map<int, int> mp;
int main() {
  scanf("%lld%lld", &n, &k);
  if (n % k == 0)
    ans = 0;
  else {
    if (n > k) {
      n %= k;
    }
    if (k - n < n)
      ans = k - n;
    else
      ans = n;
  }
  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
using namespace std;
long long n, k, ans;
map<int, int> mp;
int main() {
  scanf("%lld%lld", &n, &k);
  if (n % k == 0)
    ans = 0;
  else {
    while (n > k) {
      n -= k;
    }
    if (k - n < n)
      ans = k - n;
    else
      ans = n;
  }
  printf("%lld\\n", ans);
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
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 <iostream>
#include <math.h>
using namespace std;

int main() {
  long long N, K, res;
  cin >> N >> K;
  res = N;
  for (long long i = 0; i < 100000000000000; i++) {
    res = min(res, abs(res - K));
  }
  cout << res << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Error: TLE

Fixed submission:

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

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

#include <algorithm>
#include <iostream>

using namespace std;

long long n, k;

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


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int n, k;

int main() {
  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>
#include <iostream>
using namespace std;
int main() {
  long long n, k, n1;
  cin >> n >> k;
  long long mn, d;
  d = n % k;
  mn = abs(k - d);

  cout << min(mn, d) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  long long n, k, n1;
  cin >> n >> k;
  // long long int mn=INT_MAX,d;
  while (1) {

    n1 = abs(n - k);
    if (n1 < n)
      n = n1;
    else
      break;
  }
  cout << n << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define foi(i, k, n) for (int i = (int)k; i < (int)n; ++i)

using namespace std;
using ii = pair<int, int>;
using ll = long long;

constexpr int MAXN = 5 + 1000;
int a[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n, k;
  cin >> n >> k;
  ll ans = n;
  for (int i = 0; i < 100; ++i) {
    if (n >= k) {
      n %= k;
    } else {
      n = abs(n - k);
    }
    ans = min(ans, n);
  }
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define foi(i, k, n) for (int i = (int)k; i < (int)n; ++i)

using namespace std;
using ii = pair<int, int>;
using ll = long long;

constexpr int MAXN = 5 + 1000;
int a[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll a, b;
  cin >> a >> b;
  cout << a % abs(a - b) << '\\n';
  return 0;
}