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

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

long long int
modPow(long long int a,
       long long int n) { // 繰り返し自乗法(a^n mod p)。計算量 O(logN)
  long long int p = pow(10, 9) + 7; // 任意のp(素数) modのやつで使える
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return a % p;
  }
  if (n % 2 == 1) {
    return (a * modPow(a, n - 1)) % p;
  }
  long long t = modPow(a, n / 2);
  return (t * t) % p;
}

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

  bool flag; // true:+. false:-
  long long int count = 0;
  /*
  if(x > 0){
      //flag = true;
      while(x >= 0 && k > 0){
          x -= d;
          k--;
      }
  }else{
      //flag = false;
      while(x <= 0 && k > 0){
          x += d;
          k--;
      }
  }*/

  if (x == 0) {

  } else if (x > 0) {
    if (x % d == 0) {
      count = x / d;
    } else {
      count = x / d + 1;
    }

    if (k <= count) {
      x = x - k * d;
      k = 0;
    } else {
      x = x - d * count;
      k = k - count;
    }
  } else {
    if (x % d == 0) {
      count = abs(x) / d;
    } else {
      count = abs(x) / d + 1;
    }

    if (k <= count) {
      x = x + k * d;
      k = 0;
    } else {
      x = x + d * count;
      k = k - count;
    }
  }

  if (k == 0) {
    cout << abs(x) << endl;
  } else if (k % 2 == 0) {
    cout << abs(x) << endl;
  } else {
    ans = min(abs(x + d), abs(x - d));
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

long long int
modPow(long long int a,
       long long int n) { // 繰り返し自乗法(a^n mod p)。計算量 O(logN)
  long long int p = pow(10, 9) + 7; // 任意のp(素数) modのやつで使える
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return a % p;
  }
  if (n % 2 == 1) {
    return (a * modPow(a, n - 1)) % p;
  }
  long long t = modPow(a, n / 2);
  return (t * t) % p;
}

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

  bool flag; // true:+. false:-
  int count = 0;

  if (x > 0) {
    // flag = true;
    while (x >= 0 && k > 0) {
      x -= d;
      k--;
    }
  } else {
    // flag = false;
    while (x <= 0 && k > 0) {
      x += d;
      k--;
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define printInt(a) printf("%d\\n", a)
#define printll(a) printf("%lld\\n", a)
#define printdbl(a) printf("%lf\\n", a)
#define printYes() printf("Yes\\n")
#define printNo() printf("No\\n")
#define scanll(a) scanf("%lld", &a)
#define scanInt(a) scanf("%d", &a)
#define scan2Int(a, b) scanf("%d %d", &a, &b)
#define scan3Int(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define scan4Int(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
typedef long long ll;
const int INF = 1001001001;
using namespace std;
void swap(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
  return;
}
int gdc(int a, int b) {
  int r;
  if (a < b)
    swap(&a, &b);
  r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int factorial(int a) {
  int ans = 1;
  for (int i = 1; i <= a; ++i)
    ans *= i;
  return ans;
}

int main() {
  ll x, k, d;
  scanf("%lld %lld %lld", &x, &k, &d);
  x = abs(x);
  ll tmp = x / d;
  if (tmp <= k) {
    x -= d * tmp;
    k -= tmp;
  } else {
    x -= d * k;
    k = 0;
  }
  d *= k % 2;
  x = abs(x - d) < abs(x + d) ? abs(x - d) : abs(x + d);
  printll(x);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define printInt(a) printf("%d\\n", a)
#define printll(a) printf("%lld\\n", a)
#define printdbl(a) printf("%lf\\n", a)
#define printYes() printf("Yes\\n")
#define printNo() printf("No\\n")
#define scanll(a) scanf("%lld", &a)
#define scanInt(a) scanf("%d", &a)
#define scan2Int(a, b) scanf("%d %d", &a, &b)
#define scan3Int(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define scan4Int(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
typedef long long ll;
const int INF = 1001001001;
using namespace std;
void swap(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
  return;
}
int gdc(int a, int b) {
  int r;
  if (a < b)
    swap(&a, &b);
  r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int factorial(int a) {
  int ans = 1;
  for (int i = 1; i <= a; ++i)
    ans *= i;
  return ans;
}

int main() {
  ll x, k, d;
  scanf("%lld %lld %lld", &x, &k, &d);
  x = abs(x);
  while (x > 0 && k > 0) {
    x -= d;
    k--;
  }
  d *= k % 2;
  x = abs(x - d) < abs(x + d) ? abs(x - d) : abs(x + d);
  printll(x);
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
int main() {
  long long X, K, D;
  unsigned long long answer = 0;
  int flag = -1;
  cin >> X >> K >> D;
  unsigned long long quo = abs(X) / D;
  while (flag == -1 && K > 0) {
    if (abs(X) <= D) {
      if (K % 2 == 0)
        answer = abs(X);
      else
        answer = abs(abs(X) - D);
      flag = 1;
    } else {
      if (quo >= K) {
        answer = abs(abs(X) - K * D);
        flag = 1;
      } else { // quo<K
        X = abs(X) % D;
        K = K - quo;
      }
    }
  }

  printf("%llu\\n", answer);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
int main() {
  long long X, K, D;
  unsigned long long answer = 0;
  cin >> X >> K >> D;
  unsigned long long quo = abs(X) / D;
  while (answer == 0 && K > 0) {
    if (abs(X) < D) {
      if (K % 2 == 0)
        answer = abs(X);
      else
        answer = abs(abs(X) - D);
    } else {
      if (quo >= K) {
        answer = abs(abs(X) - K * D);
      } else { // quo<K
        X = abs(X) % D;
        K = K - quo;
      }
    }
  }

  printf("%llu\\n", answer);
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef vector<int> vi;
typedef unsigned long ul;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef set<int> si;
typedef multiset<int> mi;

const ll INF = 1e18;
const int MOD = 1e9 + 7;

int main() {

  ll X, K, D;
  cin >> X >> K >> D;

  X = abs(X);

  ll steps = X / D;

  if (steps > K) {
    cout << X - K * D << "\\n";
  }

  else {
    if ((K - steps) % 2 == 0)
      cout << X - D * steps << "\\n";
    else
      cout << D * (steps + 1) - X << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef vector<int> vi;
typedef unsigned long ul;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef set<int> si;
typedef multiset<int> mi;

const ll INF = 1e18;
const int MOD = 1e9 + 7;

int main() {

  ll X, K, D;
  cin >> X >> K >> D;

  X = abs(X);

  ll steps = 0;

  for (ll k = X / K / D + 1; k >= 1; k /= 2)
    while (steps + k <= K && ul(X) >= ul((steps + k)) * ul(D))
      steps += k;

  if (steps % 2 != K % 2)
    ++steps;

  // cout<<steps<<"\\n";

  if (steps <= K) {
    // cout<<steps*D<<" "<<X<<"\\n";
    if (X >= steps * D)
      cout << X - steps * D << "\\n";
    else
      cout << steps * D - X << "\\n";
  } else {
    if (X >= K * D)
      cout << X - K * D << "\\n";
    else
      cout << K * D - X << "\\n";
  }

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

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

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

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


Error: WA

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  ll l[n];
  for (int i = 0; i < n; ++i) {
    cin >> l[i];
  }
  sort(l, l + n);
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int k = j + 1; k < n; ++k) {
        if (l[i] == l[j] || l[j] == l[k])
          continue;
        if (l[i] + l[j] > l[k])
          ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>

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

  long long div = std::min(K, X / D);
  K -= div;
  X -= div * D;
  if (K % 2 == 0)
    std::cout << X << std::endl;
  else
    std::cout << D - X << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

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

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

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

  ll m;
  m = ceil((double)x / d);

  ll ans;
  if (m > k)
    ans = x - k * d;
  else if ((k - m) % 2 == 0)
    ans = abs(x - m * d);
  else
    ans = x - (m - 1) * d;

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


Error: TLE

Fixed submission:

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

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

  ll y;
  rep(i, k) {
    y = x - d;
    if (y < 0) {
      y = abs(y);
      if ((k - i - 1) % 2 == 0)
        x = y;
      break;
    }
    x = y;
  }

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

#include <algorithm>
#include <iostream>
using namespace std;
using ll = long long;

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

  ll straight = min(K, X / D);
  K -= straight;
  X -= straight * D;

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  long long int x, k, d;
  cin >> x >> k >> d;

  if (x < 0) {
    x = -x;
  }
  while (1) {
    if (x - d > 0) {
      x = x - d;
      k--;
    } else
      break;
    if (k == 0)
      break;
  }
  if (k % 2 != 0) {
    x = d - x;
  }

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

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

int main() {
  long long int X;
  long long int K;
  long long int D;

  // 入力
  cin >> X >> K >> D;
  long long int ans;
  long long int mo;
  long long int n;
  long long int m;
  long long int p;

  if (abs(X) / D <= K) {
    mo = abs(X) % D;
    n = abs(X) / D;
    ans = mo;
    if ((K - n) % 2 != 0) {
      m = ans - D;
      p = ans + D;
      if (abs(m) - abs(p) <= 0) {
        ans = m;
      } else {
        ans = p;
      }
    }
  } else {
    ans = abs(X) - K * D;
  }
  cout << abs(ans) << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  long long int X;
  long long int K;
  long long int D;

  // 入力
  cin >> X >> K >> D;
  long long int ans = X;
  long long int m;
  long long int p;
  long long int ans2[2];
  ans2[0] = pow(10, 15) + 5;
  ans2[1] = pow(10, 15) + 5;
  for (int i = 1; i <= K; i++) {
    m = ans - D;
    p = ans + D;
    if (abs(m) - abs(p) <= 0) {
      ans = m;
    } else {
      ans = p;
    }
    if (i % 2 == 0) {
      // iは偶数
      if (ans2[1] != ans) {
        ans2[1] = ans;
      } else {
        if ((K - i) % 2 == 0) {
          ans = ans2[1];
          break;
        } else {
          ans = ans2[0];
          break;
        }
      }
    } else {
      // iは奇数
      if (ans2[0] != ans) {
        ans2[0] = ans;
      } else {
        if ((K - i) % 2 == 0) {
          ans = ans2[0];
          break;
        } else {
          ans = ans2[1];
          break;
        }
      }
    }
  }

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

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

int main() {
  int64_t x, d, k;

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

  if (x <= d) {
    if (k % 2 == 0) {
      cout << x << endl;
      return 0;
    } else {
      cout << d - x << endl;
      return 0;
    }
  }
  if (x > d * k && k > 100000000000000 && d < 100) {
    cout << x - d * k << endl;
    return 0;
  }
  for (int i = 0; i < k; i++) {
    if (x > 0) {
      x -= d;
    } else {
      if ((k - i) % 2 == 0) {
        cout << abs(x) << endl;
        return 0;
      } else {
        cout << abs(x + d) << endl;
        return 0;
      }
    }
  }
  cout << abs(x) << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t x, d, k;

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

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll x, k, d;

int main() {
  cin >> x >> k >> d;
  x = x > 0 ? x : -x;
  if ((double)x / k >= (double)d) {
    x -= k * d;
    cout << x << endl;
    return 0;
  }
  ll t = x / d;
  k -= t;
  x -= t * d;
  if (k & 1)
    cout << d - x << endl;
  else
    cout << x << endl;
  /*	while (k--) {
                  if (x-d>=0) {
                          x-=d;
                  } else {
                          ++k;
                          break;
                  }
          }
          if (k==-1) cout<<x<<endl;
          else {
                  if (k&1) cout<<d-x<<endl;
                  else cout<<x<<endl;
          }*/
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll x, k, d;

int main() {
  cin >> x >> k >> d;
  x = x > 0 ? x : -x;
  while (k--) {
    if (x - d >= 0) {
      x -= d;
    } else {
      ++k;
      break;
    }
  }
  if (k == -1)
    cout << x << endl;
  else {
    if (k & 1)
      cout << d - x << endl;
    else
      cout << x << endl;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \\
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  ll N, M, K, count = 0;
  cin >> N >> K >> M;
  N = abs(N);
  count = N / M;
  if (count <= K) {
    N -= M * count;
    K -= count;
  } else {
    N -= M * K;
    K = 0;
  }

  if (K == 0) {
    cout << N << endl;
    return 0;
  }
  if (K % 2 == 0) {
    cout << abs(N) << endl;
  } else {
    cout << abs(N - M) << endl;
  }
}

Error: TLE

Fixed submission:

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \\
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  ll N, M, K, count = 0;
  cin >> N >> K >> M;
  N = abs(N);
  while (N - M >= 0 && K > 0) {
    count++;
    N -= M;
    K--;
  }
  if (K == 0) {
    cout << N << endl;
    return 0;
  }
  if (K % 2 == 0) {
    cout << abs(N) << endl;
  } else {
    cout << abs(N - M) << endl;
  }
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <math.h>
#include <vector>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;

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

// あきらかにKがループできる回数じゃないのでmodで判定？
// 座標原点を挟んで2K回移動しても位置は同じか。最初だけで決まる。
int main() {
  LL X, K, D;
  scanf("%lld %lld %lld", &X, &K, &D);
  LL ans = abs(X);
  if (ans / D >= K) {
    ans = ans - K * D;
    K = 0;
  } else {
    LL used = ans / D;
    ans -= used * D;
    K -= used;
    ans -= D;
    K--;
  }

  if (K % 2 == 0)
    cout << abs(ans) << endl;
  else
    cout << abs(ans + D) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <math.h>
#include <vector>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;

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

// あきらかにKがループできる回数じゃないのでmodで判定？
// 座標原点を挟んで2K回移動しても位置は同じか。最初だけで決まる。
int main() {
  LL X, K, D;
  scanf("%lld %lld %lld", &X, &K, &D);
  LL ans = abs(X);
  while (K > 0) {
    ans -= abs(ans) / ans * D;
    K--;
    if (ans <= 0)
      break;
  }
  if (K % 2 == 0)
    cout << abs(ans) << endl;
  else
    cout << abs(ans + D) << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;

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

#include <math.h>
#include <stdio.h>
int main() {

  long long int x;
  long long int k;
  long long int d;
  long long int i;

  scanf("%lld", &x);
  scanf("%lld", &k);
  scanf("%lld", &d);
  x = fabs(x);
  /*for (long long int i = 0;i < k;i++)
  {
          if (x > 0)
          {
                  x -= d;
          }
          else
          {
                  x += d;
          }
  }*/
  if (x / d < k) {
    for (i = 0; i < k; i++) {
      x -= d;
      if (x < 0) {
        break;
      }
    }
    if ((k - i) % 2 == 0) {
      x += d;
    }
  } else
    x -= d * k;
  x = fabs(x);

  printf("%lld", x);

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {

  long long int x;
  long long int k;
  long long int d;

  scanf("%lld", &x);
  scanf("%lld", &k);
  scanf("%lld", &d);
  for (long long int i = 0; i < k; i++) {
    if (x > 0) {
      x -= d;
    } else {
      x += d;
    }
  }
  if (x < 0) {
    x = -x;
  }

  printf("%lld", x);

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

#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;
  cin >> x >> k >> d;
  x = abs(x);
  ll ans;
  if (x / d >= k)
    ans = x - d * k;
  else {
    ll e = x / d; // 往復開始位置まで移動するのに必要な回数
    k -= e;       // 残りの操作回数
    x -= e * d;   // 往復開始位置
    if (k % 2 == 1)
      x = abs(
          x -
          d); // 奇数の場合往復開始位置から1回移動させる必要があるためxからd引いて絶対値を取る。
    ans = x;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

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, ans = 1001001001001001;
  cin >> x >> k >> d;
  if (k % 2 == 0) {
    for (int i = 0; i <= k; i += 2) {
      ans = min(ans, abs(abs(x) - d * i));
      if (ans < abs(abs(x) - d * i))
        break;
    }
  } else {
    for (int i = 1; i <= k; i += 2) {
      ans = min(ans, abs(abs(x) - d * i));
      if (ans < abs(abs(x) - d * i))
        break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  if (x / d > k) {
    while (k) {
      x -= d;
      k--;
    }
    cout << x;
    return 0;
  }
  k -= x / d;
  x %= d;
  if (k % 2 == 1)
    cout << abs(x - d);
  else
    cout << abs(x);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  while (x > 0 && k > 0) {
    k--;
    x -= d;
  }
  if (k == 0)
    cout << abs(x) << '\\n';
  else {
    if (k % 2 == 0)
      cout << abs(x) << '\\n';
    else
      cout << x + d << '\\n';
  }
  return 0;
}