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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
// #define INF __INT32_MAX__
#define INF 1e9
#define LINF __LONG_LONG_MAX__

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

/**
 * エラストテネスのふるい(n以下の素数を列挙する)
 */
vector<int> sieve(int n) {
  vector<int> res;
  vector<bool> is_prime(n + 1, true);
  // 0と1は素数ではない
  is_prime[0] == false;
  is_prime[1] == false;

  for (int i = 2; i <= n; i++) {
    if (!is_prime[i])
      continue;
    res.push_back(i);
    for (int j = 2 * i; j <= n; j += i) {
      is_prime[j] = false;
    }
  }

  return res;
}

vector<int> generateMiniFactor(int n) {
  vector<int> res(n + 1, -1);
  vector<bool> is_prime(n + 1, true);
  is_prime[0] == false;
  is_prime[1] == false;

  for (int i = 2; i <= n; i++) {
    if (!is_prime[i])
      continue;
    res[i] = i;
    for (int j = i * 2; j <= n; j += i) {
      if (!is_prime[j])
        continue;
      is_prime[j] = false;
      res[j] = i;
    }
  }
  return res;
}

int main() {
  const int A_MAX = 1000005;

  int N;
  cin >> N;
  vi A(N);
  rep(i, N) cin >> A[i];
  bool ok = true;

  // 1
  vi mf = generateMiniFactor(A_MAX);

  map<int, int> G;
  rep(i, N) {
    int x = A[i];

    set<int> ss;

    while (x != 1) {
      ss.insert(mf[x]);
      x /= mf[x];
    }
    for (auto sss : ss) {
      G[sss]++;
    }
  }

  for (auto x : G) {
    if (x.second > 1) {
      ok = false;
      break;
    }
  }

  if (ok) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  // 2
  ok = true;
  int y = A[0];
  rep(i, N - 1) {
    y = gcd(y, A[i + 1]);
    if (y == 1)
      break;
  }
  if (y != 1)
    ok = false;
  if (ok) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
// #define INF __INT32_MAX__
#define INF 1e9
#define LINF __LONG_LONG_MAX__

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

/**
 * エラストテネスのふるい(n以下の素数を列挙する)
 */
vector<int> sieve(int n) {
  vector<int> res;
  vector<bool> is_prime(n + 1, true);
  // 0と1は素数ではない
  is_prime[0] == false;
  is_prime[1] == false;

  for (int i = 2; i <= n; i++) {
    if (!is_prime[i])
      continue;
    res.push_back(i);
    for (int j = 2 * i; j <= n; j += i) {
      is_prime[j] = false;
    }
  }

  return res;
}

int main() {
  int N;
  cin >> N;
  vi A(N);
  rep(i, N) cin >> A[i];
  bool ok = true;

  // 1
  const int A_MAX = 1000005;
  vi primes = sieve(A_MAX);
  int pn = primes.size();

  map<int, int> G;
  rep(i, N) {
    int willbreak = false;
    rep(j, pn) {
      if (G[primes[j]] > 1) {
        willbreak = true;
        break;
      }
      if (A[i] % primes[j] == 0)
        G[primes[j]]++;
    }
    if (willbreak)
      break;
  }

  for (auto x : G) {
    if (x.second > 1) {
      ok = false;
      break;
    }
  }

  if (ok) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  // 2
  ok = true;
  int y = A[0];
  rep(i, N - 1) {
    y = gcd(y, A[i + 1]);
    if (y == 1)
      break;
  }
  if (y != 1)
    ok = false;
  if (ok) {
    cout << "setwise coprime" << endl;
    return 0;
  }

  cout << "not coprime" << endl;
}