alexjercan/bugnet huggingface dataset:
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll maxn = 1e6 + 111;
const ll mod = 1e9 + 7;
int a[maxn];
int m = 0, p[maxn];
void divide(int n) {
  for (int i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      int cnt = 0;
      while (n % i == 0)
        n /= i, ++cnt;
      p[i]++;
    }
  }
  if (n > 1)
    p[n]++;
}
int main() {
  int n;
  scanf("%d", &n);
  int num = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    divide(a[i]);
  }
  int tot = 0;
  int tot2 = 0;
  int flag1 = 0;
  for (int i = 1; i <= 1000000; ++i) {
    if (p[i] == 0)
      continue;
    if (p[i] != 1)
      flag1 = 1;
    if (p[i] == n)
      ++tot2;
  }
  if (!flag1) {
    puts("pairwise coprime");
  } else if (tot2 == 0) {
    puts("setwise coprime");
  } else {
    puts("not coprime");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
typedef long long ll;
#define lp(i, n) for (ll i = 0; i < ll(n); i++)
#define rlp(i, n) for (ll i = ll(n); i >= 0; i--)
#define rg(i, a, b) for (ll i = ll(a); i <= ll(b); i++)
#define wh(t) while (t)
#define re return
#define pb push_back
#define sz(s) (ll)(s.size())
#define fx(n) fixed << setprecision(n)
#define NeverGiveUp                                                            \\
  ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0),           \\
      cout.tie(0);
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
/*freopen("input.txt","r", stdin);
        freopen("output.txt","w", stdout);*/
