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

// #pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 9;
const int infint = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double eps = 1e-9;
const long double pi = 3.1415926535897932384626433832795;
const long double e = 2.71828182845904523536028747135266;
inline ll read() {
  ll kr = 1, xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls)) {
    if (!(ls ^ 45))
      kr = -1;
    ls = getchar();
  }
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs * kr;
}
// inline __int128 read128()
// {
//     __int128 kr=1,xs=0;char ls;ls=getchar();
//     while(!isdigit(ls)){
//         if(!(ls^45)) kr=-1;ls=getchar();
//     }
//     while(isdigit(ls)){
//         xs=(xs<<1)+(xs<<3)+(ls^48);ls=getchar();
//     }
//     return xs*kr;
// }
inline ull readull() {
  ull xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls))
    ls = getchar();
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs;
}
inline void writeull(ull x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10)
    writeull(x / 10);
  putchar('0' + x % 10);
}
inline void write(ll x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar('0' + x % 10);
}
// inline void write(__int128 x){
//     if(x<0){x*=-1;putchar('-');}
//     if(x>=10) write(x/10);
//     putchar('0'+x%10);
// }
///////////////////////////////////////////////////////////////////////////////////////
int pri[MAXN], cnt, vis[MAXN], a[MAXN], n;

int gcd(int a, int b) { return b > 0 ? gcd(b, a % b) : a; }
void getp(int nn) {
  for (int i = 2; i <= nn; i++) {
    if (!vis[i])
      pri[cnt++] = i;
    for (int j = 0; j < cnt; j++) {
      if (1ll * i * pri[j] > nn)
        break;
      vis[i * pri[j]] = 1;
      if (i % pri[j] == 0)
        break;
    }
  }
}
unordered_map<int, int> q;
int check() {
  q.clear();
  for (int i = 1; i <= n; i++) {
    int x = a[i];
    if (!vis[a[i]]) {
      if (q[a[i]] && a[i] != 1)
        return 0;
      q[a[i]] = 1;
      continue;
    }
    for (int j = 0; j < cnt && pri[j] <= x; j++) {
      if (x % pri[j] == 0) {
        if (q[pri[j]])
          return 0;
        q[pri[j]] = 1;
      }
      while (x % pri[j] == 0)
        x /= pri[j];
    }
    if (x > 1) {
      if (q[x])
        return 0;
      q[x] = 1;
    }
  }
  return 1;
}
int check1() {
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = gcd(ans, a[i]);
  if (ans == 1)
    return 1;
  else
    return 0;
}
int main() {
  int t = 1;
  // t=read();
  while (t--) {
    getp(1000000);
    n = read();
    for (int i = 1; i <= n; i++) {
      a[i] = read();
    }
    if (check())
      printf("pairwise coprime\\n");
    else if (check1())
      printf("setwise coprime\\n");
    else
      printf("not coprime\\n");
  }
  return 0;
}

/*
6
011000010010000001100001011000010000101001100001
*/


Error: TLE

Fixed submission:

// #pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 9;
const int infint = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double eps = 1e-9;
const long double pi = 3.1415926535897932384626433832795;
const long double e = 2.71828182845904523536028747135266;
inline ll read() {
  ll kr = 1, xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls)) {
    if (!(ls ^ 45))
      kr = -1;
    ls = getchar();
  }
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs * kr;
}
// inline __int128 read128()
// {
//     __int128 kr=1,xs=0;char ls;ls=getchar();
//     while(!isdigit(ls)){
//         if(!(ls^45)) kr=-1;ls=getchar();
//     }
//     while(isdigit(ls)){
//         xs=(xs<<1)+(xs<<3)+(ls^48);ls=getchar();
//     }
//     return xs*kr;
// }
inline ull readull() {
  ull xs = 0;
  char ls;
  ls = getchar();
  while (!isdigit(ls))
    ls = getchar();
  while (isdigit(ls)) {
    xs = (xs << 1) + (xs << 3) + (ls ^ 48);
    ls = getchar();
  }
  return xs;
}
inline void writeull(ull x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10)
    writeull(x / 10);
  putchar('0' + x % 10);
}
inline void write(ll x) {
  if (x < 0) {
    x *= -1;
    putchar('-');
  }
  if (x >= 10)
    write(x / 10);
  putchar('0' + x % 10);
}
// inline void write(__int128 x){
//     if(x<0){x*=-1;putchar('-');}
//     if(x>=10) write(x/10);
//     putchar('0'+x%10);
// }
///////////////////////////////////////////////////////////////////////////////////////
int pri[MAXN], cnt, vis[MAXN], a[MAXN], n;
void getp(int nn) {
  for (int i = 2; i <= nn; i++) {
    if (!vis[i])
      pri[cnt++] = i;
    for (int j = 0; j < cnt; j++) {
      if (1ll * i * pri[j] > nn)
        break;
      vis[i * pri[j]] = 1;
      if (i % pri[j] == 0)
        break;
    }
  }
}
unordered_map<int, int> q;
int check() {
  q.clear();
  for (int i = 1; i <= n; i++) {
    int x = a[i];
    for (int j = 0; j < cnt && pri[j] <= x; j++) {
      if (x % pri[j] == 0) {
        if (q[pri[j]])
          return 0;
        q[pri[j]] = 1;
      }
      while (x % pri[j] == 0)
        x /= pri[j];
    }
    if (x > 1) {
      if (q[x])
        return 0;
      q[x] = 1;
    }
  }
  return 1;
}
int check1() {
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = __gcd(ans, a[i]);
  if (ans == 1)
    return 1;
  else
    return 0;
}
int main() {
  int t = 1;
  // t=read();
  while (t--) {
    getp(1000000);
    n = read();
    for (int i = 1; i <= n; i++) {
      a[i] = read();
    }
    if (check())
      printf("pairwise coprime\\n");
    else if (check1())
      printf("setwise coprime\\n");
    else
      printf("not coprime\\n");
  }
  return 0;
}

/*
6
011000010010000001100001011000010000101001100001
*/