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

#include <bits/stdc++.h>
using namespace std;
#define pi 2 * asin(1.0)
#define MAX 10000000
#define MOD 1000000007

vector<long> fac(MAX);
vector<long> finv(MAX);
vector<long> inv(MAX);

// template functions
/////////////////////////
void Cominit() {
  fac.at(0) = fac.at(1) = 1;
  finv.at(0) = finv.at(1) = 1;
  inv.at(1) = 1;
  for (long i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
  }
}

long facComb(long N, long M) { // nCm%
  if (N < M)
    return 0;
  if (N < 0 || M < 0)
    return 0;
  return fac.at(N) * (finv.at(M) * finv.at(N - M) % MOD) % MOD;
}

long Comb(long N, long M) { // nCm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  for (long i = M; i > 0; i--) {
    ans = ans / i;
  }
  return ans;
}

long Perm(long N, long M) { // nPm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  return ans;
}

long GCM(long A, long B) {
  long W, r;
  if (A < B) {

    W = B;
    B = A;
    A = W;
  }
  while (A % B != 0) {
    r = A % B;
    A = B;
    B = r;
  }
  return B;
}
long LCM(long A, long B) { return (A * B) / GCM(A, B); }

long upDiv(long a, long b) { return (a + b - 1) / b; }

/////////////////////////

// Answer

int main() {

  long N, max = 0;
  cin >> N;
  vector<long> A(N);
  vector<long> B(N);
  vector<long> C;
  long PR = 1000001;
  vector<long> P(PR, 0);
  vector<long> R;
  for (long i = 2; i * i < PR; i++) {
    if (P.at(i) == 0) {
      long j = i;
      j += i;
      while (j < PR) {
        P.at(j) = 1;
        j += i;
      }
    }
  }
  for (long i = 2; i * i < PR; i++) {
    if (P.at(i) == 0) {
      R.push_back(i);
    }
  }

  for (long i = 0; i < N; i++) {
    cin >> A.at(i);
    B.at(i) = A.at(i);
    if (max < A.at(i)) {
      max = A.at(i);
    }
  }
  vector<long> X(max + 1, 0);
  for (long i = 0; i < N; i++) {
    long j = 0;
    long flag = 0;
    while (j < R.size()) {
      if (B.at(i) % R.at(j) == 0) {
        if (flag == 0) {
          X.at(R.at(j))++;
        }
        B.at(i) = B.at(i) / R.at(j);
        flag = 1;
      } else {
        j++;
        flag = 0;
      }
    }
    if (B.at(i) > 1) {
      X.at(B.at(i))++;
    }
  }
  long t = 1;

  for (long i = 0; i < max + 1; i++) {

    if (X.at(i) > 1) {
      t = 0;
      break;
    }
  }

  sort(A.begin(), A.end());
  long mark = 0;
  long j = 2;
  while (A.at(0) > 1) {
    if (A.at(0) % j == 0) {
      if (mark == 0) {
        C.push_back(j);
      }
      A.at(0) = A.at(0) / j;
      mark = 1;
    } else {
      j++;
      mark = 0;
    }
  }

  for (long i = 1; i < N; i++) {
    for (long k = 0; k < C.size(); k++) {
      if (!(A.at(i) % C.at(k) == 0)) {
        auto itr = next(C.begin(), k);
        C.erase(itr);
      }
    }
  }

  long s = 0;
  if (C.size() == 0) {
    s = 1;
  }
  if (s == 1 & t == 1) {
    cout << "pairwise coprime" << endl;
  } else if (s == 1 && t == 0) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pi 2 * asin(1.0)
#define MAX 10000000
#define MOD 1000000007

vector<long> fac(MAX);
vector<long> finv(MAX);
vector<long> inv(MAX);

// template functions
/////////////////////////
void Cominit() {
  fac.at(0) = fac.at(1) = 1;
  finv.at(0) = finv.at(1) = 1;
  inv.at(1) = 1;
  for (long i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
  }
}

long facComb(long N, long M) { // nCm%
  if (N < M)
    return 0;
  if (N < 0 || M < 0)
    return 0;
  return fac.at(N) * (finv.at(M) * finv.at(N - M) % MOD) % MOD;
}

long Comb(long N, long M) { // nCm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  for (long i = M; i > 0; i--) {
    ans = ans / i;
  }
  return ans;
}

long Perm(long N, long M) { // nPm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  return ans;
}

long GCM(long A, long B) {
  long W, r;
  if (A < B) {

    W = B;
    B = A;
    A = W;
  }
  while (A % B != 0) {
    r = A % B;
    A = B;
    B = r;
  }
  return B;
}
long LCM(long A, long B) { return (A * B) / GCM(A, B); }

long upDiv(long a, long b) { return (a + b - 1) / b; }

/////////////////////////

// Answer

int main() {

  long N, max = 0;
  cin >> N;
  vector<long> A(N);
  vector<long> B(N);
  vector<long> C;
  long PR = 1000002;
  vector<long> P(PR, 0);
  vector<long> R;
  for (long i = 2; i * i < PR; i++) {
    if (P.at(i) == 0) {
      long j = i;
      j += i;
      while (j < PR) {
        P.at(j) = 1;
        j += i;
      }
    }
  }
  for (long i = 2; i < PR; i++) {
    if (P.at(i) == 0) {
      R.push_back(i);
    }
  }

  for (long i = 0; i < N; i++) {
    cin >> A.at(i);
    B.at(i) = A.at(i);
    if (max < A.at(i)) {
      max = A.at(i);
    }
  }
  vector<long> X(max + 1, 0);
  for (long i = 0; i < N; i++) {
    long j = 0;
    long flag = 0;
    while (B.at(i) > 1) {
      if (B.at(i) % R.at(j) == 0) {
        if (flag == 0) {
          X.at(j)++;
        }
        B.at(i) = B.at(i) / R.at(j);
        flag = 1;
      } else {
        j++;
        flag = 0;
      }
    }
  }
  long t = 1;
  for (long i = 0; i < max + 1; i++) {
    if (X.at(i) > 1) {
      t = 0;
      break;
    }
  }

  sort(A.begin(), A.end());
  long mark = 0;
  long j = 2;
  while (A.at(0) > 1) {
    if (A.at(0) % j == 0) {
      if (mark == 0) {
        C.push_back(j);
      }
      A.at(0) = A.at(0) / j;
      mark = 1;
    } else {
      j++;
      mark = 0;
    }
  }

  for (long i = 1; i < N; i++) {
    for (long k = 0; k < C.size(); k++) {
      if (!(A.at(i) % C.at(k) == 0)) {
        auto itr = next(C.begin(), k);
        C.erase(itr);
      }
    }
  }

  long s = 0;
  if (C.size() == 0) {
    s = 1;
  }
  if (s == 1 & t == 1) {
    cout << "pairwise coprime" << endl;
  } else if (s == 1 && t == 0) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}