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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound

int main() {
  int Q;
  cin >> Q;
  VI L(Q), R(Q);
  rep(i, Q) cin >> L[i] >> R[i];
  VI X(10010);
  rep(i, 10010) if (i > 1) X[i] = 1;
  /*rep(i,10010){
    if(X[i]==1) rep(j,10010/i){
      if(i*(j+2)<10010) X[i*(j+2)]=0;
    }
  }*/
  VI B(10010);
  rep(i, 10010) if (X[i] and X[(i + 1) / 2]) B[i]++;
  VI C(10011);
  rep(i, 10010) C[i + 1] = C[i] + B[i];
  rep(i, Q) cout << C[R[i] + 1] - C[L[i]] << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound

int main() {
  int Q;
  cin >> Q;
  VI L(Q), R(Q);
  rep(i, Q) cin >> L[i] >> R[i];
  VI X(100100);
  rep(i, 100100) if (i > 1) X[i] = 1;
  rep(i, 100100) {
    if (X[i] == 1)
      rep(j, 100100 / i) {
        if (i * (j + 2) < 100100)
          X[i * (j + 2)] = 0;
      }
  }
  VI B(100100);
  rep(i, 100100) if (X[i] and X[(i + 1) / 2]) B[i]++;
  VI C(100100);
  rep(i, 100100) if (i) C[i] = C[i - 1] + B[i];
  rep(i, Q) cout << C[R[i]] - C[L[i] - 1] << endl;
}
Problem p03476 (C++)

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

constexpr int M = 10010;
int main() {
  int Q;
  cin >> Q;

  bool data[M] = {};
  for (int i = 2; i < M; ++i) {
    bool check = true;
    for (int j = 2; j * j <= i; ++j) {
      if (i % j == 0) {
        check = false;
        break;
      }
    }
    if (check)
      data[i] = 1;
  }
  vector<int> data2(M);
  for (int i = 2; i < M; ++i) {
    if (data[i] && data[(i + 1) / 2]) {
      data2[i] = 1;
    }
  }
  for (int i = 2; i < M; ++i) {
    data2[i] += data2[i - 1];
  }
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << data2[r + 1] - data2[l - 1] << endl;
  }
}

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

constexpr int M = 100010;
int main() {
  int Q;
  cin >> Q;

  bool data[M] = {};
  for (int i = 2; i < M; ++i) {
    bool check = true;
    for (int j = 2; j * j <= i; ++j) {
      if (i % j == 0) {
        check = false;
        break;
      }
    }
    if (check)
      data[i] = 1;
  }
  vector<int> data2(M);
  for (int i = 2; i < M; ++i) {
    if (data[i] && data[(i + 1) / 2]) {
      data2[i] = 1;
    }
  }
  for (int i = 2; i < M; ++i) {
    data2[i] += data2[i - 1];
  }
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << data2[r + 1] - data2[l - 1] << endl;
  }
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, a, n) for (ll i = a; i < (ll)n; ++i)
#define INF 10e12
#define MOD 1000000007
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))

using namespace std;

bool is_prime(ll N) {
  if (N <= 1)
    return false;
  for (ll i = 2; i * i <= N; ++i) {
    if (N % i == 0)
      return false;
  }
  return true;
}

int main(void) {
  int n = 10100;
  vector<ll> cusum(n, 0);
  for (ll i = 1; i < n; i++)
    cusum[i] = cusum[i - 1] + ((1 & i) && is_prime(i) && is_prime((i + 1) / 2));
  ll q;
  cin >> q;
  vector<int> l(q), r(q);
  rep(i, 0, q) cin >> l[i] >> r[i];
  rep(i, 0, q) { cout << cusum[r[i]] - cusum[l[i] - 1] << endl; }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, a, n) for (ll i = a; i < (ll)n; ++i)
#define INF 10e12
#define MOD 1000000007
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))

using namespace std;

bool is_prime(ll N) {
  if (N <= 1)
    return false;
  for (ll i = 2; i * i <= N; ++i) {
    if (N % i == 0)
      return false;
  }
  return true;
}

int main(void) {
  int n = 101000;
  vector<ll> cusum(n, 0);
  for (ll i = 1; i < n; i++)
    cusum[i] = cusum[i - 1] + ((1 & i) && is_prime(i) && is_prime((i + 1) / 2));
  ll q;
  cin >> q;
  vector<int> l(q), r(q);
  rep(i, 0, q) cin >> l[i] >> r[i];
  rep(i, 0, q) { cout << cusum[r[i]] - cusum[l[i] - 1] << endl; }
  return 0;
}
Problem p03476 (C++)

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

int main() {
  const int Nmax = 10000;
  // 下準備として[1:N]の中の2017に似た数が何個あるかを調べる
  vector<int> data(Nmax + 1);
  //// -素数判定
  bitset<Nmax + 1> bits;
  bits.set();
  bits.reset(0);
  bits.reset(1);

  int div = 2;
  while (div * div <= Nmax) {
    if (bits.test(div)) {
      for (int i = 2 * div; i <= Nmax; i += div) {
        bits.reset(i);
      }
    }
    div++;
  }
  //// -データ格納
  for (int i = 3; i <= Nmax; i += 2) { // i:奇数
    if (bits.test(i) && bits.test((i + 1) / 2)) {
      data.at(i) = data.at(i - 2) + 1;
    } else {
      data.at(i) = data.at(i - 2);
    }
  }
  for (int i = 2; i <= Nmax; i += 2) { // i:偶数
    data.at(i) = data.at(i - 1);
  }

  // クエリ処理
  int Q, left, right;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> left >> right;

    cout << data.at(right) - data.at(left - 1) << endl;
  }
}

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

