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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
template <class T> T mygcd(T a, T b) {
  T m, n, r;
  a = (a > 0) ? a : -a;
  b = (b > 0) ? b : -b;
  if (a > b) {
    m = a;
    n = b;
  } else {
    m = b;
    n = a;
  }

  while (n != 0) {
    r = m % n;
    m = n;
    n = r;
  }

  return m;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int ans0 = a[0];
  for (int i = 1; i < n; i++)
    ans0 = mygcd(ans0, a[i]);
  if (ans0 > 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  vector<bool> is_prime(1000010, true);
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i < 1000010; i++) {
    if (is_prime[i]) {
      for (int j = 2; i * j < 1000010; j++)
        is_prime[i * j] = false;
    }
  }
  vector<int> prime;
  for (int i = 2; i < 1000010; i++) {
    if (is_prime[i])
      prime.push_back(i);
  }

  vector<bool> is_used(1000010, false);
  bool ispair = true;
  for (int i = 0; i < n; i++) {
    int tmp = a[i];
    if (!ispair)
      break;
    for (int j = 0; prime[j] <= tmp; j++) {
      if (prime[j] * prime[j] > a[i]) {
        if (is_used[tmp]) {
          ispair = false;
          break;
        } else {
          is_used[tmp] = true;
          break;
        }
      }
      if (tmp % prime[j] == 0) {
        if (is_used[prime[j]]) {
          ispair = false;
          break;
        } else {
          is_used[prime[j]] = true;
          while (tmp % prime[j] == 0)
            tmp /= prime[j];
        }
      }
    }
  }

  if (ispair) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
template <class T> T mygcd(T a, T b) {
  T m, n, r;
  a = (a > 0) ? a : -a;
  b = (b > 0) ? b : -b;
  if (a > b) {
    m = a;
    n = b;
  } else {
    m = b;
    n = a;
  }

  while (n != 0) {
    r = m % n;
    m = n;
    n = r;
  }

  return m;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int ans0 = a[0];
  for (int i = 1; i < n; i++)
    ans0 = mygcd(ans0, a[i]);
  if (ans0 > 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  vector<bool> is_prime(1000010, true);
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i < 1000010; i++) {
    if (is_prime[i]) {
      for (int j = 2; i * j < 1000010; j++)
        is_prime[i * j] = false;
    }
  }
  vector<int> prime;
  for (int i = 2; i < 1000010; i++) {
    if (is_prime[i])
      prime.push_back(i);
  }

  vector<bool> is_used(1000010, false);
  bool ispair = true;
  for (int i = 0; i < n; i++) {
    int tmp = a[i];
    if (is_used[a[i]]) {
      ispair = false;
      break;
    }
    for (int j = 2; j <= tmp; j++) {
      if (tmp % j == 0) {
        if (is_used[j]) {
          ispair = false;
          break;
        } else {
          is_used[j] = true;
          while (tmp % j == 0)
            tmp /= j;
        }
      }
    }
  }

  if (ispair) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  return 0;
}