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


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

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using pll = pair<ll, ll>;

const ll MAXN = 1000001;
bool P[MAXN];

map<ll, ll> makePrimeFactorMap(ll n) {
  map<ll, ll> res;

  for (ll i = 2; i * i <= n; ++i) {
    while ((n % i) == 0) {
      n /= i;
      ++res[i];
    }
  }

  if (n != 1) {
    ++res[n];
  }

  return res;
}

template <typename T> T gcdraw(T a, T b) { return a ? gcdraw(b % a, a) : b; }
template <typename T> T gcd(T a, T b) {
  const auto aa = abs(a), bb = abs(b);
  return aa > bb ? gcdraw(aa, bb) : gcdraw(bb, aa);
}

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

  vector<ll> A(N);
  for (auto &a : A) {
    cin >> a;
  }

  bool isPairwise = true;
  ll setwise = A[0];

  for (ll i = 0; i < N; ++i) {
    for (const auto &kv : makePrimeFactorMap(A[i])) {
      isPairwise &= !P[kv.first];
      P[kv.first] = true;
    }

    setwise = gcd(setwise, A[i]);
  }

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

  return 0;
}


Error: TLE

Fixed submission:


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

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using pll = pair<ll, ll>;

const ll MAXN = 1000001;
bool P[MAXN];

template <typename T> T gcdraw(T a, T b) { return a ? gcdraw(b % a, a) : b; }
template <typename T> T gcd(T a, T b) {
  const auto aa = abs(a), bb = abs(b);
  return aa > bb ? gcdraw(aa, bb) : gcdraw(bb, aa);
}

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

  vector<ll> A(N);
  for (auto &a : A) {
    cin >> a;
  }

  bool isPairwise = true;
  ll setwise = A[0];

  for (ll i = 0; i < N; ++i) {
    for (ll j = 1; j * j <= A[i]; ++j) {
      if ((A[i] % j) != 0) {
        continue;
      }

      const auto x = j;
      const auto y = A[i] / j;

      if (x != 1 && P[x]) {
        isPairwise = false;
      }
      if (y != 1 && P[y]) {
        isPairwise = false;
      }

      P[x] = true;
      P[y] = true;
    }

    setwise = gcd(setwise, A[i]);
  }

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

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

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

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using pll = pair<ll, ll>;

template <typename T> T gcdraw(T a, T b) { return a ? gcdraw(b % a, a) : b; }

template <typename T> T gcd(T a, T b) {
  const auto aa = abs(a), bb = abs(b);
  return aa > bb ? gcdraw(aa, bb) : gcdraw(bb, aa);
}

const ll MAXN = 1000001;
ll P[MAXN + 1];
bool B[MAXN + 1];

