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

#include "bits/stdc++.h"

using namespace std;
const int N = 1e6 + 9;

vector<int> prime;
bool flag[N + 100];

void seive() {
  for (int i = 4; i < N; i += 2)
    flag[i] = true;
  for (int i = 3; i * i < N; i += 2) {
    if (!flag[i]) {
      for (int j = i * 3; j < N; j += i * 2)
        flag[j] = true;
    }
  }
  prime.push_back(2);
  for (int i = 3; i <= N; i += 2) {
    if (!flag[i])
      prime.push_back(i);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  int a[n];
  for (int &i : a)
    cin >> i;
  seive();
  // for(int i=0;i<20;i++)cout<<prime[i]<<" ";
  // cout<<endl;
  map<int, int> cnt;

  for (int i : a) {
    int temp = i;

    for (int x : prime) {
      if (x * x > temp)
        break;
      if (temp % x == 0) {
        cnt[x]++;
        while (temp % x == 0)
          temp /= x;
      }
    }
    if (temp > 1)
      cnt[temp]++;
  }
  bool flag = true;

  for (auto [x, y] : cnt) {
    if (y > 1) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int g = a[0];
  for (int i = 1; i < n; i++)
    g = __gcd(g, a[i]);
  if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
const int N = 1e6 + 9;

vector<int> prime;
bool flag[N];

void seive() {
  for (int i = 4; i < N; i += 2)
    flag[i] = true;
  for (int i = 3; i * i < N; i += 2) {
    if (!flag[i]) {
      for (int j = i * 3; j < N; j += i * 2)
        flag[j] = true;
    }
  }
  prime.push_back(2);
  for (int i = 3; i <= N; i += 2) {
    if (!flag[i])
      prime.push_back(i);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  int a[n];
  for (int &i : a)
    cin >> i;
  seive();
  // for(int i=0;i<20;i++)cout<<prime[i]<<" ";
  // cout<<endl;
  map<int, int> cnt;

  for (int i : a) {
    int temp = i;

    for (int x : prime) {
      if (x * x > temp)
        break;
      if (temp % x == 0) {
        cnt[x]++;
        while (temp % x == 0)
          temp /= x;
      }
    }
    if (temp > 1)
      cnt[temp]++;
  }
  bool flag = true;

  for (auto [x, y] : cnt) {
    if (y > 1) {
      flag = false;
      break;
    }
  }
  if (flag) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int g = a[0];
  for (int i = 1; i < n; i++)
    g = __gcd(g, a[i]);
  if (g == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INFTY 10000000000
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
const int MOD = 1000000007;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, -1, 0, 1};
const int INF = 1e9;
const int A = 1000005;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> dd(A, 0);
  rep(i, n) {
    cin >> a[i];
    dd[a[i]]++;
  }
  int sai = 0;

  bool pairwise = true;
  for (int i = 2; i < A; i++) {
    int cnt = 0;
    for (int j = i; j < A; j += i) {
      cnt += dd[j];
    }
    if (cnt > 1) {
      pairwise = false;
    }
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  rep(i, n) { sai = __gcd(sai, a[i]); }
  if (sai == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INFTY 10000000000
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
const int MOD = 1000000007;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, -1, 0, 1};
const int INF = 1e9;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> dd(1000010, 0);
  int pp = 1, sp = 0;
  int sai = a[0];
  rep(i, n) {
    sai = __gcd(sai, a[i]);
    for (int j = 1; j * j <= a[i]; j++) {
      if (a[i] % j != 0)
        continue;
      int div = a[i] / j;
      dd[1] = 0;
      if (dd[j] != 0 || dd[div] != 0) {
        pp = 0;
        // cout<<"i"<<j<<div<<endl;
      } else {
        dd[j] = 1;
        dd[div] = 1;
        // cout<<"e"<<j<<div<<endl;
      }
    }
  }
  if (pp)
    cout << "pairwise coprime" << endl;
  else if (sai == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 5;
int vis[maxn], prime[maxn], notPrime[maxn], num[maxn];
int total, n, maxx = 0, ans = 0, tmp;
void getPrime(int sum) {
  for (int i = 2; i <= sum; i++) {
    if (!notPrime[i]) {
      prime[++total] = i;
    }
    for (int j = 1; prime[j] * i <= sum; j++) {
      notPrime[i * prime[j]] = prime[j];
      if (i % prime[j] == 0)
        break;
    }
  }
}

void getInit() {
  memset(vis, 0, sizeof(vis));
  memset(notPrime, 0, sizeof(notPrime));
  memset(vis, 0, sizeof(vis));
}

int main() {
  getInit();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    maxx = max(maxx, num[i]);
  }
  getPrime(maxx);
  for (int i = 1; i <= n; i++) {
    while (notPrime[num[i]]) {
      tmp = notPrime[num[i]];
      vis[tmp]++;
      ans = max(ans, vis[tmp]);
      while (num[i] % tmp == 0)
        num[i] /= tmp;
    }
    if (num[i] != 1) {
      vis[num[i]]++;
      ans = max(ans, vis[num[i]]);
    }
  }
  if (ans <= 1)
    printf("pairwise coprime\\n");
  else if (ans < n)
    printf("setwise coprime\\n");
  else
    printf("not coprime\\n");
  getchar();
  getchar();
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define file()                                                                 \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define tt                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define nl cout << "\\n";
#define sp cout << " ";
#define rep(i, a, b) for (long long i = a; i < b; i++)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
double PI = 4 * atan(1);

int main() {
  optimize();

  int n;
  cin >> n;

  ll a[n];
  bool p = 1;

  cin >> a[0];
  ll g = a[0];

  for (int i = 1; i < n; i++) {
    cin >> a[i];
    g = __gcd(g, a[i]);

    if (__gcd(a[i], a[i - 1]) > 1)
      p = 0;
  }

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

  for (int i = 0; i < n && p == 1; i++) {
    for (int j = i + 1; j < n && p == 1; j++) {
      if (__gcd(a[i], a[j]) > 1) {
        p = 0;
        break;
      }
    }
  }

  if (p == 1)
    cout << "pairwise coprime\\n";

  else
    cout << "setwise coprime\\n";

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

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

int used_prime[1'000'001];
bool is_prime[1'000'001];
vector<int> prime_list;

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

int main(void) {
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i < 1001; i++)
    is_prime[i] = true;
  for (int i = 2; i < 1001; i++) {
    if (is_prime[i] == false)
      continue;
    prime_list.push_back(i);
    for (int j = 2; i * j < 1001; j++)
      is_prime[i * j] = false;
  }
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int tmp = 0;
  for (int i = 0; i < n; i++)
    tmp = gcd(a[i], tmp);
  bool flag0 = false;
  if (tmp == 1)
    flag0 = true;
  bool flag = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < prime_list.size(); j++) {
      if (a[i] < prime_list[j])
        break;
      if (a[i] % prime_list[j] == 0) {
        if (used_prime[prime_list[j]])
          flag = false;
        used_prime[prime_list[j]] = true;
        while (a[i] > 0 && a[i] % prime_list[j] == 0)
          a[i] /= prime_list[j];
      }
    }
    if (a[i] > 1) {
      if (used_prime[a[i]])
        flag = false;
      used_prime[a[i]] = true;
    }
  }
  if (flag)
    cout << "pairwise coprime" << endl;
  else if (flag0)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: 0

Fixed submission:

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

int used_prime[1001];
bool is_prime[1'000'001];
vector<int> prime_list;

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

int main(void) {
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i < 1001; i++)
    is_prime[i] = true;
  for (int i = 2; i < 1001; i++) {
    if (is_prime[i] == false)
      continue;
    prime_list.push_back(i);
    for (int j = 2; i * j < 1001; j++)
      is_prime[i * j] = false;
  }
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int tmp = 0;
  for (int i = 0; i < n; i++)
    tmp = gcd(a[i], tmp);
  bool flag0 = false;
  if (tmp == 1)
    flag0 = true;
  bool flag = true;
  // 10^6 * 8 * 10^4
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < prime_list.size(); j++) {
      if (a[i] < prime_list[j])
        break;
      if (a[i] % prime_list[j] == 0) {
        if (used_prime[prime_list[j]])
          flag = false;
        used_prime[prime_list[j]] = true;
        while (a[i] > 0 && a[i] % prime_list[j] == 0)
          a[i] /= prime_list[j];
      }
    }
    if (a[i] > 1) {
      if (used_prime[a[i]])
        flag = false;
      used_prime[a[i]] = true;
    }
  }
  if (flag)
    cout << "pairwise coprime" << endl;
  else if (flag0)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int mxA = 1e6;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  vector<int> count(mxA + 1);
  int n;
  cin >> n;
  vector<int> a(n);
  bool pairwise = true;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    count[a[i]]++;
  }
  vector<bool> is_prime(mxA + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 0; i <= mxA; i++) {
    if (is_prime[i]) {
      int cnt = 0;
      for (int j = i; j <= mxA; j += i) {
        cnt += count[j];
        is_prime[j] = false;
      }
      if (cnt >= 2) {
        pairwise = false;
      }
    }
  }
  if (pairwise) {
    cout << "pairwise coprime";
    return 0;
  }
  int gcd = 0;
  for (int i = 0; i < n; i++) {
    gcd = __gcd(gcd, a[i]);
  }
  if (gcd == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}


Error: TLE

Fixed submission:

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

const int mxA = 1e6;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> primes;
  vector<bool> is_prime(mxA + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 0; i <= mxA; i++) {
    if (is_prime[i]) {
      primes.push_back(i);
      for (int j = 2 * i; j <= mxA; j += i) {
        is_prime[j] = false;
      }
    }
  }
  bool pairwise = true;
  for (int p : primes) {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      cnt += a[i] % p == 0;
    }
    if (cnt >= 2) {
      pairwise = false;
      break;
    }
  }
  if (pairwise) {
    cout << "pairwise coprime";
    return 0;
  }
  int gcd = 0;
  for (int i = 0; i < n; i++) {
    gcd = __gcd(gcd, a[i]);
  }
  if (gcd == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

// 最大公約数を計算
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

// 素因数を列挙
vector<pair<ll, ll>> enumeratePrimeFactors(ll n) {
  vector<pair<ll, ll>> res;
  for (ll a = 2; a * a <= n; a++) {
    if (n % a != 0) {
      continue;
    }
    ll ex = 0; // 指数
    // 割れるだけ割る
    while (n % a == 0) {
      ++ex;
      n /= a;
    }
    res.emplace_back(a, ex);
  }
  // nが一度も割られなかった場合
  if (n != 1) {
    res.emplace_back(n, 1);
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  bool pairwiseCoprime = true;
  vector<vector<pair<ll, ll>>> d(n);
  rep(i, n) { d.at(i) = enumeratePrimeFactors(a.at(i)); }
  set<ll> s;
  rep(i, n) {
    rep(j, d.at(i).size()) {
      ll dd = d.at(i).at(j).first;
      if (s.count(dd)) {
        pairwiseCoprime = false;
        break;
      } else {
        s.insert(dd);
      }
    }
    if (!pairwiseCoprime) {
      break;
    }
  }
  ll gcdSum = 0;
  rep(i, n) { gcdSum = gcd(gcdSum, a.at(i)); }
  bool setwiseCoprime = gcdSum == 1;
  if (pairwiseCoprime) {
    cout << "pairwise coprime" << endl;
  } else if (setwiseCoprime) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)


Fixed submission:

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

// 最大公約数を計算
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  bool pairwiseCoprime = true;
  rep(i, n - 1) {
    pairwiseCoprime = gcd(a.at(i), a.at(i + 1)) == 1;
    if (!pairwiseCoprime) {
      break;
    }
  }
  if (pairwiseCoprime) {
    rep(i, n - 1) {
      pairwiseCoprime = gcd(a.at(n - 1), a.at(i)) == 1;
      if (!pairwiseCoprime) {
        break;
      }
    }
  }
  if (pairwiseCoprime) {
    int t = 3;
    rep(i, n) {
      if (i == t) {
        continue;
      }
      pairwiseCoprime = gcd(a.at(i), a.at(t)) == 1;
      if (!pairwiseCoprime) {
        break;
      }
    }
  }
  ll gcdSum = 0;
  rep(i, n) { gcdSum = gcd(gcdSum, a.at(i)); }
  bool setwiseCoprime = gcdSum == 1;
  if (pairwiseCoprime) {
    cout << "pairwise coprime" << endl;
  } else if (setwiseCoprime) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
const int N = 1e6 + 10;
int const mod = 1e9 + 7;
using namespace std;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int n, x, vis[N], cnt[N], cntm = 0;

int main() {
  cin >> n;
  bool tmam = 1;
  int GCD = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    cnt[x]++;
    GCD = gcd(GCD, x);
  }
  for (int i = 2; i < N; i++) {
    cntm = 0;
    if (vis[i] == 0) {
      for (int j = i; j < N; j += i) {
        vis[j] = 1;
        cntm += cnt[j];
      }
      if (cntm > 1)
        tmam = 0;
    }
    if (!tmam)
      break;
  }
  if (tmam)
    cout << "pairwise coprime"
         << "\\n";
  else if (!tmam && GCD == 1)
    cout << "setwise coprime"
         << "\\n";
  else
    cout << "not coprime"
         << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int arr[1000007];

int main() {
  int n, minn = 99999999, flag = 0, pairwise = 1, setwise = 1, i, j;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    // minn = min ( arr[i], minn ) ;
  }
  sort(arr, arr + n);
  for (i = 2; i <= arr[n - 2]; i++) {
    for (j = 0; j < n; j++) {
      if (arr[j] % i == 0) {
        flag++;
      }
    }
    if (flag > 1 && flag < n) {
      pairwise = 0;
    } else if (flag == n) {
      pairwise = 0;
      setwise = 0;
      break;
    }
    flag = 0;
  }
  if (pairwise) {
    printf("pairwise coprime");
  } else if (setwise) {
    printf("setwise coprime");
  } else {
    printf("not coprime");
  }
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int mx = 1e6;
  vector<int> z(mx + 1);
  for (int i = 2; i * i <= mx; i++) {
    if (!z[i]) {
      for (int j = i * i; j <= mx; j += i) {
        z[j] = 1;
      }
    }
  }
  vector<int> p;
  for (int i = 2; i <= mx; i++) {
    if (!z[i]) {
      p.push_back(i);
    }
  }
  int s = p.size(), g = a[0];
  vector<int> c(mx + 1);
  bool f = true;
  for (int i = 0; i < n; i++) {
    g = __gcd(g, a[i]);
    for (int j = 0; j < s && p[j] * p[j] <= a[i]; j++) {
      if (a[i] % p[j] == 0) {
        if (c[p[j]]) {
          f = false;
        }
        c[p[j]] = 1;
        while (a[i] % p[j] == 0) {
          a[i] /= p[j];
        }
      }
    }
    if (a[i] > 1) {
      if (c[a[i]]) {
        f = false;
      }
      c[a[i]] = 1;
    }
  }
  cout << (f        ? "pairwise coprime"
           : g == 1 ? "setwise coprime"
                    : "not coprime")
       << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int mx = 1e6;
  vector<int> z(mx + 1);
  for (int i = 2; i * i <= mx; i++) {
    if (!z[i]) {
      for (int j = i * i; j <= mx; j += i) {
        z[j] = 1;
      }
    }
  }
  vector<int> p;
  for (int i = 2; i <= mx; i++) {
    if (!z[i]) {
      p.push_back(i);
    }
  }
  int s = p.size(), g = a[0];
  vector<int> c(s);
  bool f = true;
  for (int i = 0; i < n; i++) {
    g = __gcd(g, a[i]);
    for (int j = 0; j < s && p[j] * p[j] <= a[i]; j++) {
      if (a[i] % p[j] == 0) {
        if (c[j]) {
          f = false;
        }
        c[j] = 1;
        while (a[i] % p[j] == 0) {
          a[i] /= p[j];
        }
      }
    }
    if (a[i] > 1) {
      if (c[a[i]]) {
        f = false;
      }
      c[a[i]] = 1;
    }
  }
  cout << (f        ? "pairwise coprime"
           : g == 1 ? "setwise coprime"
                    : "not coprime")
       << endl;
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1e6 + 10;
int c[N], sieve[N];
vector<int> primes;

int main() {
  int n, a;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    c[a]++;
  }
  for (int i = 2; i < N; i++) {
    if (sieve[i])
      continue;
    for (int j = 2 * i; j < N; j += i) {
      sieve[j] = 1;
    }
  }
  for (int i = 2; i < N; i++) {
    if (!sieve[i])
      primes.push_back(i);
  }
  bool pairwise = 1;
  for (auto p : primes) {
    int cnt = 0;
    for (int i = p; i < N; i += p)
      cnt += c[i];
    if (cnt > 1) {
      pairwise = 0;
      break;
    }
  }
  if (pairwise) {
    puts("pairwise coprime");
    return 0;
  }
  bool setwise = 1;
  for (auto p : primes) {
    int cnt = 0;
    for (int i = p; i < N; i += p)
      cnt += c[i];
    if (cnt == n) {
      setwise = 0;
      break;
    }
  }
  if (setwise)
    puts("setwise coprime");
  else
    puts("not coprime");
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1e6 + 10;
int c[N], sieve[N];
vector<int> primes;

int main() {
  int n, a;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    c[a]++;
  }
  for (int i = 2; i <= N; i++) {
    if (sieve[i])
      continue;
    for (int j = 2 * i; j <= N; j += i) {
      sieve[j] = 1;
    }
  }
  for (int i = 2; i <= N; i++) {
    if (!sieve[i])
      primes.push_back(i);
  }
  // check if there is a pair with GCD != 1
  bool pairwise = 1;
  for (int i = 2; i <= N; i++) {
    if (c[i] > 1) {
      pairwise = 0;
      break;
    }
    int cnt = 0;
    for (auto p : primes) {
      int e = i * p;
      if (e > N)
        break;
      if (c[e])
        cnt++;
      if (cnt > 1) {
        pairwise = 0;
        break;
      }
    }
    if (!pairwise)
      break;
  }
  if (pairwise) {
    puts("pairwise coprime");
    return 0;
  }
  bool setwise = 1;
  for (auto p : primes) {
    int sum = c[1];
    for (int i = p; i <= N; i += p)
      sum += c[i];
    if (sum == n) {
      setwise = 0;
      break;
    }
  }
  if (setwise)
    puts("setwise coprime");
  else
    puts("not coprime");
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int n, allgcd = -1, cnt[1000010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n) {
    int a;
    cin >> a;
    cnt[a]++;
    if (i == 0)
      allgcd = a;
    else
      allgcd = gcd(allgcd, a);
  }
  bool f = true;
  for (int i = 2; i <= 1000000; i++) {
    int sum = 0;
    for (int j = i; j <= 1000000; j += i) {
      sum += cnt[j];
    }
    if (sum >= 2) {
      f = false;
      break;
    }
  }
  if (f) {
    puts("pairwise coprime");
  } else if (allgcd == 1) {
    puts("setwise coprime");
  } else
    puts("not coprime");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int n, allgcd = -1;
multiset<int> st;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n) {
    int a;
    cin >> a;
    st.insert(a);
    if (i == 0)
      allgcd = a;
    else
      allgcd = gcd(allgcd, a);
  }
  bool f = true;
  for (int i = 2; i <= 1000000; i++) {
    int cnt = 0;
    for (int j = i; j <= 1000000; j += i) {
      cnt += st.count(j);
    }
    if (cnt >= 2) {
      f = false;
      break;
    }
  }
  if (f) {
    puts("pairwise coprime");
  } else if (allgcd == 1) {
    puts("setwise coprime");
  } else
    puts("not coprime");
  return 0;
}