Problem p02574 (C++)

Buggy submission (status: Time Limit Exceeded):

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << "\n"; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

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

ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }

void SOE(ll N, vi &p) {
  int m = int(floor(sqrt(N))) + 1;
  int s[m];
  REP(i, m) s[i] = i;

  FOR(i, 2, m) {
    if (s[i] != i)
      continue;

    p.pb(i);

    for (int j = i + i; j <= m; j += i) {
      s[j] = i;
    }
  }
}

void solve(int n, vll &a) {
  ll g = a[0];
  REP(i, n) g = __gcd(g, a[i]);

  vector<int> prime;
  SOE(1e7, prime);

  map<int, int> l, m;

  REP(i, n) {
    for (auto &e : prime) {
      if (a[i] % e != 0)
        continue;

      int cnt = 0;
      while (a[i] % e == 0) {
        a[i] /= e;
        cnt++;
      }

      m[e] += cnt;
      l[e] = max(l[e], cnt);
    }

    if (a[i] > 1) {
      m[a[i]] += 1;
      l[a[i]] = max(l[a[i]], 1);
    }
  }

  bool f = false;
  if (l.size() == m.size()) {
    f = true;
    for (auto &p : l) {
      f &= l[p.fi] == m[p.fi];
    }
  }

  if (f) {
    print("pairwise coprime");
  } else {
    if (g == 1) {
      print("setwise coprime");
    } else
      print("not coprime");
  }
}

int tle(int n, vll &a) {
  ll g = a[0];
  REP(i, n) g = __gcd(g, a[i]);

  bool f = true;
  REP(i, n) FOR(j, i + 1, n) {
    f &= __gcd(a[i], a[j]) == 1;
    //        debug(a[i], a[j], __gcd(a[i], a[j]))
  }

  if (f) {
    return 1;
  } else {
    if (g == 1)
      return 2;
    return 3;
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;

  vll a(n);
  REP(i, n) cin >> a[i];

  solve(n, a);
}


Fixed submission:

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << "\n"; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

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

ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }

void SOE(ll N, vi &p) {
  int m = int(floor(sqrt(N))) + 1;
  int s[m];
  REP(i, m) s[i] = i;

  FOR(i, 2, m) {
    if (s[i] != i)
      continue;

    p.pb(i);

    for (int j = i + i; j <= m; j += i) {
      s[j] = i;
    }
  }
}

void solve(int n, vll &a) {
  ll g = a[0];
  REP(i, n) g = __gcd(g, a[i]);

  vector<int> prime;
  SOE(1e6, prime);

  map<int, int> l, m;

  REP(i, n) {
    for (auto &e : prime) {
      if (a[i] % e != 0)
        continue;

      int cnt = 0;
      while (a[i] % e == 0) {
        a[i] /= e;
        cnt++;
      }

      m[e] += cnt;
      l[e] = max(l[e], cnt);
    }

    if (a[i] > 1) {
      m[a[i]] += 1;
      l[a[i]] = max(l[a[i]], 1);
    }
  }

  bool f = false;
  if (l.size() == m.size()) {
    f = true;
    for (auto &p : l) {
      f &= l[p.fi] == m[p.fi];
    }
  }

  if (f) {
    print("pairwise coprime");
  } else {
    if (g == 1) {
      print("setwise coprime");
    } else
      print("not coprime");
  }
}

int tle(int n, vll &a) {
  ll g = a[0];
  REP(i, n) g = __gcd(g, a[i]);

  bool f = true;
  REP(i, n) FOR(j, i + 1, n) {
    f &= __gcd(a[i], a[j]) == 1;
    //        debug(a[i], a[j], __gcd(a[i], a[j]))
  }

  if (f) {
    return 1;
  } else {
    if (g == 1)
      return 2;
    return 3;
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;

  vll a(n);
  REP(i, n) cin >> a[i];

  solve(n, a);
}