Problem p01392 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#pragma comment(linker, "/STACK:400000000")

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (it = X.begin(); it != X.end(); it++)
#define iss istringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sst stringstream
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

bool isp[1000011];
int last[1000011];
ll A, B, a, b;

int main() {
  cin >> A >> B;
  a = max(2LL, A - B);
  b = A + B;
  fill(isp, isp + 1000011, 1);
  fill(last, last + 2 * B + 1, INF);
  for (ll i = 2; i * i <= b; i++) {
    if (isp[i]) {
      for (ll j = 2 * i; j * j <= b; j += i)
        isp[j] = 0;
      for (ll j = max(2LL, (a + i - 1) / i) * i; j <= b; j += i) {
        if (last[j - a] > -1) {
          ll x = j, y = 0;
          while (x % i == 0)
            x /= i, y++;
          if (last[j - a] < y)
            last[j - a] = -1;
          else
            last[j - a] = y;
        }
      }
    }
  }
  int ans = 0;
  rep2(i, a, b + 1) ans += last[i - a] > -1;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#pragma comment(linker, "/STACK:400000000")

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (it = X.begin(); it != X.end(); it++)
#define iss istringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sst stringstream
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

bool isp[1000011];
int last[1000011];
ll A, B, a, b;

int main() {
  cin >> A >> B;
  a = max(2LL, A - B);
  b = A + B;
  fill(isp, isp + 1000011, 1);
  fill(last, last + 2 * B + 1, INF);
  for (ll i = 2; i * i <= b; i++) {
    if (isp[i]) {
      for (ll j = 2 * i; j * j <= b; j += i)
        isp[j] = 0;
      for (ll j = max(2LL, (a + i - 1) / i) * i; j <= b; j += i) {
        if (last[j - a] > -1) {
          ll x = j, y = 0;
          while (x % i == 0)
            x /= i, y++;
          if (last[j - a] < y)
            last[j - a] = -1;
          else
            last[j - a] = y;
        }
      }
    }
  }
  int ans = 0;
  for (ll i = a; i <= b; i++)
    ans += last[i - a] > -1;
  cout << ans << endl;
}