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

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pi(a) printf("%d", a)
#define spc printf(" ")
#define bl printf("\\n")
#define here(a) printf("#### %d ####", a)
#define pii pair<int, int>
#define fr(i, n) for (int i = 0; i < n; i++)
#define case(num) printf("Case %d:", num)
#define inf 1000000050
#define MAX 10010
#define MOD 1000000007

int flag[MAX + 10];
int flag2[1000000 + 10];
vector<int> prime;

int main() {

  // input;
  // output;
  flag[0] = 1;
  flag[1] = 1;
  int sq = sqrt(MAX);
  for (int i = 4; i < MAX; i += 2)
    flag[i] = 1;
  for (int i = 3; i < sq; i += 2) {
    if (flag[i] == 0) {
      for (int j = i * i; j < MAX; j += i) {
        flag[j] = 1;
      }
    }
  }

  for (int i = 0; i < MAX; i++)
    if (flag[i] == 0) {
      prime.push_back(i);
    }

  int n;
  cin >> n;
  vector<int> v(n);

  int pair_flag = 0;
  int set_flag = 0;
  int gcd = 0;

  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (i > 0) {
      gcd = __gcd(gcd, v[i]);
    } else
      gcd = v[0];
  }

  for (int i = 0; i < n; i++) {
    int m = v[i];

    for (int j = 0; j < prime.size(); j++) {
      if (prime[j] > m)
        break;

      if (m % prime[j] == 0) {
        while (m % prime[j] == 0) {
          m /= prime[j];
        }
        flag2[prime[j]]++;

        if (flag2[prime[j]] > 1) {
          pair_flag = 1;
          break;
        }
        if (m == 1)
          break;
      }
    }
    if (m != 1) {

      flag2[m]++;
      if (flag2[m] > 1) {
        pair_flag = 1;
        break;
      }
    }
    if (pair_flag)
      break;
  }

  if (!pair_flag) {
    cout << "pairwise coprime" << endl;
  } else if (gcd == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}
/**
 */


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pi(a) printf("%d", a)
#define spc printf(" ")
#define bl printf("\\n")
#define here(a) printf("#### %d ####", a)
#define pii pair<int, int>
#define fr(i, n) for (int i = 0; i < n; i++)
#define case(num) printf("Case %d:", num)
#define inf 1000000050
#define MAX 1000010
#define MOD 1000000007

int flag[MAX + 10];
int flag2[1000000 + 10];
vector<int> prime;

int main() {

  // input;
  // output;
  flag[0] = 1;
  flag[1] = 1;
  int sq = sqrt(MAX);
  for (int i = 4; i < MAX; i += 2)
    flag[i] = 1;
  for (int i = 3; i < sq; i += 2) {
    if (flag[i] == 0) {
      for (int j = i * i; j < MAX; j += i) {
        flag[j] = 1;
        // cout<<j<<endl;
      }
    }
  }

  for (int i = 0; i < MAX; i++)
    if (flag[i] == 0) {
      prime.push_back(i);
    }

  int n;
  cin >> n;
  vector<int> v(n);

  for (int i = 0; i < n; i++)
    cin >> v[i];

  // memset(flag, 0, sizeof(flag));

  for (int i = 0; i < n; i++) {
    int m = v[i];

    for (int j = 0; j < prime.size(); j++) {
      if (prime[j] > m)
        break;
      // cout<<prime[j]<<" ";

      if (m % prime[j] == 0) {
        while (m % prime[j] == 0) {
          m /= prime[j];
        }
        flag2[prime[j]]++;
        if (m == 1)
          break;
      }
    }
    // cout<<endl;
    if (m != 1)
      flag2[m]++;
  }

  // for(int i=0;i<5;i++) cout<<flag[i]<<endl;

  int pair_flag = 0;
  int set_flag = 0;

  for (int i = 0; i < prime.size(); i++) {
    if (pair_flag && set_flag)
      break;
    if (flag2[prime[i]] > 1)
      pair_flag = 1;
    if (flag2[prime[i]] >= n)
      set_flag = 1;
  }

  if (!pair_flag) {
    cout << "pairwise coprime" << endl;
  } else if (!set_flag) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}
/**
 */