prithivMLmods/Coder-Stat huggingface dataset:
Problem p02993 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pans cout << ans << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  string s;
  cin >> s;
  bool flag = false;
  for (int i = 0; i < 3; i++) {
    if (s[i] == s[i + 1]) {
      flag = true;
    }
  }
  int a = 0;
  for (int i = 0; i < 2500000000; i++) {
    a += 1;
  }
  if (flag) {
    cout << "Bad" << endl;
  } else {
    cout << "Good" << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pans cout << ans << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  string s;
  cin >> s;
  bool flag = false;
  for (int i = 0; i < 3; i++) {
    if (s[i] == s[i + 1]) {
      flag = true;
    }
  }
  int a = 0;
  for (int i = 0; i < 1500000000; i++) {
    a += 1;
  }
  if (flag) {
    cout << "Bad" << endl;
  } else {
    cout << "Good" << endl;
  }

  return 0;
}
Problem p02993 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s;
  cin >> s;
  int count = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == s.at(i + 1)) {
      count++;
    }
  }
  if (count > 0) {
    cout << "Bad" << endl;
  } else {
    cout << "Good" << endl;
  }
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s;
  cin >> s;
  int count = 0;
  for (int i = 0; i < s.size() - 1; i++) {
    if (s.at(i) == s.at(i + 1)) {
      count++;
    }
  }
  if (count > 0) {
    cout << "Bad" << endl;
  } else {
    cout << "Good" << endl;
  }
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>

