Problem p02719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long
#define vi vector<int>
#define vec(x) vector<x>
#define matrix vector<vector<int>>
#define pii pair<int, int>
#define rep(i, s, e) for (int i = s; i < e; i++)
#define rrep(i, e, s) for (int i = e; i >= s; i--)
#define MOD 1000000007
#define pb push_back
#define ff first
#define ss second
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
const int INF = 1e18;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
// find_by_order(k-1)  returns iterator to kth element starting from 0; Returns
// the kth smallest element order_of_key(k) returns count of elements strictly
// smaller than k;Returns the number of elements less than k erase,insert same
// as normal set

void solve() {
  int n, k;
  cin >> n >> k;
  int x = n;
  if (k < n) {
    int val = ceil(double(n) / double(k));
    val -= 1;
    x = n - val * k;
  }

  // trace(x,k);
  int ans = INF;
  for (int i = 0; i < 1000000; i++) {
    ans = min(ans, abs(k - x));
    x = abs(k - x);
  }

  cout << ans << endl;
}

int32_t 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
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long
#define vi vector<int>
#define vec(x) vector<x>
#define matrix vector<vector<int>>
#define pii pair<int, int>
#define rep(i, s, e) for (int i = s; i < e; i++)
#define rrep(i, e, s) for (int i = e; i >= s; i--)
#define MOD 1000000007
#define pb push_back
#define ff first
#define ss second
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
const int INF = 1e18;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
// find_by_order(k-1)  returns iterator to kth element starting from 0; Returns
// the kth smallest element order_of_key(k) returns count of elements strictly
// smaller than k;Returns the number of elements less than k erase,insert same
// as normal set

void solve() {
  int n, k;
  cin >> n >> k;
  int x = n;
  if (k < n) {
    int val = ceil(double(n) / double(k));
    val -= 1;
    x = n - val * k;
  }

  // trace(x,k);

  while (k < 2 * x) {
    x = abs(k - x);
  }

  cout << x << endl;
}

int32_t 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
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}