alexjercan/bugnet huggingface dataset:
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

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

const int A = 1000005;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> c(A);
  rep(i, n) {
    cin >> a[i];
    c[a[i]]++;
  }

  bool pairwise = true;
  for (int i = 2; i < A; i++) {
    int cnt = 0;
    for (int j = i; j < A; j += i) {
      cnt += c[j];
    }
    if (cnt > 1)
      pairwise = false;
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int g = 0;
  rep(i, n) g = gcd(g, a[i]);
  if (g == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime" << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
#define N 1000010
int main() {
  int n;
  cin >> n;
  vector<int> G(n);
  for (int i = 0; i < n; i++) {
    G[i] = 0;
  }
  vector<int> b;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    b.push_back(a);
    G[a]++;
    for (int j = 2; j <= sqrt(a); j++) {
      if (!(a % j))
        G[j]++;
    }
  }

  int ans = gcd(b[0], b[1]);
  for (int i = 2; i < n; i++) {
    ans = gcd(b[i], ans);
  }

  if (ans != 1) {
    cout << "not coprime" << endl;
    return 0;
  } else {
    for (int j = 2; j <= sqrt(N); j++) {
      if (G[j] >= 2) {
        cout << "setwise coprime" << endl;
        return 0;
      }
    }
  }

  cout << "pairwise coprime" << endl;
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

map<ll, bool> primes;

bool dup(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      if (primes[i]) {
        return true;
      }
      if (i != 1) {
        primes[i] = true;
      }
      // 重複しないならば i の相方である N/i も push
      if (N / i != i) {
        if (primes[N / i]) {
          return true;
        }
        if (N / i != 1) {
          primes[N / i] = true;
        }
      };
    }
  }
  return false;
}

