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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define clr(a) memset(a, 0, sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", &s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define sz(x) x.size()
#define rep(n) for (int i = 0; i < n; i++)
#define repc(i, n) for (int i = 0; i < n; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
using namespace std;

// Some typedef's
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<ll> vlli;
typedef vector<ii> vii;

template <typename T> T modpow(T base, T exp, T modulus) {
  base %= modulus;
  T result = 1;
  while (exp > 0) {
    if (exp & 1)
      result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

ll lcm(ll a, ll b) { return (a * (b / __gcd(a, b))); }

// Some contants
const ll inf = 1e12 + 7;
const double eps = 1e-9;
const long double pi = 1.00 * acos(-1.00);

int n, GC, maxi;
vi inp;

const int MAXN = 1e6 + 1;
int divisors[MAXN];

void solve() {
  clr(divisors);
  bool flag = 1;

  FOR(i, 0, n) {
    ll curr = inp[i];
    for (int j = 1; j <= sqrt(curr); j++) {
      if (curr % j == 0) {

        divisors[j]++;

        if (j != curr / j) {
          divisors[curr / j]++;
        }

        if (divisors[j] > 1 && j > 1) {
          flag = 0;
          break;
        }
        if (divisors[curr / j] > 1 && (curr / j) > 1) {
          flag = 0;
          break;
        }
      }
    }
    if (!flag) {
      break;
    }
  }

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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n;
  GC = 0;
  maxi = INT_MIN;
  rep(n) {
    int tmp;
    cin >> tmp;
    inp.pb(tmp);
    GC = __gcd(GC, tmp);
    maxi = max(maxi, tmp);
  }
  if (GC > 1) {
    cout << "not coprime\\n";
  } else {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define clr(a) memset(a, 0, sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", &s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define sz(x) x.size()
#define rep(n) for (int i = 0; i < n; i++)
#define repc(i, n) for (int i = 0; i < n; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
using namespace std;

// Some typedef's
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<ll> vlli;
typedef vector<ii> vii;

template <typename T> T modpow(T base, T exp, T modulus) {
  base %= modulus;
  T result = 1;
  while (exp > 0) {
    if (exp & 1)
      result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

ll lcm(ll a, ll b) { return (a * (b / __gcd(a, b))); }

// Some contants
const ll inf = 1e12 + 7;
const double eps = 1e-9;
const long double pi = 1.00 * acos(-1.00);

int n, GC, maxi;
vi inp;

const int MAXN = 1e6 + 1;
int divisors[MAXN];

void solve() {
  clr(divisors);
  FOR(i, 0, n) {
    ll curr = inp[i];
    for (int j = 1; j <= sqrt(curr); j++) {
      if (curr % j == 0) {
        divisors[j]++;

        if (j != curr / j) {
          divisors[curr / j]++;
        }
      }
    }
  }
  bool flag = 1;
  FOR(i, 2, maxi + 1) {
    if (divisors[i] > 1) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    cout << "pairwise coprime\\n";
  } else {
    cout << "setwise coprime\\n";
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n;
  GC = 0;
  maxi = INT_MIN;
  rep(n) {
    int tmp;
    cin >> tmp;
    inp.pb(tmp);
    GC = __gcd(GC, tmp);
    maxi = max(maxi, tmp);
  }
  if (GC > 1) {
    cout << "not coprime\\n";
  } else {
    solve();
  }

  return 0;
}