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

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

#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define nl '\\n'

/// Library
#define mem(a, b) memset(a, b, sizeof(a))
#define all(a) a.begin(), a.end()
#define Sort(x) sort(x.begin(), x.end())
#define Reverse(x) reverse(x.begin(), x.end())
#define SortA(ar, s) sort(ar, ar + s)
#define SortD(ar, s) sort(ar, ar + s, greater<int>())
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define sq(x) (x) * (x)

#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define pb push_back
#define mod 1000000007
#define end printf("\\n")
#define yes printf("YES\\n")
#define no printf("NO\\n")

/// data type
typedef long long int ll;
typedef unsigned long long int llu;

int main() {
  faster ll a, b, c, ans, x, y;
  cin >> a >> b >> c;
  if (a >= 0) {
    x = a / c;
    if (x == b)
      cout << a - (c * x) << nl;
    else if (x > b)
      cout << a - (c * b) << nl;
    else {
      a = a - (c * x) - c;
      x++;
      y = b - x;
      if (y % 2 == 0)
        cout << abs(a) << nl;
      else
        cout << a + c << nl;
    }
  } else {
    ans = abs(a);
    x = ans / c;
    if (x == b)
      cout << abs(a + c * x) << nl;
    else if (x > b)
      cout << abs(a + (c * b)) << nl;
    else {
      a = a + (c * x) + c;
      x++;
      y = b - x;
      if (y % 2 == 0)
        cout << abs(a) << nl;
      else
        cout << abs(a - c) << nl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define nl '\\n'

/// Library
#define mem(a, b) memset(a, b, sizeof(a))
#define all(a) a.begin(), a.end()
#define Sort(x) sort(x.begin(), x.end())
#define Reverse(x) reverse(x.begin(), x.end())
#define SortA(ar, s) sort(ar, ar + s)
#define SortD(ar, s) sort(ar, ar + s, greater<int>())
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define sq(x) (x) * (x)

#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define pb push_back
#define mod 1000000007
#define end printf("\\n")
#define yes printf("YES\\n")
#define no printf("NO\\n")

/// data type
typedef long long int ll;
typedef unsigned long long int llu;

int main() {
  faster ll a, b, c, ans, x, y;
  cin >> a >> b >> c;
  //	if(a>mod)
  //        a%=mod;
  //    if(b>mod)
  //        b%=mod;
  //    if(c>mod)
  //        c%=mod
  if (a == 1 && b == 1 && c == 1) {
    cout << 0 << nl;
    return 0;
  }
  if (a == b && b == c && c == a) {
    cout << a << nl;
    return 0;
  }
  while (b--) {
    if (abs(a - c) <= abs(a + c)) {
      a = abs(a - c);
    } else {
      a = abs(a + c);
    }
  }
  cout << a << nl;
  return 0;
}