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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

vb is_prime;
void p_ini(int n) {
  is_prime.resize(n, true);
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i < n; i++) {
    if (!is_prime[i])
      continue;
    for (int j = i * 2; j < n; j += i)
      is_prime[j] = false;
  }
  return;
}

vl prime_factor(ll n) {
  vl prime_factor_n;
  ll sqrt_n = sqrt(n) + 10;
  rep(i, sqrt_n) {
    if (!is_prime[i])
      continue;
    if (n % i == 0) {
      ll count = 0;
      prime_factor_n.push_back(i);
      while (1) {
        n /= i;
        count++;
        if (n % i != 0)
          break;
      }
    }
  }
  if (n > 1)
    prime_factor_n.push_back(n);
  return prime_factor_n;
}
ll gcd_ll(ll a, ll b) {
  if (a == 0)
    return 0;
  if (b == 0)
    return 0;
  ll x;
  while (1) {
    x = a % b;
    if (x == 0)
      return b;
    else {
      a = b;
      b = x;
    }
  }
}

int main() {
  int N;
  cin >> N;
  p_ini(2000000);
  set<int> pnum;
  vl AA(N);
  string ans = "pairwise coprime";
  rep(i, N) {
    ll A;
    cin >> A;
    AA[i] = A;
  }
  rep(i, N) {
    ll A = AA[i];
    vl pl = prime_factor(A);
    for (int ptmp : pl) {
      if (pnum.find(ptmp) != pnum.end()) {
        ans = "setwise coprime";
        break;
      }
      pnum.insert(ptmp);
    }
    if (ans == "setwise coprime")
      break;
  }
  ll G = AA[0];
  rep(i, N - 1) { G = gcd_ll(G, AA[i + 1]); }
  if (G > 1)
    ans = "not coprime";
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

vb is_prime;
void p_ini(int n) {
  is_prime.resize(n, true);
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i < n; i++) {
    if (!is_prime[i])
      continue;
    for (int j = i * 2; j < n; j += i)
      is_prime[j] = false;
  }
  return;
}

vl prime_factor(ll n) {
  vl prime_factor_n;
  ll sqrt_n = sqrt(n) + 10;
  rep(i, sqrt_n) {
    if (!is_prime[i])
      continue;
    if (n % i == 0) {
      ll count = 0;
      prime_factor_n.push_back(i);
      while (1) {
        n /= i;
        count++;
        if (n % i != 0)
          break;
      }
    }
  }
  if (n > 1)
    prime_factor_n.push_back(n);
  return prime_factor_n;
}

int main() {
  int N;
  cin >> N;
  p_ini(2000000);
  map<int, int> pnum;
  rep(i, N) {
    ll A;
    cin >> A;
    vl pl = prime_factor(A);
    for (int ptmp : pl) {
      pnum[ptmp]++;
    }
  }
  string ans = "pairwise coprime";
  rep(i, is_prime.size()) {
    if (pnum[i] == N) {
      ans = "not coprime";
      break;
    }
    if (pnum[i] >= 2) {
      ans = "setwise coprime";
    }
  }

  cout << ans << endl;
  return 0;
}