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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   // setprecision
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <math.h>    // sqrt
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<long long>;
using pii = pair<int, int>;
using psi = pair<string, int>;

string p = "pairwise coprime";
string s = "setwise coprime";
string n = "not coprime";

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

int main() {
  int N;
  cin >> N;
  vi data(N);
  for (int i = 0; i < N; i++) {
    cin >> data[i];
  }
  bool check = true;
  vi num(1000000, 0);
  for (int i = 0; i < N; i++) {
    int n = data[i];
    for (int a = 2; a * a <= n; a++) {
      if (n % a == 0) {
        num[a]++;
      }
      if (num[a] > 1) {
        check = false;
        break;
      }
      while (n % a == 0) {
        n /= a;
      }
    }
    if (n != 1) {
      num[n]++;
    }
    if (num[n] > 1) {
      check = false;
    }
    if (!check) {
      break;
    }
  }
  if (check) {
    cout << p << endl;
    return 0;
  }
  int ans = data[0];
  for (int i = 1; i < N; i++) {
    ans = gcd(ans, data[i]);
  }
  if (ans == 1) {
    cout << s << endl;
  } else {
    cout << n << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   // setprecision
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <math.h>    // sqrt
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<long long>;
using pii = pair<int, int>;
using psi = pair<string, int>;

string p = "pairwise coprime";
string s = "setwise coprime";
string n = "not coprime";

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

int main() {
  int N;
  cin >> N;
  vi data(N);
  for (int i = 0; i < N; i++) {
    cin >> data[i];
  }
  bool check = false;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (gcd(data[i], data[j]) != 1) {
        check = true;
        break;
      }
    }
    if (check) {
      break;
    }
  }
  if (!check) {
    cout << p << endl;
    return 0;
  }
  vi rgcd(N + 1, 1);
  rgcd[0] = data[0];
  for (int i = 0; i < N; i++) {
    rgcd[i + 1] = gcd(rgcd[i], data[i]);
  }
  if (rgcd[N] == 1) {
    cout << s << endl;
  } else {
    cout << n << endl;
  }
}