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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  bool prime[1000005];
  for (int i = 0; i < 1000005; ++i)
    prime[i] = true;
  prime[0] = false;
  prime[1] = false;
  vector<ll> primearr;
  for (int i = 2; i < 1000005; ++i) {
    if (prime[i]) {
      primearr.push_back(i);
      for (int j = 2; i * j < 1000005; ++j) {
        prime[i * j] = false;
      }
    }
  }
  int N;
  cin >> N;
  ll A[N];
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  map<int, int> amap;
  vector<ll> arr1;
  bool pairwise = true;
  ll tmp;
  for (int i = 0; i < N; ++i) {
    tmp = A[i];
    for (ll j = 0; primearr[j] * primearr[j] <= tmp; ++j) {
      if (tmp % primearr[j] == 0) {
        if (amap[primearr[j]] > 0)
          pairwise = false;
        if (i == 0)
          arr1.push_back(primearr[j]);
        amap[primearr[j]]++;
      }
      while (tmp % primearr[j] == 0)
        tmp /= primearr[j];
    }
    if (tmp > 1) {
      if (amap[tmp] > 0)
        pairwise = false;
      if (i == 0)
        arr1.push_back(tmp);
      amap[tmp]++;
    }
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  for (int i = 1; i < N; ++i) {
    vector<ll> arr2;
    for (int j = 0; j < arr1.size(); ++j) {
      if (A[i] % arr1[j] == 0) {
        arr2.push_back(arr1[j]);
      }
    }
    arr1 = arr2;
  }
  if (arr1.size() != 0)
    cout << "not coprime" << endl;
  else
    cout << "setwise coprime" << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int N;
  cin >> N;
  ll A[N];
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  map<int, int> amap;
  vector<ll> arr1;
  bool pairwise = true;
  ll tmp;
  for (int i = 0; i < N; ++i) {
    tmp = A[i];
    for (ll j = 2; j * j <= tmp; ++j) {
      if (tmp % j == 0) {
        if (amap[j] > 0)
          pairwise = false;
        if (i == 0)
          arr1.push_back(j);
        amap[j]++;
      }
      while (tmp % j == 0)
        tmp /= j;
    }
    if (tmp > 1) {
      if (amap[tmp] > 0)
        pairwise = false;
      if (i == 0)
        arr1.push_back(tmp);
      amap[tmp]++;
    }
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  for (int i = 1; i < N; ++i) {
    vector<ll> arr2;
    for (int j = 0; j < arr1.size(); ++j) {
      if (A[i] % arr1[j] == 0) {
        arr2.push_back(arr1[j]);
      }
    }
    arr1 = arr2;
  }
  if (arr1.size() != 0)
    cout << "not coprime" << endl;
  else
    cout << "setwise coprime" << endl;
}