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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

long table[1000000 + 1] = {};

std::vector<bool> IsPrime;
std::vector<long> IsPrime2;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {  // resizeで要素数が減らないように
    IsPrime.resize(max + 1, true); // IsPrimeに必要な要素数を確保
  }
  IsPrime[0] = false; // 0は素数ではない
  IsPrime[1] = false; // 1は素数ではない

  for (size_t i = 2; i * i <= max; ++i)     // 0からsqrt(max)まで調べる
    if (IsPrime[i])                         // iが素数ならば
      for (size_t j = 2; i * j <= max; ++j) // (max以下の)iの倍数は
        IsPrime[i * j] = false;             // 素数ではない
}

void prime(long num) {
  long limit = std::sqrt(num);
  if (limit * limit < num) {
    limit++;
  }
  for (long i = 0; i < IsPrime2.size(); i++) {
    long temp = 0;
    if (IsPrime2[i] > limit) {
      break;
    }
    if (num % IsPrime2[i] == 0) {
      while (num % IsPrime2[i] == 0) {
        num = num / IsPrime2[i];
        temp++;
      }
    }
    if (table[IsPrime2[i]] < temp) {
      table[IsPrime2[i]] = temp;
    }
  }
  if (num > limit) {
    long temp = 1;
    if (table[num] < temp) {
      table[num] = temp;
    }
  }
}

long gcd(long a, long b) {
  if (a < b) {
    long temp = a;
    a = b;
    b = temp;
  }
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  long N;
  std::cin >> N;
  long A[1000000];
  for (long i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  sieve(1000000);
  for (long i = 0; i <= 1000000; i++) {
    if (IsPrime[i] == true) {
      IsPrime2.push_back(i);
    }
  }
  long gcdnum = A[0];
  for (long i = 1; i < N; i++) {
    gcdnum = gcd(gcdnum, A[i]);
  }

  for (long i = 0; i < N; i++) {
    A[i] = A[i] / gcdnum;
    prime(A[i]);
  }

  long mulgcd = gcdnum;
  long MOD = 1000000000 + 7;
  for (long i = 2; i < 1000000 + 1; i++) {
    if (table[i] != 0) {
      long temp = 1;
      for (long j = 0; j < table[i]; j++) {
        temp *= i;
        temp = temp % MOD;
      }
      mulgcd = mulgcd * temp;
      mulgcd = mulgcd % MOD;
    }
  }

  long mul = 1;
  for (long i = 0; i < N; i++) {
    mul *= A[i];
    mul = mul % MOD;
  }
  if (mul == mulgcd) {
    std::cout << "pairwise coprime" << std::endl;
  } else if (gcdnum == 1) {
    std::cout << "setwise coprime" << std::endl;
  } else {
    std::cout << "not coprime" << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

long table[1000000 + 1] = {};

std::vector<bool> IsPrime;

void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {  // resizeで要素数が減らないように
    IsPrime.resize(max + 1, true); // IsPrimeに必要な要素数を確保
  }
  IsPrime[0] = false; // 0は素数ではない
  IsPrime[1] = false; // 1は素数ではない

  for (size_t i = 2; i * i <= max; ++i)     // 0からsqrt(max)まで調べる
    if (IsPrime[i])                         // iが素数ならば
      for (size_t j = 2; i * j <= max; ++j) // (max以下の)iの倍数は
        IsPrime[i * j] = false;             // 素数ではない
}

void prime(long num) {
  long limit = std::sqrt(num);
  if (limit * limit < num) {
    limit++;
  }
  for (long i = 2; i <= limit; i++) {
    if (IsPrime[i]) {
      long temp = 0;
      if (num % i == 0) {
        while (num % i == 0) {
          num = num / i;
          temp++;
        }
      }
      if (table[i] < temp) {
        table[i] = temp;
      }
    }
  }
  if (num > limit) {
    long temp = 1;
    if (table[num] < temp) {
      table[num] = temp;
    }
  }
}

long gcd(long a, long b) {
  if (a < b) {
    long temp = a;
    a = b;
    b = temp;
  }
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  long N;
  std::cin >> N;
  long A[1000000];
  for (long i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  sieve(1000000);
  long gcdnum = A[0];
  for (long i = 1; i < N; i++) {
    gcdnum = gcd(gcdnum, A[i]);
  }

  for (long i = 0; i < N; i++) {
    A[i] = A[i] / gcdnum;
    prime(A[i]);
  }

  long mulgcd = gcdnum;
  long MOD = 1000000000 + 7;
  for (long i = 2; i < 1000000 + 1; i++) {
    if (table[i] != 0) {
      long temp = 1;
      for (long j = 0; j < table[i]; j++) {
        temp *= i;
        temp = temp % MOD;
      }
      mulgcd = mulgcd * temp;
      mulgcd = mulgcd % MOD;
    }
  }

  long mul = 1;
  for (long i = 0; i < N; i++) {
    mul *= A[i];
    mul = mul % MOD;
  }
  if (mul == mulgcd) {
    std::cout << "pairwise coprime" << std::endl;
  } else if (gcdnum == 1) {
    std::cout << "setwise coprime" << std::endl;
  } else {
    std::cout << "not coprime" << std::endl;
  }
  return 0;
}