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

/* C - Walking Takahashi */

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

int main() {
  long long X, K, D, p, m, ans;
  cin >> X >> K >> D;
  if (X >= 0) {
    p = X % D;
    m = p - D;
    if (K >= (X - p) / D) {
      K -= (X - p) / D;
      if (K % 2 == 0) {
        ans = llabs(p);
      } else {
        ans = llabs(m);
      }
    } else {
      ans = llabs(X - K * D);
    }
  } else {
    m = X % D;
    p = m + D;
    if (K >= (m - X) / D) {
      K -= (m - X) / D;
      if (K % 2 == 0) {
        ans = llabs(m);
      } else {
        ans = llabs(p);
      }
    } else {
      ans = llabs(X + K * D);
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

/* C - Walking Takahashi */

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

int main() {
  long long X, K, D, p, m, ans;
  cin >> X >> K >> D;
  if (X >= 0) {
    int cnt = 0;
    while (K > 0) {
      K--;
      X -= D;
      if (X < 0) {
        p = X + D;
        m = X;
        cnt++;
        break;
      }
    }
    if (cnt == 0) {
      ans = llabs(X);
    } else {
      if (K % 2 == 0) {
        ans = llabs(m);
      } else {
        ans = llabs(p);
      }
    }
  } else {
    int cnt = 0;
    while (K > 0) {
      K--;
      X += D;
      if (X >= 0) {
        p = X;
        m = X - D;
        cnt++;
        break;
      }
    }
    if (cnt == 0) {
      ans = llabs(X);
    } else {
      if (K % 2 == 0) {
        ans = llabs(p);
      } else {
        ans = llabs(m);
      }
    }
  }
  cout << ans << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

long long abs(long long a) { return (a < 0) ? -a : a; }

int sgn(long long a) { return (a < 0) ? -1 : 1; }

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

  while (K > 0) {
    if (abs(X - sgn(X) * D) < abs(X)) {
      long long times = abs(X) / D;
      if (times == 0)
        times = 1;
      if (K < times)
        times = K;
      K -= times;
      X -= sgn(X) * times * D;
    } else {
      if (K > 1) {
        K %= 2;
      } else {
        X -= sgn(X) * D;
        K--;
      }
    }
  }

  std::cout << abs(X) << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

long long abs(long long a) { return (a < 0) ? -a : a; }

int sgn(long long a) { return (a < 0) ? -1 : 1; }

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

  while (K > 0) {
    if (abs(X - sgn(X) * D) < abs(X)) {
      long long times = abs(X) / D;
      if (K < times)
        times = K;
      K -= times;
      X -= sgn(X) * times * D;
    } else {
      if (K > 1) {
        K %= 2;
      } else {
        X -= sgn(X) * D;
        K--;
      }
    }
  }

  std::cout << abs(X) << '\\n';
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
  long long x, pos, plus_min, minus_min;
  unsigned long k, d;
  cin >> x >> k >> d;
  pos = x;
  plus_min = pow(10, 15) + 1;
  minus_min = -pow(10, 15) - 1;

  long long quotient = abs(x) / d, mod = abs(x) % d;
  if (k * d <= abs(x)) {
    if (x < 0) {
      cout << -x - k * d;
    } else {
      cout << x - k * d;
    }
    return 0;
  } else {
    k -= quotient;
    pos = x < 0 ? -mod : mod;
  }

  for (unsigned long i = 1; i <= k; i++) {
    if (pos < 0) {
      pos += d;
      if (pos == plus_min) {
        if ((k - i) % 2) {
          cout << -minus_min;
          return 0;
        } else {
          cout << plus_min;
          return 0;
        }
      }
      if (pos >= 0 && pos < plus_min)
        plus_min = pos;
    } else {
      pos -= d;
      if (pos == minus_min) {
        if ((k - i) % 2) {
          cout << plus_min;
          return 0;
        } else {
          cout << -minus_min;
          return 0;
        }
      }
      if (pos < 0 && pos > minus_min)
        minus_min = pos;
    }
  }

  cout << abs(pos);

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
  long long x, pos, plus_min, minus_min;
  unsigned long k, d;
  cin >> x >> k >> d;
  pos = x;
  plus_min = pow(10, 15) + 1;
  minus_min = -pow(10, 15) - 1;

  for (unsigned long i = 1; i <= k; i++) {
    if (pos < 0) {
      pos += d;
      if (pos == plus_min) {
        if ((k - i) % 2) {
          cout << -minus_min;
          return 0;
        } else {
          cout << plus_min;
          return 0;
        }
      }
      if (pos >= 0 && pos < plus_min)
        plus_min = pos;
    } else {
      pos -= d;
      if (pos == minus_min) {
        if ((k - i) % 2) {
          cout << plus_min;
          return 0;
        } else {
          cout << -minus_min;
          return 0;
        }
      }
      if (pos < 0 && pos > minus_min)
        minus_min = pos;
    }
  }

  cout << abs(pos);

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <new>
#include <queue>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define loop(i, x, z) for (int i = x; i < z; i++)
using ll = unsigned long long int;
#define mod 1000000007
/*bool cmp(pair<ll,ll>a,pair<ll,ll>b){
return a.second<b.second;
}*/
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = llabs(x);
  if (d == 1) {
    if (x >= k) {
      x = x - k;
      k = 0;
    } else {
      k = k - x;
      x = 0;
    }
  }
  while (k > 0 && llabs(x) > d / 2) {
    k--;
    x = x - d;
  }
  if (k % 2 == 0) {
    cout << llabs(x);
  } else {
    x = min(llabs(x - d), llabs(x + d));
    cout << x;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <new>
#include <queue>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define loop(i, x, z) for (int i = x; i < z; i++)
using ll = unsigned long long int;
#define mod 1000000007
/*bool cmp(pair<ll,ll>a,pair<ll,ll>b){
return a.second<b.second;
}*/
int main() {
  ll x, k, d;
  cin >> x >> k >> d;
  x = llabs(x);
  while (k > 0 && llabs(x) > d / 2) {
    k--;
    x = x - d;
  }
  if (k % 2 == 0) {
    cout << llabs(x);
  } else {
    x = min(llabs(x - d), llabs(x + d));
    cout << x;
  }
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

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

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

  signed long long x, k, d;
  cin >> x >> k >> d;
  if (x == 0) {
    if (k % 2 == 0)
      cout << 0 << endl;
    else
      cout << d << endl;
    return 0;
  }
  if (abs(x) / d > k) {
    cout << abs(x) - d * k << endl;
    return 0;
  }
  unsigned long long re = k - x / d;
  signed long long z1, z2;
  signed long long ans;
  z1 = x % d;
  if (x < 0)
    z2 = z1 + d;
  else if (x > 0)
    z2 = z1 - d;
  if (re % 2 == 1)
    ans = z2;
  if (re % 2 == 0)
    ans = z1;
  cout << abs(ans) << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (i = 0; i < n; i++)
#define foreach(c, itr)                                                        \\
  for (__typeof(c)::iterator itr = c.begin(); itr != c.end(); itr++)
typedef long long ll;
ll i, j;
ll x, k, d;
vector<ll> v;
int ans;

ll min_element(vector<ll> qs) {
  ll mini = ll(1e16);
  for (i = 0; i < qs.size(); i++) {
    if (qs[i] < mini)
      mini = qs[i];
  }
  return mini;
}

ll bfs(ll b, ll step, ll m) {
  v[0] = b;
  for (i = 0; i < step; i++) {
    ll len = v.size();
    for (j = 0; j < len; j++) {
      ll x = v[0];
      v.erase(v.begin(), v.begin() + 1);
      len--;
      v.push_back(abs(x + m));
      v.push_back(abs(x - m));
    }
  }
  ans = min_element(v);
  return ans;
}

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

  cin >> x >> k >> d;
  bfs(x, k, d);
  cout << ans << endl;
  return 0;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> //sort(a.begin(), a.end());reverse(a.begin(), a.end());
#include <bits/stdc++.h>
#include <iostream>
#include <math.h> //pow(x,y)=x^y
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ll long long
#define rev(s) reverse(s.begin(), s.end())
#define sor(v) sort(v.begin(), v.end())

using namespace std;

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

  if (x < 0) {
    x = abs(x);
  }

  tmpp = x % d;
  tmpm = abs(tmpp - d);

  ll kaisup = (x - tmpp) / d;
  nokori = k - kaisup;

  // cout<<"kakunin"<<endl;
  // cout<<"tmpp"<<tmpp<<endl;
  // cout<<"tmpm"<<tmpm<<endl;
  // cout<<"kaisup"<<kaisup<<endl;
  // cout<<"nokori"<<nokori<<endl;

  if (kaisup >= k) {
    cout << (x - k * d) << endl;
    return 0;
  }

  if (nokori % 2 == 0) {
    cout << tmpp << endl;
  } else {
    cout << tmpm << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm> //sort(a.begin(), a.end());reverse(a.begin(), a.end());
#include <bits/stdc++.h>
#include <iostream>
#include <math.h> //pow(x,y)=x^y
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ll long long
#define rev(s) reverse(s.begin(), s.end())
#define sor(v) sort(v.begin(), v.end())

using namespace std;

int main() {
  ll x, k, d, tmpp, tmpm, nokori;
  cin >> x >> k >> d;
  int jadge;

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

// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <bits/stdc++.h>
using namespace std;

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


Error: TLE

Fixed submission:

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

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

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::pow;
using std::string;
using std::vector;

int main() {
  long long x, k, d;
  cin >> x >> k >> d;
  x = std::abs(x);
  if (x / d > k) {
    x = x - k * d;
  } else {
    k = k - x / d;
    x = x - (x / d) * d;
    if (k % 2 != 0) {
      x -= d;
    }
  }
  cout << std::abs(x);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::pow;
using std::string;
using std::vector;

int main() {
  long long x, k, d, res;
  cin >> x >> k >> d;
  x = std::abs(x);
  while (x >= d && k > 0) {
    x -= d;
    k--;
  }
  k = k % 2;
  res = std::abs(x - d * k);
  cout << res;
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << "\\n" << (#x) << " = " << (x) << "\\n"
#define int long long int

void inputoutput() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void dfs(int src, bool *vis, vector<int> v[]) {
  vis[src] = true;
  // cout<<src<<" ";

  for (auto it : v[src]) {
    if (!vis[it])
      dfs(it, vis, v);
  }
}

int32_t main() {
  inputoutput();

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

  int hopes = min(k, x / d);
  k = k - hopes;
  x = x - hopes * d;

  if (k % 2)
    cout << d - x;
  else
    cout << x;

  return 0;
}


Error: TLE

Fixed submission:



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

void startup() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int main() {
  startup();

  long long int x, k, d;
  cin >> x >> k >> d;
  if (x == 0 && k % 2 == 2)
    cout << "0";
  else if (x == 0 && k % 2 == 1)
    cout << abs(x - k * d);
  else if (x > 0) {
    while (abs(x - d) < abs(x) && k > 0) {
      x = x - d;
      --k;
    }

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

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

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  ll k1 = x / d;
  if (k1 > k)
    cout << x - k * d;
  else {
    x = x - k1 * d;
    k = k - k1;
    if (k % 2)
      cout << abs(x - d);
    else
      cout << x;
  }
}

Error: TLE

Fixed submission:

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll x, k, d;
  cin >> x >> k >> d;
  while (k--) {
    if (x > 0)
      x -= d;
    else
      x += d;
  }
  cout << abs(x);
}
Problem p02584 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long MOD = 1e9 + 7;
int MAX_INT = 1e9;

// 最大公約数
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// 最小公倍数
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  X = abs(X);

  long long num = abs(X) / D;

  if (K >= num) {
    K -= num;
    X -= num * D;
  } else {
    X -= K * D;
    K = 0;
  }

  for (K; K > 0; --K) {
    if (X <= 0) {
      break;
    }
    X -= D;
  }

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

  return 0;
}


Error: TLE

Fixed submission:

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

long long MOD = 1e9 + 7;
int MAX_INT = 1e9;

// 最大公約数
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// 最小公倍数
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  X = abs(X);

  long long num = abs(X) / D;

  if (K >= num) {
    K -= num;
    X -= num * D;
  }

  for (K; K > 0; --K) {
    if (X <= 0) {
      break;
    }
    X -= D;
  }

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

  return 0;
}
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 y = abs(x);
  long long a = 1;
  long long ans = 0;

  a = y / d;
  y -= min(a, k) * d;

  if (a >= k)
    ans = y;
  else {
    if ((k - a) % 2 == 0) {
      ans = y;
    } else {
      ans = abs(y - abs(d));
    }
  }

  cout << ans << 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 y = abs(x);
  long long a = 1;
  long long ans = 0;
  while (y > d && a <= k) {
    a += 1;
    y -= abs(d);
  }

  if (a == k + 1 && y > abs(x))
    ans = y;
  if (y <= abs(x)) {
    if ((k - a + 1) % 2 == 0) {
      ans = y;
    } else {
      ans = abs(y - abs(d));
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef __int128 LL;
inline __int128 read() {
  __int128 x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void print(__int128 x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  LL x, k, d;
  x = read(), k = read(), d = read();

  if (x < 0)
    x = -x;
  if (x >= 0) {
    if ((x - k * d) >= 0) {
      print(x - k * d);
    } else {
      int t = (x / d);
      if ((k - t) % 2)
        print(-(x - d * t - d));
      else
        print(x - d * t);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
typedef __int128 LL;
inline __int128 read() {
  __int128 x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void print(__int128 x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  LL x, k, d;
  while (x = read(), k = read(), d = read()) {
    if (x < 0)
      x = -x;
    if (x >= 0) {
      if ((x - k * d) >= 0) {
        print(x - k * d);
      } else {
        int t = (x / d);
        if ((k - t) % 2)
          print(-(x - d * t - d));
        else
          print(x - d * t);
      }
    }
  }

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

#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define PB push_back
#define MP make_pair
#define Mod 998244353
#define MOD 1000000007
using namespace std;
using lint = long long;
using vi = vector<lint>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<lint, lint>;

// cout << fixed << setprecision(12)

lint modpow(lint a, lint n, lint m = MOD) {
  lint ans = 1;
  while (n > 0) {
    if (n % 2 == 1)
      ans *= a;
    ans %= m;
    a *= a;
    a %= m;
    n /= 2;
  }
  return ans;
}

lint inv(lint a, lint m = MOD) { return modpow(a, m - 2, m); }

///////////////////////////////////

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

  if (1000000000000000 / d < k) {
    if (k % 2 == 0) {
      k = 10 + 1000000000000000 / d;
      if (k % 2 == 1)
        k++;
    } else {
      k = 10 + 1000000000000000 / d;
      if (k % 2 == 0)
        k++;
    }
  }

  if (x - d * k > 0) {
    x -= d * k;
    k = 0;
    // cout << x << endl;
  } else {
    lint tmp = x / d;
    x -= d * tmp;
    k -= tmp;
  }
  /*
  while(k>0 && x>=d){
    x -= d;
    k--;
  }
  */
  if (k % 2 == 0)
    cout << x << endl;
  else
    cout << d - x << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define PB push_back
#define MP make_pair
#define Mod 998244353
#define MOD 1000000007
using namespace std;
using lint = long long;
using vi = vector<lint>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<lint, lint>;

// cout << fixed << setprecision(12)

lint modpow(lint a, lint n, lint m = MOD) {
  lint ans = 1;
  while (n > 0) {
    if (n % 2 == 1)
      ans *= a;
    ans %= m;
    a *= a;
    a %= m;
    n /= 2;
  }
  return ans;
}

lint inv(lint a, lint m = MOD) { return modpow(a, m - 2, m); }

///////////////////////////////////

int main() {
  lint x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  while (k > 0 && x >= d) {
    x -= d;
    k--;
  }
  if (k % 2 == 0)
    cout << x << endl;
  else
    cout << d - x << endl;
}
Problem p02584 (C++) — buggy submission, verdict: Runtime Error

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

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


Error: 0

Fixed submission:

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

int main(void) {
  ll x, k, oldk, d;
  cin >> x >> k >> d;
  oldk = k;
  while (!(abs(x) / k >= d || abs(abs(x) / d - k) <= 1)) {
    k = x / d + 1;
    if (oldk % 2 != k % 2)
      k--;
  }
  cout << abs(abs(x) - k * d) << endl;
  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)
#define REPR(i, n, m) for (int i = (n); i >= (m); --i)
using namespace std;
using LL = long long;
using SLL = signed long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;

const int INF = numeric_limits<int>::max();
const unsigned long long int ULLINF =
    numeric_limits<unsigned long long int>::max();

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

  LL x, k, d;
  cin >> x >> k >> d;

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

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n, m) for (int i = (n); i >= (m); --i)
using namespace std;
using LL = long long;
using SLL = signed long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;

const int INF = numeric_limits<int>::max();
const unsigned long long int ULLINF =
    numeric_limits<unsigned long long int>::max();

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

  LL x, k, d;
  cin >> x >> k >> d;

  LL step = 0;

  if (k > abs(x / d)) {
    LL tmp = abs(x % d);
    step = abs(x / d);
    if (x < 0)
      tmp *= -1;
    x = tmp;
  } else {
    while ((abs(x - d) < abs(x) || abs(x + d) < abs(x)) && (step < k)) {
      if (abs(x - d) < abs(x)) {
        x = x - d;
      } else {
        x = x + d;
      }
      step++;
    }
  }

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

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

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N = 256;
int main() {
  ll a, b, c;
  scanf("%lld%lld%lld", &a, &b, &c);
  a = abs(a);
  if (c > a) {
    ll d = abs(a - c);
    ll f = abs(c - d);
    if (b % 2 == 1)
      printf("%lld\\n", d);
    else
      printf("%lld\\n", f);
  } else {
    ll d = a / c;
    if (d > b) {
      printf("%lld\\n", a - b * c);
      return 0;
    }
    ll e = a % c;
    ll f = abs(e - c);
    if (d % 2 == b % 2)
      printf("%lld\\n", e);
    else
      printf("%lld\\n", f);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N = 256;
int a[105];
int main() {
  ll a, b, c;
  scanf("%lld%lld%lld", &a, &b, &c);
  if (a >= b * c)
    printf("%lld", a - b * c);
  else {
    ll d = c / a;
    ll e = a % c;
    ll f = abs(e - c);
    if (d % 2 == b % 2)
      printf("%lld\\n", e);
    else
      printf("%lld\\n", f);
  }
  return 0;
}