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

#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1000005;
const int MAXM = 20500;
const ll MOD = 1e9 + 7;

#define MP make_pair
#define INF 0x3f3f3f3f
#define closeSync                                                              \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
template <typename T> inline T min(T a, T b, T c, T d) {
  return min(min(a, b), min(c, d));
}
template <typename T, typename T1> inline T qpower(T x, T p, T1 mod) {
  T re = 1;
  x %= mod;
  while (p) {
    if (p & 1)
      re = (re * x) % mod;
    x = (x * x) % mod;
    p >>= 1;
  }
  return re;
}
#define mst(X, Y) memset(X, Y, sizeof(X))
#define rep(i, a, n) for (int i = (a); i <= (n); i++)
#define per(i, a, n) for (int i = (a); i >= (n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

const int LIM = 1000000;
int prime[MAXN];
int arr[MAXN];
int a[MAXN];
int fac[MAXN];
int cnt = 0;
void init(int n) {
  for (int i = 2; i <= n; i++) {
    if (!arr[i])
      prime[++cnt] = i;
    for (int j = 1; j <= cnt && prime[j] * i <= n; j++) {
      arr[i * prime[j]] = 1;
      if (i % prime[j] == 0)
        break;
    }
  }
}

bool pfc(int n) {
  for (int i = 2; i * i <= n; i++) {
    // if (!arr[n])
    // {
    // 	if (fac[n])
    // 		return false;
    // 	else
    // 	{
    // 		fac[n] = 1;
    // 		return true;
    // 	}
    // }
    if (n % i == 0) {
      if (fac[i])
        return false;
      else
        fac[i] = 1;
      while (n % i == 0)
        n /= i;
    }
  }
  if (n != 1) {
    if (fac[n])
      return false;
    else
      fac[n] = 1;
  }
  return true;
}

int main() {
  closeSync;
#ifdef DEBUG
  // freopen("C:/Users/86159/Desktop/input.txt", "r", stdin);
  // freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
  // clock_t c1 = clock();
  init(1000002);
  int n;
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  bool pc = true;
  rep(i, 1, n) {
    // debug(a[i],pfc(a[i]));
    if (!pfc(a[i])) {
      pc = false;
      break;
    }
  }
  if (pc)
    cout << "pairwise coprime\\n";
  else {
    int cd = a[1];
    rep(i, 2, n) cd = __gcd(cd, a[i]);
    if (cd == 1)
      cout << "setwise coprime\\n";
    else
      cout << "not coprime\\n";
  }

  // cerr << "Time:" << clock() - c1 << "ms" <<endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1000005;
const int MAXM = 20500;
const ll MOD = 1e9 + 7;

#define MP make_pair
#define INF 0x3f3f3f3f
#define closeSync                                                              \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
template <typename T> inline T min(T a, T b, T c, T d) {
  return min(min(a, b), min(c, d));
}
template <typename T, typename T1> inline T qpower(T x, T p, T1 mod) {
  T re = 1;
  x %= mod;
  while (p) {
    if (p & 1)
      re = (re * x) % mod;
    x = (x * x) % mod;
    p >>= 1;
  }
  return re;
}
#define mst(X, Y) memset(X, Y, sizeof(X))
#define rep(i, a, n) for (int i = (a); i <= (n); i++)
#define per(i, a, n) for (int i = (a); i >= (n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

const int LIM = 1000000;
int prime[MAXN];
int arr[MAXN];
int a[MAXN];
int fac[MAXN];
int cnt = 0;
void init(int n) {
  for (int i = 2; i <= n; i++) {
    if (!arr[i])
      prime[++cnt] = i; // prime[] 依序记录素数
    for (int j = 1; j <= cnt && prime[j] * i <= n;
         j++) // 合数=最小质因子*最大因子
    {
      arr[i * prime[j]] = 1;
      if (i % prime[j] == 0) // i%prime[j]=0 --> prime[j]*k=i --> k<prime[j+1]
                             // (k为素数，合数=素数*素数......*素数)
        break; // 对于i*prime[j+1] 来说 prime[j+1] 不是最小素因子
    }
  }
}

bool pfc(int n) {
  for (int i = 2; i <= n; i++) {
    // if (!arr[n])
    // {
    // 	if (fac[n])
    // 		return false;
    // 	else
    // 	{
    // 		fac[n] = 1;
    // 		return true;
    // 	}
    // }
    if (n % i == 0) {
      if (fac[i])
        return false;
      else
        fac[i] = 1;
      while (n % i != 0)
        n /= i;
    }
  }
  return true;
}

int main() {
  closeSync;
#ifdef DEBUG
  // freopen("C:/Users/86159/Desktop/input.txt", "r", stdin);
  // freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
  // clock_t c1 = clock();
  init(1000002);
  int n;
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  bool pc = true;
  rep(i, 1, n) {
    // debug(a[i],pfc(a[i]));
    if (!pfc(a[i])) {
      pc = false;
      break;
    }
  }
  if (pc)
    cout << "pairwise coprime\\n";
  else {
    int cd = a[1];
    rep(i, 2, n) cd = __gcd(cd, a[i]);
    if (cd == 1)
      cout << "setwise coprime\\n";
    else
      cout << "not coprime\\n";
  }
  // cout<<prime[500]<<endl;

  // cerr << "Time:" << clock() - c1 << "ms" <<endl;
  return 0;
}