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

// Hail god Yato

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

#define add(x) accumulate(x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define hs                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//
void solve() {
  ll n, k;
  cin >> n >> k;
  ll div = n % k;
  cout << min(div, k - div);
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

// Hail god Yato

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

#define add(x) accumulate(x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define hs                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//
void solve() {
  ll n, k;
  cin >> n >> k;
  while (true) {
    if (abs(n - k) < n)
      n = abs(n - k);
    else
      break;
  }
  cout << n;
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

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

  ll ans = min(n % k, abs(k - n % k));

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

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

  int ans = min(n % k, k - n % k);

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

#include <bits/stdc++.h>
using namespace std;
// useful library
int long long gcd(int long long a, int long long b) {
  return b ? gcd(b, a % b) : a;
} // 最大公約数
int long long lcm(int long long a, int long long b) {
  return a / gcd(a, b) * b;
} // 最小公倍数
int long long GetDigit(int long long num) {
  int long long digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
} // 桁数
int long long GetDigitN(int long long num, int long long N) {
  int long long digit = 0;
  while (num != 0) {
    num /= N;
    digit++;
  }
  return digit;
} // 十進数でnumの際のN進数における桁数

int main() {
  int long long N, K, A;
  cin >> N >> K;
  int long long t = N % K;
  A = min(t, (K - t));
  cout << A << endl;
  return 0;
}
// https://img.atcoder.jp/abc161/editorial.pdf

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 18446744073709551615) >= this->size() (which is 906)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// useful library
int long long gcd(int long long a, int long long b) {
  return b ? gcd(b, a % b) : a;
} // 最大公約数
int long long lcm(int long long a, int long long b) {
  return a / gcd(a, b) * b;
} // 最小公倍数
int long long GetDigit(int long long num) {
  int long long digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
} // 桁数
int long long GetDigitN(int long long num, int long long N) {
  int long long digit = 0;
  while (num != 0) {
    num /= N;
    digit++;
  }
  return digit;
} // 十進数でnumの際のN進数における桁数

int main() {
  int long long N, K, A;
  cin >> N >> K;
  vector<int long long> V(900);
  V.push_back(N);
  A = 0;
  if (N % K == 0) {
    goto ABC;
  }
  for (int i = 0; i < 5; i++) {
    V.reserve(i + 1);
    V.emplace_back(floor(abs(V.at(i) - K)));
  }
DEF:
  if ((V.at(A) < V.at(A + 1)) && (V.at(A) < V.at(A + 2)) &&
      (V.at(A) < V.at(A + 3)) && (V.at(A) < V.at(A + 4))) {
    goto ABC;
  } else {
    V.emplace_back(floor(abs(V.at(A - 1) - K)));
    A = A + 1;
    goto DEF;
  }
ABC:
  cout << V.at(A) << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
using ll = long long;
int main() {
  ll n, k;
  cin >> n >> k;
  if (n % k < k - (n % k))
    cout << n % k;
  else
    cout << k - (n % k);
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
using ll = long long;
int main() {
  ll n, k;
  cin >> n >> k;
  if (n < n % k)
    cout << n;
  else
    cout << n % (n % k);
  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;

  long long ans = 0;

  long long t = n % k;

  ans = min(t, k - t);

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  long long n, k;
  cin >> n >> k;

  long long tmp = abs(n - k);

  long long ans;

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

  if (n < tmp) {
    ans = 0;
  } else {
    ans = n % tmp;
  }

  cout << ans << endl;

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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(n) begin(n), end(n)
using ll = long long;
const ll INF = numeric_limits<long long>::max();

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

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

  N = N % K;
  while (N * 2 > K) {
    N = abs(N - K);
  }
  cout << N << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(n) begin(n), end(n)
using ll = long long;
const ll INF = numeric_limits<long long>::max();

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

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

  while (N * 2 > K) {
    N = N % K;
    N = abs(N - K);
  }
  cout << N << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n, k;
vector<int> v;
double EPS = 1e-9;
double pre[200010], a[200010], sieve[1000001];

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


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n, k;
vector<int> v;
double EPS = 1e-9;
double pre[200010], a[200010], sieve[1000001];

int main() {
  int n, k;
  cin >> n >> k;
  cout << n % k;
  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 %= K;

  cout << min(N, K - N) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main() {
  long long int N, K, x;

  cin >> N >> K;

  x = N;

  long long int result, pre_result;

  while (1) {
    result = x - K;
    pre_result = x;

    if (result > 0) {
      pre_result = x - K;
      continue;
    } else if (result == 0) {
      cout << 0 << endl;
    } else {
      cout << pre_result << endl;
    }
  }

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

// Replacing Integer
// xとxをKの差の絶対値で置き換える
// 最小かどうかの判断が難しい
// どのタイミングで止めればいい？

// 自力で解くのに失敗
// 得たもの->答えを絞り込めたら、どうにかして数式化できないか
// 考察する
#include <iostream>
using namespace std;

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


Error: WA

Fixed submission:

// Replacing Integer
// xとxをKの差の絶対値で置き換える
// 最小かどうかの判断が難しい
// どのタイミングで止めればいい？

// 自力で解くのに失敗
// 得たもの->答えを絞り込めたら、どうにかして数式化できないか
// 考察する
#include <iostream>
using namespace std;

int main() {
  int x, k;
  cin >> x;
  cin >> k;
  int ans = 0;
  ans = min(x % k, k - (x % k));
  cout << ans;
}
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;

  long long t = n % k;
  cout << (min(t, k - t)) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

long long calc(long long n, long long k, long long before_n) {
  if (n <= 0)
    return 0;
  if (n > before_n)
    return before_n;

  if (n > k) {
    return calc(n - k, k, n);
  } else {
    return calc(k - n, k, n);
  }
}

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

  n = calc(n, k, n);
  cout << n << 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 MIN(a, b) ((a) <= (b) ? (a) : (b))
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))

using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

int main() {

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

  N %= K;
  cout << min(N, K - N) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))

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

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

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

  map<ll, bool> can;
  ll minVal = N;
  can[N] = true;
  while (true) {

    N = abs(N - K);
    if (can[N] == false) {

      can[N] = true;
      minVal = min(N, minVal);
    } else
      break;
  }

  cout << minVal << endl;

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

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

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

Error: TLE

Fixed submission:

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

int main() {
  long n, k;
  cin >> n >> k;
  if (n == 0 || k == 1) {
    cout << 0 << endl;
  } else {
    while (n > abs(n - k)) {
      while (abs(n - k) > 1024000000000 * k) {
        n = abs(n - 1024000000000 * k);
      }
      while (abs(n - k) > 102400000000 * k) {
        n = abs(n - 102400000000 * k);
      }
      while (abs(n - k) > 10240000000 * k) {
        n = abs(n - 10240000000 * k);
      }
      while (abs(n - k) > 1024000000 * k) {
        n = abs(n - 1024000000 * k);
      }
      while (abs(n - k) > 102400000 * k) {
        n = abs(n - 102400000 * k);
      }
      while (abs(n - k) > 10240000 * k) {
        n = abs(n - 10240000 * k);
      }
      while (abs(n - k) > 1024000 * k) {
        n = abs(n - 1024000 * k);
      }
      while (abs(n - k) > 102400 * k) {
        n = abs(n - 102400 * k);
      }
      while (abs(n - k) > 10240 * k) {
        n = abs(n - 10240 * k);
      }
      while (abs(n - k) > 1024 * k) {
        n = abs(n - 1024 * k);
      }
      while (abs(n - k) > 512 * k) {
        n = abs(n - 512 * k);
      }
      while (abs(n - k) > 256 * k) {
        n = abs(n - 256 * k);
      }
      while (abs(n - k) > 128 * k) {
        n = abs(n - 128 * k);
      }
      while (abs(n - k) > 64 * k) {
        n = abs(n - 64 * k);
      }
      while (abs(n - k) > 32 * k) {
        n = abs(n - 32 * k);
      }
      while (abs(n - k) > 16 * k) {
        n = abs(n - 16 * k);
      }
      while (abs(n - k) > 8 * k) {
        n = abs(n - 8 * k);
      }
      while (abs(n - k) > 4 * k) {
        n = abs(n - 4 * k);
      }
      while (abs(n - k) > 2 * k) {
        n = abs(n - 2 * k);
      }
      n = abs(n - k);
    }

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

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

int main() {
  long long int n, k, d, s = 100000000, ans = 0;
  cin >> n >> k;
  if (n < k) {
    ans = min(n, k - n);
  } else if (n >= k) {
    d = n - (n / k) * k;
    if (d != 0) {
      s = k - (k / d) * d;
    }
    ans = min(d, s);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  long long int n, k, d, s, ans = 0;
  cin >> n >> k;
  if (n < k) {
    ans = min(n, k - n);
  } else if (n >= k) {
    d = n - (n / k) * k;
    s = k - (k / d) * d;
    ans = min(d, s);
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAX = 2e5 + 7, MOD = 2019;
void solve(ll n, ll k) {
  if (n == 0) {
    cout << n << endl;
    exit(0);
  } else {
    if (n < k) {
      if (n > k / 2) {
        cout << k % n << endl;
        exit(0);
      } else {
        cout << n % k << endl;
        exit(0);
      }
    } else {
      solve(n % k, k);
    }
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  solve(n, k);
  //    //main();
  //    ll n,k;cin>>n>>k;
  //    unordered_map<ll,int>cnt;
  //    while(n){
  //        cout<<n<<endl;
  //        n = abs(n-k);
  //        if(cnt[n])break;
  //        cnt[n]=1;
  //    }
  //    cout<<n<<endl;
  ////    //cout<<max(n,k)%min(n,k)<<endl;
  // main();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAX = 2e5 + 7, MOD = 2019;
int main() {
  ll n, k;
  cin >> n >> k;
  //    unordered_map<ll,int>cnt;
  //    while(n){
  //        cout<<n<<endl;
  //        n = abs(n-k);
  //        if(cnt[n])break;
  //        cnt[n]=1;
  //    }
  cout << max(n, k) % min(n, k) << endl;
  // main();
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

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 <algorithm>
#include <iostream>

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: Runtime Error

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

void solve(long long N, long long K) {
  N = N % K;
  cout << min(N, abs(N - K)) << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  solve(N, K);
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  ll x, y;
  cin >> x >> y;
  if (y % x == 0 || x % y == 0) {
    cout << 0 << endl;
  } else {
    cout << gcd(x, y) << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

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

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

int main() {
  long int N, K;

  cin >> N >> K;

  long int num1, num2;

  if (N > K) {
    num1 = N % K;
    num2 = K - num1;
  } else if (N < K) {
    num1 = N;
    N = K - N;
    num2 = N;
  } else {
    num1 = N;
    num2 = N - K;
  }

  if (num1 <= num2) {
    cout << num1 << endl;
  } else {
    cout << num2 << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  long int N, K;

  cin >> N >> K;

  long int num1, num2;

  if (N > K) {
    if (N % K == 0) {
      num1 = 0;
      num2 = 0;
    } else {
      while (N > K) {
        N = N - K;
      }
      num1 = N;
      N = K - N;
      num2 = N;
    }
  } else if (N < K) {
    num1 = N;
    N = K - N;
    num2 = N;
  } else {
    num1 = N;
    num2 = N - K;
  }

  if (num1 <= num2) {
    cout << num1 << endl;
  } else {
    cout << num2 << endl;
  }
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll i, j, k, l, m, n, o, p, q, r, s, t, x, a[500], tt = 0;
  cin >> n >> m;
  p = (n % m);
  q = m % n;
  r = m - p;
  s = n - q;
  cout << min({p, r}) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll i, j, k, l, m, n, o, p, q, r, s, t, x, a[500], tt = 0;
  cin >> n >> m;
  p = (n % m);
  q = m % n;
  r = m - p;
  s = n - q;
  cout << min({p, q, r, s}) << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

#include <bits/stdc++.h> //ABC 161_b
using namespace std;
int n, m, a[105], sum, num;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  double x = sum / (4.0 * m);
  for (int i = 1; i <= n; i++) {
    if (a[i] >= x)
      num++;
  }
  if (num >= m)
    cout << "Yes";
  else
    cout << "No";
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ull unsigned long long

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

bool isPalindrome(string s) {
  string t(s.rbegin(), s.rend());
  return s == t;
}

int main(void) {
  long long N, K;
  long long current;
  cin >> N >> K;
  N = abs(N - K * (N / K));
  while (true) {
    long long temp;
    temp = abs(N - K);
    if (temp >= N)
      break;
    N = temp;
  }
  printf("%lld\\n", N);
  return 0;
}


Error: TLE

Fixed submission:

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

#define ull unsigned long long

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

bool isPalindrome(string s) {
  string t(s.rbegin(), s.rend());
  return s == t;
}

int main(void) {
  long long N, K;
  cin >> N >> K;
  if (K == 1) {
    N = 0;
  } else if (K == N) {
    N = 0;
  } else {
    while (true) {
      long long temp;
      temp = abs(N - K);
      if (temp >= N)
        break;
      N = temp;
    }
  }
  printf("%lld\\n", N);
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long mi = 1e9, calc;
  if (k == 1) {
    cout << "0" << endl;
    return 0;
  } else {
    for (long long i = 0; i <= max(n, k); i++) {
      calc = abs(n - i * k);
      mi = min(calc, mi);
    }
  }
  cout << mi << endl;
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
const lint mod = 1e9 + 7;

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

  lint x = n % k;
  while (x > abs(x - k)) {
    x = abs(x - k);
  }

  cout << x << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
const lint mod = 1e9 + 7;

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

  if (k == 1) {
    cout << 0 << "\\n";
    return 0;
  }
  lint x = n;
  while (x > abs(x - k)) {
    x = abs(x - k);
  }

  cout << x << "\\n";
  return 0;
}
Problem p02719 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long Temp(long long n, long long k) {
  long long a = n;
  long long b = k;
  long long z = a;

  while (true) {
    if (a > b)
      a %= b;
    else
      a = abs(a - b);

    if (a >= z)
      return z;
    else
      z = a;
  }
}

int main() {
  long long n = 0, m = 0;
  cin >> n >> m;

  cout << Temp(n, m) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

long long Temp(long long n, long long k) {
  long long a = n;
  long long b = k;
  long long z1 = a;

  while (true) {
    if (a % b == 0 && a != 1)
      a = b;
    else if (a > b)
      a %= b;
    else
      a = abs(a - b);

    if (a > z1)
      return z1;
    else
      z1 = a;
  }
}

int main() {
  long long n = 0, m = 0;
  cin >> n >> m;

  cout << Temp(n, m) << endl;

  return 0;
}