alexjercan/bugnet huggingface dataset:
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  long long X, K, D;
  cin >> X >> K >> D;

  if (X < 0) {
    X = X * (-1);
  }

  if (K > 10000000000 && D < 10000) {
    long long S = K * D;
    if (S <= X) {
      cout << X - S << endl;
    } else {
      long long idou = 0;
      long long count = 0;

      for (int i = 0; i < K; i++) {
        idou = idou + D;
        if (idou >= X) {
          count = i + 1;
          break;
        }
      }

      if (count == 0) {
      } else if (count % 2 == K % 2) {
        cout << idou - X << endl;
      } else {
        cout << X - idou + D << endl;
      }
    }
  }

  else {

    long long idou = 0;
    long long count = 0;

    for (int i = 0; i < K; i++) {
      idou = idou + D;
      if (idou >= X) {
        count = i + 1;
        break;
      }
    }

    if (count == 0) {
      cout << X - idou << endl;
    } else if (count % 2 == K % 2) {
      cout << idou - X << endl;
    } else {
      cout << X - idou + D << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

int main() {

  long long X, K, D;
  cin >> X >> K >> D;

  if (X < 0) {
    X = X * (-1);
  }

  long long idou = 0;
  long long count = 0;

  for (int i = 0; i < K; i++) {
    idou = idou + D;
    if (idou >= X) {
      count = i + 1;
      break;
    }
  }

  if (count == 0) {
    cout << X - idou << endl;
  } else if (count % 2 == K % 2) {
    cout << idou - X << endl;
  } else {
    cout << X - idou + D << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const int MOD = 1000000007;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  ll a;

  if (x < 0)
    x *= -1;

  a = x / d;

  if (a >= k) {

    x -= d * k;
    k -= k;

  } else {
    k -= a;
    x -= d * a;
  }

  while (k > 0 && x > 0) {
    k--;
    x -= d;
  }

  if (k == 0)
    cout << abs(x);
  else if (k % 2 == 1)
    cout << x + d;
  else
    cout << abs(x);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const int MOD = 1000000007;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

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

  if (x < 0)
    x *= -1;

  while (k > 0 && x > 0) {
    k--;
    x -= d;
  }

  if (k == 0)
    cout << abs(x);
  else if (k % 2 == 1)
    cout << x + d;
  else
    cout << abs(x);
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll x, k, d;
  scanf("%lld%lld%lld", &x, &k, &d);
  x = abs(x);
  ll m = x % d;
  ll t = x / d;
  if (k < t) {
    printf("%lld\\n", x - d * k);
  } else {
    if (k == t)
      printf("%lld\\n", m);
    else {
      if ((k - t) % 2 == 0)
        printf("%lld\\n", m);
      else
        printf("%lld\\n", min(abs(m - d), abs(m + d)));
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll x, k, d, dy, dt;
void dfs(ll dx, ll time) {
  int i;
  if (time == k) {
    printf("%lld\\n", abs(dx));
    return;
  }
  time++;
  dy = dx + d;
  dt = dx - d;
  if (abs(dy) < abs(dt))
    dfs(dy, time);
  else
    dfs(dt, time);
}
int main() {
  scanf("%lld%lld%lld", &x, &k, &d);
  dfs(x, 0);
  // printf("%lld\\n",dy);
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;

// 最大公約数
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

// 最小公倍数
int lcm(int a, int b) { return a / gcd(a, b) * b; }

// 素数判定
bool isPrime(int x) {
  if (x < 2)
    return 0;
  else if (x == 2)
    return 1;
  if (x % 2 == 0)
    return 0;
  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

// 桁ごとの足し算
int digsum(int n) {
  int res = 0;
  while (n > 0) {
    res += n % 10;
    n /= 10;
  }
  return res;
}

// 約数列挙
vector<int> enum_div(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) {
        ret.push_back(n / i);
      }
    }
  }
  return ret;
}

// 累乗の計算とあまり,0で累乗そのまま出力
long long modpow(long long a, long long n, long long mod) {
  if (mod == 0)
    mod = __LONG_LONG_MAX__;
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll X;
ll K;
ll D;
int main() {
  cin >> X >> K >> D;
  X = abs(X);
  ll num = X / D;
  if (X % D > 0)
    num++;

  if (num > K)
    X -= D * K;
  else {
    X -= D * num;
    if ((K - num) % 2 == 1)
      X += D;
  }
  cout << abs(X) << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;

// 最大公約数
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

// 最小公倍数
int lcm(int a, int b) { return a / gcd(a, b) * b; }

// 素数判定
bool isPrime(int x) {
  if (x < 2)
    return 0;
  else if (x == 2)
    return 1;
  if (x % 2 == 0)
    return 0;
  for (int i = 3; i * i <= x; i += 2) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

// 桁ごとの足し算
int digsum(int n) {
  int res = 0;
  while (n > 0) {
    res += n % 10;
    n /= 10;
  }
  return res;
}

// 約数列挙
vector<int> enum_div(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) {
        ret.push_back(n / i);
      }
    }
  }
  return ret;
}

// 累乗の計算とあまり,0で累乗そのまま出力
long long modpow(long long a, long long n, long long mod) {
  if (mod == 0)
    mod = __LONG_LONG_MAX__;
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll X;
ll K;
ll D;
int main() {
  cin >> X >> K >> D;
  for (int i = 0; i < K; ++i) {
    ll X2;
    if (X > 0)
      X2 = X - D;
    else
      X2 = X + D;
    if ((X2 <= 0 && 0 <= X) || (X <= 0 && 0 <= X2)) {
      if ((K - 1 - i) % 2)
        break;
      else {
        X = X2;
        break;
      }
    }
    X = X2;
  }
  cout << abs(X) << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  if (x / d >= k) {
    cout << x - k * d << endl;
    return 0;
  }
  if ((k - (x / d)) % 2 == 0)
    cout << x % d << endl;
  else
    cout << abs(d - x % d) << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long X, K, D;
  cin >> X >> K >> D;
  long long a = 0, b = 0;
  for (int i = 0; i < K; i++) {
    if (X + D < 0)
      a = -1 * (X + D);
    else
      a = X + D;
    if (X - D < 0)
      b = -1 * (X - D);
    else
      b = X - D;
    X = min(a, b);
  }
  if (X > 0)
    cout << X << endl;
  else
    cout << -1 * X << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  long long n = abs(x) / d;

  if (k > n) {
    if (x >= 0) {
      long long x2 = x - d * n;
      if (abs(x2) > abs(x2 - d)) {
        x2 -= d;
        n++;
      }

      if ((k - n) % 2 == 0) {
        cout << abs(x2) << endl;
      } else {
        cout << abs(d - abs(x2)) << endl;
      }
    } else {
      long long x2 = x + d * n;
      if (abs(x2) > abs(x2 + d)) {
        x2 += d;
        n++;
      }

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

  return 0;
}

Error: TLE

Fixed submission:

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

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

  long long i = 0;
  while (1) {
    if (i >= k) {
      break;
    }

    if (x >= 0) {
      if (abs(x - d) >= abs(x)) {
        break;
      }

      x -= d;
      i++;
    } else {
      if (abs(x + d) >= abs(x)) {
        break;
      }

      x += d;
      i++;
    }
  }

  if (k - i > 0) {
    if ((k - i) % 2 == 0) {
      cout << abs(x) << endl;
    } else {
      cout << abs(d - abs(x)) << endl;
    }
  } else {
    cout << abs(x) << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  long long sho = X / D;
  long long amari = X % D;

  if (sho >= K) {
    cout << X - K * D << endl;
  } else {
    K -= sho;
    if (K % 2 == 0) {
      cout << amari << endl;
    } else {
      cout << abs(amari - D) << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  for (long long i = 0; i < K; ++i) {
    if (X >= 0) {
      X -= D;
    } else {
      X += D;
    }
  }

  cout << abs(X) << endl;

  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
#define ll long long int
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define sz(s) (ll)(s.size())
#define vvi vector<vector<ll>>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define pii pair<ll, ll>
#define dbg(x, y) cout << x << " " << y << endl
#define endl '\\n'
#define M 1000000007
const ll INF = 1ll << 60;

using namespace std;
//-----------------------------------------//

void solve() {
  int128_t x, k, d;
  cin >> x >> k >> d;

  // x-p*d>=0
  // p<=(x/d)

  // x+p*d<=0
  // p<=(-x/d)
  if (x < 0)
    x = -1 * x;

  int128_t f = x / d;

  if (k > f) {
    x -= f * d;
    if ((k - f) % 2 != 0)
      x = x - d;
  } else {
    x -= k * d;
  }

  if (x < 0)
    x = -1 * x;
  cout << x;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Error: -6

Fixed submission:

#pragma GCC optimize "trapv"
#include <bits/stdc++.h>

#define ll long long int
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define sz(s) (ll)(s.size())
#define vvi vector<vector<ll>>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define pii pair<ll, ll>
#define dbg(x, y) cout << x << " " << y << endl
#define endl '\\n'
#define M 1000000007
const ll INF = 1ll << 60;

using namespace std;
//-----------------------------------------//

void solve() {
  ll x, k, d;
  cin >> x >> k >> d;

  // x-p*d>=0
  // p<=(x/d)

  // x+p*d<=0
  // p<=(-x/d)
  x = abs(x);

  ll f = x / d;

  if (k > f) {
    x = x - (k - f) * d;
  } else {
    x -= k * d;
  }

  cout << abs(x);
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  // sengen
  LL x, k, d;
  // nyuryoku
  cin >> x >> k >> d;
  LL dist = 0, count = 0;
  // keisan
  LL multi;
  if (abs(x) / d <= k) {
    count = abs(x) / d;
    if (abs(x) % d != 0) {
      count++;
      if (count > k) {
        cout << abs(abs(x) - (count - 1) * d) << endl;
        return 0;
      }
    }
  } else {
    cout << abs(abs(x) - k * d) << endl;
    return 0;
  }
  if (count == k) {
    cout << abs(abs(x) - k * d) << endl;
    return 0;
  }
  k = k - count;
  if (k % 2 == 0) {
    cout << abs(abs(x) - count * d) << endl;
  } else {
    cout << abs(abs(x) - count * d + d) << endl;
  }
  // syutsuryoku
}

Error: TLE

Fixed submission:

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

int main() {
  // sengen
  LL x, k, d;
  // nyuryoku
  cin >> x >> k >> d;
  LL dist = 0, count = 0;
  // keisan
  while (dist < abs(x)) {
    count++;
    dist += d;
    if (count == k) {
      break;
    }
  }
  if (count == k) {
    cout << abs(abs(x) - dist) << endl;
    return 0;
  }
  k = k - count;
  if (k % 2 == 0) {
    cout << abs(abs(x) - dist) << endl;
  } else {
    cout << abs(abs(x) - dist + d) << endl;
  }
  // syutsuryoku
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int64_t X, K, D;
  cin >> X >> K >> D;
  if (X < 0)
    X = -X;
  if (X / D < K) {
    K = K - X / D;
    X = X % D;
    if (K % 2 == 0) {
      cout << X << endl;
    } else {
      cout << abs(X - D) << endl;
    }
  } else {
    cout << abs(X - K * D) << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t X, K, D;
  cin >> X >> K >> D;
  int64_t absmin = abs(X + K * D);
  for (int64_t i = 1; i <= K; i++) {
    if (absmin > abs(X + K * D - 2 * D * i)) {
      absmin = abs(X + K * D - 2 * D * i);
    }
  }
  cout << absmin << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <limits>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define fi(i, n) for (int i = 0; i < n; i++)
#define f(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define pb push_back
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll fact(ll n) {
  ll ans = 1e14;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ll sol = (i - 1) + (n / i - 1);
      ans = min(ans, sol);
    }
  }
  return ans;
}
int visited[200000] = {0};
vector<vector<int>> v(200000);
int dfs(int x) {
  int ans = 0;
  if (visited[x] == 0) {
    visited[x] = 1;
    ans++;
    int a = v[x].size();
    for (int j = 0; j < a; j++) {
      int data = v[x][j];
      ans = ans + dfs(data);
    }
  }
  return ans;
}
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2 == 0) {
      cout << x;
    } else {
      ll y = abs(x - d);
      ll z = abs(x + d);
      cout << min(y, z) << endl;
    }
  }
  x = abs(x);
  if ((x / k) >= d) {
    x = x - k * d;
    cout << x << endl;
  } else if (x > 0) {

    while (x >= 0 && k > 0) {
      if (x - d >= 0) {
        x = x - d;
        k--;
      } else {
        break;
      }
    }
    if (k == 0) {
      cout << x << endl;
    } else {
      if (k % 2 == 0)
        cout << x << endl;
      else {
        ll y = abs(x - d);
        ll z = abs(x + d);
        cout << min(y, z) << endl;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <limits>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define fi(i, n) for (int i = 0; i < n; i++)
#define f(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define pb push_back
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll fact(ll n) {
  ll ans = 1e14;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ll sol = (i - 1) + (n / i - 1);
      ans = min(ans, sol);
    }
  }
  return ans;
}
int visited[200000] = {0};
vector<vector<int>> v(200000);
int dfs(int x) {
  int ans = 0;
  if (visited[x] == 0) {
    visited[x] = 1;
    ans++;
    int a = v[x].size();
    for (int j = 0; j < a; j++) {
      int data = v[x][j];
      ans = ans + dfs(data);
    }
  }
  return ans;
}
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2 == 0) {
      cout << x;
    } else {
      ll y = abs(x - d);
      ll z = abs(x + d);
      cout << min(y, z) << endl;
    }
  }
  x = abs(x);
  if ((x / k) > d) {
    x = x - k * d;
    cout << x << endl;
  } else if (x > 0) {

    while (x >= 0 && k > 0) {
      if (x - d >= 0) {
        x = x - d;
        k--;
      } else {
        break;
      }
    }
    if (k == 0) {
      cout << x << endl;
    } else {
      if (k % 2 == 0)
        cout << x << endl;
      else {
        ll y = abs(x - d);
        ll z = abs(x + d);
        cout << min(y, z) << endl;
      }
    }
  }
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
  ll x, k, d, r;
  cin >> x >> k >> d;
  ll l = abs(x / d);
  if (l > k) {
    if (x < 0)
      r = x + k * d;
    else
      r = x - k * d;
  } else {
    r = x % d;
    k -= l;
    if (k % 2) {
      if (r > 0)
        r -= d;
      else
        r += d;
    }
  }
  cout << abs(r);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  ll x, k, d, r;
  cin >> x >> k >> d;
  ll l = abs(x / d);
  cerr << "l = " << l << endl;
  if (l > k) {
    if (x < 0)
      r = x + k * d;
    else
      r = x - k * d;
  } else {
    r = x % d;
    if (x < 0)
      assert(-l * d + r);
    else
      assert(l * d + r);
    k -= l;
    if (k % 2) {
      if (r > 0)
        r -= d;
      else
        r += d;
    }
  }
  cout << abs(r);
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll x, k, d, ans;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  ll y = x;
  ll i = 0;
  i = y / d;

  if (i >= k)
    ans = x - k * d;
  else if ((k - i) % 2 == 0) {
    ans = x - i * d;
  } else
    ans = abs(x - i * d - d);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll x, k, d, ans;
  cin >> x >> k >> d;
  if (x < 0)
    x = -x;
  ll y = x;
  ll i = 0;
  while (y > d) {
    y = y - d;
    i++;
  }
  if (i >= k)
    ans = x - k * d;
  else if ((k - i) % 2 == 0) {
    ans = x - i * d;
  } else
    ans = abs(x - i * d - d);
  cout << ans << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <iomanip> // std::setprecision()
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define pai 3.14159265358979323846
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
/* map<string,int> */
/* 大文字を小文字に変換 tolower*/
/* 小文字を大文字に変換 toupper*/
/* 辞書順 next_permutation(a.begin(),a.end()) */

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  ll cur = abs(x);
  ll rem = k;
  ll count = min(cur / d, k);
  cur -= count * d;
  rem -= count;

  if (rem % 2 == 1) {
    cur -= d;
  }
  cout << abs(cur) << endl;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <iomanip> // std::setprecision()
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define pai 3.14159265358979323846
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
/* map<string,int> */
/* 大文字を小文字に変換 tolower*/
/* 小文字を大文字に変換 toupper*/
/* 辞書順 next_permutation(a.begin(),a.end()) */

int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  ll mi = abs(x);
  ll count = 0;
  while (count < k) {
    if (mi >= abs(x - d)) {
      mi = abs(x - d);
      x -= d;
    } else if (mi >= abs(x + d)) {
      mi = abs(x + d);
      x += d;
    } else {
      if ((k - count) % 2 != 0) {
        mi = min(abs(x - d), abs(x + d));
      }
      break;
    }
    count++;
  }
  cout << mi << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
  long long x, k, d, ans;
  cin >> x >> k >> d;
  x = abs(x);
  if (x / d > k) {
    ans = x - d * k;
  } else {
    long long e = x / d;
    k -= e;
    x -= e * d;
    if (k % 2 == 1) {
      x = abs(x - d);
    }
    ans = x;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
  long long x, k, d, min, remain;
  cin >> x >> k >> d;
  min = abs(x);
  remain = 0;
  for (long long i = 0; i < k; i++) {
    if (abs(x - d) < min) {
      min = abs(x - d);
      x -= d;
    } else if (abs(x + d) < min) {
      min = abs(x + d);
      x += d;
    } else {
      remain = k - i;
      break;
    }
  }
  if ((remain % 2) == 0) {
    cout << min;
  } else {
    if (abs(x + d) < abs(x - d)) {
      cout << abs(x + d);
    } else {
      cout << abs(x - d);
    }
  }
}