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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 1200000
#define HUGE_NUM 99999999999999

int main() {

  int limit;
  ll *table = new ll[NUM];

  for (int i = 0; i < NUM; i++)
    table[i] = 1;
  table[0] = 0;
  table[1] = 0;

  limit = sqrt(NUM);

  for (int i = 2; i <= limit; i++) {
    if (table[i] == 1) {
      for (int k = 2 * i; k < NUM; k += i) {
        table[k] = 0;
      }
    }
  }

  int index = 0;
  ll *prime_table = new ll[550000];

  for (int i = 2; i < NUM; i++) {
    if (table[i] == 1) {
      prime_table[index++] = i;
    }
  }

  ll A, B;
  scanf("%lld %lld", &A, &B);

  ll left, right;

  if (A - B <= 2) {
    left = 2;
  } else {
    left = A - B;
  }

  right = A + B;

  if (left > right) {
    printf("0\\n");
    return 0;
  }

  ll *check = new ll[right - left + 1];
  for (int i = 0; i < right - left + 1; i++) {
    check[i] = HUGE_NUM;
  }

  bool *is_fox = new bool[right - left + 1];
  for (int i = 0; i < right - left + 1; i++)
    is_fox[i] = true;

  ll tmp, count, start;

  for (int i = 0; prime_table[i] <= sqrt(right); i++) {

    if (left % prime_table[i] == 0) {
      start = 0;
    } else {
      start = prime_table[i] * (left / prime_table[i] + 1) - left;
    }

    if (left + start > right)
      continue;

    for (int k = start; k < right - left + 1; k += prime_table[i]) {

      if (!is_fox[k])
        continue;

      tmp = left + k;

      count = 0;

      while (tmp % prime_table[i] == 0) {
        tmp /= prime_table[i];
        count++;
      }

      if (check[k] < count) {
        is_fox[k] = false;
      } else {
        check[k] = count;
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < right - left + 1; i++) {
    if (is_fox[i])
      ans++;
  }

  printf("%d\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 1200000
#define HUGE_NUM 99999999999999

int main() {

  int limit;
  ll *table = new ll[NUM];

  for (int i = 0; i < NUM; i++)
    table[i] = 1;
  table[0] = 0;
  table[1] = 0;

  limit = sqrt(NUM);

  for (int i = 2; i <= limit; i++) {
    if (table[i] == 1) {
      for (int k = 2 * i; k < NUM; k += i) {
        table[k] = 0;
      }
    }
  }

  int index = 0;
  ll *prime_table = new ll[550000];

  for (int i = 2; i < NUM; i++) {
    if (table[i] == 1) {
      prime_table[index++] = i;
    }
  }

  ll A, B;
  scanf("%lld %lld", &A, &B);

  ll left, right;

  if (A - B <= 2) {
    left = 2;
  } else {
    left = A - B;
  }

  right = A + B;

  if (left > right) {
    printf("0\\n");
    return 0;
  }

  // printf("left:%lld right:%lld\\n",left,right);

  ll *check = new ll[right - left + 1];
  for (int i = 0; i < right - left + 1; i++) {
    check[i] = HUGE_NUM;
  }

  bool *is_fox = new bool[right - left + 1];
  for (int i = 0; i < right - left + 1; i++)
    is_fox[i] = true;

  ll tmp, count, start;

  for (int i = 0; prime_table[i] <= sqrt(right); i++) {

    start = 0;
    while (left + start <= right && (left + start) % prime_table[i] != 0)
      start++;

    if (left + start > right)
      continue;

    for (int k = start; k < right - left + 1; k += prime_table[i]) {

      if (!is_fox[k])
        continue;

      tmp = left + k;
      // printf("数字:%lld\\n",tmp);

      count = 0;

      while (tmp % prime_table[i] == 0) {
        tmp /= prime_table[i];
        count++;
      }

      if (check[k] < count) {
        is_fox[k] = false;
      } else {
        check[k] = count;
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < right - left + 1; i++) {
    if (is_fox[i])
      ans++;
  }

  printf("%d\\n", ans);

  return 0;
}