using namespace std;
/*ll mx = INT_MIN;
ll mn = INT_MAX;*/
/*ll xd,dx[4]={1,0,-1,0};
ll yd,dy[4]={0,1,0,-1};
bool valid(ll xd,ll yd,ll n,ll m){return xd>-1&&xd<n&&yd>-1&&yd<m;}*/
const int sz = 2e6 + 5;
const ll md = 1e9 + 7;
// ll lcm(ll a,ll b){return a*b/__gcd(a,b);}
ll n, a[sz], g2, g;
bool f;
int main() {
  NeverGiveUp cin >> n;
  lp(i, n) cin >> a[i];
  lp(i, n) rg(j, i + 1, n - 1) {
    g = __gcd(a[i], a[j]);
    if (g != 1) {
      f = 1;
      break;
    }
  }
  g2 = a[0];
  rg(i, 1, n - 1) { g2 = __gcd(g2, a[i]); }
  if (!f)
    cout << "pairwise coprime";
  else if (g2 == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  re 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 1000005;
ll A[MAX_N], Ac[MAX_N], c[MAX_N];
vector<ll> pri;

ll mygcd(ll a, ll b) {
  if (b == 0)
    return a;
  return mygcd(b, a % b);
}

int main() {
  int N;
  cin >> N;
  ll stwise = 0;
  bool stcheck = false;
  rep(i, N) {
    cin >> A[i];
    Ac[A[i]]++;
    if (i == 0)
      stwise = A[i];
    else
      stwise = mygcd(stwise, A[i]);
    // cout << stwise << " ";
  }
  if (stwise != 1) {
    cout << "not coprime" << endl;
    return 0;
  } else {
    stcheck = true;
  }

  for (int i = 2; i < MAX_N; ++i) {
    if (Ac[i] > 1) {
      cout << "setwise coprime" << endl;
      return 0;
    }
  }

  /*
  for (ll i = 2; i <= 1005; ++i)
  {
          bool flag = true;
          for (ll j = 2; j*j <= i; ++j)
          {
                  if (i % j == 0)
                  {
                          flag = false;
                          break;
                  }
          }
          if (flag) pri.push_back(i);
  }

  rep(i,pri.size())
  {
          cout << pri[i] << " ";
          int cnt = 0;
          rep(j,N)
          {
                  if (A[j] % pri[i] == 0)
                  {
                          cnt++;
                  }
          }
          if (cnt > 1)
          {
                  cout << "setwise coprime" << endl;
                  return 0;
          }
  }
  cout << "pairwise coprime" << endl;
  */

  rep(i, N) {
    for (int j = 1; j * j < A[i]; ++j) {
      if (A[i] % j == 0) {
        if (c[j] > 0 || c[A[i] / j] > 0) {
          cout << "setwise coprime" << endl;
          return 0;
        }
        if (j != 1)
          c[j]++;
        if (A[i] / j != 1)
          c[A[i] / j]++;
      }
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 1000005;
ll A[MAX_N];
vector<ll> pri;

ll mygcd(ll a, ll b) {
  if (b == 0)
    return a;
  return mygcd(b, a % b);
}

int main() {
  int N;
  cin >> N;
  ll stwise = 0;
  bool stcheck = false;
  rep(i, N) {
    cin >> A[i];
    if (i == 0)
      stwise = A[i];
    else
      stwise = mygcd(stwise, A[i]);
  }
  if (stwise != 1) {
    cout << "not coprime" << endl;
    return 0;
  } else {
    stcheck = true;
  }

  for (int i = 2; i <= 1000005; ++i) {
    bool flag = true;
    for (int j = 2; j * j <= i; ++j) {
      if (i % j == 0) {
        flag = false;
        break;
      }
    }
    if (flag)
      pri.push_back(i);
  }

  rep(i, pri.size()) {
    int cnt = 0;
    rep(j, N) {
      if (A[j] % pri[i] == 0) {
        cnt++;
      }
    }
    if (cnt > 1) {
      cout << "setwise coprime" << endl;
      return 0;
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int maxm = 1e6 + 5;

int a[maxm], mf[maxm];
unordered_map<int, int> ump;
bool not_pairwise = false;

void init() {
  mf[1] = 1;
  for (int i = 2; i < maxm; i += 2) {
    mf[i] = 2;
  }
  for (int i = 3; i < maxm; i += 2) {
    if (!mf[i]) {
      mf[i] = i;
      for (long long j = 1ll * i * i; j < maxm; j += i) {
        mf[j] = i;
      }
    }
  }
}

void factorize(int x) {
  vector<int> a;
  while (x > 1) {
    a.push_back(mf[x]);
    x /= mf[x];
  }
  for (int y : a) {
    if (ump[y] == 1) {
      not_pairwise = true;
    }
  }
  for (int y : a) {
    ump[y] = 1;
  }
}

int main() {
  init();
  int n;
  cin >> n;
  int g = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (g == -1)
      g = a[i];
    else
      g = __gcd(g, a[i]);
    factorize(a[i]);
  }
  if (!not_pairwise)
    cout << "pairwise coprime";
  else if (g == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxm = 1e4 + 5;

int a[maxm], mf[maxm];
unordered_map<int, int> ump;
bool not_pairwise = false;

void init() {
  mf[1] = 1;
  for (int i = 2; i < maxm; i += 2) {
    mf[i] = 2;
  }
  for (int i = 3; i < maxm; i += 2) {
    if (!mf[i]) {
      mf[i] = i;
      for (long long j = 1ll * i * i; j < maxm; j += i) {
        mf[j] = i;
      }
    }
  }
}

void factorize(int x) {
  vector<int> a;
  while (x > 1) {
    a.push_back(mf[x]);
    x /= mf[x];
  }
  for (int y : a) {
    if (ump[y] == 1) {
      not_pairwise = true;
    }
  }
  for (int y : a) {
    ump[y] = 1;
  }
}

int main() {
  init();
  int n;
  cin >> n;
  int g = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (g == -1)
      g = a[i];
    else
      g = __gcd(g, a[i]);
    factorize(a[i]);
  }
  if (!not_pairwise)
    cout << "pairwise coprime";
  else if (g == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)

int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  int MAX = (int)1e6;
  vector<int> Prime(MAX + 1, 0);

  for (int i = 2; i <= sqrt(MAX); i++) {
    if (Prime[i] == 0) {
      for (int j = 2; j <= MAX; j++) {
        if (i * j > MAX)
          break;
        if (Prime[i * j] == 0)
          Prime[i * j] = -1;
      }
    }
  }
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  vector<int> P;
  for (int i = 2; i <= MAX; i++) {
    if (Prime[i] != -1)
      P.push_back(i);
  }
  vector<bool> check((int)1e6 + 1, false);
  bool flag = true;
  int allgcd = A[0];
  for (auto x : A) {
    allgcd = gcd(allgcd, x);
    // xを素因数分解する
    vector<int> num;
    for (int j = 0; Prime[x] == -1; j++) {
      if (x % P[j] == 0) {
        num.push_back(P[j]);
        while (x % P[j] == 0)
          x /= P[j];
      }
    }
    if (x != 1)
      num.push_back(x);
    for (auto y : num) {
      if (!check[y]) {
        check[y] = true;
      } else {
        flag = false;
        break;
      }
    }
    num.clear();
  }
  if (flag) {
    cout << "pairwise coprime"
         << "\\n";
  } else if (allgcd == 1) {
    cout << "setwise coprime"
         << "\\n";
  } else {
    cout << "not coprime"
         << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)

int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  int MAX = (int)1e3;
  vector<int> Prime(MAX + 1, 0);

  for (int i = 2; i <= sqrt(MAX); i++) {
    if (Prime[i] == 0) {
      for (int j = 2; j <= MAX; j++) {
        if (i * j > MAX)
          break;
        if (Prime[i * j] == 0)
          Prime[i * j] = -1;
      }
    }
  }
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) { cin >> A[i]; }
  vector<int> P;
  for (int i = 2; i <= MAX; i++) {
    if (Prime[i] != -1)
      P.push_back(i);
  }
  vector<bool> check((int)1e6 + 1, false);
  bool flag = true;
  int allgcd = A[0];
  for (auto x : A) {
    allgcd = gcd(allgcd, x);
    // xを素因数分解する
    vector<int> num;
    for (int j = 0; Prime[x] != 0; j++) {
      if (x % P[j] == 0) {
        num.push_back(P[j]);
        while (x % P[j] == 0)
          x /= P[j];
      }
    }
    num.push_back(x);
    for (auto y : num) {
      if (!check[y]) {
        check[y] = true;
      } else {
        flag = false;
        break;
      }
    }
  }
  if (flag) {
    cout << "pairwise coprime"
         << "\\n";
  } else if (allgcd == 1) {
    cout << "setwise coprime"
         << "\\n";
  } else {
    cout << "not coprime"
         << "\\n";
  }
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define mod 1000000007
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

int fact[1000001];
bool ok = false;

void f(int x) {
  if (ok)
    return;
  for (int i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      int a = i;
      int b = x / i;
      if (a == b) {
        a = i;
        b = 1;
      }
      if (a != 1) {
        if (fact[a]) {
          ok = true;
          return;
        }
        fact[a]++;
      }
      if (b != 1) {
        if (fact[b]) {
          ok = true;
          return;
        }
        fact[b]++;
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int gc = a[0];
  f(a[0]);
  for (int i = 1; i < n; i++) {
    f(a[i]);
    gc = __gcd(gc, a[i]);
  }

  if (!ok)
    cout << "pairwise coprime" << endl;
  else if (gc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define endl "\\n"
#define for(i, a, b) for (int i = a; i < b; i++)
#define mod 1000000007
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

int gcd(int a, int b) {
  if (b != 0)
    return gcd(b, a % b);
  return a;
}

bool setw(int *a, int n) {
  int g = a[0];
  for (i, 1, n) {
    g = gcd(a[i], g);
  }
  if (g != 1)
    return false;
  return true;
}

bool pairw(int *a, int n) {

  for (i, 0, n) {
    for (j, i + 1, n) {
      if ((gcd(a[i], a[j]) != 1))
        return false;
    }
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int a[n];
  for (i, 0, n)
    cin >> a[i];
  if (pairw(a, n))
    cout << "pairwise coprime" << endl;
  else if (setw(a, n))
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;

  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

string ans[3] = {"pairwise coprime", "setwise coprime", "not coprime"};

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> d(1e6 + 1, -1);
  d[1] = 1;
  for (int i = 2; i <= 1e6; i++) {
    if (d[i] != -1)
      continue;
    for (int j = i; j <= 1e6; j += i) {
      if (d[j] == -1)
        d[j] = i;
    }
  }

  bool judge = true;
  set<int> visited;
  for (int i = 0; i < n; i++) {
    int t = a[i];
    set<int> st;
    while (t != 1) {
      st.insert(d[t]);
      t /= d[t];
    }
    for (int p : st) {
      if (visited.find(p) != visited.end())
        judge = false;
      visited.insert(p);
    }
  }

  if (judge) {
    cout << ans[0] << endl;
    return 0;
  }

  int val = a[0];
  for (int i = 0; i < n; i++) {
    val = __gcd(val, a[i]);
  }
  cout << (val == 1 ? ans[1] : ans[2]) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

string ans[3] = {"pairwise coprime", "setwise coprime", "not coprime"};

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> d(1e6 + 1, -1);
  d[1] = 1;
  for (int i = 2; i <= 1e6; i++) {
    if (d[i] != -1)
      continue;
    for (int j = i; j <= 1e6; j += i) {
      if (d[j] == -1)
        d[j] = i;
    }
  }

  bool judge = true;
  vector<bool> visited(1e6 + 1, false);
  for (int i = 0; i < n; i++) {
    int t = a[i];
    auto nxt = visited;
    while (t != 1) {
      // cout << i << " " << t << endl;
      if (visited[t])
        judge = false;
      nxt[d[t]] = true;
      t /= d[t];
    }
    swap(nxt, visited);
  }

  if (judge) {
    cout << ans[0] << endl;
    return 0;
  }

  int val = a[0];
  for (int i = 0; i < n; i++) {
    val = __gcd(val, a[i]);
  }
  cout << (val == 1 ? ans[1] : ans[2]) << endl;

  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

int main() {
  llong N;
  vector<llong> A;

  cin >> N;
  A.resize(N);
  for (auto &in : A) {
    cin >> in;
  }
  sort(ALL(A));

  vector<llong> prime;
  for (int i = 2; i <= 1000; i++) {
    bool ok = true;
    for (auto &p : prime) {
      if (i % p == 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      prime.push_back(i);
    }
  }

  int cnt_one = 0;
  map<llong, llong> m;
  for (auto &a : A) {
    cnt_one += a == 1;
    for (auto &p : prime) {
      if (a % p == 0) {
        m[p]++;
        while (a % p == 0) {
          a /= p;
        }
      }
    }
    if (a != 1) {
      m[a]++;
    }
  }

  bool isN = false;
  bool all_one = true;
  for (auto it : m) {
    isN |= it.second == N;
    all_one &= it.second == 1;
  }

  if (all_one) {
    cout << "pairwise coprime" << endl;
  } else if (isN) {
    cout << "not coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

int main() {
  llong N;
  vector<llong> A;

  cin >> N;
  A.resize(N);
  for (auto &in : A) {
    cin >> in;
  }
  sort(ALL(A));

  vector<llong> prime;
  for (int i = 2; i <= 1000; i++) {
    bool ok = true;
    for (auto &p : prime) {
      if (i % p == 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      prime.push_back(i);
    }
  }

  int ichi = 0;
  map<llong, llong> m;
  for (auto &a : A) {
    ichi += a == 1;
    for (auto &p : prime) {
      if (a % p == 0) {
        m[p]++;
        while (a % p == 0) {
          a /= p;
        }
      }
    }
  }

  if (ichi) {
    bool all_one = true;
    for (auto it : m) {
      all_one &= it.second == 1;
    }
    if (ichi >= N - 1 || all_one) {
      return 1;
      cout << "pairwise coprime" << endl;
    } else {
      cout << "setwise coprime" << endl;
    }
  } else {
    bool all_one = true;
    bool all = false;
    for (auto it : m) {
      all_one &= it.second == 1;
      all |= it.second == N;
    }
    if (all_one) {
      cout << "pairwise coprime" << endl;
    } else if (all) {
      cout << "not coprime" << endl;
    } else {
      cout << "setwise coprime" << endl;
    }
  }

  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int a[1000010];
int cnt[1000010];
bool flag[1000010];

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  int x = gcd(a[1], a[2]);
  for (int i = 3; i <= n; i++) {
    x = gcd(x, a[i]);
    if (x == 1)
      break;
  }
  if (x > 1) {
    cout << "not coprime" << endl;
    return 0;
  } else {
    for (int i = 2; i <= 1000000; i++) {
      if (flag[i])
        continue;
      int t = 0;
      for (int j = i; j <= 1000000; j += i) {
        t += cnt[j];
        flag[j] = 1;
      }
      if (t > 1) {
        cout << "setwise coprime" << endl;
        return 0;
      }
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int a[1000010];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int x = gcd(a[1], a[2]);
  for (int i = 3; i <= n; i++)
    x = gcd(x, a[i]);
  if (x != 1)
    cout << "not coprime" << endl;
  else {
    for (int i = 1; i < n; i++) {
      for (int j = i + 1; j <= n; j++)
        if (gcd(a[i], a[j]) != 1) {
          cout << "setwise coprime" << endl;
          return 0;
        }
    }
    cout << "pairwise coprime" << endl;
  }
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

long long gcd(long long a, long long b) {
  long long x = max(a, b), y = min(a, b);
  if (x % y == 0) {
    return y;
  } else {
    return gcd(y, x % y);
  }
}

long long lcm(long long a, long long b) { return (a / lcm(a, b)) * b; }

int main() {

  long long n;
  cin >> n;
  vector<long long> a(n);

  long long g;

  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      g = a[0];
    } else {
      g = gcd(g, a[i]);
    }
  }

  if (g > 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  sort(a.begin(), a.end());

  long long t_size = *max_element(a.begin(), a.end());
  t_size += 10;
  vector<long long> table(t_size, 0);

  for (long long i = 0; i < n; i++) {
    for (long long j = 1; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        table[j]++;
        if (table[j] > 1 && j != 1) {
          // cout << j << " : " << table[j] << endl;
          cout << "setwise coprime" << endl;
          return 0;
        }
        if (a[i] / j != j) {
          table[a[i] / j]++;
          if (table[a[i] / j] > 1) {
            // cout << table[a[i]/j] << endl;
            cout << "setwise coprime" << endl;
            return 0;
          }
        }
      }
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

int main() {

  long long n;
  cin >> n;
  string ans = "not coprime";

  vector<long long> a(n);
  vector<long long> table(100000, 0);

  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    for (long long j = 1; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        table[j]++;
        if (a[i] / j != j)
          table[a[i] / j]++;
      }
    }
  }

  bool ok = true;
  for (long long i = 2; i < table.size(); i++) {
    // cout << table[i] << endl;
    if (table[i] > 1 && table[i] < n) {
      ans = "setwise coprime";
      ok = false;
      break;
    }
    if (table[i] > 1) {
      ok = false;
      break;
    }
  }
  if (ok) {
    ans = "pairwise coprime";
  }
  cout << ans << endl;

  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n, a[1000001];
  cin >> n;
  int b[1000001] = {0};
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
  }
  int z = a[0];
  for (int i = 1; i < n; i++) {
    z = __gcd(z, a[i]);
    if (z == 1)
      break;
  }
  if (z != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  bool k[1000001] = {0};
  for (int i = 2; i < 1000001; i += 1) {
    if (k[i])
      continue;
    z = b[i];
    for (int j = i + i; j < 1000001; j += i)
      z += b[j];
    if (z > 1)
      break;
  }
  if (z <= 1)
    cout << "pairwise coprime" << endl;
  else
    cout << "setwise coprime" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n, a[1000001];
  vector<int> c;
  bool k[1000002] = {0};
  c.push_back(2);
  int z = 0;
  for (int i = 3; i < 1000002; i += 2) {
    if (k[i])
      continue;
    c.push_back(i);
    for (int j = i * 3; j < 1000002; j += 2 * i)
      k[j] = 1;
  }
  int ci = c.size();
  cin >> n;
  bool o1 = 0;
  int b = 0;
  int d = INT_MAX;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1)
      o1 = 1;
    b = max(b, a[i]);
    d = min(d, a[i]);
  }
  int k1;
  for (int i = 0; c[i] <= b; i++) {
    k1 = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] % c[i] == 0)
        k1++;
    }
    z = max(z, k1);
    if (z >= n)
      break;
    if (o1 && z > 1)
      break;
    if (d < c[i] && z > 1)
      break;
  }
  if (z <= 1)
    cout << "pairwise coprime" << endl;
  else if (o1 || z < n)
    cout << "setwise coprime" << endl;
  else if (z >= n)
    cout << "not coprime" << endl;
  return 0;
}