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

/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\\n"
#define MOD 998244353

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

int gcd(int x, int y) {
  if (x == 0) {
    return y;
  }

  return gcd(y % x, x);
}

void solve() {
  int n;
  cin >> n;

  vector<int> a(n);

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

  int GCD = gcd(a[0], a[1]);

  for (int i = 0; i < n; i++) {
    GCD = gcd(GCD, a[i]);
    if (GCD == 1)
      break;
  }

  bool setwise = false;

  if (GCD == 1) {
    setwise = true;
  } else {
    setwise = false;
  }

  map<int, int> mp;
  bool is_pairwise = true;

  for (int i = 0; i < n; i++) {
    if (a[i] % 2 == 0) {
      mp[2]++;
      while (a[i] % 2 == 0) {
        a[i] /= 2;
      }
      if (mp[2] > 1) {
        is_pairwise = false;
        break;
      }
    }

    for (int j = 3; j * j <= n; j += 2) {
      if (a[i] % j == 0) {
        mp[j]++;
        while (a[i] % j == 0) {
          a[i] /= j;
        }
        if (mp[j] > 1) {
          is_pairwise = false;
          break;
        }
      }
    }

    if (a[i] > 2) {
      mp[a[i]]++;
      if (mp[a[i]] > 1) {
        is_pairwise = false;
        break;
      }
    }
  }

  if (is_pairwise) {
    cout << "pairwise coprime\\n";
  } else if (setwise) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\\n"
#define MOD 998244353

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

int gcd(int x, int y) {
  if (x == 0) {
    return y;
  }

  return gcd(y % x, x);
}

void solve() {
  int n;
  cin >> n;

  vector<int> a(n);

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

  int GCD = gcd(a[0], a[1]);

  for (int i = 0; i < n; i++) {
    GCD = gcd(GCD, a[i]);
  }

  bool setwise = false;

  if (GCD == 1) {
    setwise = true;
  } else {
    setwise = false;
  }

  map<int, int> mp;
  bool is_pairwise = true;

  for (int i = 0; i < n; i++) {
    if (a[i] % 2 == 0) {
      mp[2]++;
      while (a[i] % 2 == 0) {
        a[i] /= 2;
      }
      if (mp[2] > 1) {
        is_pairwise = false;
        break;
      }
    }

    for (int j = 3; j * j <= n; j++) {
      if (a[i] % j == 0) {
        mp[j]++;
        while (a[i] % j == 0) {
          a[i] /= j;
        }
        if (mp[j] > 1) {
          is_pairwise = false;
          break;
        }
      }
    }

    if (a[i] > 2) {
      mp[a[i]]++;
      if (mp[a[i]] > 1) {
        is_pairwise = false;
        break;
      }
    }
  }

  if (is_pairwise) {
    cout << "pairwise coprime\\n";
  } else if (setwise) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}