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

// Indrajeet Mishra

/*#pragma warning(disable:4996)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define test(s)                                                                \\
  if (!(s)) {                                                                  \\
    cout << __LINE__ << " " << #s << endl;                                     \\
    exit(-1);                                                                  \\
  }
#define debug(x)                                                               \\
  do {                                                                         \\
    std::cerr << x;                                                            \\
  } while (0)
#define endl "\\n"
#define pb push_back
#define pf push_front
#define vll vector<ll>
#define dll deque<ll>
#define sll set<ll, greater<ll>>
#define mll map<ll, ll>
#define vii vector<int>
#define dii deque<int>
#define sii set<int, greater<int>>
#define mii map<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mod 1000000007
#define inv2 500000004
#define inv6 166666668
#define N 1000005
#define inf 200000000
#define eps 1e-10
#define PI 3.14159265359
#define all(v) (v).begin(), (v).end()
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define gcd(a, b) __gcd(a, b)

ll dp[101][101];
void dp_initialiser() { memset(dp, -1, sizeof(dp)); }

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  dp_initialiser();
  ll x = 0, k = 0, d = 0;
  cin >> x >> k >> d;
  x = abs(x);
  ll p = x / d;
  if (p >= k)
    cout << (x - k * d);
  else {
    ll a = (x - d * (x / d));
    ll count1 = k - (x / d);
    if (count1 % 2)
      a = abs(a - d);
    a = abs(a);
    ll b = x - d * ((x / d) + 1);
    ll count2 = k - 1 - (x / d);
    if (count2 % 2)
      b = abs(b + d);
    b = abs(b);
    cout << min(a, b);
  }
  return 0;
}

Error: TLE

Fixed submission:

// Indrajeet Mishra

/*#pragma warning(disable:4996)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("-ffloat-store")*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define test(s)                                                                \\
  if (!(s)) {                                                                  \\
    cout << __LINE__ << " " << #s << endl;                                     \\
    exit(-1);                                                                  \\
  }
#define debug(x)                                                               \\
  do {                                                                         \\
    std::cerr << x;                                                            \\
  } while (0)
#define endl "\\n"
#define pb push_back
#define pf push_front
#define vll vector<ll>
#define dll deque<ll>
#define sll set<ll, greater<ll>>
#define mll map<ll, ll>
#define vii vector<int>
#define dii deque<int>
#define sii set<int, greater<int>>
#define mii map<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mod 1000000007
#define inv2 500000004
#define inv6 166666668
#define N 1000005
#define inf 200000000
#define eps 1e-10
#define PI 3.14159265359
#define all(v) (v).begin(), (v).end()
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define gcd(a, b) __gcd(a, b)

ll dp[101][101];
void dp_initialiser() { memset(dp, -1, sizeof(dp)); }

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  dp_initialiser();
  ll x = 0, k = 0, d = 0;
  cin >> x >> k >> d;
  rep(i, 0, k) {
    if (x >= 0)
      x -= d;
    else
      x += d;
  }
  cout << abs(x);
  return 0;
}