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

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int len;

  while (scanf("%d", &len), len) {
    int a[25];
    for (int i = 0; i < len; i++) {
      scanf("%d", &a[i]);
    }
    int n = 32;
    for (int i = 0, j = 0;; i++) {
      if (i % 2 == 0) {
        n -= (n - 1) % 5;
      } else {
        n -= a[j % len];
        j++;
      }
      if (n <= 0) {
        n = 0;
        printf("%d\\n", n);
        break;
      }
      printf("%d\\n", n);
    }
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int len;

  while (scanf("%d", &len), len) {
    int a[4];
    for (int i = 0; i < len; i++) {
      scanf("%d", &a[i]);
    }
    int n = 32;
    for (int i = 0, j = 0;; i++) {
      if (i % 2 == 0) {
        n -= (n - 1) % 5;
      } else {
        n -= a[j % len];
        j++;
      }
      if (n <= 0) {
        n = 0;
        printf("%d\\n", n);
        break;
      }
      printf("%d\\n", n);
    }
  }

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

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int n = 32;
  int size;
  while (cin >> size) {
    n = 31;
    if (size == 0)
      break;
    int dt[size];
    rep(i, size) { cin >> dt[i]; }
    int turn = 1;
    int ite = 0;
    while (n) {
      if (n <= 0)
        break;
      if (turn % 2 == 1) {
        n -= (n - 1) % 5;
        if (n < 0)
          cout << "0" << endl;
        else
          cout << n << endl;
      } else {
        n -= dt[ite % size];
        if (n < 0)
          cout << "0" << endl;
        else
          cout << n << endl;
        ite++;
      }
      turn++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int n = 31;
  int size;
  while (cin >> size) {
    n = 31;
    if (size == 0)
      break;
    int dt[size];
    rep(i, size) { cin >> dt[i]; }
    int turn = 1;
    int ite = 0;
    while (n >= 0) {
      if (turn % 2 == 1) {
        n -= (n - 1) % 5;
        if (n < 0)
          cout << 0 << endl;
        else
          cout << n << endl;
      } else {
        n -= dt[ite % size];
        if (n < 0)
          cout << 0 << endl;
        else
          cout << n << endl;
        ite++;
      }
      turn++;
    }
  }
  return 0;
}
Problem p00164 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int k;
  while (cin >> k, k) {
    int n = 32, a[30];
    for (int i = 0; i < k; i++)
      cin >> a[i];
    for (int i = 0; n; i++) {
      int p;
      p = (n - 1) % 5;
      n -= p;
      cout << n << endl;
      p = a[i % k];
      n -= p;
      if (n < 0)
        n = 0;
      cout << n << endl;
    }
  }
}

Error: -8

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int k;
  while (cin >> k) {
    int n = 32, a[30];
    for (int i = 0; i < k; i++)
      cin >> a[i];
    for (int i = 0; n; i++) {
      int p;
      p = (n - 1) % 5;
      n -= p;
      cout << n << endl;
      p = a[i % k];
      n -= p;
      if (n < 0)
        n = 0;
      cout << n << endl;
    }
  }
}
Problem p00164 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int main(void) {
  while (1) {
    int n;
    vector<int> num;
    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      num.push_back(t);
    }

    int a = 32;
    int i = 0;
    while (1) {
      a -= (a - 1) % 5;
      if (a <= 0) {
        cout << 0 << endl;
        break;
      }
      cout << a << endl;
      a -= num[i % num.size()];
      i++;
      if (a <= 0) {
        cout << 0 << endl;
        break;
      }
      cout << a << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

int main(void) {
  while (1) {
    int n;
    vector<int> num;
    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      num.push_back(t);
    }

    int a = 32;
    int i = 0;
    while (1) {
      a -= (a - 1) % 5;
      if (a < 0) {
        cout << 0 << endl;
        break;
      }
      cout << a << endl;
      a -= num[i % num.size()];
      i++;
      if (a < 0) {
        cout << 0 << endl;
        break;
      }
      cout << a << endl;
    }
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

const int MAX = 10000100;

#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define all(a) (a).begin(), (a).end()
#define ll long long int
#define gcd(a, b) __gcd(a, b)
#define pb(a) push_back(a)
int GCD(int a, int b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    int b = 31;

    int i = 0;
    cout << b << endl;
    while (b - a[i] > 0) {
      b -= a[i];
      i = (i + 1) % n;
      cout << b << endl;
      if (b - (b - 1) % 5 <= 0)
        break;
      b -= (b - 1) % 5;
      cout << b << endl;
    }
    cout << 0 << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<int> ary(n);
    for (int i = 0; i < n; i++)
      cin >> ary[i];
    int cnt = 31;
    int i = 0;
    int c = 0;
    while (cnt >= 0) {
      // cout<<" a ";
      if (cnt < 0)
        break;
      cout << cnt << endl;
      cnt -= ary[i];
      i = (i + 1) % n;
      // cout<<" b ";
      if (cnt < 0)
        break;
      cout << cnt << endl;
      cnt -= (cnt - 1) % 5;
    }
    cout << 0 << endl;
  }
}
Problem p00164 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int i = 0, n, total = 32, length;
  while (cin >> length) {
    if (length == 0) {
      break;
    }
    int jiro[length];

    for (int j = 0; j < length; j++) { // 次郎の取る数
      cin >> jiro[j];
    }

    while (1) {
      n = total; // 一郎のターン
      total -= (n - 1) % 5;
      if (total <= 0) {
        cout << "0" << endl;
        break;
      }
      cout << total << endl;

      total -= jiro[i]; // 次郎のターン
      i++;
      if (i == length) {
        i = 0;
      }
      if (total <= 0) {
        cout << "0" << endl;
        break;
      }
      cout << total << endl;
    }
    total = 32;
    i = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int i = 0, n, total = 32, length;
  while (cin >> length) {
    if (length == 0) {
      break;
    }
    int jiro[length];

    for (int j = 0; j < length; j++) { // 次郎の取る数
      cin >> jiro[j];
    }

    while (1) {
      n = total; // 一郎のターン
      total -= (n - 1) % 5;
      if (total < 0) {
        cout << "0" << endl;
        break;
      }
      cout << total << endl;

      total -= jiro[i]; // 次郎のターン
      i++;
      if (i == length) {
        i = 0;
      }
      if (total < 0) {
        cout << "0" << endl;
        break;
      }
      cout << total << endl;
    }
    total = 32;
    i = 0;
  }
  return 0;
}
Problem p00164 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int main() {
  int m, n[100];
  while (true) {
    cin >> m;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++)
      cin >> n[i];
    int ans = 31;
    int count = 0;
    cout << "31" << endl;
    while (true) {
      if (ans <= n[count]) {
        cout << "0" << endl;
        break;
      }
      ans -= n[count];
      cout << ans << endl;
      ans -= (ans - 1) % 5;
      cout << ans << endl;
      count++;
      count %= m;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int m, n[100];
  while (true) {
    cin >> m;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++)
      cin >> n[i];
    int ans = 31;
    int count = 0;
    cout << "31" << endl;
    while (true) {
      if (ans < n[count]) {
        cout << "0" << endl;
        break;
      }
      ans -= n[count];
      cout << ans << endl;
      ans -= (ans - 1) % 5;
      cout << ans << endl;
      count++;
      count %= m;
    }
  }
  return 0;
}
Problem p00164 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int operate[25];
int main() {
  int n;
  while (cin >> n) {
    if (!n)
      break;
    int m = 31;
    for (int i = 0; i < n; i++) {
      cin >> operate[i];
    }
    int pos = 0;
    while (m > 0) {
      m -= (m - 1) % 5;
      if (m <= 0)
        break;
      cout << m << endl;
      m -= operate[pos];
      if (m <= 0)
        break;
      cout << m << endl;
      pos++;
      if (pos == n)
        pos = 0;
    }
    cout << 0 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int operate[25];
int main() {
  int n;
  while (cin >> n) {
    if (!n)
      break;
    int m = 31;
    for (int i = 0; i < n; i++) {
      cin >> operate[i];
    }
    int pos = 0;
    while (m >= 0) {
      m -= (m - 1) % 5;
      if (m < 0)
        break;
      cout << m << endl;
      m -= operate[pos];
      if (m < 0)
        break;
      cout << m << endl;
      pos++;
      if (pos == n)
        pos = 0;
    }
    cout << 0 << endl;
  }
  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

using namespace std;

int prime[10000001];
int num[1000001];

void CreatePrime() {
  rep(i, 1000001) prime[i] = 1;

  prime[0] = prime[1] = 0;

  REP(i, 2, sqrt(1000001) + 1) {
    for (int j = 0; i * (j + 2) < 1000001; j++) {
      prime[i * (j + 2)] = 0;
    }
  }

  memset(num, 0, sizeof(num));
  int cnt = 1;
  REP(i, 1, 1000001) {
    if (prime[i]) {
      num[i] = cnt;
      cnt++;
    } else {
      num[i] = num[i - 1];
    }
  }
}

int main() {
  CreatePrime();

  int n;
  while (cin >> n && n) {
    int ans = -n;

    rep(i, n) {
      int p, m;
      cin >> p >> m;

      int s = p - m;
      int e = p + m;

      if (s < 0)
        s = 0;
      if (e > 1000000)
        e = 1000000;

      int count = num[e] - num[s];
      if (prime[s])
        count++;

      if (count < 0)
        ans--;
      else
        ans += count;
    }

    if (ans < 0)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

using namespace std;

int prime[10000001];

void CreatePrime() {
  rep(i, 1000001) prime[i] = 1;

  prime[0] = prime[1] = 0;

  REP(i, 2, sqrt(1000001) + 1) {
    for (int j = 0; i * (j + 2) < 1000001; j++) {
      prime[i * (j + 2)] = 0;
    }
  }
}

int main() {
  CreatePrime();

  int n;
  while (cin >> n && n) {
    int ans = -n;

    rep(i, n) {
      int p, m;
      cin >> p >> m;

      int count = 0;

      int s = p - m;
      int e = p + m;

      if (s < 0)
        s = 0;
      if (e > 1000000)
        e = 1000000;

      REP(j, s, e + 1) {
        if (prime[j])
          count++;
      }

      if (count < 0)
        ans--;
      else
        ans += count;
    }

    if (ans < 0)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

// 42
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  static bool cmp[1000000] = {true, true};
  static int ps[1000000] = {};
  for (int i = 2; i < 1000000; i++) {
    ps[i] = ps[i - 1];
    if (!cmp[i]) {
      ps[i]++;
      for (int j = 2; j * i < 1000000; j++) {
        cmp[i * j] = true;
      }
    }
  }
  for (int n; cin >> n, n;) {
    int k = 0;
    for (int i = 0; i < n; i++) {
      int p, m;
      cin >> p >> m;
      k += ps[min(999999, p + m)] - ps[max(p - m - 1, 0)] - 1;
    }
    cout << k << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 42
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  static bool cmp[1000000] = {true, true};
  for (int i = 2; i < 1000000; i++) {
    if (!cmp[i]) {
      for (int j = 2; j * i < 1000000; j++) {
        cmp[i * j] = true;
      }
    }
  }
  for (int n; cin >> n, n;) {
    int k = 0;
    for (int i = 0; i < n; i++) {
      int p, m;
      cin >> p >> m;
      k--;
      for (int j = max(0, p - m); j <= min(p + m, 999999); j++) {
        k += !cmp[j];
      }
    }
    cout << k << endl;
  }
  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX = 1000000;
const int MP = 999983;
int n;
bool is_prime[(MAX + 1) * 2 + 1];
int prime_count[(MAX + 1) * 2 + 1];

void sieve() {
  for (int i = 0; i <= (MAX + 1) * 2; i++)
    is_prime[i] = false;

  is_prime[2] = true;
  for (int i = 3; i <= MP; i += 2) {
    is_prime[i] = true;
    for (int j = 3; j * j <= i; j += 2) {
      if (i % j == 0) {
        is_prime[i] = false;
        break;
      }
    }
  }
}

void calc_count() {
  prime_count[0] = 0;
  for (int i = 1; i <= (MAX + 1) * 2; i++) {
    prime_count[i] = prime_count[i - 1];
    if (is_prime[i])
      prime_count[i]++;
  }
}

int main() {
  sieve();
  calc_count();
  while (cin >> n && n) {
    int p, m, sum, x;
    sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> p >> m;
      int lb, ub;
      lb = (p - m - 1 >= 0) ? p - m - 1 : 0;
      ub = p + m;
      x = prime_count[ub] - prime_count[lb];
      sum += x - 1;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

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

const int MP = 999983;
int n;

bool is_prime(int num) {
  if (num > MP)
    return false;
  if (num == 2)
    return true;
  if (num % 2 == 0)
    return false;

  bool ret = true;
  for (int i = 3; i * i <= num; i += 2) {
    if (num % i == 0) {
      ret = false;
      break;
    }
  }
  return ret;
}

int main() {
  while (cin >> n && n) {
    int p, m, sum, x;
    sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> p >> m;
      x = 0;
      for (int a = p - m; a <= p + m; a++) {
        if (a <= 1)
          continue;
        if (is_prime(a))
          x++;
      }
      sum += x - 1;
    }
    cout << sum << endl;
  }
}
Problem p00165 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

#define MP 999983
#define MAX 1000000
int Prime[MAX + 1];
bool prime[MAX + 1];

void make() {
  fill(prime, prime + MAX, true);
  prime[0] = prime[1] = false;

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

  int sum = 0;
  for (int i = 0; i <= MAX; i++) {
    if (prime[i]) {
      sum++;
    }
    Prime[i] = sum;
  }
}

int main() {
  int n;

  make();

  while (cin >> n, n) {
    int p, m, ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> p >> m;

      int large = p + m;
      int small = p - m;

      if (large > MP) {
        large = MP;
      }
      if (small < 0) {
        small = 0;
      }
      int X = Prime[large] - Prime[small] + prime[small];
      if (X == 0) {
        ans--;
      } else {
        ans += X - 1;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define MAX_P 999983
#define MAX 1000000
int Prime[MAX + 1];
bool prime[MAX + 1];

void make() {
  fill(prime, prime + MAX, true);
  prime[0] = prime[1] = false;

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

  int sum = 0;
  for (int i = 0; i <= MAX; i++) {
    if (prime[i]) {
      sum++;
    }
    Prime[i] = sum;
  }
}

int main() {
  int n;

  make();

  while (cin >> n, n) {
    int p, m, ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> p >> m;

      int large = p + m;
      int small = p - m;

      if (large > MAX_P) {
        large = MAX_P;
      }
      if (!prime[large] && !prime[small] && Prime[large] == Prime[small])
        continue;

      if (small < 0) {
        small = 0;
      }
      ans += Prime[large] - Prime[small];
      if (Prime[large] || Prime[small])
        ans--;
    }
    cout << ans << endl;
  }

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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 10e-8;

bool primeTable[1000001];
int primeBIT[1000001];

int sumBIT(int i, int *bit) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void addBIT(int i, int x, int *bit, int n) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  fill((bool *)primeTable, (bool *)primeTable + 1000001, true);
  fill((int *)primeBIT, (int *)primeBIT + 1000001, 0);

  primeTable[0] = false;
  primeTable[1] = false;

  for (int i = 1; i * i <= 1000000; i++) {
    if (primeTable[i]) {
      for (int j = i + i; j <= 1000000; j += i) {
        primeTable[j] = false;
      }
    }
  }

  for (int i = 1; i <= 1000000; i++) {
    addBIT(i, primeTable[i], primeBIT, 1000000);
  }

  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    int sum = 0;
    for (int i = 0; i < n; i++) {
      int p, m;
      scanf("%d %d", &p, &m);

      int tmp = 0;
      tmp = sumBIT(p + m >= 1000000 ? 1000000 : p + m, primeBIT) -
            sumBIT(p - m - 1 >= 0 ? p - m - 1 : 0, primeBIT);

      tmp > 0 ? sum += tmp - 1 : sum--;
    }

    printf("%d\\n", sum >= 0 ? sum : 0);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 10e-8;

bool primeTable[1000001];

int main() {
  fill((bool *)primeTable, (bool *)primeTable + 1000001, true);

  primeTable[0] = false;
  primeTable[1] = false;

  for (int i = 1; i * i <= 1000000; i++) {
    if (primeTable[i]) {
      for (int j = i + i; j <= 1000000; j += i) {
        primeTable[j] = false;
      }
    }
  }

  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    int sum = 0;
    for (int i = 0; i < n; i++) {
      int p, m;
      scanf("%d %d", &p, &m);

      int tmp = 0;
      for (int j = p - m >= 0 ? p - m : 0; j <= p + m && j <= 1000000; j++) {
        if (primeTable[j]) {
          tmp++;
        }
      }

      tmp > 0 ? sum += tmp - 1 : sum--;
    }

    printf("%d\\n", sum >= 0 ? sum : 0);
  }
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define all(n) (n).begin(), (n).end()
#define MAX 1000004
using namespace std;

vector<int> prime;
bool isntprime[MAX + 1];
void init() {
  isntprime[0] = isntprime[1] = true;
  int j;
  for (int i = 2; i <= MAX; i++)
    if (!isntprime[i])
      for (prime.push_back(i), j = 2 * i; j <= MAX; j += i)
        isntprime[j] = true;
}

int main() {
  init();
  int n;
  while (cin >> n, n) {
    int p, m;
    // int ans = n/101;
    int ans = 0;
    rep(i, n) {
      cin >> p >> m;
      int bottom = max(0, p - m);
      int top = min(999983, p + m);
      int X = upper_bound(all(prime), top) - lower_bound(all(prime), bottom);
      if (X == 0)
        ans--;
      else
        ans += X - 1;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define MAX 1000004
using namespace std;

vector<int> prime;
bool isntprime[MAX + 1];
void init() {
  isntprime[0] = isntprime[1] = true;
  int j;
  for (int i = 2; i <= MAX; i++)
    if (!isntprime[i])
      for (prime.push_back(i), j = 2 * i; j <= MAX; j += i)
        isntprime[j] = true;
}

int main() {
  init();
  int n;
  while (cin >> n, n) {
    int p, m;
    int ans = n / 101;
    rep(i, n) {
      cin >> p >> m;
      int X = 0;
      REP(j, max(0, p - m), min(999983, p + m) + 1) {
        if (!isntprime[j])
          X++;
      }
      if (X == 0)
        ans--;
      else
        ans += X - 1;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00165 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int PRIME_SIZE = 1000000;

int main() {
  bool prime[PRIME_SIZE] = {0};
  prime[0] = prime[1] = true;
  for (int i = 2; i < PRIME_SIZE; ++i) {
    if (prime[i])
      continue;
    for (int j = i + i; j < PRIME_SIZE; j += i) {
      prime[j] = true;
    }
  }

  int n;
  while (cin >> n && n) {
    int pay = -n;
    int *hit = new int[PRIME_SIZE + 1]();
    for (int i = 0; i < n; ++i) {
      int p, m;
      cin >> p >> m;
      int low = max(0, p - m);
      int high = min(PRIME_SIZE - 1, p + m);
      ++hit[low];
      --hit[high + 1];
    }
    int count = 0;
    for (int i = 0; i < PRIME_SIZE; ++i) {
      count += hit[i];
      if (!prime[i]) {
        pay += count;
      }
    }

    cout << (pay < 0 ? 0 : pay) << endl;

    delete[] hit;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int PRIME_SIZE = 1000000;

int main() {
  bool prime[PRIME_SIZE] = {0};
  vector<int> pList;
  prime[0] = prime[1] = true;
  for (int i = 2; i < PRIME_SIZE; ++i) {
    if (prime[i])
      continue;
    pList.push_back(i);
    for (int j = i + i; j < PRIME_SIZE; j += i) {
      prime[j] = true;
    }
  }

  int n;
  while (cin >> n && n) {
    int pay = -n;
    int *hit = new int[PRIME_SIZE]();
    for (int i = 0; i < n; ++i) {
      int p, m;
      cin >> p >> m;
      int low = max(0, p - m);
      int high = min(PRIME_SIZE - 1, p + m);
      int count = 0;
      for (int j = low; j <= high; ++j) {
        ++hit[j];
      }
    }
    for (int i = 0; i < pList.size(); ++i) {
      pay += hit[pList[i]];
    }

    cout << (pay < 0 ? 0 : pay) << endl;

    delete[] hit;
  }
  return 0;
}