Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<ll>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = a; i < (b); ++i)

const ll MOD = 1e9 + 7;

int *Eratosthenes(int N) {
  int *arr = new int[N];

  for (int i = 0; i < N; i++) {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(N); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < N; j++) {
        arr[i * (j + 2)] = 0;
      }
    }
  }
  return arr;
}

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

int main() {

  int N;
  cin >> N;

  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }
  int g = A[0];
  FOR(i, 1, N) g = gcd(g, A[i]);
  if (g != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  int *v = Eratosthenes(1e3 + 9);
  v[0] = 0;
  v[1] = 0;
  vector<int> B(1e6 + 9, 0);
  vector<int> pl;
  rep(i, 1e3 + 2) {
    if (v[i] == 1)
      pl.push_back(i);
  }

  int max_s = 0;

  rep(i, N) {
    int tmp = A[i];
    for (int j : pl) {
      if (tmp == 1)
        break;
      if (tmp % j != 0)
        continue;
      B[j]++;
      while (tmp % j == 0) {
        tmp /= j;
      }
    }
    if (tmp > 1)
      B[tmp]++;
  }

  FOR(i, 2, 1e6 + 2) { max_s = max(max_s, B[i]); }

  if (max_s <= 1)
    cout << "pairwise coprime" << endl;
  else
    cout << "setwise coprime" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<ll>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = a; i < (b); ++i)

const ll MOD = 1e9 + 7;

int *Eratosthenes(int N) {
  int *arr = new int[N];

  for (int i = 0; i < N; i++) {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(N); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < N; j++) {
        arr[i * (j + 2)] = 0;
      }
    }
  }
  return arr;
}

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

int main() {

  int N;
  cin >> N;

  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }
  int g = A[0];
  FOR(i, 1, N) g = gcd(g, A[i]);
  if (g != 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  int *v = Eratosthenes(1e6 + 9);
  v[0] = 0;
  v[1] = 0;
  vector<int> B(1e6 + 9, 0);
  vector<int> pl;
  rep(i, 1e3 + 2) {
    if (v[i] == 1)
      pl.push_back(i);
  }
  // cout << pl.size() << endl;

  int max_s = 0;
  set<int> cl;

  rep(i, N) {
    int tmp = A[i];
    set<int> s;
    while (true) {
      if (v[tmp] == 1) {
        s.insert(tmp);
        break;
      }
      for (int j : pl) {
        if (tmp % j == 0) {
          s.insert(j);
          tmp /= j;
          break;
        }
      }
    }
    for (auto itr = s.begin(); itr != s.end(); ++itr) {
      B[*itr]++;
      max_s = max(max_s, B[*itr]);
    }
  }

  if (max_s == 1)
    cout << "pairwise coprime" << endl;
  else
    cout << "setwise coprime" << endl;

  return 0;
}