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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#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)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;

int main() {
  int n;
  cin >> n;
  int c[n - 1], s[n - 1], f[n - 1];
  rep(i, n - 1) { cin >> c[i] >> s[i] >> f[i]; }
  rep(i, n - 1) {
    int t = 0;
    int pos = i;
    while (pos < n) {
      while ((s[pos] > t || t % (f[pos]) != 0) && pos != n - 1) {
        t++;
      }
      if (pos < n - 1)
        t += c[pos];
      pos++;
    }
    cout << t << endl;
  }
  cout << 0 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#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)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;

int main() {
  int n;
  cin >> n;
  int c[n - 1], s[n - 1], f[n - 1];
  rep(i, n - 1) { cin >> c[i] >> s[i] >> f[i]; }
  rep(i, n - 1) {
    int t = 0;
    int pos = i;
    while (pos < n) {
      while (pos != n - 1 && (s[pos] > t || t % (f[pos]) != 0)) {
        t++;
      }
      if (pos < n - 1)
        t += c[pos];
      pos++;
    }
    cout << t << endl;
  }
  cout << 0 << endl;
  return 0;
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (ll i = 0; i < N; ++i)
const ll INF = 1LL << 60;

int main() {
  ll N;
  cin >> N;
  vector<ll> C(N), S(N), F(N), ans(N);
  ans[N] = 0;
  rep(i, N - 1) cin >> C[i] >> S[i] >> F[i];
  rep(i, N - 1) {
    ll arrival = C[i] + S[i];
    for (int j = i + 1; j < N - 1; j++) {
      ll departure = S[j];
      if (arrival > departure)
        arrival += (((arrival - departure - 1) / F[j] + 1) * F[j] -
                    (arrival - departure)) +
                   C[j];
      else
        arrival = departure + C[j];
    }
    ans[i] = arrival;
  }
  for (auto it : ans) {
    cout << it << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (ll i = 0; i < N; ++i)
const ll INF = 1LL << 60;

int main() {
  ll N;
  cin >> N;
  vector<ll> C(N), S(N), F(N), ans(N);
  rep(i, N - 1) cin >> C[i] >> S[i] >> F[i];
  rep(i, N - 1) {
    ll arrival = C[i] + S[i];
    for (int j = i + 1; j < N - 1; j++) {
      ll departure = S[j];
      if (arrival > departure)
        arrival += (((arrival - departure - 1) / F[j] + 1) * F[j] -
                    (arrival - departure)) +
                   C[j];
      else
        arrival = departure + C[j];
    }
    ans[i] = arrival;
  }
  for (auto it : ans) {
    cout << it << endl;
  }
}
Problem p03475 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long int C[1000], S[1000], F[1005];
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> C[i] >> S[i] >> F[i];
  }

  for (int i = 0; i < N - 1; i++) {

    long long int Y = 0;
    Y += S[i];
    Y += C[i];
    for (int k = i + 1; k < N - 1; k++) {

      if ((Y % F[k]) == 0) {

        if (Y >= S[k]) {
        exit:;
          Y += C[k];

        } else {
          Y = S[k];
          goto exit;
        }
      } else {

        Y += (Y % F[k]);
        k--;
      }
    }
    cout << Y << endl;
  }
  cout << 0 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long int C[1000], S[1000], F[1005];
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> C[i] >> S[i] >> F[i];
  }

  for (int i = 0; i < N - 1; i++) {

    long long int Y = 0;
    Y += S[i];
    Y += C[i];
    for (int k = i + 1; k < N - 1; k++) {

      if ((Y % F[k]) == 0) {

        if (Y >= S[k]) {
        exit:;
          Y += C[k];

        } else {
          Y = S[k];
          goto exit;
        }
      } else {

        Y += (F[k] - Y % F[k]);
        k--;
      }
    }
    cout << Y << endl;
  }
  cout << 0 << endl;
  return 0;
}
Problem p03475 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;
int main() {
  ll N;
  cin >> N;
  vector<ll> C(N), S(N), F(N);
  vector<vector<ll>> dist_time(N);
  ll sum_s = 0;
  for (int i = 0; i < N - 1; i++) {
    cin >> C[i] >> S[i] >> F[i];
    sum_s += S[i] + C[i];
  }
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < sum_s * 2; j += F[i]) {
      dist_time[i + 1].push_back(S[i] + C[i] + j);
    }
  }
  for (int i = 0; i < N - 1; i++) {
    ll t = S[i];
    for (int j = i + 1; j < N; j++) {
      for (int k = 0; k < dist_time[j].size(); k++) {
        if (t + C[j - 1] <= dist_time[j][k]) {
          t = dist_time[j][k];
          break;
        }
      }
    }
    cout << t << endl;
  }
  cout << 0 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;
int main() {
  ll N;
  cin >> N;
  vector<ll> C(N), S(N), F(N);
  vector<vector<ll>> dist_time(N);
  ll sum_s = 0;
  for (int i = 0; i < N - 1; i++) {
    cin >> C[i] >> S[i] >> F[i];
    sum_s += S[i] + C[i];
  }
  for (int i = 0; i < N - 1; i++) {
    ll t = S[i] + C[i];
    for (int j = i + 1; j < N - 1; j++) {
      ll start = (t % F[j] == 0) ? t : F[j] * (t / F[j] + 1);
      start = max(S[j], start);
      // cout << i + 1 << "," << j + 1 << "," << start << endl;
      t = start + C[j];
    }
    cout << t << endl;
  }
  cout << 0 << endl;
  return 0;
}
Problem p03475 (C++)

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

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF 1e9

