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

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

#define int long long int
#define ff first
#define ss second
#define pb push_back

int bexp(int a, int b, int mod) {
  int res = 1;
  if (mod <= 0)
    return 0;

  while (b) {
    if (b & 1)
      res = (res * a) % mod;

    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

#define N 300010
#define MOD 1000000007

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

struct comp {
  bool operator()(const pair<int, int> a, const pair<int, int> b) const {
    return a.ss * (a.ff - a.ff / 2) > b.ss * (b.ff - b.ff / 2);
  }
};

int f[1000001] = {};

vector<int> getprimes() {
  vector<int> ans;

  f[1] = 1, f[0] = 1;
  f[2] = 0;
  for (int i = 2; i < 1001; ++i) {
    if (f[i] == 0) {
      for (int j = i * i; j < 1001; j += i) {
        f[j] = 1;
      }
    }
  }
  ans.push_back(2);

  for (int i = 3; i < 1001; i += 2)
    if (f[i] == 0)
      ans.push_back(i);

  return ans;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);

  int n;
  cin >> n;

  int a;
  int k = 0;
  int p = 1;

  vector<int> primes = getprimes();
  memset(f, 0, sizeof(f));

  for (int i = 0; i < n; ++i) {
    cin >> a;
    k = __gcd(k, a);

    int l = a;

    for (int k : primes) {
      if (a % k == 0) {
        f[k]++;

        while (a % k == 0)
          a /= k;

        if (f[k] == 2) {
          // cout << a << " " << k << endl;
          p = 0;
        }
      }
    }

    if (a > 1) {
      f[a]++;
      if (f[a] == 2)
        p = 0;
    }
  }

  if (p == 1)
    cout << "pairwise coprime\\n";
  else if (k == 1)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

#define int long long int
#define ff first
#define ss second
#define pb push_back

int bexp(int a, int b, int mod) {
  int res = 1;
  if (mod <= 0)
    return 0;

  while (b) {
    if (b & 1)
      res = (res * a) % mod;

    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

#define N 300010
#define MOD 1000000007

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

struct comp {
  bool operator()(const pair<int, int> a, const pair<int, int> b) const {
    return a.ss * (a.ff - a.ff / 2) > b.ss * (b.ff - b.ff / 2);
  }
};

int f[1000001] = {};

vector<int> getprimes() {
  vector<int> ans;

  f[1] = 1, f[0] = 1;
  f[2] = 0;
  for (int i = 2; i < 1000001; ++i) {
    if (f[i] == 0) {
      for (int j = i * i; j < 1000001; j += i) {
        f[j] = 1;
      }
    }
  }
  ans.push_back(2);

  for (int i = 3; i < 1000001; i += 2)
    if (f[i] == 0)
      ans.push_back(i);

  return ans;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);

  int n;
  cin >> n;

  int a[n];
  int k = 0;
  int p = 1;

  vector<int> primes = getprimes();
  memset(f, 0, sizeof(f));

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

    int l = a[i];

    for (int k : primes) {
      if (a[i] % k == 0) {
        f[k]++;
        if (f[k] == 2) {
          p = 0;
        }
      }
    }
  }

  if (p == 1)
    cout << "pairwise coprime\\n";
  else if (k == 1)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
  return 0;
}