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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define int int64_t
#define vi vector<int>
#define vb vector<bool>
#define vbb vector<vector<bool>>
#define all(x) (x).begin(), (x).end()
#define vii vector<vector<int>>
#define rip(dp, n, m) vector<vector<int>> dp(n, vector<int>(m, 0))
#define ff first
#define ss second
#define pii pair<int64_t, int64_t>
#define miv map<int, vector<int>>
#define mp make_pair
#define pb push_back
#define prr(x) cout << (#x) << '	' << (x) << endl
#define pr(x) cout << (x) << endl
#define f(i, n) for (i = 0; i < n; i++)
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dr8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dc8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dr4[] = {1, 0, 0, -1};
int dc4[] = {0, 1, -1, 0};

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

int findMaxGCD(vi arr, int n) {
  // Calculating MAX in array
  int high = 0;
  for (int i = 0; i < n; i++)
    high = max(high, arr[i]);

  // Maintaining count array
  // int count[high + 1] = {0};
  vi count(high + 1, 0);
  for (int i = 0; i < n; i++)
    count[arr[i]]++;

  // Variable to store the
  // multiples of a number
  int counter = 0;

  // Iterating from MAX to 1
  // GCD is always between
  // MAX and 1. The first
  // GCD found will be the
  // highest as we are
  // decrementing the potential
  // GCD
  for (int i = high; i >= 1; i--) {
    int j = i;
    counter = 0;

    // Iterating from current
    // potential GCD
    // till it is less than
    // MAX
    while (j <= high) {
      // A multiple found

      if (count[j] >= 2)
        return j;

      else if (count[j] == 1)
        counter++;

      // Incrementing potential
      // GCD by itself
      // To check i, 2i, 3i....
      j += i;

      // 2 multiples found,
      // max GCD found
      if (counter == 2)
        return i;
    }
  }
}

int32_t main() {
  FIO;
  int i, n, j, m, k, l, t;
  // w(X)
  {
    cin >> n;
    vi v(n);
    f(i, n) cin >> v[i];

    k = findMaxGCD(v, n);
    if (k != 1) {
      t = v[0];
      f(i, n) t = gcd(t, v[i]);
      if (t == 1)
        cout << "setwise coprime";

      else
        cout << "not coprime";

    } else
      cout << "pairwise coprime";
  }

  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define int int64_t
#define vi vector<int>
#define vb vector<bool>
#define vbb vector<vector<bool>>
#define all(x) (x).begin(), (x).end()
#define vii vector<vector<int>>
#define rip(dp, n, m) vector<vector<int>> dp(n, vector<int>(m, 0))
#define ff first
#define ss second
#define pii pair<int64_t, int64_t>
#define miv map<int, vector<int>>
#define mp make_pair
#define pb push_back
#define prr(x) cout << (#x) << '	' << (x) << endl
#define pr(x) cout << (x) << endl
#define f(i, n) for (i = 0; i < n; i++)
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dr8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dc8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dr4[] = {1, 0, 0, -1};
int dc4[] = {0, 1, -1, 0};

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

int findMaxGCD(vi arr, int n) {

  int high = 0;
  for (int i = 0; i < n; i++)
    high = max(high, arr[i]);
  // int divisors[high + 1] = { 0 };
  vi divisors(high + 1, 0);

  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= sqrt(arr[i]); j++) {
      if (arr[i] % j == 0) {
        divisors[j]++;
        if (j != arr[i] / j)
          divisors[arr[i] / j]++;
      }
    }
  }
  for (int i = high; i >= 1; i--) {
    if (divisors[i] > 1)
      return i;
  }

  return 1;
}

int32_t main() {
  FIO;
  int i, n, j, m, k, l, t;
  // w(X)
  {
    cin >> n;
    vi v(n);
    f(i, n) cin >> v[i];

    k = findMaxGCD(v, n);
    if (k != 1) {
      t = v[0];
      f(i, n) t = gcd(t, v[i]);
      if (t == 1)
        cout << "setwise coprime";

      else
        cout << "not coprime";

    } else
      cout << "pairwise coprime";
  }
}