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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

int PRIMES[] = {2,   3,   5,   7,   11,  13,  17,  19,  23,  29,  31,  37,  41,
                43,  47,  53,  59,  61,  67,  71,  73,  79,  83,  89,  97,  101,
                103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
                173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
                241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
                317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
                401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
                479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569,
                571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643,
                647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
                739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823,
                827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
                919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};

int PRIME_COUNTS = 168;

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

void solve(int N, std::vector<int> A) {
  vector<int> counts(1000001, 0);
  for (int ai = 0; ai < N; ++ai) {
    int a = A[ai];
    for (int i = 0; i < PRIME_COUNTS; ++i) {
      int n = PRIMES[i];
      if (n > A[ai]) {
        break;
      }
      if (a % n == 0) {
        counts[n] += 1;
      }
      while (a % n == 0) {
        a /= n;
      }
    }
    if (a != 1) {
      counts[a] += 1;
    }
  }
  int max = *std::max_element(counts.begin(), counts.end());
  if (max <= 1) {
    cout << "pairwise coprime" << endl;
    return;
  }
  int n = A[0];
  for (int i = 0; i < N; ++i) {
    n = GCD(n, A[i]);
  }
  if (n == 1) {
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  int N;
  scanf("%d", &N);
  std::vector<int> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

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

void solve(int N, std::vector<int> A) {
  vector<int> counts(1000001, 0);
  for (int ai = 0; ai < N; ++ai) {
    int a = A[ai];
    for (int i = 2; i * i <= A[ai]; ++i) {
      if (a % i == 0) {
        counts[i] += 1;
      }
      while (a % i == 0) {
        a /= i;
      }
    }
    if (a != 1) {
      counts[a] += 1;
    }
  }
  int max = *std::max_element(counts.begin(), counts.end());
  if (max <= 1) {
    cout << "pairwise coprime" << endl;
    return;
  }
  int n = A[0];
  for (int i = 0; i < N; ++i) {
    n = GCD(n, A[i]);
  }
  if (n == 1) {
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  int N;
  scanf("%d", &N);
  std::vector<int> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}