int main(int argc, char const *argv[]) {
  ll n;
  cin >> n;
  vector<ll> c(n), s(n), f(n);
  REP(i, n) { cin >> c[i] >> s[i] >> f[n]; }
  REP(i, n) {
    ll t = 0;
    FOR(j, i, n - 1) {
      if (t <= s[j])
        t = s[j];
      else if (t % f[j])
        t = t + f[j] - t % f[j];
      t += c[j];
    }
    cout << t << endl;
  }
  return 0;
}


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

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF 1e9

int main(int argc, char const *argv[]) {
  ll n;
  cin >> n;
  vector<ll> c(n - 1), s(n - 1), f(n - 1);
  REP(i, n - 1) { cin >> c[i] >> s[i] >> f[i]; }
  REP(i, n) {
    ll t = 0;
    FOR(j, i, n - 1) {
      if (t <= s[j])
        t = s[j];
      else if (t % f[j])
        t = t + f[j] - t % f[j];
      t += c[j];
    }
    cout << t << endl;
  }
  return 0;
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
const int INF = 100000000, mod = 1000000007;
const ll LLINF = 1LL << 50;
using namespace std;

int main() {
  int n, c[500], s[500], f[500];
  cin >> n;
  for (int i = 0; i < n - 1; i++)
    c[i] >> s[i] >> f[i];
  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = i; j < n - 1; j++) {
      if (t < s[j])
        t = s[j];
      else if (t % f[j] == 0)
        ;
      else
        t = t + f[j] - t % f[j];
      t += c[j];
    }
    cout << t << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
const int INF = 100000000, mod = 1000000007;
const ll LLINF = 1LL << 50;
using namespace std;

int main() {
  int n, c[500], s[500], f[500];
  cin >> n;
  for (int i = 0; i < n - 1; i++)
    cin >> c[i] >> s[i] >> f[i];
  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = i; j < n - 1; j++) {
      if (t < s[j])
        t = s[j];
      else if (t % f[j] == 0)
        ;
      else
        t = t + f[j] - t % f[j];
      t += c[j];
    }
    cout << t << endl;
  }
  return 0;
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "rt", stdin);
  ll n;
  cin >> n;
  ll C[501];
  ll S[501];
  ll F[501];
  for (int i = 1; i <= n - 1; i++)
    cin >> C[i] >> S[i] >> F[i];

  for (int i = 1; i <= n; i++) {
    ll x = 0;
    for (int j = i; j <= n - 1; j++) {
      if (x <= S[j])
        x = S[j];
      else if (x % F[j] != 0)
        x = (x / F[j + 1]) * F[j];
      // cout <<"x: " << x << endl;
      x += C[j];
      // cout << "x+c[j] " << x << endl;
    }
    cout << x << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "rt", stdin);
  ll n;
  cin >> n;
  ll C[501];
  ll S[501];
  ll F[501];
  for (int i = 1; i <= n - 1; i++)
    cin >> C[i] >> S[i] >> F[i];

  for (int i = 1; i <= n; i++) {
    ll x = 0;
    for (int j = i; j <= n - 1; j++) {
      if (x <= S[j])
        x = S[j];
      else if (x % F[j] != 0)
        x = (x / F[j] + 1) * F[j];
      // cout <<"x: " << x << endl;
      x += C[j];
      // cout << "x+c[j] " << x << endl;
    }
    cout << x << endl;
  }

  return 0;
}
Problem p03475 (C++)

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

int main() {
  int n, c[100], s[100], f[100], ans[100], time;

  cin >> n;
  for (int i = 1; i < n; i++)
    cin >> c[i] >> s[i] >> f[i];

  time = 0;
  for (int i = 1; i < n; i++) { // スタート位置;
    time = 0;
    for (int j = i; j < n; j++) {
      if (time <= s[j])
        time = s[j] + c[j];
      else {
        if (time % f[j])
          time += f[j] - (time % f[j]) + c[j];
        else
          time += c[j];
      }
    }
    ans[i] = time;
  }
  for (int i = 1; i < n; i++)
    cout << ans[i] << endl;
  cout << "0" << endl;
}

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

int main() {
  int n, c[500], s[500], f[500], ans[500], time;

  cin >> n;
  for (int i = 1; i < n; i++)
    cin >> c[i] >> s[i] >> f[i];

  time = 0;
  for (int i = 1; i < n; i++) { // スタート位置;
    time = 0;
    for (int j = i; j < n; j++) {
      if (time <= s[j])
        time = s[j] + c[j];
      else {
        if (time % f[j])
          time += f[j] - (time % f[j]) + c[j];
        else
          time += c[j];
      }
    }
    ans[i] = time;
  }
  for (int i = 1; i < n; i++)
    cout << ans[i] << endl;
  cout << "0" << endl;
}
Problem p03475 (C++)

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