int main() {
  // std::cout << std::fixed << std::setprecision(10);
  int n, l;
  std::cin >> n >> l;
  std::vector<int> a(n);
  int val = 1000, ans = 0, sum = 0;
  for (int i = 1; i < n + 1; i++) {
    a[i] = l + i - 1;
    std::cerr << a[i] << std::endl;
    if (std::abs(a[i]) < val) {
      ans = i;
      val = std::abs(a[i]);
    }
    sum += a[i];
  }
  std::cout << sum - a[ans] << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>

int main() {
  // std::cout << std::fixed << std::setprecision(10);
  int n, l;
  std::cin >> n >> l;
  std::vector<int> a(n);
  int val = 1000, ans = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    a[i] = l + i;
    std::cerr << a[i] << std::endl;
    if (std::abs(a[i]) < val) {
      ans = i;
      val = std::abs(a[i]);
    }
    sum += a[i];
  }
  std::cout << sum - a[ans] << std::endl;

  return 0;
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define ALL(a) (a).begin(), (a).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int MOD = 1000000007;
const double PI = acos(-1); // 3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n, l;
  cin >> n >> l;
  int tot = 0;
  int mini = MOD;
  rep(i, n) {
    int ap = l + i;
    tot += ap;
    ap = abs(ap - 0);
    mini = min(mini, ap);
  }
  cout << abs(abs(tot - 0) - abs(mini - 0)) * (tot / abs(tot - 0)) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define ALL(a) (a).begin(), (a).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int MOD = 1000000007;
const double PI = acos(-1); // 3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n, l;
  cin >> n >> l;
  int tot = 0;
  int mini = MOD;
  rep(i, n) {
    int ap = l + i;
    tot += ap;
    ap = abs(ap - 0);
    mini = min(mini, ap);
  }
  cout << abs(abs(tot - 0) - abs(mini - 0)) *
              (tot / (abs(tot - 0) == 0 ? 1 : abs(tot - 0)))
       << endl;
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
// #include "graph.cpp"
#define ll long long
#define loop(i, n) for (int i = 0; i < n; ++i)
#define rep(i, x, n) for (int i = x; i <= n; ++i)
#define iteloop(type, data, name, it)                                          \
  for (type<data>::iterator it = name.begin(); it != name.end(); ++it)
using namespace std;
using namespace __gnu_pbds;

int getSum(int st, int en) {
  int res = 0;
  for (int i = st; i <= en; i++)
    res += i;
  return res;
}

int main() {
  cin.sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ///  freopen("in.txt","rt",stdin);
  int n, l;
  cin >> n >> l;
  int st = l, en = l + n - 1;
  if (st / abs(st) != en / abs(en)) {
    cout << getSum(st, en) << endl;
    return 0;
  }
  if (st / abs(st) == 1) {
    cout << getSum(st, en) - st << endl;
  } else {
    cout << getSum(st, en) - en << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
// #include "graph.cpp"
#define ll long long
#define loop(i, n) for (int i = 0; i < n; ++i)
#define rep(i, x, n) for (int i = x; i <= n; ++i)
#define iteloop(type, data, name, it)                                          \
  for (type<data>::iterator it = name.begin(); it != name.end(); ++it)
using namespace std;
using namespace __gnu_pbds;

int getSum(int st, int en) {
  int res = 0;
  for (int i = st; i <= en; i++)
    res += i;
  return res;
}

int main() {
  cin.sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ///  freopen("in.txt","rt",stdin);
  int n, l;
  cin >> n >> l;
  int st = l, en = l + n - 1;
  if (st == 0 || en == 0) {
    cout << getSum(st, en) << endl;
    return 0;
  }
  if (st / abs(st) != en / abs(en)) {
    cout << getSum(st, en) << endl;
    return 0;
  }
  if (st / abs(st) == 1) {
    cout << getSum(st, en) - st << endl;
  } else {
    cout << getSum(st, en) - en << endl;
  }
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n, l;
  cin >> n >> l;
  int A[30];
  int shuma = 0;
  for (int i = 0; i < n; i++) {
    A[i] = l + i;
    shuma += A[i];
  }
  int max = abs(A[0]);
  for (int i = 1; i < n; i++) {
    if (abs(A[i]) < abs(max)) {
      max = A[i];
    }
  }
  cout << shuma - max;

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n, l;
  cin >> n >> l;
  int A[n];
  int shuma = 0;
  for (int i = 0; i < n; i++) {
    A[i] = l + i;
    shuma += A[i];
  }
  int max = abs(A[0]);
  for (int i = 1; i < n; i++) {
    if (abs(A[i]) < abs(max)) {
      max = A[i];
    }
  }
  cout << shuma - max;

  return 0;
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, l;
  cin >> n >> l;
  vector<int> vec(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += l + i;
    vec.at(i) = l + i;
  }
  int eat = 0;
  if (vec.at(0) > 0) {
    eat = vec.at(0);
  } else if (vec.at(n) < 0) {
    eat = vec.at(n);
  }
  cout << sum - eat << endl;
}

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

int main() {
  int n, l;
  cin >> n >> l;
  vector<int> vec(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum += l + i;
    vec.at(i) = l + i;
  }
  int eat = 0;
  if (vec.at(0) > 0) {
    eat = vec.at(0);
  } else if (vec.at(n - 1) < 0) {
    eat = vec.at(n - 1);
  }
  cout << sum - eat << endl;
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, L;
  vector<int> apple;
  vector<int> abs_apple;
  cin >> N >> L;
  for (int i = 0; i < N; i++) {
    apple.push_back(L + i);
    abs_apple.push_back(abs(L + i));
  }
  int x, y;
  for (int i = 0; i < N; i++) {
    if (abs_apple[i] < y) {
      y = abs_apple[i];
      x = i;
    }
  }

  apple.erase(apple.begin() + x);
  int total = 0;
  for (int i = 0; i < N - 1; i++) {
    total += apple[i];
  }
  cout << total << endl;
  return 0;
}

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, L;
  vector<int> apple;
  vector<int> abs_apple;
  cin >> N >> L;
  for (int i = 0; i < N; i++) {
    apple.push_back(L + i);
    abs_apple.push_back(abs(L + i));
  }
  int x = 200, y = 300;
  for (int i = 0; i < N; i++) {
    if (abs_apple[i] < y) {
      y = abs_apple[i];
      x = i;
    }
  }

  apple.erase(apple.begin() + x);
  int total = 0;
  for (int i = 0; i < N - 1; i++) {
    total += apple[i];
  }
  cout << total << endl;
  return 0;
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1e9 + 7
using namespace std;
typedef long long ll;

int main() {
  int N, L, sum = 0;
  cin >> N >> L;

  vector<int> r(N, 1000);
  for (int i = 1; i <= N; i++) {
    r[i] = L + i - 1;
    sum += r[i];
  }

  int mi = 100000;
  bool a = true;
  for (int i = 1; i <= N; i++) {
    mi = min(mi, abs(r[i]));
    if (r[i] < 0) {
      a = false;
    }
  }
  if (a)
    cout << sum - mi;
  else {
    cout << sum + mi;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1e9 + 7
using namespace std;
typedef long long ll;

int main() {
  int N, L, sum = 0;
  cin >> N >> L;

  vector<int> r(N + 1, 1000);
  for (int i = 1; i <= N; i++) {
    r[i] = L + i - 1;
    sum += r[i];
  }

  int mi = 100000;
  bool a = true;
  for (int i = 1; i <= N; i++) {
    mi = min(mi, abs(r[i]));
    if (r[i] < 0) {
      a = false;
    }
  }
  if (a)
    cout << sum - mi;
  else {
    cout << sum + mi;
  }
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, L;
  cin >> N >> L;

  vector<int> v(N);
  for (int i = 0; i <= N; i++) {
    v[i] = L + i;
  }

  int m = 100000;
  int t;

  for (size_t i = 0; i < N; i++) {
    if (m > abs(v[i])) {
      m = abs(v[i]);
      t = i;
    }
  }

  int sum = accumulate(v.begin(), v.end(), 0) - v[t];
  cout << sum << endl;

  return 0;
}

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

int main() {

  int N, L;
  cin >> N >> L;

  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    v[i] = L + i;
  }

  int m = 100000;
  int t;

  for (size_t i = 0; i < N; i++) {
    if (m > abs(v[i])) {
      m = abs(v[i]);
      t = i;
    }
  }

  int sum = accumulate(v.begin(), v.end(), 0) - v[t];
  cout << sum << endl;

  return 0;
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> v;
  bool positive = false, negative = false, zero = false;
  int n, l, j = 0, sum = 0;

  cin >> n >> l;
  for (int i = 1; i <= n; i++) {
    int k = l + i - 1;
    if (k > 0)
      positive = true;
    else if (k < 0)
      negative = true;
    else
      zero = true;
    sum += k;
    v[j++] = k;
  }
  if ((positive && negative) || zero)
    cout << sum << endl;
  else if (positive) {
    int min = 9999;
    for (int i = 0; i < n; i++) {
      if (v[i] < min)
        min = v[i];
    }
    cout << sum - min << endl;
  } else {
    int max = -9999;
    for (int i = 0; i < n; i++) {
      if (v[i] > max)
        max = v[i];
    }
    cout << sum - max << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> v;
  bool positive = false, negative = false, zero = false;
  int n, l, j = 0, sum = 0;

  cin >> n >> l;
  for (int i = 1; i <= n; i++) {
    int k = l + i - 1;
    if (k > 0)
      positive = true;
    else if (k < 0)
      negative = true;
    else
      zero = true;
    sum += k;
    v.push_back(k);
  }
  if ((positive && negative) || zero)
    cout << sum << endl;
  else if (positive) {
    int min = 9999;
    for (int i = 0; i < n; i++) {
      if (v[i] < min)
        min = v[i];
    }
    cout << sum - min << endl;
  } else {
    int max = -9999;
    for (int i = 0; i < n; i++) {
      if (v[i] > max)
        max = v[i];
    }
    cout << sum - max << endl;
  }

  return 0;
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <numeric>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)-1); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
using lint = long long;
using p = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int N, L;

int main() {
  cin >> N >> L;
  vector<int> taste(N);
  rep(i, N) taste[i] = L + i;

  int sum = 0;
  sum = accumulate(taste.begin(), taste.end(), sum);

  int m = 1e9;
  int sign;
  rep(i, N) {
    if (abs(taste[i]) < m) {
      m = abs(taste[i]);
      sign = taste[i] / abs(taste[i]);
    }
  }
  cout << sum - (sign * m) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <numeric>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)-1); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
using lint = long long;
using p = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int N, L;

int main() {
  cin >> N >> L;
  vector<int> taste(N);
  rep(i, N) taste[i] = L + i;

  int sum = 0;
  sum = accumulate(taste.begin(), taste.end(), sum);

  int m = 1e9;
  int sign;
  rep(i, N) {
    if (abs(taste[i]) < m) {
      m = abs(taste[i]);
      sign = taste[i] == 0 ? 0 : taste[i] / abs(taste[i]);
    }
  }
  cout << sum - (sign * m) << endl;
  return 0;
}
Problem p02994 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, L;
  cin >> N >> L;

  vector<int> a(N);

  for (int i = 0; i < N; i++) {
    a.at(i) = L + i;
  }

  int ans = 0;

  if (a.at(0) >= 0) {
    for (int i = 1; i < N; i++) {
      ans += a.at(i);
    }
  } else if (a.at(N) <= 0) {
    for (int i = 0; i < N - 1; i++) {
      ans += a.at(i);
    }
  } else {
    for (int i = 0; i < N; i++) {
      ans += a.at(i);
    }
  }

  cout << ans << endl;
}

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

int main() {
  int N, L;
  cin >> N >> L;

  vector<int> a(N);

  for (int i = 0; i < N; i++) {
    a.at(i) = L + i;
  }

  int ans = 0;

  if (a.at(0) >= 0) {
    for (int i = 1; i < N; i++) {
      ans += a.at(i);
    }
  } else if (a.at(N - 1) <= 0) {
    for (int i = 0; i < N - 1; i++) {
      ans += a.at(i);
    }
  } else {
    for (int i = 0; i < N; i++) {
      ans += a.at(i);
    }
  }

  cout << ans << endl;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPA(i, n) for (int i = 1; i < (n); ++i)
#define PII pair<int, int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define PDD pair<double, double>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(), (x).end()
#define ctoi(x) (x - 'a')
#define CTOI(x) (x - 'A')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a) {
  LL b = MOD, u = 1, v = 0;
  while (b) {
    LL t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= MOD;
  if (u < 0)
    u += MOD;
  return u;
}

LL exp(LL a, LL b) {
  LL res = 1;
  LL sum = a;
  while (b) {
    if (b & 1)
      res = (res * sum);
    sum = (sum * sum);
    b >>= 1;
  }
  return res;
}

double exp(double a, LL b) {
  double res = 1;
  double sum = a;
  while (b) {
    if (b & 1)
      res = (res * sum);
    sum = (sum * sum);
    b >>= 1;
  }
  return res;
}

LL LCM(LL a, LL b) {
  LL c = a;
  LL d = b;
  while (d) {
    c %= d;
    swap(c, d);
  }
  return a / c * b;
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  LL sum = 0;
  LL A, B, C, D;
  cin >> A >> B >> C >> D;
  sum += B / C;
  sum += B / D;
  sum -= B / LCM(C, D);
  sum -= (A - 1) / C;
  sum -= (A - 1) / D;
  sum += (A - 1) / LCM(C, D);
  cout << B - A + 1 - sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPA(i, n) for (int i = 1; i < (n); ++i)
#define PII pair<int, int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define PDD pair<double, double>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(), (x).end()
#define ctoi(x) (x - 'a')
#define CTOI(x) (x - 'A')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a) {
  LL b = MOD, u = 1, v = 0;
  while (b) {
    LL t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= MOD;
  if (u < 0)
    u += MOD;
  return u;
}

LL exp(LL a, LL b) {
  LL res = 1;
  LL sum = a;
  while (b) {
    if (b & 1)
      res = (res * sum);
    sum = (sum * sum);
    b >>= 1;
  }
  return res;
}

double exp(double a, LL b) {
  double res = 1;
  double sum = a;
  while (b) {
    if (b & 1)
      res = (res * sum);
    sum = (sum * sum);
    b >>= 1;
  }
  return res;
}

LL LCM(LL a, LL b) {
  LL c = a;
  LL d = b;
  while (d) {
    c %= d;
    swap(c, d);
  }
  return a / c * b;
}

signed main() {
  LL sum = 0;
  LL A, B, C, D;
  cin >> A >> B >> C >> D;
  sum += B / C;
  sum += B / D;
  sum -= B / LCM(C, D);
  sum -= (A - 1) / C;
  sum -= (A - 1) / D;
  sum += (A - 1) / LCM(C, D);
  cout << B - A + 1 - sum << endl;
  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  // ユークリッド互除法
  long long p = min(c, d);
  long long q = max(c, d);
  long long r = q % p;

  while (r != 0) {
    r = q % p;
    q = p;
    p = r;
  }
  //    long long lcm_cd = c * d / q; //cとdの最小公倍数
  long long lcm_cd = c * d / gcd(a, b); // cとdの最小公倍数

  long long num_m_c = b / c - (a - 1) / c;            // cの倍数の数
  long long num_m_d = b / d - (a - 1) / d;            // dの倍数の数
  long long num_m_cd = b / lcm_cd - (a - 1) / lcm_cd; // c,dの最小公倍数の数

  long long ans = b - (a - 1) - (num_m_c + num_m_d - num_m_cd);
  cout << ans << endl;
}

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

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  // ユークリッド互除法
  long long p = min(c, d);
  long long q = max(c, d);
  long long r = q % p;

  while (r != 0) {
    r = q % p;
    q = p;
    p = r;
  }
  //    long long lcm_cd = c * d / q; //cとdの最小公倍数
  long long lcm_cd = c * d / gcd(c, d); // cとdの最小公倍数

  long long num_m_c = b / c - (a - 1) / c;            // cの倍数の数
  long long num_m_d = b / d - (a - 1) / d;            // dの倍数の数
  long long num_m_cd = b / lcm_cd - (a - 1) / lcm_cd; // c,dの最小公倍数の数

  long long ans = b - (a - 1) - (num_m_c + num_m_d - num_m_cd);
  cout << ans << endl;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define MAX 100000000
using namespace std;
int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll lc = (c * d) / (__gcd(c, d)), ct1 = 0, ct2 = 0, ct3 = 0, res = b - a + 1;
  for (ll i = c; i <= b; i += c) {
    if (i >= a)
      ct1++;
  }
  for (ll i = d; i <= b; i += d) {
    if (i >= a)
      ct2++;
  }

  for (ll i = lc; i <= b; i += lc) {
    if (i >= a)
      ct3++;
  }
  res -= (ct1 + ct2 - ct3);
  cout << res;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define MAX 100000000
using namespace std;
int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll lc = (c * d) / (__gcd(c, d)), res = b - a + 1;
  a--;
  res -= (b / c - a / c + b / d - a / d - b / lc + a / lc);
  cout << res;

  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  long long int a, b, c, d, Cmax, Cmin, Dmax, Dmin;
  long long int ans;
  long long int Emax, Emin;
  cin >> a >> b >> c >> d;
  Cmax = b / c;
  Cmin = a / c;
  if (a % c == 0) {
    Cmin--;
  }
  Dmax = b / d;
  Dmin = a / d;
  if (a % d == 0) {
    Dmin--;
  }
  // 公約数 E
  long long int L = 0, g = 0, m, n;
  if (c > d) {
    m = c;
    n = d;
  } else {
    m = d;
    n = c;
  }

  while (m % n != 0) {
    g = m % n;
    m = n;
    n = g;
  }

  L = c * d / g;
  Emax = b / L;
  Emin = a / L;
  if (a % L == 0) {
    Emin--;
  }
  ans = (b - a + 1) - (Cmax - Cmin) - (Dmax - Dmin) + (Emax - Emin);

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

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long long int a, b, c, d, Cmax, Cmin, Dmax, Dmin;
  long long int ans;
  long long int Emax, Emin;
  cin >> a >> b >> c >> d;
  Cmax = b / c;
  Cmin = a / c;
  if (a % c == 0) {
    Cmin--;
  }
  Dmax = b / d;
  Dmin = a / d;
  if (a % d == 0) {
    Dmin--;
  }
  // 公約数 E
  long long int L = 0, g = c, m, n;
  if (c > d) {
    m = c;
    n = d;
  } else {
    m = d;
    n = c;
  }

  while (m % n != 0) {
    g = m % n;
    m = n;
    n = g;
  }

  L = c * d / g;
  Emax = b / L;
  Emin = a / L;
  if (a % L == 0) {
    Emin--;
  }
  ans = (b - a + 1) - (Cmax - Cmin) - (Dmax - Dmin) + (Emax - Emin);

  cout << ans << endl;
  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  long long lcm = (c * d) / __gcd(c, d);
  long long abc = (b / c - (a - 1) / c);
  long long abd = (b / d - (a - 1) / d);
  long long ablcm = (b / ablcm - (a - 1) / ablcm);
  long long cnt = b - a - abc - abd + ablcm;
  cout << cnt << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  long long lcm = (c * d) / __gcd(c, d);
  long long abc = (b / c - a / c);
  long long abd = (b / d - a / d);
  long long ablcm = (b / lcm - a / lcm);
  long long cnt = b - a - abc - abd + ablcm;
  cout << cnt << '\n';
  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long h = max(c, d);
  d = min(c, d);
  c = h;

  long long k = 1;
  while ((k * c) % d != 0) {
    k++;
  }
  k = k * c;
  long long sc = a / c;
  if (a % c) {
    sc++;
  }
  long long ec = b / c;

  long long sd = a / d;
  if (a % d) {
    sd++;
  }
  long long ed = b / d;

  long long sk = a / k;
  if (a % k) {
    sk++;
  }
  long long ek = b / k;

  long long tans = ec + ed + sk - ek - sc - sd + 1;
  long long ans = b - a + 1 - tans;
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long h = max(c, d);
  d = min(c, d);
  c = h;

  long long k = __gcd(c, d);
  k = d / k;
  k = k * c;
  long long sc = a / c;
  if (a % c) {
    sc++;
  }
  long long ec = b / c;

  long long sd = a / d;
  if (a % d) {
    sd++;
  }
  long long ed = b / d;

  long long sk = a / k;
  if (a % k) {
    sk++;
  }
  long long ek = b / k;

  long long tans = ec + ed + sk - ek - sc - sd + 1;
  long long ans = b - a + 1 - tans;
  cout << ans;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  // 最大公約数
  long long x, y, gcd = 1;
  x = c;
  y = d;
  long long sw;
  if (y < x) {
    sw = x;
    x = y;
    y = sw;
  }

  vector<long long> gc(1000, 0);

  // xの約数
  int count = 0;
  for (long long i = 1; i * i < x; i++) {
    if (x % i == 0) {
      count++;
      gc.at(count * 2) = i;
      gc.at(count * 2 + 1) = x / i;
    }
  }
  sort(gc.begin(), gc.end(), greater<long long>());

  for (long long i = 0; i < gc.size(); i++) {
    if (y % gc.at(i) == 0) {
      gcd = gc.at(i);
      break;
    }
  }

  long long v = (c * d) / gcd;

  // 個数判定
  long long da, db;
  da = (a - 1) - ((a - 1) / c + (a - 1) / d - (a - 1) / v); // aの個数
  db = b - (b / c + b / d - b / v);                         // bの個数
  long long ans;
  ans = db - da;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  // 最大公約数
  long long x, y, gcd = 1;
  x = c;
  y = d;
  long long sw;
  if (y < x) {
    sw = x;
    x = y;
    y = sw;
  }

  vector<long long> gc(1000, 1);

  // xの約数
  int count = 0;
  for (long long i = 1; i * i < x; i++) {
    if (x % i == 0) {
      count++;
      gc.at(count * 2) = i;
      gc.at(count * 2 + 1) = x / i;
    }
  }
  sort(gc.begin(), gc.end(), greater<long long>());

  for (long long i = 0; i < gc.size(); i++) {
    if (y % gc.at(i) == 0) {
      gcd = gc.at(i);
      break;
    }
  }

  long long v = (c * d) / gcd;

  // 個数判定
  long long da, db;
  da = (a - 1) - ((a - 1) / c + (a - 1) / d - (a - 1) / v); // aの個数
  db = b - (b / c + b / d - b / v);                         // bの個数
  long long ans;
  ans = db - da;
  cout << ans << endl;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define repeat(i, n) for (int(i) = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const double EPS = numeric_limits<double>::epsilon();

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

ll f(ll x, ll c, ll d) {
  ll result = x;
  result -= x / c;
  result -= x / d;
  result += x / lcm(c, d);
  return result;
}

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  ll result = f(B, C, D) - f(A - 1, C, D);

  cout << result << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define repeat(i, n) for (int(i) = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const double EPS = numeric_limits<double>::epsilon();

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

ll f(ll x, ll c, ll d) {
  ll result = x;
  result -= x / c;
  result -= x / d;
  result += x / lcm(c, d);
  return result;
}

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  ll result = f(B, C, D) - f(A - 1, C, D);

  cout << result << endl;
  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):

// C - Anti-Division

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const int MOD = 1e9 + 7;

ll get_gcd(ll x, ll y) {
  if (x < y) {
    swap(x, y);
  }

  ll m = x % y;

  while (m != 0) {
    x = y;
    y = m;
    m = x % y;
  }

  return y;
}

ll get_lcm(ll x, ll y) { return x * y / get_gcd(x, y); }

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;

  ll lcm_CD = get_lcm(C, D);

  ll div_C = B / C - (A - 1) / C;
  ll div_D = B / D - (A - 1) / D;
  ll div_CD = B / lcm_CD - (A - 1) / lcm_CD;

  ll ans = B - A + 1 - (div_C + div_D - div_CD);

  cout << ans << endl;

  return 0;
}

Fixed submission:

// C - Anti-Division

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const int MOD = 1e9 + 7;

ll get_gcd(ll x, ll y) {
  if (x < y) {
    swap(x, y);
  }

  ll m = x % y;

  while (m != 0) {
    x = y;
    y = m;
    m = x % y;
  }

  return y;
}

ll get_lcm(ll x, ll y) { return x * y / get_gcd(x, y); }

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;

  ll lcm_CD = get_lcm(C, D);

  ll div_C = B / C - (A - 1) / C;
  ll div_D = B / D - (A - 1) / D;
  ll div_CD = B / lcm_CD - (A - 1) / lcm_CD;

  ll ans = B - A + 1 - (div_C + div_D - div_CD);

  cout << ans << endl;

  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  long a, b, c, d, cd = 0, r[1000000000000]; // cdはcとdの最小公倍数
  cin >> a >> b >> c >> d;
  r[0] = max(c, d) % min(c, d);
  if (r[0] == 0)
    cd = c * d / min(c, d);
  else {
    r[1] = min(c, d) % r[0];
    for (int j = 0;; j++) {
      if (r[j + 1] == 0) {
        cd = c * d / r[j];
        break;
      } else {
        r[j + 2] = r[j] % r[j + 1];
        if (r[j + 2] == 0) {
          cd = c * d / r[j + 1];
          break;
        }
      }
    }
  }
  cout << (b - (b / c + b / d - b / cd)) -
              (a - 1 - ((a - 1) / c + (a - 1) / d - (a - 1) / cd));
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  long a, b, c, d, cd = 0, r[1000000]; // cdはcとdの最小公倍数
  cin >> a >> b >> c >> d;
  r[0] = max(c, d) % min(c, d);
  if (r[0] == 0)
    cd = c * d / min(c, d);
  else {
    r[1] = min(c, d) % r[0];
    for (int j = 0;; j++) {
      if (r[j + 1] == 0) {
        cd = c * d / r[j];
        break;
      } else {
        r[j + 2] = r[j] % r[j + 1];
        if (r[j + 2] == 0) {
          cd = c * d / r[j + 1];
          break;
        }
      }
    }
  }
  cout << (b - (b / c + b / d - b / cd)) -
              (a - 1 - ((a - 1) / c + (a - 1) / d - (a - 1) / cd));
  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define ull unsigned long long
using namespace std;
ull gcd(ull x, ull y) {
  ull r;
  do {
    r = x % y;
    x = y;
    y = r;
  } while (r != 0);
  return x;
}
ull hcd(ull x, int y, ull z) { return (x * y / z); }
int main() {
  ull a, b, c, d, i, c1 = 0, g, h;
  cin >> a >> b >> c >> d;
  c1 += b / c - a / c;
  if (a % c == 0)
    c1++;
  c1 += b / d - a / d;
  if (a % d == 0)
    c1++;
  g = gcd(c, d);
  h = hcd(c, d, g);
  for (i = 1;; i++) {
    if (h * i >= a) {
      if (h * i <= b) {
        c1--;
      } else {
        break;
      }
    }
  }
  cout << b - a - c1 + 1 << endl;
}

Fixed submission:
#include <iostream>
#define ull unsigned long long
using namespace std;
ull gcd(ull x, ull y) {
  ull r;
  do {
    r = x % y;
    x = y;
    y = r;
  } while (r != 0);
  return x;
}
ull hcd(ull x, int y, ull z) { return (x * y / z); }
int main() {
  ull a, b, c, d, i, c1 = 0, g, h;
  cin >> a >> b >> c >> d;
  c1 += b / c - a / c;
  if (a % c == 0)
    c1++;
  c1 += b / d - a / d;
  if (a % d == 0)
    c1++;
  g = gcd(c, d);
  h = hcd(c, d, g);
  ull l, r;
  l = a / h;
  r = b / h;
  c1 -= r - l;
  if (a % h == 0)
    c1--;
  cout << b - a - c1 + 1 << endl;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll findLCD(ll X, ll Y) {
  ll g;
  ll A = max(X, Y);
  ll B = min(X, Y);
  ll iniA = A;
  ll iniB = B;

  if (A == B)
    g = A;
  else {
    if (A % B == 0)
      g = B;
    while (true) {
      g = A % B;
      A = B;
      B = g;
      if (A % B == 0)
        break;
    }
  }
  ll a = iniA / g;
  ll b = iniB / g;
  ll ans = a * b * g;
  return ans;
}

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  ll whole = B - A + 1;
  ll ac = A / C;
  ll bc = B / C;
  ll canC = bc - ac;
  if (A % C == 0)
    canC++;

  ll ad = A / D;
  ll bd = B / D;
  ll canD = bd - ad;
  if (A % D == 0)
    canD++;

  ll LCD = findLCD(C, D);
  ll acd = A / LCD;
  ll bcd = B / LCD;
  ll canboth = bcd - acd;
  if (A % LCD == 0)
    canboth++;

  ll ans = whole - (canC + canD - canboth);
  // cout << canC << endl;
  // cout << canD << endl;
  // cout << canboth << endl;
  // cout << whole << endl;
  cout << ans << endl;
  return 0;
}

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

ll findLCD(ll X, ll Y) {
  ll g;
  ll A = max(X, Y);
  ll B = min(X, Y);
  ll iniA = A;
  ll iniB = B;

  if (A == B)
    g = A;
  else {
    if (A % B == 0)
      g = B;
    else {
      while (true) {
        g = A % B;
        A = B;
        B = g;
        if (A % B == 0)
          break;
      }
    }
  }
  ll a = iniA / g;
  ll b = iniB / g;
  ll ans = a * b * g;
  return ans;
}

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  ll whole = B - A + 1;
  ll ac = A / C;
  ll bc = B / C;
  ll canC = bc - ac;
  if (A % C == 0)
    canC++;

  ll ad = A / D;
  ll bd = B / D;
  ll canD = bd - ad;
  if (A % D == 0)
    canD++;

  ll LCD = findLCD(C, D);
  ll acd = A / LCD;
  ll bcd = B / LCD;
  ll canboth = bcd - acd;
  if (A % LCD == 0)
    canboth++;

  ll ans = whole - (canC + canD - canboth);
  // cout << canC << endl;
  // cout << canD << endl;
  // cout << canboth << endl;
  // cout << whole << endl;
  cout << ans << endl;
  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
ID: arunesh2
TASK: ride
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
  while (a != b) {
    if (a > b)
      a = a - b;
    else
      b = b - a;
  }
  return a;
}

// Function to return LCM of two numbers
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int main() {
  /*ofstream fout ("ride.out");
  ifstream fin ("ride.in");*/
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll c1 = a;
  ll c2 = b;
  while (a % c != 0)
    a++;
  while (b % c != 0)
    b--;
  ll n1 = (b - a) / c + 1;
  a = c1;
  b = c2;
  while (a % d != 0)
    a++;
  while (b % d != 0)
    b--;
  ll n2 = (b - a) / d + 1;
  ll prod = lcm(c, d);
  a = c1;
  b = c2;
  while (a % prod != 0)
    a++;
  while (b % prod != 0)
    b--;
  ll n3 = (b - a) / prod + 1;
  cout << ((c2 - c1 + 1) - (n1 + n2 - n3)) << "\n";
  return 0;
}

Fixed submission:
/*
ID: arunesh2
TASK: ride
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
  while (a != b) {
    if (a > b)
      a = a - b;
    else
      b = b - a;
  }
  return a;
}

// Function to return LCM of two numbers
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

int main() {
  /*ofstream fout ("ride.out");
  ifstream fin ("ride.in");*/
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll c1 = a;
  ll c2 = b;
  ll n1 = b / c;
  ll n2 = (a - 1) / c;
  ll n3 = b / d;
  ll n4 = (a - 1) / d;
  ll n5 = lcm(c, d);
  ll n6 = (b / n5) - ((a - 1) / n5);
  cout << ((b - a + 1) - ((n1 - n2) + (n3 - n4) - n6)) << "\n";
  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long count = 0;
  for (long long i = a; a <= i && i <= b; ++i) {
    if (i % c != 0 && i % d != 0) {
      ++count;
    }
  }
  cout << count << endl;
}

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

int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long ans = b - a + 1;
  ans -= ((b / c) - ((a - 1) / c));
  ans -= ((b / d) - ((a - 1) / d));
  long long p = (d * c) / __gcd(d, c);
  ans += ((b / p)) - ((a - 1) / (p));
  cout << ans << endl;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  ll e = a / __gcd(a, b) * b;
  ll ans = b - (b / c) - (b / d) + (b / e) - a + (a / c) + (a / d) - (a / e);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  ll e = c * d / __gcd(c, d);
  ll ans = b - (b / c) - (b / d) + (b / e) - a + (a / c) + (a / d) - (a / e);
  cout << ans << endl;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll gd(ll x, ll y) {
  if (x <= y)
    return gd(y, x);

  if (x % y == 0)
    return y;
  else
    return gd(min(x, y), max(x, y) % min(x, y));
}

ll lm(ll x, ll y) { return (x * y) / gd(x, y); }

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;

  cout << b - a + 1 - (b / c - (a - 1) / c) - (b / d - (a - 1) / d) +
              (b / lm(c, d) - (a - 1) / lm(c, d))
       << endl;
}

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

ll gd(ll x, ll y) {
  if (x < y)
    return gd(y, x);

  if (x % y == 0)
    return y;
  else
    return gd(min(x, y), max(x, y) % min(x, y));
}

ll lm(ll x, ll y) { return (x * y) / gd(x, y); }

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;

  cout << b - a + 1 - (b / c - (a - 1) / c) - (b / d - (a - 1) / d) +
              (b / lm(c, d) - (a - 1) / lm(c, d))
       << endl;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#include <map>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const double pi = acos(-1);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  if (c == 1 || d == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll num_of_C = 0, num_of_D = 0, num_of_lcm = 0;
  ll numC;
  if (a <= c)
    numC = c;
  else {
    numC = a / c * c;
    if (a % c != 0)
      numC += c;
  }
  for (ll i = numC; i <= b; i += c) {
    ++num_of_C;
  }
  ll numD;
  if (a <= d)
    numD = d;
  else {
    numD = a / d * d;
    if (a % d != 0)
      numD += d;
  }
  for (ll i = numD; i <= b; i += d) {
    ++num_of_D;
  }
  ll num = lcm(c, d);
  ll numlcm;
  if (a <= num)
    numlcm = num;
  else {
    numlcm = a / num * num;
    if (a % num != 0)
      numlcm += num;
  }
  for (ll i = numlcm; i <= b; i += num) {
    ++num_of_lcm;
  }
  cout << b - a + 1 - num_of_C - num_of_D + num_of_lcm << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#include <map>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const double pi = acos(-1);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  if (c == 1 || d == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll numC = b / c - a / c;
  if (a % c == 0)
    numC++;
  ll numD = b / d - a / d;
  if (a % d == 0)
    numD++;
  ll numlcm = b / lcm(c, d) - a / lcm(c, d);
  if (a % lcm(c, d) == 0)
    numlcm++;
  cout << b - a + 1 - numC - numD + numlcm << endl;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll a, b, c, d, r1, r2;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {

  cin >> a >> b >> c >> d;

  --a;

  r1 = b - b / c - b / d + b / lcm(c, d);
  r2 = a - a / c - a / a + a / lcm(c, d);

  cout << r1 - r2 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll a, b, c, d, r1, r2;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int main() {

  cin >> a >> b >> c >> d;

  --a;

  r1 = b - b / c - b / d + b / lcm(c, d);
  r2 = a - a / c - a / d + a / lcm(c, d);

  cout << r1 - r2 << endl;

  return 0;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
/* a と b の最大公約数を返す関数 */
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
long long f1(long long X1, long long C1, long long D1) {
  long long G1 = GCD(C1, D1);
  long long L1 = C1 / G1 * D1;
  return X1 - X1 / C1 - X1 / D1 + X1 / L1;
}
int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  cout << f1(B, C, D) - f1(A - 1, C, D) << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
/* a と b の最大公約数を返す関数 */
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
long long f1(long long X1, long long C1, long long D1) {
  long long G1 = GCD(C1, D1);
  long long L1 = C1 / G1 * D1;
  return X1 - X1 / C1 - X1 / D1 + X1 / L1;
}
int main() {
  long long A, B, C, D;
  cin >> A >> B >> C >> D;
  cout << f1(B, C, D) - f1(A - 1, C, D) << endl;
}
Problem p02995 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

ll gcd(ll c, ll d) { return c ? gcd(c, d % c) : c; }

ll lcm(ll c, ll d) { return (c / gcd(c, d) * d); }

ll f(ll x, ll c, ll d) {
  ll res = x;
  res -= x / c;
  res -= x / d;
  res += x / lcm(c, d);

  return res;
}

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll ans = 0;

  ans = f(b, c, d) - f(a - 1, c, d);

  cout << ans << endl;
}


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

ll gcd(ll c, ll d) { return d ? gcd(d, c % d) : c; }

ll lcm(ll c, ll d) { return (c / gcd(c, d) * d); }

ll f(ll x, ll c, ll d) {
  ll res = x;
  res -= x / c;
  res -= x / d;
  res += x / lcm(c, d);

  return res;
}

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll ans = 0;

  ans = f(b, c, d) - f(a - 1, c, d);

  cout << ans << endl;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

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

ll f(ll a, ll b, ll c) {

  ll ans = a;
  ans -= a / b;
  ans -= a / c;
  ans += a / lcm(b, c);

  return ans;
}

int main() {

  ll A, B, C, D;
  cin >> A, B, C, D;

  ll ans = f(B, C, D) - f(A - 1, C, D);

  cout << ans << endl;
}


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

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

ll f(ll a, ll b, ll c) {

  ll ans = a;
  ans -= a / b;
  ans -= a / c;
  ans += a / lcm(b, c);

  return ans;
}

int main() {

  ll A, B, C, D;

  cin >> A >> B >> C >> D;

  ll ans = f(B, C, D) - f(A - 1, C, D);

  cout << ans << endl;
}
Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int g = (c * d) / __gcd(c, d);
  // cout << g << endl;
  int factc = (b / c) - ((a - 1) / c);
  int factd = (b / d) - ((a - 1) / d);
  int factcom = ((b / (g)) - ((a - 1) / (g)));
  // cout << factc << factd << factcom << endl;
  // factc-=factcom;
  // factd-=factcom;
  cout << (b - a + 1) - (factc + factd - factcom);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
int32_t main() {

  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int g = (c * d) / __gcd(c, d);
  // cout << g << endl;
  int factc = (b / c) - ((a - 1) / c);
  int factd = (b / d) - ((a - 1) / d);
  int factcom = ((b / (g)) - ((a - 1) / (g)));
  // cout << factc << factd << factcom << endl;
  // factc-=factcom;
  // factd-=factcom;
  cout << (b - a + 1) - (factc + factd - factcom);
}