void makeLowestPrimeTable() {
  for (ll i = 2; i * i <= MAXN; ++i) {
    if (P[i]) {
      continue;
    }
    for (ll j = i * 2; j <= MAXN; j += i)
      if (!P[j]) {
        P[j] = i;
      }
  }
}

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

  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }

  makeLowestPrimeTable();

  bool isPairwise = true;
  ll setwiseValue = A[0];

  for (ll i = 0; i < N; ++i) {
    for (ll x = A[i]; x != 1;) {
      const auto p = P[x] ? P[x] : x;
      while ((x % p) == 0) {
        x /= p;
      }

      isPairwise &= !B[p];
      B[p] = true;
    }

    setwiseValue = gcd(setwiseValue, A[i]);
  }

  if (isPairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwiseValue == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using pll = pair<ll, ll>;

template <typename T> T gcdraw(T a, T b) { return a ? gcdraw(b % a, a) : b; }

template <typename T> T gcd(T a, T b) {
  const auto aa = abs(a), bb = abs(b);
  return aa > bb ? gcdraw(aa, bb) : gcdraw(bb, aa);
}

const ll MAXN = 1000001;
ll P[MAXN + 1];
bool B[MAXN + 1];

void makePrimeTable() {
  for (ll i = 2; i * i <= MAXN; ++i) {
    if (P[i]) {
      continue;
    }
    for (ll j = i; j <= MAXN; j += i)
      if (!P[j]) {
        P[j] = i;
      }
  }
}

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

  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }

  makePrimeTable();

  bool isPairwise = true;
  ll setwiseValue = A[0];

  for (ll i = 0; i < N; ++i) {
    for (ll x = A[i]; x != 1;) {
      const auto p = P[x];
      while ((x % p) == 0) {
        x /= p;
      }

      isPairwise &= !B[p];
      B[p] = true;
    }

    setwiseValue = gcd(setwiseValue, A[i]);
  }

  if (isPairwise) {
    cout << "pairwise coprime" << endl;
  } else if (setwiseValue == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

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

/**
 *  winners never quit
 **/

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define pp pop_back
#define mp make_pair
#define IO cin.tie(0), cout.tie(0)
#define ISWS ios::sync_with_stdio(0)
typedef long long Long;
void FastIO() {
  ISWS;
  IO;
}

const int N = 1e6;

int main() {
  FastIO();
  int tc, ca = 0;
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  string ans = "pairwise coprime";
  vector<int> cnt(N + 1, 0);
  vector<int> m(N + 1, 0);
  for (int i = 0; i < n; i++) {
    if (m[a[i]] == 0) {
      m[a[i]] = 1;
      cnt[a[i]]++;
      if (cnt[a[i]] > 1 && a[i] > 1) {
        ans = "setwise coprime";
        break;
      }
      for (int j = 2; j * j <= a[i]; j++) {
        if (a[i] % j == 0) {
          int num1 = j;
          int num2 = a[i] / j;
          cnt[num1]++;
          if (num1 != num2)
            cnt[num2]++;
          if ((cnt[num1] > 1) || (cnt[num2] > 1)) {
            ans = "setwise coprime";
            break;
          }
        }
      }
      if (ans == "setwise coprime")
        break;
    }
  }
  int gc = a[0];
  for (int i = 1; i < n; i++) {
    gc = __gcd(gc, a[i]);
  }
  if (gc > 1) {
    ans = "not coprime";
  }
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

/**
 *  winners never quit
 **/

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define pp pop_back
#define mp make_pair
#define IO cin.tie(0), cout.tie(0)
#define ISWS ios::sync_with_stdio(0)
typedef long long Long;
void FastIO() {
  ISWS;
  IO;
}

const int N = 1e5;

const Long len = 100000;
vector<int> p;
bool mark[len + 1];

void sieve() {
  p.pb(2);
  for (Long i = 3; i <= len; i += 2) {
    if (mark[i] == false) {
      p.pb(i);
      for (Long j = i * i; j <= len; j += i + i) {
        mark[j] = true;
      }
    }
  }
}

int main() {
  FastIO();
  int tc, ca = 0;
  // sieve();
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  string ans = "pairwise coprime";
  //  for (int i = 2;i < 101;i++){
  //    int cnt = 0;
  //    for (int j = 0;j < n;j++){
  //      if (a[j] % i == 0)cnt++;
  //    }
  //    if (cnt > 1){
  //      ans = "setwise coprime";
  //      break;
  //    }
  //  }
  vector<int> cnt(N + N + 1, 0);
  vector<int> m(N + N + 1, 0);
  for (int i = 0; i < n; i++) {
    if (m[a[i]] == 0) {
      m[a[i]] = 1;
      cnt[a[i]]++;
      if (cnt[a[i]] > 1) {
        ans = "setwise coprime";
        break;
      }
      for (Long j = 2; j * j <= a[i]; j++) {
        if (a[i] % j == 0) {
          m[j]++;
          if (j * j != a[i])
            m[a[i] / j]++;
          if (m[j] > 1 || m[a[i] / j] > 1) {
            ans = "setwise coprime";
            break;
          }
        }
      }
      if (ans == "setwise coprime")
        break;
    }
  }
  int gc = a[0];
  for (int i = 1; i < n; i++) {
    gc = __gcd(gc, a[i]);
  }
  if (gc > 1) {
    ans = "not coprime";
  }
  cout << ans << '\\n';
  return 0;
}
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++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  int A = a[n - 1];

  vector<int> ch(A + 1);
  for (int i = 0; i < n; i++) {
    ch[a[i]]++;
  }
  for (int i = 2; i <= A; i++) {
    int now = 0;
    for (int j = i; j <= A; j += i) {
      if (ch[j]) {
        now += ch[j];
      }
    }
    if (now >= 2) {
      int gcd = 0;
      for (int k = 0; k < n; k++)
        gcd = __gcd(gcd, a[k]);
      if (gcd != 1)
        cout << "not coprime" << endl;
      else
        cout << "setwise coprime" << endl;
      return 0;
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  int A = a[n - 1];

  vector<bool> ch(A + 1, false);
  for (int i = 1; i <= A; i++) {
    ch[a[i]] = true;
  }
  for (int i = 2; i <= A; i++) {
    int now = 0;
    for (int j = i; j <= A; j += i) {
      if (ch[j]) {
        now++;
      }
    }
    if (now >= 2) {
      int gcd = 0;
      for (int k = 0; k < n; k++)
        gcd = __gcd(gcd, a[k]);
      if (gcd != 1)
        cout << "not coprime" << endl;
      else
        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>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> e(1000001);

  rep(i, n) {
    cin >> a[i];
    e[a[i]]++;
  }
  bool pairwise = true;
  for (int i = 2; i <= 1000001; i++) {
    int count = 0;
    for (int j = i; j <= 1000001; j += i) {
      count += e[j];
    }
    if (count >= 2)
      pairwise = false;
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  int gcda = 0;
  rep(i, n) gcda = __gcd(gcda, a[i]);
  if (gcda != 1)
    cout << "not coprime" << endl;
  else
    cout << "setwise coprime" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int gcd = 0;
  for (int i = 0; i < n; i++) {
    gcd = __gcd(a[i], gcd);
  }
  if (gcd != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  vector<int> freq(1000001);
  for (int ii = 0; ii < n; ii++) {
    vector<bool> check(2000001, true);
    for (int i = 2; i * i <= a[ii]; i++) {
      if (check[i]) {
        int now = 1;
        while (i * now < 1000001) {
          check[i * now] = false;
          if (a[ii] % (i * now) == 0)
            freq[i * now]++;
          now++;
        }
      }
    }
  }

  for (int i = 2; i < 1000001; i++) {
    if (freq[i] >= 2) {
      cout << "setwise coprime" << endl;
      return 0;
    }
  }
  cout << "pairwise coprime" << endl;
  /*
  vector<int> prime;
  for(int i=2; i<1000001; i++){
      if(check[i]) prime.push_back(i);
  }

  sort(a.begin(),a.end());
  for(int i=0; i<int(prime.size()); i++){
      int now = 0;
      for(int j=0; j<n; j++){
          if(a[j]%prime[i]==0) now++;
      }
      if(now >= 2){
          cout << "setwise coprime" << endl;
          return 0;
      }
  }
  cout << "pairwise coprime" << endl;
*/
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  string ans = "pairwise";
  ll n;
  cin >> n;
  bool set = false;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    if (a[i] == 1) {
      a[n - 1] = 1e9;
      i--;
      n--;
      set = true;
    }
  }
  sort(a.begin(), a.end());
  if (n < 100000) {
    vector<bool> ok(1000005, true);
    rep(i, n) {
      if (ok[a[i]] == false)
        ans = "setwise";
      else {
        ll now = a[i];
        for (ll j = 2; j * j <= a[i]; j++) {
          while (now % j == 0)
            now /= j;
          if (a[i] % j == 0) {
            if (ok[j] == true) {
              ll k = 1;
              while (j * k < 1000005) {
                ok[j * k] = false;
                k++;
              }
            }
          }
        }
        if (now != 1) {
          ll k = 1;
          while (now * k < 1000005) {
            ok[now * k] = false;
            k++;
          }
        }
      }
      if (ans == "setwise")
        i = n - 1;
    }
  } else
    ans = "setwise";
  ll x = a[0];
  rep(i, n) { x = __gcd(a[i], x); }
  if (x != 1 && !set)
    ans = "not";
  cout << ans << " "
       << "coprime" << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  string ans = "pairwise";
  ll n;
  cin >> n;
  vector<ll> a;
  rep(i, n) {
    cin >> a[i];
    if (a[i] == 1) {
      a[n - 1] = 100000000;
      i--;
      n--;
    }
  }
  sort(a.begin(), a.end());
  if (n < 79000) {
    vector<bool> ok(1000005, true);
    rep(i, n) {
      if (ok[a[i]] == false)
        ans = "setwise";
      else {
        for (ll j = 2; j * j <= a[i]; j++) {
          if (a[i] % j == 0) {
            if (ok[j] == true) {
              ll k = 1;
              while (j * k < 1000005) {
                ok[j * k] = false;
                k++;
              }
            }
          }
        }
        if (ok[a[i]] == true) {
          ll k = 1;
          while (a[i] * k < 1000005) {
            ok[a[i] * k] = false;
            k++;
          }
        }
      }
      if (ans == "setwise")
        i = n - 1;
    }
  } else
    ans = "setwise";
  ll x = a[0];
  rep(i, n) { x = __gcd(a[i], x); }
  if (x != 1)
    ans = "not";
  cout << ans << " "
       << "coprime" << endl;
  return 0;
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> P;

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

int N = 1000100;
vector<int> prime;      // i番目の素数
bool is_prime[1000100]; // is_prime[i] がTrueならiは素数

int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime.emplace_back(i);
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p; // pは素数の数
}

int main() {
  sieve(N);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> c(N);
  rep(i, n) {
    cin >> a[i];
    c[a[i]]++;
  }
  bool flag = true;
  for (int i : prime) {
    int cnt = 0;
    for (int j = i; j <= N; j += i) {
      cnt += c[j];
    }
    if (cnt > 1) {
      flag = false;
      break;
    }
  }
  int gcd = a[0];
  rep(i, n - 1) { gcd = __gcd(a[i + 1], gcd); }
  if (flag)
    cout << "pairwise coprime" << endl;
  else if (gcd == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> P;

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

int N = 100010;
vector<int> prime;     // i番目の素数
bool is_prime[100011]; // is_prime[i] がTrueならiは素数

int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime.emplace_back(i);
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p; // pは素数の数
}

int main() {
  sieve(N);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> c(N);
  rep(i, n) {
    cin >> a[i];
    c[a[i]]++;
  }
  bool flag = true;
  for (int i : prime) {
    int cnt = 0;
    for (int j = i; j <= N; j += i) {
      cnt += c[j];
    }
    if (cnt > 1) {
      flag = false;
      break;
    }
  }
  int gcd = a[0];
  rep(i, n - 1) { gcd = __gcd(a[i + 1], gcd); }
  if (flag)
    cout << "pairwise coprime" << endl;
  else if (gcd == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}
Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int32_t main() {
  int t, n, i, j, k, l = 0, max1 = 0;
  cin >> n;
  int a[n + 1];
  for (i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  k = a[n];
  map<int, int> m;
  for (i = 1; i <= n; i++)
    m[a[i]]++;
  bool hi = true;
  bool dp[k + 1];
  memset(dp, 1, sizeof(dp));
  dp[1] = dp[0] = 0;
  for (i = 2; i <= k; i++) {
    if (dp[i]) {
      for (j = i * i; j <= k; j += i)
        dp[j] = 0;
    }
  }
  for (i = 2; i <= k; i++) {
    l = 0;
    if (dp[i]) {
      for (j = i; j <= k; j += i) {
        if (m.count(j))
          l += m[j];
      }
      if (l > 1) {
        hi = false;
        break;
      }
    }
  }

  if (hi)
    cout << "pairwise coprime";
  else {
    l = a[1];
    for (i = 2; i <= n; i++) {
      l = gcd(l, a[i]);
    }
    if (l == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int32_t main() {
  int t, n, i, j, k, l = 0, max1 = 0;
  cin >> n;
  int a[n + 1];
  for (i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  k = a[n];
  map<int, int> m;
  for (i = 1; i <= n; i++)
    m[a[i]]++;
  bool hi = true;
  for (i = 2; i <= k; i++) {
    l = 0;
    for (j = i; j <= k; j += i) {
      if (m.count(j))
        l += m[j];
    }
    if (l > 1) {
      hi = false;
      break;
    }
  }

  if (hi)
    cout << "pairwise coprime";
  else {
    l = a[1];
    for (i = 2; i <= n; i++) {
      l = gcd(l, a[i]);
    }
    if (l == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
}
Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, N) for (ll i = 0; i < N; i++)
using ll = long long;
using namespace std;
#define MOD 1000000007
typedef pair<ll, ll> Pl;
typedef vector<int> vi;

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

  int ggg = a[0];
  rep(i, n - 1) ggg = __gcd(ggg, a[i + 1]);
  int cnt = 0, flag = 1;
  rep(i, n) {
    if (a[i] % 2 == 0) {
      while (a[i] % 2 == 0)
        a[i] /= 2;
      cnt++;
    }
  }
  if (cnt > 1) {
    flag = 0;
  }
  if (flag) {
    for (int j = 3; j < 1000; j += 2) {
      cnt = 0;
      rep(i, n) {
        if (a[i] % j == 0) {
          if (a[i] % j == 0) {
            while (a[i] % j == 0)
              a[i] /= j;
            cnt++;
          }
        }
      }
      if (cnt > 1) {
        flag = 0;
        break;
      }
    }
  }
  if (flag) {
    int ch[1010101] = {0};
    rep(i, n) {
      if (a[i] != 1) {
        if (ch[a[i]]) {
          flag = 0;
          break;
        } else {
          ch[a[i]]++;
        }
      }
    }
  }

  if (flag) {
    puts("pairwise coprime");
  } else if (ggg == 1) {
    puts("setwise coprime");
  } else {
    puts("not coprime");
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, N) for (ll i = 0; i < N; i++)
using ll = long long;
using namespace std;
#define MOD 1000000007
typedef pair<ll, ll> Pl;
typedef vector<int> vi;

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

  int ggg = a[0];
  rep(i, n - 1) ggg = __gcd(ggg, a[i + 1]);
  int cnt = 0, flag = 1;
  rep(i, n) {
    if (a[i] % 2 == 0) {
      while (a[i] % 2 == 0)
        a[i] /= 2;
      cnt++;
    }
  }
  if (cnt > 1) {
    flag = 0;
  }
  if (flag) {
    for (int j = 3; j < 1000; j += 2) {
      cnt = 0;
      rep(i, n) {
        if (a[i] % j == 0) {
          if (a[i] % j == 0) {
            while (a[i] % j == 0)
              a[i] /= j;
            cnt++;
          }
        }
      }
      if (cnt > 1) {
        flag = 0;
        break;
      }
    }
  }
  if (flag) {
    int ch[101010] = {0};
    rep(i, n) {
      if (a[i] != 1) {
        if (ch[a[i]]) {
          flag = 0;
          break;
        } else {
          ch[a[i]]++;
        }
      }
    }
  }

  if (flag) {
    puts("pairwise coprime");
  } else if (ggg == 1) {
    puts("setwise coprime");
  } else {
    puts("not coprime");
  }
}
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;
using ll = long long;
using P = pair<int, int>;

const int A = 1e6 + 5;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

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

  bool pairwise = true;
  for (int i = 2; i < A; ++i) {
    int count = 0;
    for (int j = i; j < A; j += i) {
      count += c.at(j);
    }
    if (count > 1)
      pairwise = false;
  }
  if (pairwise) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

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

  cout << "not 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;
using ll = long long;
using P = pair<int, int>;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  int sw = gcd(a.at(0), a.at(1));
  for (int i = 2; i < n; ++i) {
    if (sw == 1)
      break;
    sw = gcd(sw, a.at(i));
  }
  if (sw != 1) {
    cout << "not coprime" << endl;
    return 0;
  }
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (gcd(a.at(i), a.at(j)) != 1) {
        cout << "setwise coprime" << endl;
        return 0;
      }
    }
  }
  cout << "pairwise coprime" << endl;

  return 0;
}