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

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

unordered_map<int, bool> prime_used;
vector<int> divisors;
int num_cnt;
vector<int> nums;
int max_num;

bool is_pairwise(void);
long long calc_gcd(long long a, long long b);

int main(void) {
  cin >> num_cnt;

  max_num = 0;
  for (int i = 0; i < num_cnt; i++) {
    int n;
    cin >> n;
    if (n > max_num) {
      max_num = n;
    }

    nums.push_back(n);
  }

  divisors.resize(max_num + 1);
  for (int i = 0; i <= max_num; i++) {
    divisors[i] = i;
  }

  divisors[2] = 2;
  for (int i = 2; i <= max_num; i++) {
    if (divisors[i] == i) {
      for (int mul = 2; mul * i <= max_num; mul += 1) {
        if (divisors[mul * i] == mul * i) {
          divisors[mul * i] = i;
        }
      }
    }
  }

  if (is_pairwise()) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int current_gcd = nums[0];
  for (int i = 1; i < num_cnt; i++) {
    current_gcd = calc_gcd(current_gcd, nums[i]);
  }

  if (current_gcd == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime" << endl;

  return 0;
}

bool is_pairwise(void) {
  for (int i = 0; i < num_cnt; i++) {
    int num_cp = nums[i];
    while (num_cp != 1) {
      int divisor = divisors[num_cp];

      while (num_cp % divisor == 0) {
        num_cp /= divisor;
      }
      if (prime_used.find(divisor) == prime_used.end()) {
        prime_used[divisor] == true;
      } else {
        return false;
      }
    }
  }

  return true;
}

long long calc_gcd(long long a, long long b) {
  long long c = a % b;

  if (c == 0) {
    return b;
  }

  return calc_gcd(b, c);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

unordered_map<int, bool> prime_used;
vector<bool> are_prime;
int num_cnt;
vector<int> nums;
int max_num;

bool is_pairwise(void);
long long calc_gcd(long long a, long long b);

int main(void) {
  cin >> num_cnt;

  max_num = 0;
  for (int i = 0; i < num_cnt; i++) {
    int n;
    cin >> n;
    if (n > max_num) {
      max_num = n;
    }

    nums.push_back(n);
  }

  are_prime.resize(max_num);

  are_prime[1] = false;
  are_prime[2] = true;
  for (int i = 2; i < max_num; i++) {
    if (are_prime[i] == true) {
      for (int mul = 2; mul * i <= max_num; mul += 1) {
        are_prime[mul * i] = false;
      }
    }
  }

  if (is_pairwise()) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int current_gcd = nums[0];
  for (int i = 1; i < num_cnt; i++) {
    current_gcd = calc_gcd(current_gcd, nums[i]);
  }

  if (current_gcd == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime" << endl;

  return 0;
}

bool is_pairwise(void) {
  for (int i = 0; i < num_cnt; i++) {
    for (int j = 2; j < max_num; j++) {
      if (are_prime[j] == true) {
        if (nums[i] % j == 0) {
          if (prime_used.find(j) == prime_used.end()) {
            prime_used[j] == true;
          } else {
            return false;
          }
        }
      }
    }
  }

  return true;
}

long long calc_gcd(long long a, long long b) {
  long long c = a % b;

  if (c == 0) {
    return b;
  }

  return calc_gcd(b, c);
}