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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

bool is_prime[2000000];
vector<ll> primes;
bool is_fox_number[11000000];
int cnt[11000000];

void Sieve() {
  fill_n(is_prime, 2000000, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 2000000; ++i) {
    if (is_prime[i]) {
      primes.push_back(i);
      for (int j = i * 2; j < 2000000; j += i)
        is_prime[j] = false;
    }
  }
}

void Solve() {
  Sieve();
  ll a, b, l, h;
  cin >> a >> b;
  l = max(a - b, 2LL);
  h = a + b + 1;

  fill_n(is_fox_number, 11000000, true);
  fill_n(cnt, 11000000, INF);

  for (auto p : primes) {
    ll x = (l + p - 1) / p * p;
    // if(x*x>h) break;
    while (x < h) {
      if (!is_fox_number[x - l]) {
        x += p;
        continue;
      }

      ll tmp = x;
      int c = 0;
      while (tmp % p == 0) {
        tmp /= p;
        ++c;
      }

      if (c > cnt[x - l])
        is_fox_number[x - l] = false;

      cnt[x - l] = c;
      x += p;
    }
  }

  int ans = 0;
  rep(i, 0, h - l) ans += is_fox_number[i];

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: MLE

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

bool is_prime[10000000];
vector<ll> primes;
bool is_fox_number[11000000];
int cnt[11000000];

void Sieve() {
  fill_n(is_prime, 10000000, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 10000000; ++i) {
    if (is_prime[i]) {
      primes.push_back(i);
      for (int j = i * 2; j < 10000000; j += i)
        is_prime[j] = false;
    }
  }
}

void Solve() {
  Sieve();
  ll a, b, l, h;
  cin >> a >> b;
  l = max(a - b, 2LL);
  h = a + b + 1;

  fill_n(is_fox_number, 11000000, true);
  fill_n(cnt, 11000000, INF);

  for (auto p : primes) {
    ll x = (l + p - 1) / p * p;
    if (x >= h)
      break;
    while (x < h) {
      if (!is_fox_number[x - l]) {
        x += p;
        continue;
      }

      ll tmp = x;
      int c = 0;
      while (tmp % p == 0) {
        tmp /= p;
        ++c;
      }

      if (c > cnt[x - (a - b)])
        is_fox_number[x - l] = false;

      cnt[x - l] = c;
      x += p;
    }
  }

  int ans = 0;
  rep(i, 0, h - l) ans += is_fox_number[i];

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}