int main() {
  lld t;
  cin >> t;
  while (t--) {
    lld n;
    cin >> n;
    vector<pair<lld, pair<lld, lld>>> v;
    v.resize(n + 2);
    for (lld i = 1; i <= n - 1; i++) {
      cin >> v[i].first >> v[i].second.first >> v[i].second.second;
    }
    vector<lld> ans(n);
    for (lld i = 1; i <= n - 1; i++) {
      lld tm = v[i].first + v[i].second.first;
      lld j = i + 1;
      while (j <= n - 1) {
        if (tm < v[j].second.first) {
          tm += (v[j].second.first - tm);
        } else {
          lld rem = tm % (v[j].second.second);
          if (rem != 0) {
            tm += (v[j].second.second - rem);
          }
        }

        tm += v[j].first;
        j++;
      }
      cout << tm << "\n";
    }
    cout << 0 << "\n";
  }
}

Fixed submission:
// AtCoder ABC084 Contest
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;

int main() {
  lld t;
  t = 1;
  while (t--) {
    lld n;
    cin >> n;
    vector<pair<lld, pair<lld, lld>>> v;
    v.resize(n + 2);
    for (lld i = 1; i <= n - 1; i++) {
      cin >> v[i].first >> v[i].second.first >> v[i].second.second;
    }
    vector<lld> ans(n);
    for (lld i = 1; i <= n - 1; i++) {
      lld tm = v[i].first + v[i].second.first;
      lld j = i + 1;
      while (j <= n - 1) {
        if (tm < v[j].second.first) {
          tm += (v[j].second.first - tm);
        } else {
          lld rem = tm % (v[j].second.second);
          if (rem != 0) {
            tm += (v[j].second.second - rem);
          }
        }

        tm += v[j].first;
        j++;
      }
      cout << tm << "\n";
    }
    cout << 0 << "\n";
  }
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> c(n - 1), s(n - 1), f(n - 1);
  for (int i = 0; i < n - 1; i++)
    cin >> c[i] >> s[i] >> f[i];
  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = i; j < n; j++) {
      t += (t < s[j] ? s[i] - t : 0);
      t += (f[i] - t % f[i]) % f[i];
      t += c[i];
    }
    cout << t << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> c(n - 1), s(n - 1), f(n - 1);
  for (int i = 0; i < n - 1; i++)
    cin >> c[i] >> s[i] >> f[i];
  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = i; j < n - 1; j++) {
      t += (t < s[j] ? s[j] - t : 0);
      t += (f[j] - t % f[j]) % f[j];
      t += c[j];
    }
    cout << t << endl;
  }
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> C(N);
  vector<int> S(N);
  vector<int> F(N);
  for (int i = 0; i < N - 1; i++)
    cin >> C.at(i) >> S.at(i) >> F.at(i);

  for (int i = 0; i < N; i++) {
    int ans = S.at(i);
    for (int j = i; j < N; j++) {
      if (ans < S.at(j))
        ans = S.at(j);
      else if (ans % F.at(j) == 0)
        ;
      else
        ans = ans + F.at(j) - ans % F.at(j);
      ans += C.at(j);
    }
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> C(N);
  vector<int> S(N);
  vector<int> F(N);
  for (int i = 0; i < N - 1; i++)
    cin >> C.at(i) >> S.at(i) >> F.at(i);

  for (int i = 0; i < N; i++) {
    int ans = S.at(i);
    for (int j = i; j < N - 1; j++) {
      if (ans < S.at(j))
        ans = S.at(j);
      else if (ans % F.at(j) == 0)
        ;
      else
        ans = ans + F.at(j) - ans % F.at(j);
      ans += C.at(j);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;

#define INF 100000000;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
int main() {
  int n;
  cin >> n;
  int c[500], s[500], f[500];
  rep(i, n) cin >> c[i] >> s[i] >> f[i];

  rep(i, n) {
    int thisans = 0;
    rep(j, n) {
      if (thisans < s[j])
        thisans = s[j];
      else if (thisans % f[j] == 0)
        ;
      else
        thisans += f[j] - thisans % f[j];
      thisans += c[j];
    }
    cout << thisans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;

#define INF 100000000;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
int main() {
  int n;
  cin >> n;
  int c[500], s[500], f[500];
  rep(i, n) cin >> c[i] >> s[i] >> f[i];

  rep(i, n) {
    int thisans = 0;
    for (int j = i; j < n - 1; j++) {
      if (thisans < s[j])
        thisans = s[j];
      else if (thisans % f[j] == 0)
        ;
      else
        thisans += f[j] - thisans % f[j];
      thisans += c[j];
    }
    cout << thisans << endl;
  }
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

static const int tMax = 510;

int C[tMax], F[tMax];
long int S[tMax];
int N;

long int TimeUntilDestination(int station_id, long int time) {
  if (station_id == N)
    return time;
  if (time <= S[station_id])
    return TimeUntilDestination(station_id + 1, S[station_id] + C[station_id]);
  return time % F[station_id]
             ? TimeUntilDestination(station_id + 1,
                                    F[station_id] * (time / F[station_id + 1]) +
                                        C[station_id])
             : TimeUntilDestination(station_id + 1, time + C[station_id]);
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i < N; ++i)
    scanf("%d %ld %d", &C[i], &S[i], &F[i]);
  for (int i = 1; i <= N; ++i)
    printf("%ld\n", TimeUntilDestination(i, 0));
  return 0;
}

Fixed submission:
#include <cstdio>

static const int tMax = 510;

int C[tMax], F[tMax];
long int S[tMax];
int N;

long int TimeUntilDestination(int station_id, long int time) {
  if (station_id == N)
    return time;
  if (time <= S[station_id])
    return TimeUntilDestination(station_id + 1, S[station_id] + C[station_id]);
  return time % F[station_id]
             ? TimeUntilDestination(station_id + 1,
                                    F[station_id] * (time / F[station_id] + 1) +
                                        C[station_id])
             : TimeUntilDestination(station_id + 1, time + C[station_id]);
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i < N; ++i)
    scanf("%d %ld %d", &C[i], &S[i], &F[i]);
  for (int i = 1; i <= N; ++i)
    printf("%ld\n", TimeUntilDestination(i, 0));
  return 0;
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
signed main() {
  ll N, i, j;
  cin >> N;
  ll C[N - 1], S[N - 1], F[N - 1];
  REP(i, N - 1) cin >> C[i] >> S[i] >> F[i];
  REP(i, N) {
    ll T = 0;
    FOR(j, i, N) T = (max(T, S[j]) + F[j] - 1) / F[j] + C[j];
    cout << T << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
signed main() {
  ll N, i, j;
  cin >> N;
  ll C[N - 1], S[N - 1], F[N - 1];
  REP(i, N - 1) cin >> C[i] >> S[i] >> F[i];
  REP(i, N) {
    ll T = 0;
    FOR(j, i, N - 1) T = (max(T, S[j]) + F[j] - 1) / F[j] * F[j] + C[j];
    cout << T << endl;
  }
  return 0;
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;

using namespace std;

int main() {
  int n;
  int c[100];
  int s[100];
  int f[100];
  int time = 0;

  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> c[i] >> s[i] >> f[i];
  }

  for (int i = 0; i < n - 1; i++) {
    time = s[i] + c[i];
    for (int j = i + 1; j < n - 1; j++) {
      if (time < s[j]) {
        time = s[j];
      } else if (time % f[j] != 0) {
        time = f[j] * (time / f[j] + 1);
      }
      time += c[j];
    }
    cout << time << endl;
  }
  cout << 0 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;

using namespace std;

int main() {
  int n;
  int c[500];
  int s[500];
  int f[500];
  int time = 0;

  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> c[i] >> s[i] >> f[i];
  }

  for (int i = 0; i < n - 1; i++) {
    time = s[i] + c[i];
    for (int j = i + 1; j < n - 1; j++) {
      if (time < s[j]) {
        time = s[j];
      } else if (time % f[j] != 0) {
        time = f[j] * (time / f[j] + 1);
      }
      time += c[j];
    }
    cout << time << endl;
  }
  cout << 0 << endl;
  return 0;
}
Problem p03475 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> c(n, 0), s(n, 0), f(n, 0);
  for (int i = 0; i < n - 1; i++) {
    cin >> c.at(i) >> s.at(i) >> f.at(i);
  }
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    cnt = s.at(i) + c.at(i);
    for (int j = i + 1; j < n; j++) {
      if (s.at(j) >= cnt) {
        cnt = s.at(j) + c.at(j);
      } else {
        int z = (cnt - s.at(j)) / f.at(j);
        int zz = cnt % f.at(j), zzz;
        if (zz == 0) {
          cnt += c.at(j);
        } else {
          cnt = c.at(j) + s.at(j) + f.at(j) * (z + 1);
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> c(n, 0), s(n, 0), f(n, 0);
  for (int i = 0; i < n - 1; i++) {
    cin >> c.at(i) >> s.at(i) >> f.at(i);
  }
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    cnt = s.at(i) + c.at(i);
    for (int j = i + 1; j < n - 1; j++) {
      if (s.at(j) >= cnt) {
        cnt = s.at(j) + c.at(j);
      } else {
        int z = (cnt - s.at(j)) / f.at(j);
        int zz = cnt % f.at(j), zzz;
        if (zz == 0) {
          cnt += c.at(j);
        } else {
          cnt = c.at(j) + s.at(j) + f.at(j) * (z + 1);
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p03475 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define N 200100
int c[110], s[110], f[110], out[110];
int dp[2][N + 10];
int main() {
  int n;
  cin >> n;
  rep(i, n - 1) cin >> c[i] >> s[i] >> f[i];
  for (int i = n - 2; i >= 0; i--) {
    int now = i % 2, ne = 1 - now;
    dp[now][N] = 0;
    for (int j = N - 1; j >= 0; j--) {
      dp[now][j] = dp[now][j + 1] + 1;
      if (j >= s[i] && j % f[i] == 0)
        dp[now][j] = min(dp[now][j], dp[ne][j + c[i]] + c[i]);
    }
    out[i] = dp[now][0];
  }
  rep(i, n - 1) cout << out[i] << endl;
  cout << 0 << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define N 200100
int c[510], s[510], f[510], out[510];
int dp[2][N + 10];
int main() {
  int n;
  cin >> n;
  rep(i, n - 1) cin >> c[i] >> s[i] >> f[i];
  for (int i = n - 2; i >= 0; i--) {
    int now = i % 2, ne = 1 - now;
    dp[now][N] = 0;
    for (int j = N - 1; j >= 0; j--) {
      dp[now][j] = dp[now][j + 1] + 1;
      if (j >= s[i] && j % f[i] == 0)
        dp[now][j] = min(dp[now][j], dp[ne][j + c[i]] + c[i]);
    }
    out[i] = dp[now][0];
  }
  rep(i, n - 1) cout << out[i] << endl;
  cout << 0 << endl;
}
Problem p03476 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<int> vc;
const int N = 10e5 + 2;
bool p[N];
void prime() {

  p[0] = false;
  p[1] = false;
  memset(p, true, sizeof(p));
  for (int i = 2; i * i <= N; i++) {
    if (p[i] == true) {
      for (int j = i * i; j <= N; j += i) {
        p[j] = false;
      }
    }
  }
  for (int i = 2; i <= N; i++) {

    if (p[i] == true) {

      vc.push_back(i);
    }
  }
}

int main() {

  prime();
  // for(auto x: vc)cout<<x;

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {

    int x, y;
    int counter = 0;
    cin >> x >> y;
    for (int j = 0; j < vc.size(); j++) {

      if (vc[j] % 2 != 0 && vc[j] >= x && vc[j] <= y &&
          p[(vc[j] + 1) / 2] == true) {
        counter++;
      }
    }
    cout << counter << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<int> vc;
const int N = 1e5 + 2;
bool p[N];
void prime() {

  p[0] = false;
  p[1] = false;
  memset(p, true, sizeof(p));
  for (int i = 2; i * i <= N; i++) {
    if (p[i] == true) {
      for (int j = i * i; j <= N; j += i) {
        p[j] = false;
      }
    }
  }
  for (int i = 2; i <= N; i++) {

    if (p[i] == true) {

      vc.push_back(i);
    }
  }
}

int main() {

  prime();
  // for(auto x: vc)cout<<x;

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {

    int x, y;
    int counter = 0;
    cin >> x >> y;
    for (int j = 0; j < vc.size(); j++) {

      if (vc[j] % 2 != 0 && vc[j] >= x && vc[j] <= y &&
          p[(vc[j] + 1) / 2] == true) {
        counter++;
      }
    }
    cout << counter << endl;
  }
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define so(v) sort(v.begin(), v.end())
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
const int MAX = 510000;
const int MOD = 1000000007;
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
// 二項係数計算
long long com(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  ll MAX = 11111;
  vector<int> s(MAX, 1);
  s[0] = 0;
  s[1] = 0;
  for (int i = 2; i < MAX; i++) {
    if (s[i] != 1)
      continue;
    for (int j = i * 2; j < MAX; j += i)
      s[j] = 0;
  }
  vector<ll> sum(MAX, 0);
  sum[1] = 0;
  for (int i = 3; i < MAX; i += 2) {
    if (s[i] == 1 && s[(i + 1) / 2] == 1)
      sum[i] = sum[i - 2] + 1;
    else
      sum[i] = sum[i - 2];
  }
  int q;
  cin >> q;
  int a[q];
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    a[i] = sum[r] - sum[max(0, l - 2)];
  }
  rep(i, q) cout << a[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define so(v) sort(v.begin(), v.end())
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
const int MAX = 510000;
const int MOD = 1000000007;
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
// 二項係数計算
long long com(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  ll MAX = 111111;
  vector<int> s(MAX, 1);
  s[0] = 0;
  s[1] = 0;
  for (int i = 2; i < MAX; i++) {
    if (s[i] != 1)
      continue;
    for (int j = i * 2; j < MAX; j += i)
      s[j] = 0;
  }
  vector<ll> sum(MAX, 0);
  sum[1] = 0;
  for (int i = 3; i < MAX; i += 2) {
    if (s[i] == 1 && s[(i + 1) / 2] == 1)
      sum[i] = sum[i - 2] + 1;
    else
      sum[i] = sum[i - 2];
  }
  int q;
  cin >> q;
  int a[q];
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    a[i] = sum[r] - sum[max(0, l - 2)];
  }
  rep(i, q) cout << a[i] << endl;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;
#define MAX 10000

bool isprime[MAX + 1];

int eratos(int n) {
  for (int i = 0; i <= n; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
}

int main() {
  int q;
  cin >> q;
  int l[q], r[q];
  REP(i, q) {
    cin >> l[i] >> r[i];
    r[i]++;
  }
  int sum = 0, s[MAX + 1];
  eratos(MAX);
  for (int n = 1; n <= MAX; n++) {
    s[n] = sum;
    if (n % 2 == 1 && isprime[n] && isprime[(n + 1) / 2])
      sum++;
  }
  REP(i, q) cout << s[r[i]] - s[l[i]] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;
#define MAX 100000

bool isprime[MAX + 1];

int eratos(int n) {
  for (int i = 0; i <= n; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
}

int main() {
  int q;
  cin >> q;
  int l[q], r[q];
  REP(i, q) {
    cin >> l[i] >> r[i];
    r[i]++;
  }
  int sum = 0, s[MAX + 1];
  eratos(MAX);
  for (int n = 1; n <= MAX; n++) {
    s[n] = sum;
    if (n % 2 == 1 && isprime[n] && isprime[(n + 1) / 2])
      sum++;
  }
  REP(i, q) cout << s[r[i]] - s[l[i]] << endl;
}
Problem p03476 (C++)

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

using namespace std;

const int N_MAX = 10007;
int main() {
  vector<bool> is_prime(N_MAX, true);
  vector<int> cumsum(N_MAX, 0);
  int Q;
  cin >> Q;

  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < N_MAX; i++) {
    if (is_prime[i]) {
      for (int k = 2; k * i < N_MAX; k++) {
        is_prime[k * i] = false;
      }
    }

    cumsum[i] = cumsum[i - 1];
    if (i % 2 && is_prime[i] && is_prime[(i + 1) / 2]) {
      cumsum[i]++;
    }
  }

  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    cout << cumsum[r] - cumsum[l - 1] << endl;
  }

  return 0;
}

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

using namespace std;

const int N_MAX = 100007;
int main() {
  vector<bool> is_prime(N_MAX, true);
  vector<int> cumsum(N_MAX, 0);
  int Q;
  cin >> Q;

  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < N_MAX; i++) {
    if (is_prime[i]) {
      for (int k = 2; k * i < N_MAX; k++) {
        is_prime[k * i] = false;
      }
    }

    cumsum[i] = cumsum[i - 1];
    if (i % 2 && is_prime[i] && is_prime[(i + 1) / 2]) {
      cumsum[i]++;
    }
  }

  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    cout << cumsum[r] - cumsum[l - 1] << endl;
  }

  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool prime[316];
ll N = 100000;
void seive() {
  int sqrtn, i, j;
  prime[0] = 1, prime[1] = 1, prime[2] = 0;
  for (i = 4; i <= N; i++) {
    prime[i] = 0;
    if (i % 2 == 0)
      prime[i] = 1;
  }
  sqrtn = ((int)sqrt((double)N));
  for (i = 3; i <= sqrtn; i += 2) {
    if (prime[i] == 0) {
      for (j = i * i; j <= N; j += i + i) {
        prime[j] = 1;
      }
    }
  }
}
int main() {
  seive();
  ll q;
  ll a, b;
  ll sum = 0;
  ll ans[100045];
  for (int i = 0; i < 100005; i++) {
    a = (i + 1) / 2;
    if (prime[i] == 0 && prime[a] == 0) {
      ans[i] = ans[i - 1] + 1;
    } else {
      ans[i] = ans[i - 1];
    }
  }
  /* for(int i=0;i<10;i++)
   {
       cout<<ans[i]<<endl;
   }*/
  scanf("%lld", &q);
  for (int i = 0; i < q; i++) {
    ll cnt = 0;
    scanf("%lld %lld", &a, &b);
    cnt = ans[b] - ans[a - 1];
    printf("%lld\n", cnt);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool prime[100000];
ll N = 100000;
void seive() {
  int sqrtn, i, j;
  prime[0] = 1, prime[1] = 1, prime[2] = 0;
  for (i = 4; i <= N; i++) {
    prime[i] = 0;
    if (i % 2 == 0)
      prime[i] = 1;
  }
  sqrtn = ((int)sqrt((double)N));
  for (i = 3; i <= sqrtn; i += 2) {
    if (prime[i] == 0) {
      for (j = i * i; j <= N; j += i + i) {
        prime[j] = 1;
      }
    }
  }
}
int main() {
  seive();
  ll q;
  ll a, b;
  ll sum = 0;
  ll ans[100045];
  for (int i = 0; i < 100005; i++) {
    a = (i + 1) / 2;
    if (prime[i] == 0 && prime[a] == 0) {
      ans[i] = ans[i - 1] + 1;
    } else {
      ans[i] = ans[i - 1];
    }
  }
  /* for(int i=0;i<10;i++)
   {
       cout<<ans[i]<<endl;
   }*/
  scanf("%lld", &q);
  for (int i = 0; i < q; i++) {
    ll cnt = 0;
    scanf("%lld %lld", &a, &b);
    cnt = ans[b] - ans[a - 1];
    printf("%lld\n", cnt);
  }
}
Problem p03476 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define en '\n'
#define pb push_back
#define P pair<int, int>

typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = (int)1e9;
const ll inf = (ll)1e18;
const int mod = 1000000007;

const double pi = 3.1415926535;

bool is_prime[100001] = {};
bool num[100001];
int cnt[100001];

void eratos() {
  is_prime[0] = is_prime[1] = false;
  rep(i, 100001) {
    is_prime[i] = true;
    cnt[i] = 0;
  }

  for (int i = 2; i <= 100000; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j <= 100000; j += i) {
        is_prime[j] = false;
      }
    }
  }

  return;
}

int main() {
  int q;
  cin >> q;

  eratos();

  for (int i = 3; i <= 100000; i += 2) {
    if (is_prime[i] && is_prime[(i + 1) / 2]) {
      cnt[i]++;
    }
  }

  for (int i = 1; i <= 100000; i++) {
    cnt[i] += cnt[i - 1];
  }

  int l, r;
  rep(i, q) {
    cin >> l >> r;
    cout << cnt[r] - cnt[l - 1] << en;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define en '\n'
#define pb push_back
#define P pair<int, int>

typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = (int)1e9;
const ll inf = (ll)1e18;
const int mod = 1000000007;

const double pi = 3.1415926535;

bool is_prime[100001] = {};
bool num[100001];
int cnt[100001];

void eratos() {
  is_prime[0] = is_prime[1] = false;
  rep(i, 100001) {
    is_prime[i] = true;
    cnt[i] = 0;
  }

  for (int i = 2; i * i <= 100000; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j <= 100000; j += i) {
        is_prime[j] = false;
      }
    }
  }

  return;
}

int main() {
  int q;
  cin >> q;

  eratos();

  for (int i = 3; i <= 100000; i += 2) {
    if (is_prime[i] && is_prime[(i + 1) / 2]) {
      cnt[i]++;
    }
  }

  for (int i = 1; i <= 100000; i++) {
    cnt[i] += cnt[i - 1];
  }

  int l, r;
  rep(i, q) {
    cin >> l >> r;
    cout << cnt[r] - cnt[l - 1] << en;
  }

  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
const int M = 100010;
const int MOD = 1000000007;
const int INF = 1000000007;
using ll = long long;

int main() {
  int sosu[M] = {}, by2[M] = {};
  int Q, l[M], r[M], ans = 0, cnt = 0;
  sosu[0] = 2;
  rep(i, 3, 100001) {
    for (int j = 0; sosu[j] != 0; j++) {
      if (i % sosu[j] == 0)
        break;
      if (sosu[j + 1] == 0)
        sosu[j + 1] = i, cnt = j + 2;
    }
  }
  int z = 0;
  rep(i, 0, cnt) {
    rep(j, 0, cnt) {
      if ((sosu[i] + 1) / 2 == sosu[j]) {
        by2[z] = sosu[j], z++;
        break;
      }
    }
  }
  cin >> Q;
  rep(i, 0, Q) {
    ans = 0;
    cin >> l[i] >> r[i];
    for (int k = l[i]; k <= r[i]; k += 2) {
      rep(i, 0, cnt) {
        rep(j, 0, z) {
          if (k == sosu[i] && (k + 1) / 2 == by2[j]) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
const int M = 100010;
const int MOD = 1000000007;
const int INF = 1000000007;
using ll = long long;

int main() {
  bool f[100001] = {};
  int c[100002] = {};
  int N, L, R;
  rep(i, 2, 100001) if (!f[i]) for (int j = i + i; j <= 100000; j += i) f[j] =
      true;
  for (int i = 3; i <= 100000; i += 2)
    if (!f[i] && !f[(i + 1) / 2])
      c[i]++;
  rep(i, 3, 100001) c[i] += c[i - 1];
  cin >> N;
  while (N--) {
    cin >> L >> R;
    cout << c[R] - c[L - 1] << endl;
  }
}
Problem p03476 (C++)

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

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

using namespace std;

const int MAX_Q = 10005;

int Q;
int L[MAX_Q], R[MAX_Q];

bool prime[MAX_Q];
int imos[MAX_Q];

void initPrime() {
  for (int i = 0; i < MAX_Q; i++) {
    prime[i] = true;
  }

  prime[0] = prime[1] = false;
  for (int i = 2; i <= sqrt(MAX_Q); i++) {
    if (prime[i]) {
      for (int j = 2; i * j < MAX_Q; j++) {
        prime[i * j] = false;
      }
    }
  }

  for (int i = 0; i < MAX_Q; i++) {
    if (i % 2 == 1 && prime[i] && prime[(i + 1) / 2]) {
      imos[i]++;
    }
    if (i > 0) {
      imos[i] += imos[i - 1];
    }
  }
}

int main() {
  initPrime();

  cin >> Q;
  for (int q = 0; q < Q; q++) {
    cin >> L[q] >> R[q];
  }

  for (int q = 0; q < Q; q++) {
    cout << imos[R[q]] - imos[L[q] - 1] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

using namespace std;

const int MAX_Q = 100005;

int Q;
int L[MAX_Q], R[MAX_Q];

bool prime[MAX_Q];
int imos[MAX_Q];

void initPrime() {
  for (int i = 0; i < MAX_Q; i++) {
    prime[i] = true;
  }

  prime[0] = prime[1] = false;
  for (int i = 2; i <= sqrt(MAX_Q); i++) {
    if (prime[i]) {
      for (int j = 2; i * j < MAX_Q; j++) {
        prime[i * j] = false;
      }
    }
  }

  for (int i = 0; i < MAX_Q; i++) {
    if (i % 2 == 1 && prime[i] && prime[(i + 1) / 2]) {
      imos[i]++;
    }
    if (i > 0) {
      imos[i] += imos[i - 1];
    }
  }
}

int main() {
  initPrime();

  cin >> Q;
  for (int q = 0; q < Q; q++) {
    cin >> L[q] >> R[q];
  }

  for (int q = 0; q < Q; q++) {
    cout << imos[R[q]] - imos[L[q] - 1] << endl;
  }

  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const ll mod = 1000000007;
const int MAX_C = 100010;
bool t[MAX_C];
int t2[MAX_C];
void likeNumber() {

  fill(t, t + MAX_C, true);
  for (int i = 2; i * i <= 100000; i++) {
    if (!t[i])
      continue;
    int j = i * 2;
    while (j <= 100000) {
      t[j] = false;
      j += i;
    }
  }
  t2[0] = 0;
  t2[1] = 0;
  for (size_t i = 2; i <= 100000; i++) {
    if (t[i]) {
      if (t[i * 2 - 1]) {
        t2[i * 2 - 1] = 1;
      }
    }
  }
  for (size_t i = 1; i <= 100000; i++) {
    t2[i] += t2[i - 1];
  }

  int q;
  cin >> q;
  for (size_t i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << (t2[r] - t2[l - 1]) << endl;
  }
}
int main() {
  likeNumber();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const ll mod = 1000000007;
const int MAX_C = 100010;
bool t[MAX_C];
int t2[MAX_C];
void likeNumber() {

  fill(t, t + MAX_C, true);
  for (int i = 2; i * i <= 100000; i++) {
    if (!t[i])
      continue;
    int j = i * 2;
    while (j <= 100000) {
      t[j] = false;
      j += i;
    }
  }
  t2[0] = 0;
  t2[1] = 0;
  for (size_t i = 2; i <= 50000; i++) {
    if (t[i]) {
      if (t[i * 2 - 1]) {
        t2[i * 2 - 1] = 1;
      }
    }
  }
  for (size_t i = 1; i <= 100000; i++) {
    t2[i] += t2[i - 1];
  }

  int q;
  cin >> q;
  for (size_t i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << (t2[r] - t2[l - 1]) << endl;
  }
}
int main() {
  likeNumber();
  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define all(x) begin(x), end(x)

using namespace std;
using ll = long long;

int main() {
  int MAX = 10001;
  vector<int> is_prime(MAX, 1);
  is_prime[0] = is_prime[1] = 0;
  for (int i = 2; i < MAX; ++i) {
    if (!is_prime[i])
      continue;
    for (int j = i * 2; j < MAX; j += i)
      is_prime[j] = 0;
  }

  vector<int> a(MAX, 0);
  for (int i = 0; i < MAX; ++i) {
    if (i % 2 == 0)
      continue;
    if (is_prime[i] && is_prime[(i + 1) / 2])
      a[i] = 1;
  }

  vector<int> s(MAX + 1, 0);
  for (int i = 0; i < MAX; ++i)
    s[i + 1] = s[i] + a[i];

  int Q;
  cin >> Q;
  for (int q = 0; q < Q; ++q) {
    int l, r;
    cin >> l >> r;
    ++r;

    cout << s[r] - s[l] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define all(x) begin(x), end(x)

using namespace std;
using ll = long long;

int main() {
  int MAX = 101010;
  vector<int> is_prime(MAX, 1);
  is_prime[0] = is_prime[1] = 0;
  for (int i = 2; i < MAX; ++i) {
    if (!is_prime[i])
      continue;
    for (int j = i * 2; j < MAX; j += i)
      is_prime[j] = 0;
  }

  vector<int> a(MAX, 0);
  for (int i = 0; i < MAX; ++i) {
    if (i % 2 == 0)
      continue;
    if (is_prime[i] && is_prime[(i + 1) / 2])
      a[i] = 1;
  }

  vector<int> s(MAX + 1, 0);
  for (int i = 0; i < MAX; ++i)
    s[i + 1] = s[i] + a[i];

  int Q;
  cin >> Q;
  for (int q = 0; q < Q; ++q) {
    int l, r;
    cin >> l >> r;
    ++r;

    cout << s[r] - s[l] << endl;
  }
}
Problem p03476 (C++)

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

vector<int> era(int N) {
  vector<int> is_prime(N + 1, 1);
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int p = 2; p * p <= N; p++) {
    if (!is_prime[p])
      continue;
    for (int i = p * 2; i <= N; i += p)
      is_prime[i] = 0;
  }
  return is_prime;
}

signed main() {
  vector<int> pri = era(100000 + 1000);
  int Q;
  cin >> Q;
  vector<int> l(Q), r(Q);
  for (int i = 0; i < Q; i++)
    cin >> l[i] >> r[i];
  vector<int> ans(100000 + 1000, 0);
  for (int i = 1; i < *max_element(r.begin(), r.end()) + 100; i++) {
    ans[i] += ans[i - 1];
    if (i % 2 == 1 && pri[i] && pri[(i + 1) / 2])
      ans[i]++;
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[r[i]] - ans[l[i] - 1] << endl;
  }
  return 0;
}

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

vector<int> era(int N) {
  vector<int> is_prime(N + 1, 1);
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int p = 2; p * p <= N; p++) {
    if (!is_prime[p])
      continue;
    for (int i = p * 2; i <= N; i += p)
      is_prime[i] = 0;
  }
  return is_prime;
}

signed main() {
  vector<int> pri = era(100000 + 1000);
  int Q;
  cin >> Q;
  vector<int> l(Q), r(Q);
  for (int i = 0; i < Q; i++)
    cin >> l[i] >> r[i];
  vector<int> ans(100000 + 1000, 0);
  int maxr = *max_element(r.begin(), r.end());
  for (int i = 1; i < maxr + 100; i++) {
    ans[i] += ans[i - 1];
    if (i % 2 == 1 && pri[i] && pri[(i + 1) / 2])
      ans[i]++;
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[r[i]] - ans[l[i] - 1] << endl;
  }
  return 0;
}