alexjercan/bugnet huggingface dataset:
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f(a, b) for (ll i = a; i <= b; i++)
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
#define N 200005
const ll INF = 1000000000000000001;
const ll MOD = 1000000007;

// vector< vector<ll> > g(N);
// vector< pair<ll, pair< ll, ll > > > g(N);
// ll vis[N] = {0}, par[N] = {0}, child[N] = {0},cnt = 0;

void solve() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll step = x / d;
  if (k <= step) {
    cout << abs(x - d * k);
    return;
  }

  k -= x / d;

  if (k % 2 == 0) {
    cout << abs(x - (x / d) * d) << '\\n';
  } else
    cout << abs(d - (x - (x / d) * d)) << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  // ll t;cin >> t;for(ll index=1; index<=t; index++){
  // cout << "Case #" << index << ": " ;
  solve();
  // }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f(a, b) for (ll i = a; i <= b; i++)
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
#define N 200005
const ll INF = 1000000000000000001;
const ll MOD = 1000000007;

// vector< vector<ll> > g(N);
// vector< pair<ll, pair< ll, ll > > > g(N);
// ll vis[N] = {0}, par[N] = {0}, child[N] = {0},cnt = 0;

void solve() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll rem = x % d;
  ll step = x / d;
  if (k < step) {
    cout << abs(x - d * k);
    return;
  }
  if (k == step) {
    cout << "0\\n";
    return;
  }
  ll rem1 = abs(rem - d);
  if ((k / step) % 2 == 0) {
    cout << rem << '\\n';
  } else
    cout << rem1 << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  // ll t;cin >> t;for(ll index=1; index<=t; index++){
  // cout << "Case #" << index << ": " ;
  solve();
  // }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long x, k, d, c;
  long long a = -10000000000000000, b, i = 0, f = 0;
  cin >> x >> k >> d;
  b = abs(x);
  while (i < k) {
    if (x > 0) {
      if (d > 0) {
        c = min(max(x / d, 1ll), k - i);
        x -= c * abs(d);
      } else {
        c = min(max(x / abs(d), 1ll), k - i);
        x += c * abs(d);
      }
      if (x <= 0) {
        f = 1;
      }
    } else {
      if (d > 0) {
        c = min(max(abs(x) / d, 1ll), k - i);
        x += c * d;
        if (x >= 0) {
          f = 1;
        }
      } else {
        c = min(max(abs(x) / abs(d), 1ll), k - i);
        x -= c * abs(d);
        if (x >= 0) {
          f = 1;
        }
      }
    }
    i += c;
    if (i % 2 == 1 && abs(x) < abs(a)) {
      a = abs(x);
    }
    if (i % 2 == 0 && abs(x) < abs(b)) {
      b = abs(x);
    }
    if (f) {
      break;
    }
  }
  if (k % 2 == 1) {
    cout << a << endl;
  } else {
    cout << b << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long x, k, d;
  long long a = 0, b, i = 0, f = 0;
  cin >> x >> k >> d;
  b = abs(x);
  while (i < k) {
    if (x > 0) {
      if (d > 0) {
        x -= d;
        if (x < 0) {
          f = 1;
        }
      } else {
        x += d;
        if (x < 0) {
          f = 1;
        }
      }
    } else {
      if (d > 0) {
        x += d;
        if (x > 0) {
          f = 1;
        }
      } else {
        x -= d;
        if (x > 0) {
          f = 1;
        }
      }
    }
    if (i == 0 || i % 2 == 0 && abs(x) < abs(a)) {
      a = abs(x);
    }
    if (i % 2 == 1 && abs(x) < abs(b)) {
      b = abs(x);
    }
    if (f) {
      break;
    }
    i++;
  }
  if (k % 2 == 1) {
    cout << a << endl;
  } else {
    cout << b << endl;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<ll>
#define vvll vector<vll>
#define mll map<ll, ll>
#define f(i, m, n) for (i = m; i < n; i++)
#define ee endl
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define take(v, n)                                                             \\
  for (i = 0; i < n; i++)                                                      \\
    cin >> v[i];
#define NO() cout << "NO" << endl
#define YES() cout << "YES" << endl
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sot(v) sort(v.begin(), v.end());
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

void solve() {
  ll i, j, k, l, n;
  cin >> n >> k >> j;
  n = abs(n);
  ll cnt = n / j;
  l = k;
  k -= cnt;
  // cout<<cnt<<" "<<k<<ee;

  // while(n>0 && k>0)
  // {
  //     n-=j;
  //     k--;
  // }
  if (k <= 0) {
    cout << abs(n - l * j) << ee;
  } else {
    n -= (cnt * j);
    if (k % 2 == 0)
      cout << n << ee;
    else
      cout << abs(n - j) << ee;
  }
}

int main() {
  fast;
  ll i, j, l, k, t, n;
  t = 1;
  while (t--)
    solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<ll>
#define vvll vector<vll>
#define mll map<ll, ll>
#define f(i, m, n) for (i = m; i < n; i++)
#define ee endl
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define take(v, n)                                                             \\
  for (i = 0; i < n; i++)                                                      \\
    cin >> v[i];
#define NO() cout << "NO" << endl
#define YES() cout << "YES" << endl
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sot(v) sort(v.begin(), v.end());
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

void solve() {
  ll i, j, k, l, n;
  cin >> n >> k >> j;
  while (k--) {
    if (n > 0)
      n = n - j;
    else
      n += j;
  }
  cout << abs(n) << ee;
}

int main() {
  fast;
  ll i, j, l, k, t, n;
  t = 1;
  while (t--)
    solve();
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using namespace boost::multiprecision;

inline bool isEven(const int128_t a) { return 0 == a % 2; }

int main() {
  int128_t X, K, D;
  cin >> X >> K >> D;
  int128_t absX = abs(X);

  int128_t minAbs;

  if (absX >= D) {
    if (K <= absX / D) {
      minAbs = absX - K * D;
    } else {
      if (isEven(K)) {
        if (isEven(absX / D)) {
          minAbs = absX - (absX / D) * D;
          if (K >= absX / D + 2) {
            int128_t _abs = abs(minAbs - 2 * D);
            minAbs = min(minAbs, _abs);
          }
        } else {
          minAbs = absX - (absX / D - 1) * D;
          if (K >= absX / D + 1) {
            int128_t _abs = abs(minAbs - 2 * D);
            minAbs = min(minAbs, _abs);
          }
        }
      } else {
        if (isEven(absX / D)) {
          minAbs = absX - (absX / D - 1) * D;
          if (K >= absX / D + 1) {
            int128_t _abs = abs(minAbs - 2 * D);
            minAbs = min(minAbs, _abs);
          }
        } else {
          minAbs = absX - (absX / D) * D;
          if (K >= absX / D + 2) {
            int128_t _abs = abs(minAbs - 2 * D);
            minAbs = min(minAbs, _abs);
          }
        }
      }
    }
  } else {
    if (isEven(K)) {
      minAbs = absX;
    } else {
      minAbs = abs(absX - D);
    }
  }

  cout << minAbs << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using namespace boost::multiprecision;

int main() {
  int128_t X, K, D;
  cin >> X >> K >> D;

  uint128_t minAbs = numeric_limits<uint128_t>::max();
  uint128_t _abs;

  if (0 <= X) {
    for (int128_t i = -1 * K * D; i <= K * D; i += 2 * D) {
      _abs = (uint128_t)abs(X + i);
      if (minAbs > _abs) {
        minAbs = _abs;
      } else {
        break;
      }
    }
  } else {
    for (int128_t i = (0 == K % 2) ? 0 : -1 * D; i <= K * D; i += 2 * D) {
      _abs = (uint128_t)abs(X + i);
      if (minAbs > _abs) {
        minAbs = _abs;
      } else {
        break;
      }
    }
  }
  cout << minAbs << endl;

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

#include <iostream>
#include <map>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  if (X < 0)
    X = -X;
  ll r = X / D;
  if (r >= K) {
    cout << X - D * K << "\\n";
  } else {
    K -= r;
    cout << abs((X % D) - (K % 2 == 0 ? 0 : D)) << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  ll X, K, D;
  cin >> X >> K >> D;
  vector<ll> path;
  map<ll, ll> idx;
  ll now = X;

  while (path.size() < K) {
    if (now < 0)
      now += D;
    else
      now -= D;
    if (idx.find(now) != idx.end()) {
      ll rep_s = idx[now];
      ll f = path.size() - rep_s;
      ll rem = K - path.size();
      ll last_idx = rep_s + ((rem - 1) % f);
      ll last_x = path[last_idx];
      path.push_back(last_x);
      break;
    } else {
      path.push_back(now);
      idx[now] = path.size() - 1;
    }
  }
  cout << abs(path.back()) << "\\n";
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  long long x, k, d;
  cin >> x;
  cin >> k;
  cin >> d;
  int i;

  if (x < 0)
    x = -x;
  if (k % 2 == 0) {
    long long id;
    id = x / d;
    // if(id<0) id = -id;

    if (id >= k) {
      cout << abs(x - k * d) << endl;
    } else {
      if (id % 2 == 0) {
        cout << abs(x - id * d) << endl;
      } else {
        long long t1 = abs(x - (id + 1) * d);
        long long t2 = abs(x - (id - 1) * d);
        cout << min(t1, t2) << endl;
      }
    }
  } else {
    long long id;
    id = x / d;
    // if(id<0) id = -id;
    if (id >= k) {
      cout << abs(x - k * d) << endl;
    } else {
      if (id % 2 == 1) {
        cout << abs(x - id * d) << endl;
      } else {
        long long t1 = abs(x - (id + 1) * d);
        long long t2 = abs(x - (id - 1) * d);
        cout << min(t1, t2) << endl;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long long x, k, d;
  cin >> x;
  cin >> k;
  cin >> d;
  int i;
  long long val = 100000000000000000;

  if (k % 2 == 0) {
    for (int i = 0; i <= k; i += 2) {
      long long t;
      if (x > 0)
        t = abs(x - d * i);
      else
        t = abs(x + d * i);

      if (t < val)
        val = t;
      else
        break;
    }
  } else {
    for (int i = 1; i <= k; i += 2) {
      long long t;
      if (x > 0)
        t = abs(x - d * i);
      else
        t = abs(x + d * i);
      if (t < val)
        val = t;
      else
        break;
    }
  }
  cout << val << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  s64 x, k, d;
  cin >> x >> k >> d;
  x = abs(x);

  s64 ans;
  if (abs(x) / d > k) {
    ans = abs(x) - d * k;
  } else {
    k -= abs(x) / d;
    x -= abs(x) / d * d;
    if (k % 2 == 0)
      ans = x;
    else
      ans = abs(x - d);
  }

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


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  s64 n, k;
  cin >> n >> k;

  vs64 p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    --p[i];
  }
  rep(i, n) { cin >> c[i]; }

  s64 dp[n + 100][n + 100];
  int dst[n + 100][n + 100];
  rep(i, n) dp[i][0] = 0;
  rep(i, n) dst[i][0] = i;

  rep(j, n) {
    rep(i, n) { dst[i][j + 1] = p[dst[i][j]]; }
  }

  rep(j, n) {
    rep(i, n) { dp[i][j + 1] = dp[i][j] + c[dst[i][j + 1]]; }
  }

  s64 ans = -1e18;
  rep(i, n) {
    s64 loop;
    for (loop = 1; loop <= n; ++loop) {
      if (dst[i][loop] == i) {
        break;
      }
    }

    s64 tk = k;
    s64 sum = -1e18;
    if (dp[i][loop] > 0) {
      tk -= k / loop * loop;
      sum = dp[i][loop] * (k / loop);
      s64 temp = 0;
      rep(j, MIN(n + 1, tk + 1)) {
        if (temp < dp[i][j]) {
          temp = dp[i][j];
        }
      }
      sum += temp;
    } else {
      rep(j, n) { sum = max(sum, dp[i][j + 1]); }
    }
    ans = max(ans, sum);
  }

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

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long int n, k, d;
  cin >> n >> k >> d;
  long long int tmp = k;

  k = (abs(n) / d > k) ? 0 : k - (abs(n) / d);
  n = (abs(n) / d <= tmp) ? abs(n) % d : abs(abs(n) - (d * tmp));

  if (k % 2 == 1) {
    if (abs(n + d) > abs(n - d)) {
      n = abs(n - d);
    } else {
      n = abs(n + d);
    }
  }
  cout << abs(n) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long int n, k, d;
  cin >> n >> k >> d;
  for (int i = 0; i < k; i++) {
    if (abs(n - d) < abs(n + d)) {
      n -= d;
    } else {
      n += d;
    }
  }
  cout << abs(n) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, o) for (ll i = 0; i < (o); i++)
#define rep_s(i, o) for (int i = 1; i <= (o); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) < (int)(b); i++)
#define NUM 1e5

typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    return b;
  }
  return a;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return a;
  }
  return a;
}

const int MOD = 1000000007;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;

  if (x < 0)
    x = -x;
  ll ans = x;
  if (x / d < k) {
    ans = x % d;
    if ((x / d) % 2 == 0) {
      ans = (k % 2 == 0) ? ans : abs(ans - d);
    } else {
      ans = (k % 2 == 1) ? ans : abs(ans - d);
    }
  } else {
    ans = x - k * d;
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, o) for (ll i = 0; i < (o); i++)
#define rep_s(i, o) for (int i = 1; i <= (o); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) < (int)(b); i++)
#define NUM 1e5

typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    return b;
  }
  return a;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return a;
  }
  return a;
}

const int MOD = 1000000007;
int main() {
  ll x, k, d;
  cin >> x >> k >> d;

  ll ans = x;
  ll count = 0;
  if (x < 0)
    x = -x;
  rep(i, k) {
    x -= d;
    count++;
    ans = x;
    if (x < 0) {
      if (count % 2 == 1) {
        ans = (k % 2 == 1) ? abs(x) : x + d;
      } else {
        ans = (k % 2 == 0) ? abs(x) : x + d;
      }
      break;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
using namespace std;

int main() {
  ll X, D, K, ans = 0;
  cin >> X >> K >> D;
  if (X > 0) {
    if (X / D >= K) {
      ans = X - K * D;
      K = 0;
    } else {
      K -= X / D;
      X -= D * (X / D);
    }
    if (K > 0) {
      if (K % 2 == 0) {
        ans = X;
      } else {
        ans = min(abs(X + D), abs(X - D));
      }
    }
  } else {
    if (abs(X) / D >= K) {
      ans = X + K * D;
      K = 0;
    } else {
      K -= abs(X) / D;
      X -= D * (X / D);
    }
    if (K > 0) {
      if (K % 2 == 0) {
        ans = X;
      } else {
        ans = min(abs(X + D), abs(X - D));
      }
    }
  }

  cout << abs(ans) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
using namespace std;

char c[10][10];
char C[10][10];
char C2[10][10];

int main() {
  ll X, D, K, ans = 0;
  cin >> X >> K >> D;
  if (X > 0) {
    while (K != 0 && X > 0) {
      X -= D;
      K--;
    }
    if (X > 0) {
      ans = X;
    } else {
      if (K % 2 == 0) {
        ans = X;
      } else {
        ans = X + D;
      }
    }
  } else {
    while (K != 0 && X < 0) {
      X += D;
      K--;
    }
    if (X < 0) {
      ans = X;
    } else {
      if (K % 2 == 0) {
        ans = X;
      } else {
        ans = X - D;
      }
    }
  }

  cout << abs(ans) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  long long n = (d ? x / d : 0), r = (d ? x % d : 0);
  if (n > k)
    cout << (x - k * d);
  else if ((k - n) % 2)
    cout << abs(d - r);
  else
    cout << r;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  while (k--) {
    if (x > 0)
      x -= d; // 현재위치(x)가 양수인 경우
    else
      x += d; // 현재위치(x)가 음수거나, 0인 경우
  }
  cout << abs(x);
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define _GLIBCXX_DEBUG

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

typedef long long ll;
ll x, k, d, ans, mydiv, q;

int main() {
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;

  mydiv = x / d;
  q = x % d;

  if (k < mydiv)
    ans = x - k * d;
  else if ((k - mydiv) % 2 == 1)
    ans = d - q;
  else
    ans = q;

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define _GLIBCXX_DEBUG

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

typedef long long ll;
ll x, k, d;

int main() {
  cin >> x >> k >> d;

  for (int tmp = k / 2; k > 0; tmp /= 2) {
    if (tmp == 0)
      tmp++;
    if (abs(x - d * tmp) > abs(x + d * tmp)) {
      x += d * tmp;
    } else {
      x -= d * tmp;
    }
    k -= tmp;
  }

  cout << abs(x) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ABS(x) ((x) >= 0 ? (x) : -(x))

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long x, k, d;
  cin >> x >> k >> d;
  if (x > 0) {
    long long cat = ABS(x) / ABS(d);
    if (k >= cat) {
      x -= cat * d;
      k -= cat;
    } else {
      x -= k * d;
      k = 0;
    }
  } else if (x < 0) {
    long long cat = ABS(x) / ABS(d);
    if (k >= cat) {
      x += cat * d;
      k -= cat;
    } else {
      x += k * d;
      k = 0;
    }
  }
  if (k & 1)
    cout << min(ABS(x - d), ABS(x + d));
  else
    cout << ABS(x);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ABS(x) ((x) >= 0 ? (x) : -(x))

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long x, k, d;
  cin >> x >> k >> d;
  if (x > 0) {
    long long cat = x / d + ((x % d) != 0);
    if (k >= cat) {
      x -= cat * d;
      k -= cat;
    } else {
      x -= k * d;
      k = 0;
    }
    while (x > 0 && k > 0) {
      x -= d;
      --k;
    }
  } else if (x < 0) {
    long long cat = x / d + ((x % d) != 0);
    if (k >= cat) {
      x += cat * d;
      k -= cat;
    } else {
      x += k * d;
      k = 0;
    }
    while (x < 0 && k > 0) {
      x += d;
      --k;
    }
  }
  if (k & 1)
    cout << min(ABS(x - d), ABS(x + d));
  else
    cout << ABS(x);
}