int main() {
  ll N;
  cin >> N;

  ll g1;
  cin >> g1;
  dup(g1);
  bool pairwise = true;
  for (ll i = 2; i <= N; i++) {
    ll a;
    cin >> a;
    if (dup(a)) {
      pairwise = false;
    }
    g1 = __gcd(g1, a);
  }

  if (pairwise) {
    cout << "pairwise coprime\\n";
  } else if (g1 == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

map<P, ll> memo;

ll gcd(ll x, ll y) {
  if (memo[P(x, y)] != 0) {
    return memo[P(x, y)];
  }

  if (x % y == 0) {
    memo[P(x, y)] = y;
    memo[P(y, x)] = y;
    return y;
  } else {
    ll t = x % y;
    ll g = gcd(y, t);
    memo[P(y, t)] = g;
    memo[P(t, y)] = g;
    return g;
    // x%y==0でないときはユーグリットの互除法を使って再帰的に関数を呼び出す。
  }
}

int main() {
  ll N;
  cin >> N;

  ll A[1000000 + 1];
  ll g1;
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
    if (i == 1) {
      g1 = A[i];
    } else {
      g1 = gcd(g1, A[i]);
    }
  }

  for (ll i = 1; i <= N - 1; i++) {
    for (ll j = i + 1; j <= N; j++) {
      if (gcd(A[i], A[j]) != 1) {
        if (g1 == 1) {
          cout << "setwise coprime\\n";
          return 0;
        } else {
          cout << "not coprime\\n";
          return 0;
        }
      }
    }
  }

  cout << "pairwise coprime\\n";
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const int A = 1000001;

ll GCD(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return GCD(b, a % b);
  }
}
int main() {
  int n;
  cin >> n;
  ll ref = 0;
  ll sum = 0;
  vector<ll> a(n);
  vector<ll> c(A, 0);
  bool pairwise = true;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ref = GCD(a[i], ref);
    sum += a[i];
    c[a[i]]++;
  }
  for (int i = 2; i < A; i++) {
    int cnt = 0;
    for (int j = i; j < A; j += i) {
      cnt += c[j];
    }
    if (cnt != 0 && cnt != 1) {
      pairwise = false;
      break;
    }
  }

  if (pairwise == 1) {
    cout << "pairwise coprime" << endl;
  } else if (ref == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

long long GCD(long long a, long long b) {
  if (b == 0) {
    return a;
  } else {
    return GCD(b, a % b);
  }
}
int main() {
  int n;
  cin >> n;
  long long ref = 0;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ref = GCD(a[i], ref);
  }
  int b = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (GCD(a[i], a[j]) == 1) {
        b = 1;
      }
    }
  }
  if (b == 1) {
    cout << "pairwise coprime" << endl;
  } else if (ref == 1) {
    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;
using ll = long long;

const int L = 1e6 + 1;
vector<bool> p(L);
vector<int> g(L);
int st[L];

void solve() {
  // INPUT
  int n;
  cin >> n;
  fill(st, st + L, 0);
  bool pairwise = true;
  int cp = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    // if (st.count(a)) pairwise = false;
    st[a]++;
    cp = __gcd(cp, a);
  }

  // LOGIC
  for (int i = L; i >= 2; --i) {
    int tmp = 0;
    for (int j = i; j <= L; j += i) {
      tmp += st[j];
    }
    if (tmp > 1 || !pairwise) {
      pairwise = false;
      break;
    }
  }

  if (pairwise)
    cout << "pairwise coprime\\n";
  else if (cp == 1)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int L = 1e6 + 1;
vector<bool> p(L);
vector<int> g(L);

void solve() {
  // INPUT
  int n;
  cin >> n;
  multiset<int> st;
  bool pairwise = true;
  int cp = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    // if (st.count(a)) pairwise = false;
    st.insert(a);
    cp = __gcd(cp, a);
  }

  // LOGIC
  for (int i = L; i >= 2; --i) {
    int tmp = 0;
    for (int j = i; j <= L; j += i) {
      tmp += st.count(j);
    }
    if (tmp > 1 || !pairwise) {
      pairwise = false;
      break;
    }
  }

  if (pairwise)
    cout << "pairwise coprime\\n";
  else if (cp == 1)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  while (t--) {
    solve();
  }
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;

map<int, int> mp;
const int MAX_INT = 1000000;
int is_prime[MAX_INT + 1];
vector<int> P;
void calc(int N) {
  while (-1 != is_prime[N]) {
    int p = is_prime[N];
    while (0 == N % p) {
      N /= p;
    }
    mp[p]++;
  }
  if (1 != N) {
    mp[N]++;
  }
  return;
}
// エラトステネスの篩(素数の数を返している)
void sieve(int n) {
  is_prime[0] = -1;
  is_prime[1] = -1;
  for (int m = 2; m <= n; ++m) { // 初期化
    is_prime[m] = -1;
  }
  for (int m = 2; m <= n; ++m) {
    if (-1 == is_prime[m]) {
      P.push_back(m);
      for (int l = 2 * m; l <= n; l += m) {
        is_prime[l] = m;
      }
    }
  }
  return;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int maxA = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    maxA = max(maxA, A[i]);
  }
  sieve(maxA);
  for (int i = 0; i < N; ++i) {
    calc(A[i]);
  }

  bool b1 = true;
  bool b2 = false;
  for (auto p : mp) {
    if (1 != p.second) {
      b1 = false;
    }
    if (N == p.second) {
      b2 = true;
    }
  }
  if (b1) {
    cout << "pairwise coprime" << endl;
  } else if (!b2) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

map<int, int> mp;
const int MAX_INT = 1000000;
bool is_prime[MAX_INT + 1];
vector<int> P;
void calc(int N) {
  for (int i = 0; i < P.size(); ++i) {
    if (0 == N % P[i]) {
      mp[i]++;
    }
  }
  return;
}
// エラトステネスの篩(素数の数を返している)
void sieve(int n) {
  is_prime[0] = false;
  is_prime[1] = false;
  for (int m = 2; m <= n; ++m) { // 初期化
    is_prime[m] = true;
  }
  for (int m = 2; m <= n; ++m) {
    if (is_prime[m]) {
      P.push_back(m);
      for (int l = 2 * m; l <= n; l += m) {
        is_prime[l] = false;
      }
    }
  }
  return;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int maxA = 0;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    maxA = max(maxA, A[i]);
  }
  sieve(maxA);
  for (int i = 0; i < N; ++i) {
    calc(A[i]);
  }

  bool b1 = true;
  bool b2 = false;
  for (auto p : mp) {
    if (1 != p.second) {
      b1 = false;
    }
    if (N == p.second) {
      b2 = true;
    }
  }
  if (b1) {
    cout << "pairwise coprime" << endl;
  } else if (!b2) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MAXN 4000005
using namespace std;

int n, a[MAXN], b[MAXN], ok, js = 0, maxl = 0;
int vis[MAXN], prime[MAXN], tot, judge[MAXN];

void init() {
  memset(vis, -1, sizeof(vis));
  tot = 0;
  for (int i = 2; i <= 2000000; i++) {
    if (vis[i] == (-1)) {
      tot++;
      prime[tot] = i;
    }
    for (int j = 1; j <= tot && prime[j] * i <= 2000000; j++) {
      vis[prime[j] * i] = 1;
      if (i % prime[j] == 0)
        break;
    }
  }
}

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

int cmp(int x, int y) { return x < y; }

int main() {
  init();
  ok = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    maxl = max(maxl, a[i]);
    b[i] = a[i];
  }
  while (prime[js] * prime[js] <= maxl)
    js++;
  int cnt;
  for (int j = 1; j <= js; j++) {
    cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i] % prime[j] == 0) {
        cnt++;
        while (a[i] % prime[j] == 0)
          a[i] /= prime[j];
      }
    }
    if (cnt > 1) {
      ok = 1;
      break;
    }
  }
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i < n; i++) {
    if (a[i] == 1)
      continue;
    if (a[i] == a[i + 1]) {
      ok = 1;
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    a[i] = b[i];
  }
  if (ok == 1) {
    ok = 0;
    int zz = gcd(a[1], a[2]);
    for (int i = 1; i <= n; i++)
      zz = gcd(zz, a[i]);
    if (zz == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  } else {
    cout << "pairwise coprime" << endl;
    exit(0);
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define MAXN 7000005
using namespace std;

int n, a[MAXN], ok;
int vis[MAXN], prime[MAXN], tot, judge[MAXN];

void init() {
  memset(vis, -1, sizeof(vis));
  tot = 0;
  for (int i = 2; i <= 2000000; i++) {
    if (vis[i] == (-1)) {
      tot++;
      prime[tot] = i;
    }
    for (int j = 1; j <= tot && prime[j] * i <= 2000000; j++) {
      vis[prime[j] * i] = 1;
      if (i % prime[j] == 0)
        break;
    }
  }
}

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

int main() {
  init();
  ok = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int x;
  for (int i = 1; i <= n && ok == 0; i++) {
    x = a[i];
    for (int j = 1; j <= tot && prime[j] <= x; j++) {
      if (x % prime[j] == 0) {
        judge[j]++;
        if (judge[j] > 1) {
          ok = 1;
          break;
        }
        while (x % prime[i] == 0)
          x /= prime[i];
      }
    }
  }
  if (ok == 1) {
    ok = 0;
    int zz = gcd(a[1], a[2]);
    for (int i = 1; i <= n; i++)
      zz = gcd(zz, a[i]);
    if (zz == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  } else {
    cout << "pairwise coprime" << endl;
    exit(0);
  }
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define endl '\\n'
#define maxn 1000010
const int MOD = 1000000007;

int dp[maxn];

void solve() {
  for (int i = 2; i <= 1000000; i++)
    for (int j = i; j <= 1000000; j += i)
      if (dp[j] == 0)
        dp[j] = i;
  int n;
  cin >> n;

  vector<int> v(n);
  int gcd = 0;
  unordered_map<int, int> mp;
  bool fd = false;

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

    if (gcd == 0)
      gcd = v[i];
    else
      gcd = __gcd(v[i], gcd);

    int now = dp[v[i]], cur = v[i];

    if (fd)
      continue;
    while (cur > 1) {
      while (cur % now == 0)
        cur /= now;
      if (mp[now] > 0) {
        fd = true;
        break;
      } else
        mp[now]++;
      now = dp[cur];
    }
  }
  if (!fd)
    cout << "pairwise coprime";
  else if (gcd == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl '\\n'
#define maxn 100010
const int MOD = 1000000007;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

void solve() {
  int n;
  cin >> n;

  vector<int> v(n);
  int gcd = 0;
  unordered_map<int, int, custom_hash> mp;
  bool fd = false;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    gcd = __gcd(v[i], gcd);

    vector<int> prm;
    int tp = v[i];
    for (int j = 2; j * j <= tp; j++) {
      int ct = 0;
      while (tp % j == 0)
        tp /= j, ct++;
      if (mp[j] > 0 and ct > 0)
        fd = true;
      else if (ct > 0)
        mp[j]++;
    }
    if (tp > 1) {
      if (mp[tp])
        fd = true;
      else
        mp[tp]++;
    }
  }
  if (!fd)
    cout << "pairwise coprime";
  else if (gcd == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  //    cin >> t;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define oo 0x3f3f3f3f

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  vector<int> spf(1e6 + 1);
  for (int i = 0; i < spf.size(); i++)
    spf[i] = i;

  for (int i = 2; i < spf.size(); i++)
    if (spf[i] == i)
      for (int j = 2 * i; j < spf.size(); j += i)
        if (spf[j] == j)
          spf[j] = i;

  vector<int> f(1e6 + 1);
  int gcd = 0;

  int n;
  cin >> n;
  vector<int> A(n);
  int idx = 0;

  bool pairwise = true;

  for (int &x : A) {
    idx++;
    cin >> x;
    if (gcd != 1)
      gcd = __gcd(gcd, x);
    while (x > 1 && pairwise) {
      if (f[spf[x]] > 0 && f[spf[x]] != idx)
        pairwise = 0;
      f[spf[x]] = idx;
      x /= spf[x];
    }
  }

  if (pairwise)
    return cout << "pairwise coprime\\n", 0;
  if (gcd == 1)
    return cout << "setwise coprime\\n", 0;
  cout << "not coprime";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define oo 0x3f3f3f3f

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  vector<int> spf(1e6 + 1);
  for (int i = 0; i < spf.size(); i++)
    spf[i] = i;

  for (int i = 2; i < spf.size(); i++)
    if (spf[i] == i)
      for (int j = 2 * i; j < spf.size(); j += i)
        if (spf[j] == j)
          spf[j] = i;

  vector<set<int>> f(1e6 + 1);
  int gcd = 0;

  int n;
  cin >> n;
  vector<int> A(n);
  int idx = 0;
  for (int &x : A) {
    cin >> x;
    gcd = __gcd(gcd, x);
    while (x > 1) {
      f[spf[x]].insert(idx);
      x /= spf[x];
    }
    idx++;
  }

  bool pairwise = true;
  for (auto &st : f)
    pairwise &= st.size() <= 1;

  if (pairwise)
    return cout << "pairwise coprime\\n", 0;
  if (gcd == 1)
    return cout << "setwise coprime\\n", 0;
  cout << "not coprime";
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mod 1000000007
#define ma 1000005

ll gcd(ll, ll);

int main() {
  ll n;
  ll x;
  ll gc = 0;
  bool pc = true;
  cin >> n;
  vector<ll> a(n);
  vector<ll> c(ma);
  rep(i, n) {
    cin >> a[i];
    c[a[i]]++;
  }
  for (ll i = 2; i < ma; i++) {
    int count = 0;
    for (ll j = i; j < ma; j += i) {
      count += c[j];
    }
    if (count > 1) {
      pc = false;
    }
  }
  if (pc) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  rep(i, n) gc = gcd(gc, a[i]);

  if (gc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}

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

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define mod 1000000007
#define ma 1000005

ll gcd(ll, ll);

int main() {
  ll n;
  ll x;
  ll gc = 0;
  bool pc = true;
  cin >> n;
  vector<ll> a(n);
  vector<ll> c(ma);
  rep(i, n) {
    cin >> a[i];
    c[a[i]]++;
  }
  for (ll i = 2; i < ma; i++) {
    int count = 0;
    for (ll j = i; j < ma; j += i) {
      count += c[j];
    }
    if (count > 1) {
      pc = false;
    }
  }
  if (pc) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  rep(i, n) gc = gcd(gc, a[i]);

  if (gc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;

map<ll, int> prime(ll n) {
  map<ll, int> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      n /= i;
      res[i]++;
    }
  }
  if (n != 1)
    res[n]++;
  return res;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];

  int flag = 1;
  map<ll, int> mp;
  for (int i = 0; i < n; i++) {
    map<ll, int> mp1 = prime(a[i]);
    for (auto x : mp1) {
      if (x.first == 1)
        continue;
      if (mp[x.first] == 1) {
        flag = 0;
        break;
      };
      mp[x.first] = 1;
    }
    if (flag == 0)
      break;
  }
  if (flag == 0) {
    int aa = a[0];
    for (int i = 1; i < n; i++)
      aa = __gcd(aa, a[i]);
    if (aa == 1)
      flag = 2;
  }

  if (flag == 0)
    cout << "not coprime" << endl;
  else if (flag == 1)
    cout << "pairwise coprime" << endl;
  else
    cout << "setwise coprime" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;

map<ll, int> prime(ll n) {
  map<ll, int> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      n /= i;
      res[i]++;
    }
  }
  if (n != 1)
    res[n]++;
  return res;
}

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];

  int flag = 1;
  map<ll, int> mp;
  for (int i = 0; i < n; i++) {
    map<ll, int> mp1 = prime(a[i]);
    for (auto x : mp1) {
      if (x.first == 1)
        continue;
      if (mp[x.first] == 1)
        flag = 0;
      mp[x.first] = 1;
    }
  }
  if (flag == 0) {
    int aa = a[0];
    for (int i = 1; i < n; i++)
      aa = __gcd(aa, a[i]);
    if (aa == 1)
      flag = 2;
  }

  if (flag == 0)
    cout << "not coprime" << endl;
  else if (flag == 1)
    cout << "pairwise 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;
using ll = long long;

const int maxn = 1e6 + 6;
int n, a[maxn];
vector<int> divs[maxn];

bool comp[maxn];

int cnt[maxn];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int gc = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    gc = __gcd(gc, a[i]);
  }
  for (int i = 1; i <= 1000000; i++) {
    divs[i].reserve(30);
  }
  for (int i = 2; i <= 1000000; i++) {
    if (!comp[i]) {
      for (int j = i; j <= 1000000; j += i) {
        divs[j].push_back(i);
        if (j > i)
          comp[i] = true;
      }
    }
  }
  bool isPairwise = true;
  for (int i = 0; i < n; i++) {
    for (auto d : divs[a[i]]) {
      if (++cnt[d] > 1) {
        isPairwise = false;
        break;
      }
    }
  }

  if (isPairwise) {
    cout << "pairwise coprime\\n";
  } else if (gc == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int maxn = 1e6 + 6;
int n, a[maxn];
vector<int> divs[maxn];

bool comp[maxn];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int gc = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    gc = __gcd(gc, a[i]);
  }
  for (int i = 1; i <= 1000000; i++) {
    divs[i].reserve(10);
  }
  for (int i = 2; i <= 1000000; i++) {
    if (!comp[i]) {
      for (int j = i; j <= 1000000; j += i) {
        divs[j].push_back(i);
        if (j > i)
          comp[i] = true;
      }
    }
  }
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    for (auto d : divs[a[i]]) {
      mp[d]++;
    }
  }
  bool isPairwise = true;
  for (auto p : mp) {
    if (p.second > 1) {
      isPairwise = false;
      break;
    }
  }
  if (isPairwise) {
    cout << "pairwise coprime\\n";
  } else if (gc == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}