Problem p01392 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int popcount(T n) {
  return n ? 1 + popcount(n & (n - 1)) : 0;
}
#endif
#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) {
  return n ? __builtin_popcount(n) : 0;
}
template <> int popcount(int n) { return n ? __builtin_popcount(n) : 0; }
template <> int popcount(unsigned long long n) {
  return n ? __builtin_popcountll(n) : 0;
}
template <> int popcount(long long n) {
  return n ? __builtin_popcountll(n) : 0;
}
#endif

#define rep (i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

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

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

template <class T>
void print(T a, int n, int br = 1, const string &deli = ", ") {
  cout << "{ ";
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  cout << " }";
  while (br--)
    cout << endl;
}
template <class T>
void print(const vector<T> &v, int br = 1, const string &deli = ", ") {
  print(v, v.size(), br, deli);
}

int main() {
  ll a, b;
  cin >> a >> b;
  ll low = max(2LL, a - b), upper = a + b;

  static bool is_prime[1000100];
  static int prev_e[1000100];
  for (ll i = 2; i * i <= upper; ++i)
    is_prime[i] = true;
  for (int i = 0; i <= 2 * b; ++i)
    prev_e[i] = 1000000;

  for (ll i = 2; i * i <= upper; ++i) {
    if (is_prime[i]) {
      for (ll j = 2 * i; j * j <= upper; j += i)
        is_prime[j] = false;

      for (ll j = (low + i - 1) / i * i; j <= upper; j += i) {
        if (prev_e[j - low] == -1)
          continue;

        int e = 0;
        for (ll num = j; num % i == 0; num /= i)
          ++e;

        if (e > prev_e[j - low])
          prev_e[j - low] = -1;
        else
          prev_e[j - low] = e;
      }
    }
  }

  int res = 0;
  for (ll i = low; i <= upper; ++i)
    if (prev_e[i - low] >= 1)
      ++res;

  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int popcount(T n) {
  return n ? 1 + popcount(n & (n - 1)) : 0;
}
#endif
#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) {
  return n ? __builtin_popcount(n) : 0;
}
template <> int popcount(int n) { return n ? __builtin_popcount(n) : 0; }
template <> int popcount(unsigned long long n) {
  return n ? __builtin_popcountll(n) : 0;
}
template <> int popcount(long long n) {
  return n ? __builtin_popcountll(n) : 0;
}
#endif

#define rep (i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

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

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

template <class T>
void print(T a, int n, int br = 1, const string &deli = ", ") {
  cout << "{ ";
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  cout << " }";
  while (br--)
    cout << endl;
}
template <class T>
void print(const vector<T> &v, int br = 1, const string &deli = ", ") {
  print(v, v.size(), br, deli);
}

int main() {
  ll a, b;
  cin >> a >> b;
  ll low = a - b, upper = a + b;

  static bool is_prime[1000100];
  static int prev_e[1000100];
  for (ll i = 2; i * i <= upper; ++i)
    is_prime[i] = true;
  for (int i = 0; i <= 2 * b; ++i)
    prev_e[i] = 1000000;

  for (ll i = 2; i * i <= upper; ++i) {
    if (is_prime[i]) {
      for (ll j = 2 * i; j * j <= upper; j += i)
        is_prime[j] = false;

      for (ll j = (low + i - 1) / i * i; j <= upper; j += i) {
        if (prev_e[j - low] == -1)
          continue;

        int e = 0;
        for (ll num = j; num % i == 0; num /= i)
          ++e;

        if (e > prev_e[j - low])
          prev_e[j - low] = -1;
        else
          prev_e[j - low] = e;
      }
    }
  }

  int res = 0;
  for (ll i = max(2LL, low); i <= upper; ++i)
    if (prev_e[i - low] >= 1)
      ++res;

  cout << res << endl;
}