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

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

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


Error: 0

Fixed submission:

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

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

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

int main() {
  long long n, k, sol;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0 << '\\n';
  } else {
    if (n > k) {
      n -= n / k * k;
    }
    sol = abs(n - k);
    while (true) {
      if (abs(sol - k) < sol) {
        sol = abs(sol - k);
        continue;
      }
      break;
    }
    cout << sol << '\\n';
  }
}


Error: TLE

Fixed submission:

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

int main() {
  long long n, k, sol;
  cin >> n >> k;
  if (n % k == 0) {
    cout << 0 << '\\n';
  } else {
    sol = abs(n - k);
    while (true) {
      if (abs(sol - k) < sol) {
        sol = abs(sol - k);
        continue;
      }
      break;
    }
    cout << sol << '\\n';
  }
}
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;
  long long a = n % k;
  long long ans = min(a, k - a);
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
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: Runtime Error

#include <bits/stdc++.h>
#define N 1000005
#define LL long long
using namespace std;
int A[N];
char str[N];
bool cmp(int a, int b) { return a > b; }
int main() {
  long long n, m, p;
  cin >> n >> m;
  p = n % m;
  cout << min(p, m - p) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 1000005
#define LL long long
using namespace std;
int A[N];
char str[N];
bool cmp(int a, int b) { return a > b; }
int main() {
  int n, m;
  cin >> n >> m;
  int p = n % m;
  printf("%d\\n", min(p, m - p));
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {

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

Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

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

int main() {
  long N, K, n;
  cin >> N >> K;
  if (N % K == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (K % N == 0) {
    cout << N << endl;
    return 0;
  }
  n = N % K;
  do {
    N = n;
    n = abs(N - K);
  } while (N > n);

  cout << N << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long N, K, n;
  cin >> N >> K;
  n = N;
  if (N % K == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (K % N == 0) {
    cout << N << endl;
    return 0;
  }
  do {
    N = n;
    n = abs(N - K);
  } while (N > n);

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  cout << min(abs(n % k), abs(k - (n % k)));
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  if (n < k)
    cout << n;
  else if (n % k == 0)
    cout << 0;
  else if (k == 0)
    cout << n;
  else
    cout << k - (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;
using ll = long long;

int main() {

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

int main() {

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

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

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


Error: 0

Fixed submission:

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

int main() {
  int N, K, n, k, ans;
  cin >> N >> K;
  n = N % K;
  k = K - n;
  ans = min(n, k);
  cout << ans << endl;
}
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 amari = N % K;
  int64_t hanten = K - amari;
  if (amari < hanten)
    cout << amari << endl;
  else
    cout << hanten << endl;
}

Error: TLE

Fixed submission:

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

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

#include <iostream>
using namespace std;

int main(void) {
  long n, k;
  long b, a, temp = 0;
  cin >> n >> k;
  temp = n / k;
  b = n - k * temp;
  a = b - k;
  // cout << a << " "<< b<<endl;
  a = a * -1;
  temp = min(a, b);
  cout << temp << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {
  long n, k;
  long b, a, temp = 0;
  cin >> n >> k;
  b = n;
  while (1) {
    a = b - k;
    // cout<<a<< " " << b << endl;
    if (a < 0) {
      break;
    }
    b = a;
  }
  // cout << a << " "<< b<<endl;
  a = a * -1;
  temp = min(a, b);
  cout << temp << "\\n";
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// 素数判定
bool isPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}

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

  n = n % k;
  ll ans = n;
  bool flag = false; // 処理終了フラグ
  for (;;) {
    ll tmp = abs(ans - k);
    if (ans < tmp)
      break;
    ans = tmp;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

// 素数判定
bool isPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}

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

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

  ll ans = n;
  bool flag = false; // 処理終了フラグ
  for (;;) {
    ll tmp = abs(ans - k);
    if (ans < tmp)
      break;
    ans = tmp;
  }
  cout << ans << endl;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: 0

Fixed submission:

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

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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int ll;

#define EPS (1e-9)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, n) for (int i = 0; i < n; i++)

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

ll beki(ll a, ll b) {
  ll tmp = 1;
  REP(i, b) tmp *= a;
  return tmp;
}

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

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int ll;

#define EPS (1e-9)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, n) for (int i = 0; i < n; i++)

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

ll beki(ll a, ll b) {
  ll tmp = 1;
  REP(i, b) tmp *= a;
  return tmp;
}

ll N, K;

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

  cout << min(N % K, K % 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 (N >= K) {
    if (abs(N % K - K) < N % K) {
      cout << abs(N % K - K) << endl;
    } else {
      cout << N % K << endl;
    }
  }

  if (N < K && K <= 2 * N) {
    cout << K - N << endl;
  }
  if (K > 2 * N) {
    cout << N << endl;
  }
}

Error: TLE

Fixed submission:

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

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

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  long long int n, k, b, ans, minn = 1000000000000000000;
  cin >> n >> k;
  b = n;
  if (k == 1 || b == 0) {
    minn = 0;
    cout << minn;
    return 0;
  }
  while (1) {
    ans = abs(k - b);
    if (ans < minn) {
      minn = ans;
    }
    b = ans;
    if (b > minn) {
      break;
    }
  }
  cout << minn;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll n, k;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  n = n % k;
  if (n > abs(n - k)) {
    n = abs(n - k);
  }
  cout << n << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, y;
  cin >> x >> y;
  while (2 * x > y) {
    x = abs(x - y);
  }
  cout << x << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

long long m, n, y, x;

int main() {
  scanf("%lld%lld", &x, &y);

  if (y == 0)
    printf("%lld", x);

  else {
    x %= y;
    printf("%lld", min(y - x, x));
  }

  return 0;
}

Error: -8

Fixed submission:

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

long long m, n, y, x;

int main() {
  scanf("%lld%lld", &m, &n);
  x %= y;
  printf("%lld", min(y - x, x));

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  long long int n, k, d, b = 0, a;
  cin >> n >> k;
  if (n > k) {
    a = n / k;
    n = n - a * k;
  }
  while (n != 0) {
    d = abs(n - k);
    if (d > n)
      break;
    n = d;
    if (b == n)
      break;
    b = n;
  }
  cout << n << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

#define ll long long

int main() {
  ll n, k;
  cin >> n >> k;
  // cout<<(n%k)<<"\\n";

  ll cont;
  cont = n % k;
  if (abs(cont - k) < cont)
    cout << abs(cont - k);
  else
    cout << cont;
  cout << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, s, m, t;
  cin >> n >> k;
  if (n > k) {
    s = n / k;
    t = n - (s * k);
    m = abs(t - k);
    if (m < t) {
      cout << m << "\\n";
    } else
      cout << t << "\\n";
  } else if (k > n) {
    m = abs(k - n);
    if (m < n) {
      cout << m << "\\n";
    } else
      cout << n << "\\n";
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
typedef long long ll;
ll abs(ll a, ll b) {
  if (a > b)
    return a - b;
  return b - a;
}
int main() {
  ll n, k;
  while (~scanf("%lld %lld", &n, &k)) {
    n = n % k;
    while (n >= abs(n, k)) {
      n = abs(n, k);
    }
    printf("%lld\\n", n);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define inf 0x3f3f3f3f
#define N

int main() {
  std::ios::sync_with_stdio(false);
  long long k, minn;
  while (cin >> minn >> k) {
    if (k == 1 && minn) {
      minn = 0;
    } else
      while (abs(minn - k) < minn) {
        minn = abs(minn - k);
      }
    cout << minn << endl;
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void printVec(std::vector<int> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

int main() {
  ll n, k;

  cin >> n >> k;
  ll min = n;
  ll diff = abs(n - k);
  if (diff > min) {
    cout << min << endl;
  } else {
    min = diff;
    bool flag = true;
    ll div = n / k;
    // cout << "diff = " << diff << endl;
    // cout << "div =  " << div << endl;
    diff = abs(n - (div * k));
    if (diff > abs(diff - k)) {
      cout << abs(diff - k) << endl;
    } else {
      cout << diff << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

void printVec(std::vector<int> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

int main() {
  ll n, k;

  cin >> n >> k;
  ll min = n;
  ll diff = abs(n - k);
  if (diff > min) {
    cout << min << endl;
  } else {
    min = diff;
    bool flag = true;
    while (flag) {
      diff = abs(diff - k);
      if (diff > min) {
        cout << min << endl;
        break;
      } else {
        min = diff;
      }
    }
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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


Error: TLE

Fixed submission:

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

#include <math.h>
#include <stdio.h>

int main() {
  long long n, k;
  scanf("%lld %lld", &n, &k);
  n = n % k;
  while (abs(n - k) < n) {
    n = abs(n - k);
  }
  printf("%lld\\n", n);

  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>

int main() {
  long long n, k;
  scanf("%lld %lld", &n, &k);
  while (abs(n - k) < n) {
    n = abs(n - k);
  }
  printf("%lld\\n", n);

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

#include <iostream>
using namespace std;

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


Error: 0

Fixed submission:

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


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

#define ll long long
#define pb push_back
#define nn cout << "\\n";

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

  ll t, b, j, c, d, e, f = 0, i, n = 0, k, m = 0, w = 0;

  cin >> b >> c;
  d = b % c;
  e = abs(d - c);

  cout << min(e, d);

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, s, m, t, y;
  cin >> n >> k;
  if (n > k) {
    s = n / k;
    t = n - (s * k);
    m = abs(t - k);
    if (m < t) {
      cout << m << "\\n";
    } else
      cout << t << "\\n";
  } else if (k > n) {
    m = abs(k - n);
    if (m < n) {
      cout << m << "\\n";
    } else
      cout << n << "\\n";
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  long long n, k, a, b;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << endl;
  else {
    a = n % k;
    b = k - a;
    cout << min(a, b) << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, k, a, b;
  cin >> n >> k;
  if (n % k == 0)
    cout << 0 << endl;
  else {
    a = n % k;
    b = k - a;
    cout << min(a, b) << endl;
  }
}
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(k - n % k, n % k) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include <bits/stdc++.h>
#define INF 9223372036854775807LL
#define mod 998244353
#define SYOU(x) fixed << setprecision(x + 1)
#define abs(x, y) (max(x, y) - min(x, y))
#define lol long long
#define mp make_pair
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
lol gcd(lol x, lol y) { // 最大公約数
  if (y > x)
    swap(x, y);
  lol r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  lol n, m, k;
  string s, w;
  lol a, b, c, x, y;
  cin >> n >> m;
  if (n == 0 || m == 0) {
    if (n == 0)
      cout << "0\\n";
    else
      cout << n << '\\n';
    return 0;
  }
  if (m == 1) {
    cout << "0\\n";
    return 0;
  }
  if (n > m) {
    lol ans = n % m;
    cout << min(ans, m - ans) << '\\n';
  } else {
    cout << min(n, m - n) << '\\n';
  }
  return (0);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define INF 9223372036854775807LL
#define mod 998244353
#define SYOU(x) fixed << setprecision(x + 1)
#define abs(x, y) (max(x, y) - min(x, y))
#define lol long long
#define mp make_pair
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
lol gcd(lol x, lol y) { // 最大公約数
  if (y > x)
    swap(x, y);
  lol r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  lol n, m, k;
  string s, w;
  lol a, b, c, x, y;
  cin >> n >> m;
  lol ans = gcd(n, m);
  if (ans == 1)
    cout << "0\\n";
  else
    cout << ans << '\\n';

  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 a = n % k;
  long long int b = abs(k - a);

  cout << min(a, b) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

  priority_queue<int, vector<int>, greater<int>> data;
  data.push(n);

  while (true) {
    n = abs(n - k);
    if (n > data.top()) {
      break;
    }
    data.push(n);
  }

  cout << data.top() << endl;

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <memory>
#include <string>
#include <vector>
typedef long long ll;

int main() {
  ll n, k;
  ll x;
  std::cin >> n >> k;
  if (n > k) {
    if (n % k == 0) {
      n = n - (n / k - 1) * k;
    } else {
      n = n - (n / k) * k;
    }
  }
  if (k - n < n) {
    std::cout << k - n << std::endl;
  } else {
    std::cout << n << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <memory>
#include <string>
#include <vector>
typedef long long ll;

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

int main() {
  ll n, k;
  ll a;
  std::cin >> n >> k;
  while (1) {
    a = sousa(n, k);
    if (n < a) {
      break;
    } else {
      n = a;
    }
  }
  std::cout << n << std::endl;
  return 0;
}