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

#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#define f first
#define s second
#define mp make_pair

using namespace std;

int main() {
  long long a, b;
  cin >> a >> b;

  long long s = a - b;
  long long e = a + b;

  const int MAX = 1500000;
  vector<int> prime;
  vector<bool> p(MAX, true);
  vector<bool> ans(2 * b + 1, true);

  p[0] = p[1] = false;

  for (int i = 4; i < MAX; i += 2)
    p[i] = false;
  for (int i = 3; (long long)i * i < MAX; i += 2)
    if (p[i])
      for (int j = i + i; j < MAX; j += i)
        p[j] = false;

  REP(i, MAX) if (p[i]) prime.push_back(i);
  // REP(i,prime.size()) cout << prime[i] << endl;

  const int n = prime.size();

  if (s < 0) {
    ans = vector<bool>(e + 1, true);
    s = 0;
  }

  if (s == 0)
    ans[0] = false;
  if (s <= 1)
    ans[1 - s] = false;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      long long t = 1;
      t *= prime[i];
      t *= prime[j];
      t *= prime[j];

      // printf("test: %d %d\\n", i, j);

      if (t > e)
        break;
      long long k = t * ((s + t - 1) / t);
      if (k > e)
        continue;

      for (long long kk = k; kk <= e; kk += t) {
        if (ans[kk - s]) {
          int icnt = 0;
          int jcnt = 0;
          long long kkk = kk;
          while (kkk % prime[i] == 0) {
            kkk /= prime[i];
            icnt++;
          }
          while (kkk % prime[j] == 0) {
            kkk /= prime[j];
            jcnt++;
          }

          if (icnt < jcnt)
            ans[kk - s] = false;
        }
      }
    }
  }

  int cnt = 0;
  REP(i, ans.size()) if (ans[i]) cnt++;

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#define f first
#define s second
#define mp make_pair

using namespace std;

int main() {
  long long a, b;
  cin >> a >> b;

  long long s = a - b;
  long long e = a + b;

  const int MAX = 1500000;
  vector<int> prime;
  vector<bool> p(MAX, true);
  vector<bool> ans(2 * b + 1, true);

  p[0] = p[1] = false;

  for (int i = 4; i < MAX; i += 2)
    p[i] = false;
  for (int i = 3; (long long)i * i < MAX; i += 2)
    if (p[i])
      for (int j = i + i; j < MAX; j += i)
        p[j] = false;

  REP(i, MAX) if (p[i]) prime.push_back(i);
  // REP(i,prime.size()) cout << prime[i] << endl;

  const int n = prime.size();

  if (s == 0)
    ans[0] = false;
  if (s <= 1)
    ans[1 - s] = false;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      long long t = 1;
      t *= prime[i];
      t *= prime[j];
      t *= prime[j];

      // printf("test: %d %d\\n", i, j);

      if (t > e)
        break;
      long long k = t * ((s + t - 1) / t);
      if (k > e)
        continue;

      for (long long kk = k; kk <= e; kk += t) {
        if (ans[kk - s]) {
          int icnt = 0;
          int jcnt = 0;
          long long kkk = kk;
          while (kkk % prime[i] == 0) {
            kkk /= prime[i];
            icnt++;
          }
          while (kkk % prime[j] == 0) {
            kkk /= prime[j];
            jcnt++;
          }

          if (icnt < jcnt)
            ans[kk - s] = false;
        }
      }
    }
  }

  int cnt = 0;
  REP(i, ans.size()) if (ans[i]) cnt++;

  cout << cnt << endl;

  return 0;
}