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

#include <bits/stdc++.h>
using namespace std;
#define l long int
#define ll long long int
vector<int> adj[1000001];
int vis[1000001];
int cnt = 0;
int fact[1000001];
bool temp = false;
void f(int x) {
  if (temp == true)
    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]) {
          temp = true;
          return;
        }
        fact[a]++;
      }
      if (b != 1) {
        if (fact[b]) {
          temp = true;
          return;
        }
        fact[b]++;
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int ele;
    cin >> ele;
    v.push_back(ele);
  }
  int gcd = v[0];
  f(v[0]);
  for (int i = 1; i < n; i++) {
    f(v[i]);
    gcd = __gcd(gcd, v[i]);
  }
  if (!temp) {
    cout << "pairwise coprime"
         << "\\n";
  } else if (gcd == 1) {
    cout << "setwise coprime"
         << "\\n";
  } else {
    cout << "not coprime"
         << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool coprime(int a, int b) { return (__gcd(a, b) == 1); }
int numOfPairs(int arr[], int n) {

  int count = 0;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++)
      if (coprime(arr[i], arr[j]))
        count++;

  return count;
}
int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int total = (n * (n - 1)) / 2;
  if (numOfPairs(arr, n) == total)
    cout << " pairwise coprime"
         << "\\n";
  else {
    int g = __gcd(arr[1], arr[0]);
    for (int j = 2; j < n; j++) {
      g = __gcd(g, arr[j]);
    }
    if (g == 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>
#define endl '\\n'
#define pb push_back
#define mp make_pair
#define LL long long
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
int n, m;
int a[1000010], ans[1000010];
int gcd(int x, int y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
int f[1000010];
int main() {
  cin >> n;
  int sumgcd = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i], sumgcd = gcd(sumgcd, a[i]);
  bool flg = 1;
  for (int i = 1; i <= n; i++) {
    int x = a[i];
    for (int ii = 2; ii * ii <= x && x > 1; ii++) {
      if (x % ii == 0) {
        if (f[ii]) {
          flg = 0;
          break;
        }
        f[ii] = 1;
        while (x % ii == 0)
          x /= ii;
      }
    }
    if (x > 1) {
      if (f[x]) {
        flg = 0;
        break;
      }
      f[x] = 1;
    }
    //		while(x>1){
    ////			cout<<x<<endl;
    //			ii++;
    //		}
    if (flg == 0)
      break;
  }
  if (flg == 1) {
    puts("pairwise coprime");
  } else {
    if (sumgcd == 1)
      puts("setwise coprime");
    else
      puts("not coprime");
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define pb push_back
#define mp make_pair
#define LL long long
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
int n, m;
int a[1000010], ans[1000010];
int gcd(int x, int y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
int f[1000010];
int main() {
  cin >> n;
  int sumgcd = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i], sumgcd = gcd(sumgcd, a[i]);
  bool flg = 1;
  for (int i = 1; i <= n; i++) {
    int x = a[i], ii = 2;
    while (x > 1) {
      //			cout<<x<<endl;
      if (x % ii == 0) {
        if (f[ii]) {
          flg = 0;
          break;
        }
        f[ii] = 1;
        while (x % ii == 0)
          x /= ii;
      }
      ii++;
    }
    if (flg == 0)
      break;
  }
  if (flg == 1) {
    puts("pairwise coprime");
  } else {
    if (sumgcd == 1)
      puts("setwise coprime");
    else
      puts("not coprime");
  }
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 1e6 + 1;
const ll INF = 1e10;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  set<int> s;
  int g = a[0];
  bool cp = true;
  for (int i = 0; i < n; i++) {
    g = __gcd(a[i], g);
    if (!cp)
      continue;
    for (int j = 2; j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        if (s.find(a[i] / j) == s.end() and s.find(j) == s.end()) {
          s.insert(a[i] / j);
          s.insert(j);
        } else {
          cp = false;
          break;
        }
      }
    }
    if (s.find(a[i]) == s.end())
      s.insert(a[i]);
    else if (a[i] != 1)
      cp = false;
  }
  if (cp)
    cout << "pairwise coprime";
  else if (g == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}


Error: TLE

Fixed submission:

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

typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 1e6 + 1;
const ll INF = 1e10;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n, g;
  bool cp = true;
  vector<bool> isPrime(N, true);
  for (int i = 2; i * i < N; i++) {
    if (!isPrime[i])
      continue;
    for (int j = i * i; j < N; j += i)
      isPrime[j] = false;
  }
  map<int, int> cnt;
  for (int i = 2; i < N; i++)
    if (isPrime[i])
      cnt[i] = 0;
  cin >> n;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  g = a[0];
  for (int i = 0; i < n; i++) {
    g = __gcd(g, a[i]);
    if (!cp)
      continue;
    for (auto &j : cnt) {
      if (a[i] % j.first == 0) {
        if (j.second == 0)
          j.second++;
        else {
          // cout<<j.first<<' '<<a[i]<<'\\n';
          cp = false;
          break;
        }
      }
      if (j.first > a[i])
        break;
    }
  }
  if (cp)
    cout << "pairwise coprime";
  else if (g == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<bool> judge(1000000, false);
  bool stop = false;
  for (int i = 0; i < N; i++) {
    int x = A.at(i);
    for (int j = 2; j * j <= A.at(i); j++) {
      if (x % j == 0) {
        if (judge.at(j - 1)) {
          stop = true;
          break;
        } else {
          judge.at(j - 1) = true;
          while (x % j == 0) {
            x /= j;
          }
        }
      }
    }
    int64_t y = x;
    int64_t z = A.at(i);
    if (y * y > z) {
      if (judge.at(x - 1)) {
        stop = true;
      } else {
        judge.at(x - 1) = true;
      }
    }
    if (stop) {
      break;
    }
    if (i == N - 1) {
      cout << "pairwise coprime" << endl;
      return 0;
    }
  }
  int keep = gcd(A.at(0), A.at(1));
  for (int i = 2; i < N; i++) {
    keep = gcd(keep, A.at(i));
  }
  if (keep == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  cout << "not coprime" << endl;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<bool> judge(1000000, false);
  bool stop = false;
  for (int i = 0; i < N; i++) {
    int x = A.at(i);
    for (int j = 2; j <= 1000; j++) {
      if (x % j == 0) {
        if (judge.at(j - 1)) {
          stop = true;
          break;
        } else {
          judge.at(j - 1) = true;
          while (x % j == 0) {
            x /= j;
          }
        }
      }
    }
    if (x > 1000) {
      if (judge.at(x - 1)) {
        stop = true;
      } else {
        judge.at(x - 1) = true;
      }
    }
    if (stop) {
      break;
    }
    if (i == N - 1) {
      cout << "pairwise coprime" << endl;
      return 0;
    }
  }
  int keep = gcd(A.at(0), A.at(1));
  for (int i = 2; i < N; i++) {
    keep = gcd(keep, A.at(i));
  }
  if (keep == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }
  cout << "not coprime" << endl;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep_lr(i, l, r) for (int i = (l); i < (r); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;

ll gcd(ll i, ll j) { return i % j ? gcd(j, i % j) : j; }

vi prime, mnpf;
void preprime(int n) {

  prime.resize(0);
  mnpf.assign(n + 1, 1);
  rep(i, n + 1) {
    if (i < 2)
      continue;
    if (mnpf[i] != 1)
      continue;

    prime.push_back(i);
    mnpf[i] = i;
    int ii = i;
    while (ii <= n) {
      if (mnpf[ii] == 1)
        mnpf[ii] = i;
      ii += i;
    }
  }
}

int main() {

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

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

  preprime(1010100);

  map<int, int> pri;

  rep(i, n) {
    int k = a[i];
    while (k > 1) {
      pri[mnpf[k]]++;
      int memo = mnpf[k];
      while (mnpf[k] == memo)
        k /= mnpf[k];
    }
  }

  bool pairwise = true;
  for (auto x : pri) {
    if (x.second > 1)
      pairwise = false;
  }
  if (pairwise)
    cout << "pairwise coprime" << endl;
  else
    cout << "setwise coprime" << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep_lr(i, l, r) for (int i = (l); i < (r); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;

ll gcd(ll i, ll j) { return i % j ? gcd(j, i % j) : j; }

vi prime, mnpf;
void preprime(int n) {

  prime.resize(0);
  mnpf.assign(n + 1, 1);
  rep(i, n + 1) {
    if (i < 2)
      continue;
    if (mnpf[i] != 1)
      continue;

    prime.push_back(i);
    mnpf[i] = i;
    int ii = i;
    while (ii <= n) {
      if (mnpf[ii] == 1)
        mnpf[ii] = i;
      ii += i;
    }
  }
}

int main() {

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

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

  preprime(n + 1000);

  map<int, int> pri;

  rep(i, n) {
    int k = a[i];
    while (k > 1) {
      pri[mnpf[k]]++;
      int memo = mnpf[k];
      while (mnpf[k] == memo)
        k /= mnpf[k];
    }
  }

  bool pairwise = true;
  for (auto x : pri) {
    if (x.second > 1)
      pairwise = false;
  }
  if (pairwise)
    cout << "pairwise coprime" << endl;
  else
    cout << "setwise coprime" << endl;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

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

struct primegroup {
  vector<int> r;

  primegroup(int n) {
    r = vector<int>(1000001, 0);
    r[1] = n;
  }

  void unite(int x) {
    for (int i = 2; i * i <= x; i++) {
      if (x % i != 0)
        continue;

      while (x % i == 0) {
        x /= i;
      }

      r[i]++;
    }

    if (x != 1)
      r[x]++;
  }

  void judge() {
    int maxval = 0;

    for (auto i = 2; i < r.size(); i++) {
      if (r[i] > 0)
        maxval = max(maxval, r[i]);
    }

    if (maxval <= 1)
      cout << "pairwise coprime" << endl;
    else if (maxval != r[1])
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  }
};

int main() {
  int n;
  cin >> n;
  primegroup pg(n);

  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    pg.unite(m);
  }

  pg.judge();

  return 0;
}


Error: WA

Fixed submission:

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

struct primegroup {
  vector<int> r;

  primegroup(int n) { r = vector<int>(1000001, 0); }

  bool unite(int x) {
    r[1]++;
    for (int i = 2; i * i <= x; i++) {
      if (x % i != 0)
        continue;

      while (x % i == 0) {
        x /= i;
      }

      r[i]++;
    }

    if (x != 1)
      r[x]++;
  }

  void judge() {
    int maxval = 0;

    for (int i = 2; i < r.size(); i++) {
      if (r[i] > 0)
        maxval = max(maxval, r[i]);
    }

    if (maxval == 1)
      cout << "pariwse coprime" << endl;
    else if (maxval != r[1])
      cout << "setwise coprime" << endl;
    else
      cout << "no coprime" << endl;
  }
};

int main() {
  int n;
  cin >> n;
  primegroup pg(n);

  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    pg.unite(m);
  }

  pg.judge();

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

#include <cstdio>
#include <iostream>
using namespace std;
int t[1000001], p[1000001], cnt = 0;
int a[1000001];
int c[1000001];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &t[i]);
  for (int i = 1; i <= 1000; i++)
    a[i] = i;
  for (int i = 2; i <= 1000; i++) {
    if (a[i] == i)
      p[++cnt] = i;
    for (int j = 1; j <= cnt && p[j] <= a[i] && i * p[j] <= 1000; j++)
      a[i * p[j]] = p[j];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= cnt && p[j] <= t[i]; j++)
      if (t[i] % p[j] == 0) {
        c[p[j]]++;
        while (t[i] % p[j] == 0)
          t[i] /= p[j];
      }
    if (t[i] != 1)
      c[t[i]]++;
  }
  bool f1 = true, f2 = true;
  for (int i = 1; i <= 1000000; i++) {
    if (c[i] > 1)
      f1 = false;
    if (c[i] == n)
      f2 = false;
  }
  if (f1)
    cout << "pairwise coprime";
  else if (f2)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int t[1000001], p[1000001], cnt = 0;
int a[1000001];
int c[1000001];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &t[i]);
  for (int i = 1; i <= 1000000; i++)
    a[i] = i;
  for (int i = 2; i <= 1000000; i++) {
    if (a[i] == i)
      p[++cnt] = i;
    for (int j = 1; j <= cnt && p[j] <= a[i] && i * p[j] <= 1000000; j++)
      a[i * p[j]] = p[j];
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= cnt && p[j] <= t[i]; j++)
      if (t[i] % p[j] == 0) {
        c[j]++;
        while (t[i] % p[j] == 0)
          t[i] /= p[j];
      }
  bool f1 = true, f2 = true;
  for (int i = 1; i <= cnt; i++) {
    if (c[i] > 1)
      f1 = false;
    if (c[i] == n)
      f2 = false;
  }
  if (f1)
    cout << "pairwise coprime";
  else if (f2)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const int MAX = 1000005;
const int mod = 1000000007;
/* global variables */
vector<int> cnt(MAX, 0);
vector<int> a;
int N;
/* function */
int gcd(int a, int b) { return (a % b) ? gcd(b, a % b) : b; }
bool isPc1() {
  for (int i = 0; i < N; i++) {
    cnt[a[i]]++;
  }
  vector<bool> isPrime(MAX, true);
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < MAX; i++) {
    int num = cnt[i];
    if (isPrime[i]) {
      for (int j = i + i; j < MAX; j += i) {
        isPrime[j] = false;
        num += cnt[j];
      }
    }
    if (num >= 2)
      return false;
  }
  return true;
}
bool isSc() {
  int ret = 0;
  for (int i = 0; i < N; i++) {
    ret = gcd(ret, a[i]);
  }
  if (ret == 1)
    return true;
  else
    return false;
}
/* main */
int main() {
  cin >> N;
  a.resize(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];

  if (isPc1()) {
    cout << "pairwise coprime" << '\\n';
  } else if (isSc()) {
    cout << "setwise coprime" << '\\n';
  } else {
    cout << "not coprime" << '\\n';
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
// const int MAX = 1000005;
const int MAX = 60;
const int mod = 1000000007;
/* global variables */
vector<int> cnt(MAX, 0);
vector<int> a;
int N;
/* function */
int gcd(int a, int b) { return (a % b) ? gcd(b, a % b) : b; }
bool isPc1() {
  for (int i = 0; i < N; i++) {
    cnt[a[i]]++;
  }
  vector<bool> isPrime(MAX, true);
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < MAX; i++) {
    int num = cnt[i];
    if (isPrime[i]) {
      for (int j = i + i; j < MAX; j += i) {
        isPrime[j] = false;
        num += cnt[j];
      }
    }
    if (num >= 2)
      return false;
  }
  return true;
}
bool isSc() {
  int ret = 0;
  for (int i = 0; i < N; i++) {
    ret = gcd(ret, a[i]);
  }
  if (ret == 1)
    return true;
  else
    return false;
}
/* main */
int main() {
  cin >> N;
  a.resize(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];

  if (isPc1()) {
    cout << "pairwise coprime" << '\\n';
  } else if (isSc()) {
    cout << "setwise coprime" << '\\n';
  } else {
    cout << "not coprime" << '\\n';
  }
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

// Youtube解説AC
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using ll = long long;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
// const ll INF = 1000000000000000000LL;

const int A = 1000005;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  bool have_mod = true;
  while (have_mod) {
    ll mod = a % b;
    if (mod == 0)
      have_mod = false;
    a = b;
    b = mod;
  }
  return a;
}

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: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
using ll = long long;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
// const ll INF = 1000000000000000000LL;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  bool have_mod = true;
  while (have_mod) {
    ll mod = a % b;
    if (mod == 0)
      have_mod = false;
    a = b;
    b = mod;
  }
  return a;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int nax = -INF;
  rep(i, n) nax = max(nax, a[i]);
  bool con = true;
  bool flag1 = true; // setwise conprime
  bool flag2 = true; // pairwise conprime
  int cur_nax = nax;
  while (cur_nax > 1) {
    int cnt = 0;
    bool div = true;
    rep(i, n) {
      if (a[i] % cur_nax != 0)
        div = false;
      else
        cnt++;
      if (div == false && flag2 == false)
        break;
    }
    if (cnt > 1)
      flag2 = false;
    if (div) {
      flag1 = false;
      break;
    }
    cur_nax--;
    if (flag1 == false && flag2 == false)
      break;
  }
  if (flag2)
    cout << "pairwise coprime" << endl;
  else if (flag1)
    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;

constexpr int MAX = 1e6;

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

int main() {
  int n;
  cin >> n;

  vector<int> a(n);
  map<int, int> count;
  for (int &ai : a) {
    cin >> ai;
    ++count[ai];
  }

  vector<bool> p(MAX + 1, true);
  bool pairwise = true;
  for (int i = 2; i <= MAX; ++i) {
    if (!p[i]) {
      continue;
    }

    int k = count[i];
    for (int j = i * 2; j <= MAX; j += i) {
      p[j] = false;
      k += count[j];
    }

    if (k > 1) {
      pairwise = false;
      break;
    }
  }

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

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

  if (g == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime";

  return 0;
}


Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  cin >> n;

  vector<int> a(n);
  for (int &ai : a) {
    cin >> ai;
  }

  map<int, int> count;
  for (int i = 0; i < n; ++i) {
    int ai = a[i];
    for (int k = 2; k <= ai; ++k) {
      if (ai % k == 0) {
        ++count[k];
      }

      while (ai % k == 0) {
        ai /= k;
      }
    }
  }

  bool p = true;
  for (pair<int, int> c : count) {
    if (c.second != 1) {
      p = false;
      break;
    }
  }

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

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

  if (g == 1) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime";

  return 0;
}