Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << " ; ";
  err(++it, args...);
  cout << endl;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*Let's start.............*/
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define srt(v) sort(v.begin(), v.end())
#define inf 200000000000000000
#define mxn 100009
#define MOD 1000000007
#define pi acos(-1)

int main() {

  // freopen("input.txt","r",stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll X, a, b;
  cin >> X >> a >> b;
  X = abs(X);
  ll keep = (X / b);
  if (X == 0) {
    if (a % 2 == 0)
      cout << 0 << endl;
    else
      cout << b << endl;
  } else if (a <= keep) {
    cout << X - (a * b) << endl;
  } else {
    ll rem = (X - (X / b) * b);
    ll keep = (b - rem);

    ll peek = (X / b);
    ll ans = 0;
    if (peek % 2 == (a % 2))
      ans = rem;
    else
      ans = keep;
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << " ; ";
  err(++it, args...);
  cout << endl;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------*/
/*Let's start.............*/
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define srt(v) sort(v.begin(), v.end())
#define inf 200000000000000000
#define mxn 100009
#define MOD 1000000007
#define pi acos(-1)

int main() {

  // freopen("input.txt","r",stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  while (1) {
    ll X, a, b;
    cin >> X >> a >> b;
    X = abs(X);
    ll keep = (X / b);
    if (a <= keep) {
      cout << X - (a * b) << endl;
    } else if (X == 0) {
      if (a % 2 == 0)
        cout << 0 << endl;
      else
        cout << b << endl;
    } else {
      ll ans = 0;
      ll rem = X % b;
      if (b % 2 == 0)
        ans = b - rem;
      else
        ans = rem;
      cout << ans << endl;
    }
  }

  return 0;
}