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

#include <bits/stdc++.h>
#include <chrono>
using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define ALL(a) (a).begin(), (a).end()
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

LL prime_factor(LL x) {
  LL p = x;
  for (LL i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      p = i;
      break;
    }
  }
  return p;
}
LL gcd(LL a, LL b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int counter[(LL)1e6 + 3];
int main() {
  CLR(counter);
  map<LL, LL> mp;
  FOR(i, 1, 1e6 + 1) {
    LL p = prime_factor(i); // iを割り切る最小の素数
    if (p == i) {           // mp[i]:iを割り切る最大の数
      mp[i] = p;
    } else {
      mp[i] = i / p;
    }
  }
  LL N;
  cin >> N;
  vector<LL> A(N);
  LL maxA = 0;
  REP(i, N) {
    cin >> A[i];
    maxA = max(A[i], maxA);
  }
  int g = gcd(A[0], A[1]);
  FOR(i, 1, N) { g = gcd(A[i], g); }
  if (g != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  REP(i, N) {
    LL t = A[i];
    counter[t] += 1;
    if (t >= 2 && counter[t] >= 2) {
      cout << "setwise coprime" << endl;
      return 0;
    }
    while (t != mp[t]) {
      t = mp[t];
      counter[t]++;
      if (t >= 2 && counter[t] >= 2) {
        cout << "setwise coprime" << endl;
        return 0;
      }
    }
  }
  bool resb = true;
  FOR(i, 2, maxA + 1) if (counter[i] > 1) {
    resb = false;
    break;
  }
  if (resb) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  return 0;
}
/*
3
1 1 1
*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define ALL(a) (a).begin(), (a).end()
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

LL prime_factor(LL x) {
  LL p = x;
  for (LL i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      p = i;
      break;
    }
  }
  return p;
}
LL gcd(LL a, LL b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int counter[(LL)1e6 + 3];
int main() {
  CLR(counter);
  map<LL, LL> mp;
  FOR(i, 1, 1e6 + 1) {
    LL p = prime_factor(i); // iを割り切る最小の素数
    if (p == i) {           // mp[i]:iを割り切る最大の数
      mp[i] = p;
    } else {
      mp[i] = i / p;
    }
  }
  LL N;
  cin >> N;
  vector<LL> A(N);
  LL maxA = 0;
  REP(i, N) {
    cin >> A[i];
    maxA = max(A[i], maxA);
  }
  int g = gcd(A[0], A[1]);
  FOR(i, 1, N) { g = gcd(A[i], g); }
  if (g != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  REP(i, N) {
    LL t = A[i];
    counter[t]++;
    while (t != mp[t]) {
      t = mp[t];
      counter[t]++;
    }
  }
  bool resb = true;
  FOR(i, 2, maxA + 1) if (counter[i] > 1) {
    resb = false;
    break;
  }
  if (resb) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  return 0;
}
/*
3
1 1 1
*/