int main() {
  const int Nmax = 100000;
  // 下準備として[1:N]の中の2017に似た数が何個あるかを調べる
  vector<int> data(Nmax + 1);
  //// -素数判定
  bitset<Nmax + 1> bits;
  bits.set();
  bits.reset(0);
  bits.reset(1);

  int div = 2;
  while (div * div <= Nmax) {
    if (bits.test(div)) {
      for (int i = 2 * div; i <= Nmax; i += div) {
        bits.reset(i);
      }
    }
    div++;
  }
  //// -データ格納
  for (int i = 3; i <= Nmax; i += 2) { // i:奇数
    if (bits.test(i) && bits.test((i + 1) / 2)) {
      data.at(i) = data.at(i - 2) + 1;
    } else {
      data.at(i) = data.at(i - 2);
    }
  }
  for (int i = 2; i <= Nmax; i += 2) { // i:偶数
    data.at(i) = data.at(i - 1);
  }

  // クエリ処理
  int Q, left, right;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> left >> right;

    cout << data.at(right) - data.at(left - 1) << endl;
  }
}
Problem p03476 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000

using namespace std;

int l[100001], r[100001];
int dp[100001] = {};

int main(void) {
  int q;

  cin >> q;
  FOR(i, 1, q) { cin >> l[i] >> r[i]; }

  for (int i = 3; i <= 100000; i += 2) {
    FOR(t, 2, i - 1) {
      if (i % t == 0) {
        break;
      }
      if (((i + 1) / 2) % t == 0 && ((i + 1) / 2) > t) {
        break;
      }
      if (t == i - 1) {
        dp[i] = 1;
      }
    }
  }
  FOR(i, 1, q) {
    int count = 0;
    for (int t = l[i]; t <= min(r[i], 100000); t += 2) {
      count += dp[t];
    }
    cout << count << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000

using namespace std;

int l[100001], r[100001];
int dp[100001] = {};

int main(void) {
  int q;

  cin >> q;
  FOR(i, 1, q) { cin >> l[i] >> r[i]; }

  for (int i = 3; i <= 100000; i += 2) {
    FOR(t, 2, i - 1) {
      if (i % t == 0) {
        break;
      }
      if (((i + 1) / 2) % t == 0 && ((i + 1) / 2) > t) {
        break;
      }
      if (t == i - 1) {
        dp[i] = 1;
      }
    }
  }
  FOR(i, 1, 100000) { dp[i] += dp[i - 1]; }
  FOR(i, 1, q) { cout << dp[r[i]] - dp[l[i] - 1] << endl; }
  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define N 1000
using namespace std;
bool ip[N + 10];
int sum[N + 10], p[N + 10];
void pre() {
  for (int i = 2; i <= N; ++i) {
    if (!ip[i])
      p[++p[0]] = i;
    for (int j = 1; j <= p[0]; ++j) {
      if (i * p[j] > N)
        break;
      ip[i * p[j]] = 1;
      if (i % p[j] == 0)
        break;
    }
  }
}
int main() {
  //	freopen ("1.in","r",stdin);
  pre();
  for (int i = 2; i <= N; ++i) {
    sum[i] = sum[i - 1];
    if ((i % 2 == 1) && (!ip[i]) && (!(ip[(i + 1) / 2])))
      sum[i]++;
  }
  int Q, l, r;
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", sum[r] - sum[l - 1]);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define N 100010
using namespace std;
bool ip[N + 10];
int sum[N + 10], p[N + 10];
void pre() {
  for (int i = 2; i <= N; ++i) {
    if (!ip[i])
      p[++p[0]] = i;
    for (int j = 1; j <= p[0]; ++j) {
      if (i * p[j] > N)
        break;
      ip[i * p[j]] = 1;
      if (i % p[j] == 0)
        break;
    }
  }
}
int main() {
  //	freopen ("1.in","r",stdin);
  pre();
  for (int i = 2; i <= N; ++i) {
    sum[i] = sum[i - 1];
    if ((i % 2 == 1) && (!ip[i]) && (!(ip[(i + 1) / 2])))
      sum[i]++;
  }
  int Q, l, r;
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", sum[r] - sum[l - 1]);
  }
  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include "prettyprint.hpp"

using namespace std;
using ll = long long;

vector<int> calc_prime_number(int upper_num) {
  vector<int> is_prime(upper_num, 1); // 0 is not prime number.
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < upper_num; ++i) {
    if (!is_prime[i]) {
      continue;
    }
    for (int j = i * 2; j < upper_num; j += i) {
      is_prime[j] = 0;
    }
  }
  return is_prime;
}

int main() {
  int MAX = 10010;
  auto is_prime = calc_prime_number(MAX);
  // cout << is_prime << endl;

  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 <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include "prettyprint.hpp"

using namespace std;
using ll = long long;

vector<int> calc_prime_number(int upper_num) {
  vector<int> is_prime(upper_num, 1); // 0 is not prime number.
  is_prime[0] = 0;
  is_prime[1] = 0;
  for (int i = 2; i < upper_num; ++i) {
    if (!is_prime[i]) {
      continue;
    }
    for (int j = i * 2; j < upper_num; j += i) {
      is_prime[j] = 0;
    }
  }
  return is_prime;
}

int main() {
  int MAX = 100100;
  auto is_prime = calc_prime_number(MAX);
  // cout << is_prime << endl;

  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: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

vector<ll> prime(100000);
bool is_prime[100000];

ll sieve(ll n) {
  ll p = 0;
  for (int i = 0; i <= n; i++) { // 初期化
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

int main() {
  int q;
  cin >> q;
  vector<P> v(q);
  for (int i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    v[i] = mp(l, r);
  }

  sieve(100000);
  vector<int> a(100010, 0);
  for (int i = 3; i <= 100009; i += 2) {
    if ((is_prime[i]) && (is_prime[(i + 1) / 2])) {
      a[i] = (1 + a[i - 2]);
    } else {
      a[i] = a[i - 2];
    }
  }
  for (int i = 0; i < q; i++) {
    cout << a[v[i].sc] - a[max((v[i].fs - 2LL), 1LL)] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

vector<ll> prime(100010);
bool is_prime[100010];

ll sieve(ll n) {
  ll p = 0;
  for (int i = 0; i <= n; i++) { // 初期化
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

int main() {
  int q;
  cin >> q;
  vector<P> v(q);
  for (int i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    v[i] = mp(l, r);
  }

  sieve(100000);
  vector<int> a(100010, 0);
  for (int i = 3; i <= 100009; i += 2) {
    if ((is_prime[i]) && (is_prime[(i + 1) / 2])) {
      a[i] = (1 + a[i - 2]);
    } else {
      a[i] = a[i - 2];
    }
  }
  for (int i = 0; i < q; i++) {
    cout << a[v[i].sc] - a[max((v[i].fs - 2LL), 1LL)] << endl;
  }
  return 0;
}
Problem p03476 (C++)

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

bool isPrime(ll x) {
  if (x == 2)
    return true;
  if (x < 2 or x % 2 == 0)
    return false;

  for (ll i = 3; i <= sqrt(x); i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

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

  vector<ll> sum(10100);
  for (ll i = 3; i <= 10010; i++) {
    sum[i] = sum[i - 1];
    if (isPrime(i) && isPrime((i + 1) / 2))
      sum[i]++;
  }

  ll q;
  cin >> q;

  while (q--) {
    ll a, b;
    cin >> a >> b;

    cout << sum[b] - sum[a - 1] << endl;
  }
}


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

bool isPrime(ll x) {
  if (x == 2)
    return true;
  if (x < 2 or x % 2 == 0)
    return false;

  for (ll i = 3; i <= sqrt(x); i += 2) {
    if (x % i == 0)
      return false;
  }
  return true;
}

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

  vector<ll> sum(101000);
  for (ll i = 3; i <= 100100; i++) {
    sum[i] = sum[i - 1];
    if (isPrime(i) && isPrime((i + 1) / 2))
      sum[i]++;
  }

  ll q;
  cin >> q;

  while (q--) {
    ll a, b;
    cin >> a >> b;

    cout << sum[b] - sum[a - 1] << endl;
  }
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, f, n) for (int i = (f); (i) < (n); i++)
#define repe(i, f, n) for (int i = (f); (i) <= (n); i++)
using namespace std;

typedef long long ll;
ll INF = 1LL << 60;
int MAX = 3000;

bool check_prime(int n) {
  if (n <= 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int q;
  cin >> q;
  vector<int> arr(MAX, 0);
  vector<int> acum(MAX, 0);
  rep(i, 1, MAX) {
    if (check_prime(i) && check_prime((i + 1) / 2))
      arr[i]++;
  }

  rep(i, 1, MAX) acum[i] = acum[i - 1] + arr[i];

  rep(i, 0, q) {
    int l, r;
    cin >> l >> r;
    if (l == r) {
      cout << acum[r] - acum[r - 1] << endl;
    } else {
      cout << acum[r] - acum[l - 1] << endl;
    }
  }

  return 0;
}


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

typedef long long ll;
ll INF = 1LL << 60;
int MAX = 100010;

bool check_prime(int n) {
  if (n <= 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int q;
  cin >> q;
  vector<int> arr(MAX, 0);
  vector<int> acum(MAX, 0);
  rep(i, 1, MAX) {
    if (check_prime(i) && check_prime((i + 1) / 2))
      arr[i]++;
  }

  rep(i, 1, MAX) acum[i] = acum[i - 1] + arr[i];

  rep(i, 0, q) {
    int l, r;
    cin >> l >> r;
    if (l == r) {
      cout << acum[r] - acum[r - 1] << endl;
    } else {
      cout << acum[r] - acum[l - 1] << endl;
    }
  }

  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define rep(i, b) for (int i = 0; i < b; i++)
#define rrep(i, a, b) for (int i = a; i <= b; i++)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;

bool f[100001];
int c[10002];
int N, L, R;

int main() {
  // 篩。
  for (int i = 2; i <= 100000; i++)
    if (!f[i])
      for (int j = i + i; j <= 100000; j += i) {
        f[j] = true;
      }

  // 2017likeナンバーを記録していく。
  for (int i = 3; i <= 100000; i += 2)
    if (!f[i] && !f[(i + 1) / 2])
      c[i]++;

  // 累積和
  for (int i = 3; i <= 100000; i++)
    c[i] += c[i - 1];

  // 前処理終了。クエリをさばいていく。

  cin >> N;

  while (N--) {
    cin >> L >> R;

    cout << c[R] - c[L - 1] << endl;
  }

  return 0;
}


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define rep(i, b) for (int i = 0; i < b; i++)
#define rrep(i, a, b) for (int i = a; i <= b; i++)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;

bool f[100001];
int c[100002];
int N, L, R;

int main() {
  // 篩。
  for (int i = 2; i <= 100000; i++)
    if (!f[i])
      for (int j = i + i; j <= 100000; j += i) {
        f[j] = true;
      }

  // 2017likeナンバーを記録していく。
  for (int i = 3; i <= 100000; i += 2)
    if (!f[i] && !f[(i + 1) / 2])
      c[i]++;

  // 累積和
  for (int i = 3; i <= 100000; i++)
    c[i] += c[i - 1];

  // 前処理終了。クエリをさばいていく。

  cin >> N;

  while (N--) {
    cin >> L >> R;

    cout << c[R] - c[L - 1] << endl;
  }

  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const double pi = 3.14159265358979323846;

int isPrime(int i) {
  bool ok = 1;
  if (i == 1)
    return 0;
  else if (i == 2 || i == 3)
    return 1;
  else {
    for (int k = 2; k * k <= i; k++)
      if (i % k == 0)
        ok = 0;
    if (ok)
      return 1;
    else
      return 0;
  }
}

int main() {
  int q;
  cin >> q;
  int s[10005] = {0};
  for (int i = 1; i <= 10000; i++) {
    if (i % 2 == 1 && isPrime(i) && isPrime((i + 1) / 2))
      s[i + 1] = s[i] + 1;
    else
      s[i + 1] = s[i];
  }
  int r, l;
  while (q) {
    cin >> l >> r;
    int ans = s[r + 1] - s[l];
    cout << ans << endl;
    q--;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const double pi = 3.14159265358979323846;

int isPrime(int i) {
  bool ok = 1;
  if (i == 1)
    return 0;
  else if (i == 2 || i == 3)
    return 1;
  else {
    for (int k = 2; k * k <= i; k++)
      if (i % k == 0)
        ok = 0;
    if (ok)
      return 1;
    else
      return 0;
  }
}

int main() {
  int q;
  cin >> q;
  int s[100005] = {0};
  for (int i = 1; i <= 100000; i++) {
    if (i % 2 == 1 && isPrime(i) && isPrime((i + 1) / 2))
      s[i + 1] = s[i] + 1;
    else
      s[i + 1] = s[i];
  }
  int r, l;
  while (q) {
    cin >> l >> r;
    int ans = s[r + 1] - s[l];
    cout << ans << endl;
    q--;
  }
}
Problem p03476 (Python)

Buggy submission (status: Time Limit Exceeded):
from math import ceil, sqrt

Q = int(input())
querys = [tuple(map(int, input().split())) for i in range(Q)]

N = max(map(max, querys))

Primes = [False] * 2 + [True] * (N - 1)
for i in range(3, N + 1):
    for j in range(2, ceil(sqrt(i)) + 1):
        if i % j == 0:
            Primes[i] = False

s2017s = [False] * (N + 1)
for i in range(1, N + 1, 2):
    if Primes[i] and Primes[(i + 1) // 2]:
        s2017s[i] = True

nums = [0] * (N + 1)
for i in range(1, N + 1, 2):
    nums[i] = nums[i - 2]
    if s2017s[i]:
        nums[i] += 1

for li, ri in querys:
    print(nums[ri] - nums[max(0, li - 2)])


Fixed submission:
from math import ceil, sqrt

Q = int(input())
querys = [tuple(map(int, input().split())) for i in range(Q)]

N = max(map(max, querys))

Primes = [False] * 2 + [True] * (N - 1)
for i in range(3, N + 1):
    for j in range(2, ceil(sqrt(i)) + 1):
        if i % j == 0:
            Primes[i] = False
            break

s2017s = [False] * (N + 1)
for i in range(1, N + 1, 2):
    if Primes[i] and Primes[(i + 1) // 2]:
        s2017s[i] = True

nums = [0] * (N + 1)
for i in range(1, N + 1, 2):
    nums[i] = nums[i - 2]
    if s2017s[i]:
        nums[i] += 1

for li, ri in querys:
    print(nums[ri] - nums[max(0, li - 2)])
Problem p03476 (Python)

Buggy submission (status: Time Limit Exceeded):
q = int(input())
lr = []
for i in range(q):
    lr.append(list(map(int, input().split())))


def binarySearch(alist, item):
    first = 0
    last = len(alist) - 1
    found = False
    while first <= last and not found:
        midpoint = (first + last) // 2
        if alist[midpoint] == item:
            found = True
        else:
            if item < alist[midpoint]:
                last = midpoint - 1
            else:
                first = midpoint + 1
    return found


def is_prime(num):
    i = 2
    while i**2 <= num:
        if num % i == 0:
            return False
        i += 1
    return True


primes = []
for i in range(2, 10**5 + 1):
    if is_prime(i):
        primes.append(i)

like_2017 = []
count = [0 for _ in range(10**5)]
for i in range(len(primes)):
    if primes[i] * 2 - 1 in primes:
        like = 2 * primes[i] - 1
        count[like] += 1
for i in range(len(count) - 1):
    count[i + 1] += count[i]
for i in range(q):
    print(count[lr[i][1]] - count[lr[i][0] - 1])


Fixed submission:
q = int(input())
lr = []
for i in range(q):
    lr.append(list(map(int, input().split())))


def binarySearch(alist, item):
    first = 0
    last = len(alist) - 1
    found = False
    while first <= last and not found:
        midpoint = (first + last) // 2
        if alist[midpoint] == item:
            found = True
        else:
            if item < alist[midpoint]:
                last = midpoint - 1
            else:
                first = midpoint + 1
    return found


def is_prime(num):
    i = 2
    while i**2 <= num:
        if num % i == 0:
            return False
        i += 1
    return True


primes = []
for i in range(2, 10**5 + 1):
    if is_prime(i):
        primes.append(i)

like_2017 = []
count = [0 for _ in range(10**5)]
for i in range(len(primes)):
    if binarySearch(primes, primes[i] * 2 - 1):
        like = 2 * primes[i] - 1
        count[like] += 1
for i in range(len(count) - 1):
    count[i + 1] += count[i]
for i in range(q):
    print(count[lr[i][1]] - count[lr[i][0] - 1])
Problem p03476 (Python)

Buggy submission (status: Time Limit Exceeded):
import math

q = int(input())
cnt = [0] * (5 * 10**4)
cnt[1] = 1
cnt[2] = 2


def is_prime(n):
    visit = []
    if n % 2 == 0:
        return 0
    for i in range(3, 1 + math.ceil(n ** (0.5))):
        if i not in visit:
            if n % i == 0:
                return 0
            else:
                visit.append(i)
    return 1


for i in range(3, 5 * 10**4):
    odd = 2 * i + 1
    if odd % 3 != 1:
        cnt[i] = cnt[i - 1]
    else:
        k = 0
        if is_prime(odd) and is_prime((odd + 1) // 2):
            k = 1
            # print(odd, is_prime(odd), is_prime((odd + 1) // 2))
        cnt[i] = cnt[i - 1] + k
cnt.append(0)
for i in range(q):
    l, r = map(int, input().split())
    print(cnt[(r - 1) // 2] - cnt[(l - 3) // 2])


Fixed submission:
import math

q = int(input())
cnt = [0] * (5 * 10**4)
cnt[1] = 1
cnt[2] = 2


def is_prime(n):
    visit = []
    if n % 2 == 0:
        return 0
    for i in range(3, 1 + math.ceil(n ** (0.5))):
        if i not in visit:
            if n % i == 0:
                return 0
            else:
                visit.append(i)
    return 1


for i in range(3, 5 * 10**4):
    odd = 2 * i + 1
    if odd % 3 != 1 or (odd + 1) % 4 == 0:
        cnt[i] = cnt[i - 1]
    else:
        k = 0
        if is_prime(odd) and is_prime((odd + 1) // 2):
            k = 1
            # print(odd, is_prime(odd), is_prime((odd + 1) // 2))
        cnt[i] = cnt[i - 1] + k
cnt.append(0)
for i in range(q):
    l, r = map(int, input().split())
    print(cnt[(r - 1) // 2] - cnt[(l - 3) // 2])
Problem p03476 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
import numpy as np

input = sys.stdin.buffer.readline
Q = int(input())


def is_prime(v):
    for i in range(2, v):
        if i * i > v:
            break
        if v % i == 0:
            return False
    return True


is_primes = np.ones(10**5 + 2, dtype="bool")
is_primes[:2] = False
for i in np.arange(2, is_primes.shape[0]):
    if not is_primes[i]:
        continue
    if is_prime(i):
        is_primes[i * 2 :: i] = False
# print('is_primes', is_primes[:14])

V = [0] * (10**5 + 2)
for i in range(1, len(V)):
    if i % 2 == 0:
        V[i] = V[i - 1]
        continue
    if is_primes[i] and is_primes[(i + 1) // 2]:
        V[i] = V[i - 1] + 1
    else:
        V[i] = V[i - 1]
# print('V', V)

for _ in range(Q):
    l, r = map(int, input().split())
    # print('l - 1, r, V[l- 1], V[r]', l - 1, r, V[l - 1], V[r])
    print(V[r] - V[l - 1])


Fixed submission:
import sys
import numpy as np

input = sys.stdin.buffer.readline
Q = int(input())


def is_prime(v):
    for i in range(2, v):
        if i * i > v:
            break
        if v % i == 0:
            return False
    return True


def make_prime(U):
    is_prime = np.zeros(U, np.bool)
    is_prime[2] = 1
    is_prime[3::2] = 1
    M = int(U**0.5) + 1
    for p in range(3, M, 2):
        if is_prime[p]:
            is_prime[p * p :: p + p] = 0
    return is_prime


is_primes = make_prime(10**5 + 2)
# print('is_primes', is_primes[:14])

V = [0] * (10**5 + 2)
for i in range(1, len(V)):
    if i % 2 == 0:
        V[i] = V[i - 1]
        continue
    if is_primes[i] and is_primes[(i + 1) // 2]:
        V[i] = V[i - 1] + 1
    else:
        V[i] = V[i - 1]
# print('V', V)

for _ in range(Q):
    l, r = map(int, input().split())
    # print('l - 1, r, V[l- 1], V[r]', l - 1, r, V[l - 1], V[r])
    print(V[r] - V[l - 1])
Problem p03476 (C++)

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

using namespace std;

int isPrime[MAX + 1];
int count[MAX + 1], tmp;

int main(void) {
  int q, l, r;

  // pre-process
  for (int i = 2; i <= MAX; i++) {
    if (isPrime[i] == 0) {
      isPrime[i] = 1;
      for (int j = 2 * i; j <= MAX; j += i) {
        isPrime[j] = -1;
      }
    }
  }
  for (int i = 2; i <= MAX; i++) {
    if (isPrime[i] == 1 && isPrime[(i + 1) / 2] == 1) {
      tmp++;
    }
    count[i] = tmp;
  }

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &l, &r);
    printf("%d\n", count[r] - count[l - 1]);
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#define MAX 100000

using namespace std;

int isPrime[MAX + 1];
int count[MAX + 1], tmp;

int main(void) {
  int q, l, r;

  // pre-process
  for (int i = 2; i <= MAX; i++) {
    if (isPrime[i] == 0) {
      isPrime[i] = 1;
      for (int j = 2 * i; j <= MAX; j += i) {
        isPrime[j] = -1;
      }
    }
  }
  for (int i = 2; i <= MAX; i++) {
    if (isPrime[i] == 1 && isPrime[(i + 1) / 2] == 1) {
      tmp++;
    }
    count[i] = tmp;
  }

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &l, &r);
    printf("%d\n", count[r] - count[l - 1]);
  }

  return 0;
}
Problem p03476 (C++)

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

void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {  // resizeで要素数が減らないように
    IsPrime.resize(max + 1, true); // IsPrimeに必要な要素数を確保
  }
  IsPrime[0] = false; // 0は素数ではない
  IsPrime[1] = false; // 1は素数ではない

  for (size_t i = 2; i * i <= max; ++i)     // 0からsqrt(max)まで調べる
    if (IsPrime[i])                         // iが素数ならば
      for (size_t j = 2; i * j <= max; ++j) // (max以下の)iの倍数は
        IsPrime[i * j] = false;             // 素数ではない
}

int main() {
  sieve(100003);
  cin >> q;
  int count[1000 - 5] = {0};
  for (int i = 3; i < 100003; i++) {
    count[i] = count[i - 1];
    if (IsPrime[i] && IsPrime[(i + 1) / 2]) {
      count[i]++;
    }
  }
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << count[r] - count[l - 1] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
std::vector<bool> IsPrime;
int q;

void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {  // resizeで要素数が減らないように
    IsPrime.resize(max + 1, true); // IsPrimeに必要な要素数を確保
  }
  IsPrime[0] = false; // 0は素数ではない
  IsPrime[1] = false; // 1は素数ではない

  for (size_t i = 2; i * i <= max; ++i)     // 0からsqrt(max)まで調べる
    if (IsPrime[i])                         // iが素数ならば
      for (size_t j = 2; i * j <= max; ++j) // (max以下の)iの倍数は
        IsPrime[i * j] = false;             // 素数ではない
}

int main() {
  sieve(100003);
  cin >> q;
  int count[100005] = {0};
  for (int i = 3; i < 100003; i++) {
    count[i] = count[i - 1];
    if (IsPrime[i] && IsPrime[(i + 1) / 2]) {
      count[i]++;
    }
  }
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << count[r] - count[l - 1] << endl;
  }
  return 0;
}
Problem p03476 (C++)

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

using namespace std;
using ll = long long;
const int N = 10001;

vector<int> ruiseki(N);
vector<int> is_prime(N, 1); // 1 true; 0 false

bool is_like2017(int n) {
  if (n % 2 == 0) {
    return false;
  }
  if (is_prime[n] == 1 and is_prime[(n + 1) / 2] == 1) {
    return true;
  } else {
    return false;
  }
}

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

  is_prime[1] = 0;
  for (int i = 2; i < N; ++i) {
    if (is_prime[i] == 0)
      continue;
    int count = 2 * i;
    while (count < N) {
      is_prime[count] = 0;
      count += i;
    }
  }
  ruiseki[0] = 0;
  ruiseki[1] = 0;
  for (int i = 2; i < N; ++i) {
    if (is_like2017(i)) {
      ruiseki[i] = ruiseki[i - 1] + 1;
    } else {
      ruiseki[i] = ruiseki[i - 1];
    }
  }
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << ruiseki[r] - ruiseki[l - 1] << endl;
  }
  return 0;
}


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

using namespace std;
using ll = long long;
const int N = 100001;

vector<int> ruiseki(N);
vector<int> is_prime(N, 1); // 1 true; 0 false

bool is_like2017(int n) {
  if (n % 2 == 0) {
    return false;
  }
  if (is_prime[n] == 1 and is_prime[(n + 1) / 2] == 1) {
    return true;
  } else {
    return false;
  }
}

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

  is_prime[1] = 0;
  for (int i = 2; i < N; ++i) {
    if (is_prime[i] == 0)
      continue;
    int count = 2 * i;
    while (count < N) {
      is_prime[count] = 0;
      count += i;
    }
  }
  ruiseki[0] = 0;
  ruiseki[1] = 0;
  for (int i = 2; i < N; ++i) {
    if (is_like2017(i)) {
      ruiseki[i] = ruiseki[i - 1] + 1;
    } else {
      ruiseki[i] = ruiseki[i - 1];
    }
  }
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    cout << ruiseki[r] - ruiseki[l - 1] << endl;
  }
  return 0;
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep_1(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

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

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

  vector<int> s(mx + 1);
  rep(i, mx) s[i + 1] = a[i] + s[i];

  int Q;
  cin >> Q;
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    ++r;
    cout << s[r] - s[l] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep_1(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int INF = 101010;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

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

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

  vector<int> s(mx + 1);
  rep(i, mx) s[i + 1] = a[i] + s[i];

  int Q;
  cin >> Q;
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    ++r;
    cout << s[r] - s[l] << 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 < (int)(n); i++)
#define ll long long
int in() {
  int x;
  cin >> x;
  return x;
}
ll lin() {
  ll x;
  cin >> x;
  return x;
}
string sin() {
  string x;
  cin >> x;
  return x;
}
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

bool ifprime(int x) {
  if (x == 1) {
    return false;
  }
  if (x == 2) {
    return true;
  } else if (x % 2 == 0) {
    return false;
  } else {
    for (int i = 2; i * i <= x; i++) {
      if (x % i == 0) {
        return false;
      }
    }
    return true;
  }
}

int main() {
  int Q = in();
  int a[5000];
  rep(i, 5000) { a[i] = 0; }
  rep(i, 5000) {
    if (ifprime(2 * i + 1) == true && ifprime(i + 1) == true) {
      a[i] = a[i - 1] + 1;
    } else {
      a[i] = a[i - 1];
    }
  }
  int l, r;
  rep(i, Q) {
    cin >> l >> r;
    if (l == 1) {
      cout << a[(r - 1) / 2] << endl;
    } else {
      cout << a[(r - 1) / 2] - a[(l - 1) / 2 - 1] << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int in() {
  int x;
  cin >> x;
  return x;
}
ll lin() {
  ll x;
  cin >> x;
  return x;
}
string sin() {
  string x;
  cin >> x;
  return x;
}
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

bool ifprime(int x) {
  if (x == 1) {
    return false;
  }
  if (x == 2) {
    return true;
  } else if (x % 2 == 0) {
    return false;
  } else {
    for (int i = 2; i * i <= x; i++) {
      if (x % i == 0) {
        return false;
      }
    }
    return true;
  }
}

int main() {
  int Q = in();
  int a[50000];
  rep(i, 50000) { a[i] = 0; }
  rep(i, 50000) {
    if (ifprime(2 * i + 1) == true && ifprime(i + 1) == true) {
      a[i] = a[i - 1] + 1;
    } else {
      a[i] = a[i - 1];
    }
  }
  int l, r;
  rep(i, Q) {
    cin >> l >> r;
    if (l == 1) {
      cout << a[(r - 1) / 2] << endl;
    } else {
      cout << a[(r - 1) / 2] - a[(l - 1) / 2 - 1] << endl;
    }
  }
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int> sum(100008);
vector<int> prime(100009, 1);
int ans[100];
int main() {
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i < 100009; i++) {
    if (prime[i] == 1) {
      for (int j = i + i; j < 100009; j = j + i)
        prime[j] = 0;
    }
  }
  sum[0] = 0;
  for (int i = 1; i < 100008; i++) {
    if (i % 2 == 0)
      sum[i] = sum[i - 1];
    else {
      if (prime[i] == 1 && prime[(i + 1) / 2] == 1)
        sum[i] = sum[i - 1] + 1;
      else
        sum[i] = sum[i - 1];
    }
  }
  int q;
  cin >> q;
  for (int y = 0; y < q; y++) {
    int l, r;
    cin >> l >> r;
    ans[y] = sum[r] - sum[l - 1];
  }
  for (int j = 0; j < q; j++) {
    cout << ans[j] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

vector<int> sum(100008);
vector<int> prime(100009, 1);
int ans[100009];
int main() {
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i < 100009; i++) {
    if (prime[i] == 1) {
      for (int j = i + i; j < 100009; j = j + i)
        prime[j] = 0;
    }
  }
  sum[0] = 0;
  for (int i = 1; i < 100008; i++) {
    if (i % 2 == 0)
      sum[i] = sum[i - 1];
    else {
      if (prime[i] == 1 && prime[(i + 1) / 2] == 1)
        sum[i] = sum[i - 1] + 1;
      else
        sum[i] = sum[i - 1];
    }
  }
  int q;
  cin >> q;
  for (int y = 0; y < q; y++) {
    int l, r;
    cin >> l >> r;
    ans[y] = sum[r] - sum[l - 1];
  }
  for (int j = 0; j < q; j++) {
    cout << ans[j] << endl;
  }
  return 0;
}
Problem p03476 (C++)

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

int main() {
  int max = 101010;

  // 素数なら1の配列準備
  vector<int> prime(max);
  for (int i = 2; i < max; i++)
    prime[i] = 1;
  for (int i = 2; i < max; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < max; j += i)
        prime[j] = 0;
    }
  }

  // like2017なら1の配列を準備
  vector<int> like(max);
  for (int i = 3; i < max; i += 2) {
    if (prime[i] == 1 && prime[(i + 1) / 2] == 1)
      like[i] = 1;
  }

  // 累積和
  vector<int> s(max + 1, 0);
  for (int i = 0; i < max; i++)
    s[i + 1] = s[i] + like[i];

  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int l, r;
    l--;
    r--;

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


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

int main() {
  int max = 101010;

  // 素数なら1の配列準備
  vector<int> prime(max);
  for (int i = 2; i < max; i++)
    prime[i] = 1;
  for (int i = 2; i < max; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < max; j += i)
        prime[j] = 0;
    }
  }

  // like2017なら1の配列を準備
  vector<int> like(max);
  for (int i = 3; i < max; i += 2) {
    if (prime[i] == 1 && prime[(i + 1) / 2] == 1)
      like[i] = 1;
  }

  // 累積和
  vector<int> s(max + 1, 0);
  for (int i = 0; i < max; i++)
    s[i + 1] = s[i] + like[i];

  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;

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

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

using namespace std;

template <typename T> T gcd(T x, T y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <typename T> T lcm(T x, T y) {
  if (x == 0 || y == 0)
    return 0;
  return x / gcd(x, y) * y;
}

int main() {
  const int NN = 10001;
  vector<bool> flag(NN, true);
  flag[0] = false;
  flag[1] = false;
  for (int i = 2; i < sqrt(NN); i++) {
    if (flag[i]) {
      for (int j = 0; i * (j + 2) < NN; j++) {
        flag[i * (j + 2)] = 0;
      }
    }
  }
  int Q;
  cin >> Q;
  vector<int> cnt(NN, 0);
  for (int i = 2; i < NN; i++) {
    if (flag[i] && flag[(i + 1) / 2])
      cnt[i]++;
    cnt[i] += cnt[i - 1];
    // cout << cnt[i] << endl;
  }
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    cout << cnt[r] - cnt[l - 1] << endl;
  }
}

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

using namespace std;

template <typename T> T gcd(T x, T y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <typename T> T lcm(T x, T y) {
  if (x == 0 || y == 0)
    return 0;
  return x / gcd(x, y) * y;
}

int main() {
  const int NN = 100001;
  vector<bool> flag(NN, true);
  flag[0] = false;
  flag[1] = false;
  for (int i = 2; i < sqrt(NN); i++) {
    if (flag[i]) {
      for (int j = 0; i * (j + 2) < NN; j++) {
        flag[i * (j + 2)] = 0;
      }
    }
  }
  int Q;
  cin >> Q;
  vector<int> cnt(NN, 0);
  for (int i = 2; i < NN; i++) {
    if (flag[i] && flag[(i + 1) / 2])
      cnt[i]++;
    cnt[i] += cnt[i - 1];
    // cout << cnt[i] << endl;
  }
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    cout << cnt[r] - cnt[l - 1] << endl;
  }
}
Problem p03476 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (size_t i = 0; i < n; ++i)
using namespace std;
vector<bool> IsPrime;
vector<size_t> primes;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {  // resizeで要素数が減らないように
    IsPrime.resize(max + 1, true); // IsPrimeに必要な要素数を確保
  }
  IsPrime[0] = false; // 0は素数ではない
  IsPrime[1] = false; // 1は素数ではない

  for (size_t i = 2; i * i <= max; ++i)     // 0からsqrt(max)まで調べる
    if (IsPrime[i])                         // iが素数ならば
      for (size_t j = 2; i * j <= max; ++j) // (max以下の)iの倍数は
        IsPrime[i * j] = false;             // 素数ではない
  rep(i, max + 1) {
    if (IsPrime[i])
      primes.push_back(i);
  }
}

int main() {
  size_t n;
  cin >> n;
  vector<size_t> l(n), r(n);
  rep(i, n) { cin >> l[i] >> r[i]; }
  auto max = *max_element(r.begin(), r.end());
  sieve(max);
  rep(i, n) {
    size_t ans = 0;
    for (auto itr = lower_bound(primes.begin(), primes.end(), l[i]);
         *itr <= r[i]; ++itr) {
      if (IsPrime[(*itr + 1) / 2])
        ++ans;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (size_t i = 0; i < n; ++i)
using namespace std;
vector<bool> IsPrime;
vector<size_t> primes;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {  // resizeで要素数が減らないように
    IsPrime.resize(max + 1, true); // IsPrimeに必要な要素数を確保
  }
  IsPrime[0] = false; // 0は素数ではない
  IsPrime[1] = false; // 1は素数ではない

  for (size_t i = 2; i * i <= max; ++i)     // 0からsqrt(max)まで調べる
    if (IsPrime[i])                         // iが素数ならば
      for (size_t j = 2; i * j <= max; ++j) // (max以下の)iの倍数は
        IsPrime[i * j] = false;             // 素数ではない
  rep(i, max + 1) {
    if (IsPrime[i])
      primes.push_back(i);
  }
}

int main() {
  size_t n;
  cin >> n;
  vector<size_t> l(n), r(n);
  rep(i, n) { cin >> l[i] >> r[i]; }
  auto max = *max_element(r.begin(), r.end());
  sieve(max);
  rep(i, n) {
    size_t ans = 0;
    for (auto itr = lower_bound(primes.begin(), primes.end(), l[i]);
         itr != primes.end() && *itr <= r[i]; ++itr) {
      if (IsPrime[(*itr + 1) / 2])
        ++ans;
    }
    cout << ans << endl;
  }
}
Problem p03476 (C++)

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

using namespace std;

const int MAX_N = 2020;
bool isPrime[MAX_N];
int cumulative2017LikeNumberCount[MAX_N];

void SieveOfEratosthenes(int N) {
  fill(isPrime, isPrime + sizeof(isPrime), true);
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i < N; i++) {
    if (!isPrime[i]) {
      continue;
    }

    for (int j = 2 * i; j < N; j += i) {
      isPrime[j] = false;
    }
  }
}

bool Is2017LikeNumber(int i) { return isPrime[i] && isPrime[(i + 1) / 2]; }

void ComputeCumulative2017LikeNumberCount(int N) {
  memset(cumulative2017LikeNumberCount, 0,
         sizeof(cumulative2017LikeNumberCount));
  for (int i = 1; i < N; i++) {
    if (Is2017LikeNumber(i)) {
      cumulative2017LikeNumberCount[i] =
          cumulative2017LikeNumberCount[i - 1] + 1;
    } else {
      cumulative2017LikeNumberCount[i] = cumulative2017LikeNumberCount[i - 1];
    }
  }
}

int main() {
  SieveOfEratosthenes(MAX_N);
  ComputeCumulative2017LikeNumberCount(MAX_N);

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int L, R;
    cin >> L >> R;
    printf("%d\n", cumulative2017LikeNumberCount[R] -
                       cumulative2017LikeNumberCount[L - 1]);
  }
  return 0;
}


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

using namespace std;

const int MAX_N = 100010;
bool isPrime[MAX_N];
int cumulative2017LikeNumberCount[MAX_N];

void SieveOfEratosthenes(int N) {
  fill(isPrime, isPrime + sizeof(isPrime), true);
  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 2; i < N; i++) {
    if (!isPrime[i]) {
      continue;
    }

    for (int j = 2 * i; j < N; j += i) {
      isPrime[j] = false;
    }
  }
}

bool Is2017LikeNumber(int i) { return isPrime[i] && isPrime[(i + 1) / 2]; }

void ComputeCumulative2017LikeNumberCount(int N) {
  memset(cumulative2017LikeNumberCount, 0,
         sizeof(cumulative2017LikeNumberCount));
  for (int i = 1; i < N; i++) {
    if (Is2017LikeNumber(i)) {
      cumulative2017LikeNumberCount[i] =
          cumulative2017LikeNumberCount[i - 1] + 1;
    } else {
      cumulative2017LikeNumberCount[i] = cumulative2017LikeNumberCount[i - 1];
    }
  }
}

int main() {
  SieveOfEratosthenes(MAX_N);
  ComputeCumulative2017LikeNumberCount(MAX_N);

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int L, R;
    cin >> L >> R;
    printf("%d\n", cumulative2017LikeNumberCount[R] -
                       cumulative2017LikeNumberCount[L - 1]);
  }
  return 0;
}