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

#include <bits/stdc++.h>

using namespace std;

const int N = 2000010;

int a[N], st[N];

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

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

int main() {
  int n;
  cin >> n;
  int sgcd;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (i == 1)
      sgcd = a[i];
    sgcd = gcd(a[i], sgcd);
    ++st[a[i]];
  }
  bool F = true, S = sgcd == 1;
  for (int i = 2; i < N; ++i) {
    int sum = 0;
    for (int j = i; j < N && F; j += i)
      sum += st[j];
    if (sum > 1)
      F = false;
  }
  if (F)
    puts("pairwise coprime");
  else if (S)
    puts("setwise coprime");
  else
    puts("not coprime");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 2000010;

int a[N], st[N];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    int t = a[i];
    for (int j = 2; j <= t; ++j) {
      if (t % j == 0) {
        ++st[j];
        while (t % j == 0)
          t /= j;
      }
    }
  }
  bool F = true, S = true;
  for (int i = 2; i <= N; ++i) {
    if (st[i] > 1)
      F = false;
    if (st[i] >= n)
      S = false;
  }
  if (F)
    puts("pairwise coprime");
  else if (S)
    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;

const int MAXN = 1e6 + 9;

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<vector<pair<int, int>>> primes(MAXN);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  vector<int> spf(MAXN);
  for (int i = 2; i < MAXN; i++) {
    spf[i] = i;
  }

  for (int i = 4; i < MAXN; i += 2) {
    spf[i] = 2;
  }

  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }

  for (int i = 1; i < MAXN; i++) {
    unordered_map<int, int> pfreq;
    int x = i;
    while (x > 1) {
      pfreq[spf[x]]++;
      x = x / spf[x];
    }

    for (pair<int, int> p : pfreq) {
      primes[i].push_back(p);
    }
  }

  vector<bool> seen_prime(MAXN);
  vector<int> times(MAXN);

  bool flag = true;
  for (int i = 0; i < n; i++) {
    for (pair<int, int> p : primes[v[i]]) {
      if (seen_prime[p.first]) {
        flag = false;
        break;
      } else {
        seen_prime[p.first] = true;
      }
    }
  }

  if (flag) {
    cout << "pairwise coprime\\n";
  } else {
    bool check = true;
    for (int i = 0; i < n; i++) {
      for (pair<int, int> p : primes[v[i]]) {
        times[p.first]++;
      }
    }

    for (int p : times) {
      if (p >= n) {
        check = false;
        break;
      }
    }

    if (check) {
      cout << "setwise coprime\\n";
    } else {
      cout << "not coprime\\n";
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void factor(int x, vector<vector<pair<int, int>>> &primes) {
  int y = x;
  for (int i = 2; i * i <= x; i++) {
    int count = 0;
    while (x % i == 0) {
      x /= i;
      count++;
    }

    if (count > 0) {
      primes[y].push_back({i, count});
    }
  }

  if (x != 1) {
    primes[y].push_back({x, 1});
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<vector<pair<int, int>>> primes(1e6 + 5);
  vector<bool> seen_prime(1e6 + 5);
  vector<int> times(1e6 + 5);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    factor(v[i], primes);
  }

  bool flag = true;
  for (int i = 0; i < n; i++) {
    for (pair<int, int> p : primes[v[i]]) {
      if (seen_prime[p.first]) {
        flag = false;
        break;
      } else {
        seen_prime[p.first] = true;
      }
    }
  }

  if (flag) {
    cout << "pairwise coprime\\n";
  } else {
    bool check = true;
    for (int i = 0; i < n; i++) {
      for (pair<int, int> p : primes[v[i]]) {
        times[p.first]++;
      }
    }

    for (int p : times) {
      if (p >= n) {
        check = false;
        break;
      }
    }

    if (check) {
      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;

using vi = vector<int>;
using ii = pair<int, int>;

#define MAX 1000010

const int MOD = 1e9 + 7;

using ll = long long;

bitset<MAX> prime, used;

vi fact;

void sieve(int n) {
  prime.set();
  int i;
  for (i = 2; i * i <= n; i++) {
    if (prime[i]) {
      fact[i] = i;
      for (int j = i * i; j <= n; j += i) {
        prime[j] = 0;
        if (fact[j] == -1)
          fact[j] = i;
      }
    }
  }

  for (; i <= n; i++)
    if (prime[i])
      fact[i] = i;
}

int main() {
  int n;
  scanf("%d", &n);
  vi v(n);

  int mx = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
    mx = max(mx, v[i]);
  }

  fact.resize(mx + 1, -1);
  sieve(mx + 1);

  bool pairwise = true;
  int gcd;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      gcd = v[i];
    else if (gcd > 1)
      gcd = __gcd(gcd, v[i]);

    if (pairwise) {
      while (v[i] > 1) {
        int &p = fact[v[i]];

        if (used[p]) {
          pairwise = false;
          break;
        }

        used[p] = true;
        while (v[i] % p == 0)
          v[i] /= p;
      }
    } else if (gcd == 1)
      break;
  }

  if (pairwise)
    printf("pairwise coprime\\n");
  else if (gcd == 1)
    printf("setwise coprime\\n");
  else
    printf("not coprime\\n");

  return 0;
}


Error: TLE

Fixed submission:

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

using vi = vector<int>;
using ii = pair<int, int>;

#define MAX 1000010

const int MOD = 1e9 + 7;

using ll = long long;

bitset<MAX> prime;

vi primes;

void sieve(int n) {
  primes.push_back(2);
  prime.set();

  int i;
  for (i = 3; i * i <= n; i += 2) {
    if (prime[i]) {
      primes.push_back(i);
      for (int j = i * i; j <= n; j += i)
        prime[j] = 0;
    }
  }

  for (; i <= n; i += 2)
    if (prime[i])
      primes.push_back(i);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  vi v(n);

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

  sieve(mx + 1);

  vector<char> used(primes.size(), false);

  bool pairwise = true;
  int gcd;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      gcd = v[i];
    else if (gcd > 1)
      gcd = __gcd(gcd, v[i]);

    if (!pairwise) {
      if (gcd == 1)
        break;
      continue;
    }

    for (int j = 0; v[i] > 1 && j < (int)primes.size(); j++) {
      int &p = primes[j];
      if (v[i] % p == 0) {
        if (used[j]) {
          pairwise = false;
          break;
        }
        used[j] = true;
      }

      while (v[i] > 1 && v[i] % p == 0)
        v[i] /= p;
    }
  }

  if (pairwise)
    cout << "pairwise coprime" << endl;
  else if (gcd == 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;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

// MOD確認

ll N;
ll B[1000010];

int main() {
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  rep(i, N) {
    ll n = A[i];
    for (ll j = 2; j * j <= n; j++) {
      if (n % j == 0)
        B[j]++;
      while (n % j == 0) {
        n /= j;
      }
    }
    if (n != 1)
      B[n]++;
  }
  bool flag = 1;
  rep(i, 1000010) if (B[i] > 1) flag = 0;
  if (flag)
    cout << "pairwise coprime" << endl;
  else {
    ll G = __gcd(A[0], A[1]);
    rep(i, N - 1) { G = __gcd(G, A[i + 1]); }
    if (G == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

// MOD確認

ll N;
ll A[1000010];

int main() {
  cin >> N;
  rep(i, N) cin >> A[i];
  map<ll, ll> mp;
  rep(i, N) {
    ll n = A[i];
    for (ll j = 2; j * j <= n; j++) {
      if (n % j == 0)
        mp[j]++;
      while (n % j == 0) {
        n /= j;
      }
    }
    if (n != 1)
      mp[n]++;
  }
  bool flag = 1;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    if (itr->second > 1)
      flag = 0;
  }
  if (flag)
    cout << "pairwise coprime" << endl;
  else {
    ll G = __gcd(A[0], A[1]);
    rep(i, N - 1) { G = __gcd(G, A[i + 1]); }
    if (G == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  }
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
#include <vector>

using ll = long long;
ll n;
const ll MAX = 1000000;

// table[x]:=xの最小の素因数
std::vector<ll> table(MAX + 1, 1);

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

void initTable() {
  table[0] = 0;
  table[1] = 0;
  // init
  for (int i = 2; i <= MAX; i++)
    table[i] = i;

  for (ll i = 2; i * i <= MAX; i++) {
    if (table[i] == i) {
      for (ll j = i * i; j <= MAX; j += i) {
        table[j] = i;
      }
    }
  }
}

int main() {
  initTable();
  std::vector<ll> cnt(MAX + 1, 0);
  ll maxCnt = 0;

  std::cin >> n;
  std::vector<ll> a(n);
  std::cin >> a[0];
  ll g = a[0];
  while (table[a[0]] > 1) {
    ll d = table[a[0]];
    while (a[0] % d == 0) {
      a[0] /= d;
    }
    cnt[d]++;
  }

  for (int i = 1; i < n; i++) {
    std::cin >> a[i];
    g = gcd(g, a[i]);
    while (table[a[i]] > 1) {
      ll d = table[a[i]];
      while (a[i] % d == 0) {
        a[i] /= d;
      }
      cnt[d]++;
      maxCnt = std::max(maxCnt, cnt[d]);
    }
  }

  if (maxCnt >= 2 && g > 1) {
    std::cout << "not coprime" << std::endl;
  } else if (maxCnt >= 2 && g == 1) {
    std::cout << "setwise coprime" << std::endl;
  } else {
    std::cout << "pairwise coprime" << std::endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>

using ll = long long;
ll n;
const ll MAX = 1000000;

// D[x]:=xの最小の素因数
std::vector<ll> table(MAX + 1, 1);

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

void initTable() {
  table[0] = 0;
  table[1] = 0;
  for (ll i = 2; i * i <= MAX; i++) {
    if (table[i]) {
      table[i] = i;
      for (ll j = i * i; j <= MAX; j += i) {
        table[j] = i;
      }
    }
  }
}

int main() {
  initTable();
  std::vector<ll> cnt(MAX + 1, 0);
  ll maxCnt = 0;

  std::cin >> n;
  std::vector<ll> a(n);
  std::cin >> a[0];
  ll g = a[0];
  while (table[a[0]] > 1) {
    ll d = table[a[0]];
    while (a[0] % d == 0) {
      a[0] /= d;
    }
    cnt[d]++;
  }

  for (int i = 1; i < n; i++) {
    std::cin >> a[i];
    g = gcd(g, a[i]);
    while (table[a[i]] > 1) {
      ll d = table[a[i]];
      while (a[i] % d == 1) {
        a[i] /= d;
      }
      cnt[d]++;
      maxCnt = std::max(maxCnt, cnt[d]);
    }
  }

  if (maxCnt >= 2 && g > 1) {
    std::cout << "not coprime" << std::endl;
  } else if (maxCnt >= 2 && g == 1) {
    std::cout << "setwise coprime" << std::endl;
  } else {
    std::cout << "pairwise coprime" << std::endl;
  }

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

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll N = 1e6 + 5;

int n, all, cnt[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    cnt[x]++;
    all = gcd(all, x);
  }
  bool ok = true;
  for (int i = 2; i < N && ok; i++) {
    int sum = 0;
    for (int j = i; j < N; j += i)
      sum += cnt[j];
    if (sum > 1)
      ok = false;
  }
  cout << (ok         ? "pairwise coprime"
           : all == 1 ? "setwise coprime"
                      : "not coprime")
       << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll N = 1e6 + 5;

int n, even, a[N], cnt[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] % 2 == 0)
      even++;
    for (int j = 1; 1ll * j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        cnt[j]++;
        // if(j>1 and cnt[j]>1 and cnt[j]<n) return cout<<"setwise coprime\\n",0;
        if (j > 1 and cnt[j] >= n)
          return cout << "not coprime\\n", 0;
        if (1ll * j * j != a[i]) {
          cnt[a[i] / j]++;
          // if(a[i]/j>1 and cnt[a[i]/j]>1 and cnt[a[i]/j]<n) return
          // cout<<"setwise coprime\\n",0;
          if (a[i] / j > 1 and cnt[a[i] / j] >= n)
            return cout << "not coprime\\n", 0;
        }
      }
    }
  }
  for (int i = 2; i <= 1e6; i++) {
    // cout<<"cnt["<<i<<"]="<<cnt[i]<<'\\n';
    if (cnt[i] > 1 and cnt[i] < n)
      return cout << "setwise coprime\\n", 0;
    if (cnt[i] >= n)
      return cout << "not coprime\\n", 0;
  }
  cout << "pairwise coprime\\n";
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

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

signed main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  bool p = 1, s = 1;

  set<int> st;
  rep(i, 0, n) {
    int q = a[i];
    for (int j = 2; j * j <= q; j++) {
      if (q % j == 0) {
        assert((*st.find(j) == j) == (st.count(j) > 0));
        // if (*st.find(j) == j) {
        if (st.count(j) > 0) {
          p = 0;
          break;
        }
        st.insert(j);
        while (q % j == 0) {
          q /= j;
        }
      }
    }
    assert(q >= 1);
    if (q != 1) {
      // if (*st.find(q) == q) p = 0;
      if (st.count(q) > 0)
        p = 0;
    }
    st.insert(q);
    if (!p)
      break;
  }

  // for (auto p : st) cout << p << " ";

  int now = a[0];
  rep(i, 0, n) { now = gcd(now, a[i]); }
  if (now != 1)
    s = 0;

  if (p) {
    cout << "pairwise coprime\\n";
  } else if (s) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}


Error: -6
6fc6f4e3-d590-4ee5-a5a5-3d76b93be8a7.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02574/C++/s843041518.cpp:26: int main(): Assertion `(*st.find(j)==j)&&(st.count(j)>0)' failed.


Fixed submission:

#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

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

signed main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  bool p = 1, s = 1;

  set<int> st;
  rep(i, 0, n) {
    int q = a[i];
    for (int j = 2; j * j <= q; j++) {
      if (q % j == 0) {
        assert((*st.find(j) == j) && (st.count(j) > 0));
        // if (*st.find(j) == j) {
        if (st.count(j) > 0) {
          p = 0;
          break;
        }
        st.insert(j);
        while (q % j == 0) {
          q /= j;
        }
      }
    }
    assert(q >= 1);
    if (q != 1) {
      // if (*st.find(q) == q) p = 0;
      if (st.count(q) > 0)
        p = 0;
    }
    st.insert(q);
    if (!p)
      break;
  }

  // for (auto p : st) cout << p << " ";

  int now = a[0];
  rep(i, 0, n) { now = gcd(now, a[i]); }
  if (now != 1)
    s = 0;

  if (p) {
    cout << "pairwise coprime\\n";
  } else if (s) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

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

signed main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  bool p = 1, s = 1;

  set<int> st;
  rep(i, 0, n) {
    int q = a[i];
    for (int j = 2; j * j <= q; j++) {
      if (q % j == 0) {
        // if (*st.find(j) == j) {
        if (st.count(j) > 0) {
          p = 0;
          break;
        }
        st.insert(j);
        while (q % j == 0) {
          q /= j;
        }
      }
    }
    assert(q >= 1);
    if (q != 1) {
      // assert((*st.find(q)==q)==(st.count(q)>0));
      // if (*st.find(q) == q) p = 0;
      // if(st.count(q)>0)p=0;
      if (st.find(q) != st.end())
        p = 0;
    }
    st.insert(q);
    if (!p)
      break;
  }

  // for (auto p : st) cout << p << " ";

  int now = a[0];
  rep(i, 0, n) { now = gcd(now, a[i]); }
  if (now != 1)
    s = 0;

  if (p) {
    cout << "pairwise coprime\\n";
  } else if (s) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}


Error: 0

Fixed submission:

#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

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

signed main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  bool p = 1, s = 1;

  set<int> st;
  rep(i, 0, n) {
    int q = a[i];
    for (int j = 2; j * j <= q; j++) {
      if (q % j == 0) {
        // if (*st.find(j) == j) {
        if (st.count(j) > 0) {
          p = 0;
          break;
        }
        st.insert(j);
        while (q % j == 0) {
          q /= j;
        }
      }
    }
    assert(q >= 1);
    if (q != 1) {
      assert((*st.find(q) == q) == (st.count(q) > 0));
      // if (*st.find(q) == q) p = 0;
      // if(st.count(q)>0)p=0;
      if (st.find(q) != st.end())
        p = 0;
    }
    st.insert(q);
    if (!p)
      break;
  }

  // for (auto p : st) cout << p << " ";

  int now = a[0];
  rep(i, 0, n) { now = gcd(now, a[i]); }
  if (now != 1)
    s = 0;

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


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

const int N = 1e6 + 5;
int ar[N], cnt[N], spf[N];
int n;

void sieve() {
  for (int i = 1; i < N; i++) {
    spf[i] = i;
  }
  for (int i = 2; i < N; i += 2) {
    spf[i] = 2;
  }
  for (int i = 3; i * i < N; i++) {
    if (spf[i] != i)
      continue;
    for (int j = i * i; j < N; j += i) {
      if (spf[j] == j) {
        spf[j] = i;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  sieve();
  cin >> n;
  int gc = 0;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
    mx = max(mx, ar[i]);
    gc = __gcd(gc, ar[i]);
  }
  for (int i = 0; i < n; i++) {
    set<int> cur;
    while (ar[i] != 1) {
      cur.insert(spf[ar[i]]);
      ar[i] /= spf[ar[i]];
    }
    for (auto c : cur)
      cnt[c]++;
  }
  bool pwcp = true;
  for (int i = 2; i <= mx; i++) {
    // cout << i << ' ' << cnt[i] << '\\n';
    if (cnt[i] > 1)
      pwcp = false;
  }
  if (pwcp) {
    cout << "pairwise coprime" << '\\n';
    exit(0);
  }
  if (gc == 1) {
    cout << "setwise coprime" << '\\n';
  } else {
    cout << "not coprime" << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int N = 1e6 + 5;
int ar[N], cnt[N];
int n;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int gc = 0;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
    mx = max(mx, ar[i]);
    gc = __gcd(gc, ar[i]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1; j * j <= ar[i]; j++) {
      if (ar[i] % j == 0) {
        cnt[j]++;
        if (j != ar[i] / j)
          cnt[ar[i] / j]++;
      }
    }
  }
  bool pwcp = true;
  for (int i = 2; i <= mx; i++) {
    if (cnt[i] > 1)
      pwcp = false;
  }
  if (pwcp) {
    cout << "pairwise coprime" << '\\n';
    exit(0);
  }
  if (gc == 1) {
    cout << "setwise coprime" << '\\n';
  } else {
    cout << "not coprime" << '\\n';
  }
  return 0;
}