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

#include <bits/stdc++.h>
#include <fstream>
#define ll long long
#define ld long double
// #define int long long
#define MOD 1000000007
#define mod 1000000007
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define mp make_pair
#define endl "\\n"
using namespace std;
int gcd(int a, int b) {
  // Everything divides 0
  if (a == 0)
    return b;
  if (b == 0)
    return a;

  // base case
  if (a == b)
    return a;

  // a is greater
  if (a > b)
    return gcd(a - b, b);
  return gcd(a, b - a);
}
bool allprime[1000001] = {0};

vector<int> sieve_of_primes_upto_10e6() {
  int n = 1000007;
  vector<int> set_of_primes;
  set_of_primes.push_back(2);
  int p;
  for (p = 3; p * p <= n; p += 2) {
    if (allprime[p] == 0) {
      set_of_primes.push_back(p);
      for (int i = p * p; i <= n; i += p) {
        allprime[i] = 1;
      }
    }
  }
  for (p; p <= n; p += 2) {
    if (allprime[p] == 0) {
      set_of_primes.push_back(p);
    }
  }
  return set_of_primes;
}
void solve1();
void solve() {
  int T;
  cin >> T;
  for (int z = 1; z <= T; z++) {
    solve1();
  }
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve1();
  return 0;
}
int vis[1000007] = {0};
void solve1() {
  int n;
  cin >> n;
  int arr[n];
  cin >> arr[0];
  int c = arr[0];
  for (int i = 1; i < n; i++) {
    cin >> arr[i];
    c = __gcd(c, arr[i]);
  }
  if (c > 1) {
    cout << "not coprime" << endl;
    return;
  }
  vi vec = sieve_of_primes_upto_10e6();
  for (int i = 0; i < n; i++) {
    if (arr[i] != 1 && vis[arr[i]] > 0) {
      cout << "setwise coprime" << endl;
      return;
    }
    int j = 0;
    while (arr[i] >= vec[j] * vec[j]) {
      if (vis[vec[j]] == 0) {
        while (arr[i] % vec[j] == 0) {
          arr[i] /= vec[j];
          vis[vec[j]]++;
        }
      } else if (arr[i] % vec[j] == 0) {
        cout << "setwise coprime" << endl;
        return;
      }
      j++;
    }
    if (vis[arr[i]] == 0)
      vis[arr[i]]++;
    else if (arr[i] != 1) {
      cout << "setwise coprime" << endl;
      return;
    }
  }
  /*for(int i=0;i<10;i++)
  {
      cout<<vis[i]<<" ";
  }*/
  cout << "pairwise coprime" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <fstream>
#define ll long long
#define ld long double
// #define int long long
#define MOD 1000000007
#define mod 1000000007
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define mp make_pair
#define endl "\\n"
using namespace std;
int gcd(int a, int b) {
  // Everything divides 0
  if (a == 0)
    return b;
  if (b == 0)
    return a;

  // base case
  if (a == b)
    return a;

  // a is greater
  if (a > b)
    return gcd(a - b, b);
  return gcd(a, b - a);
}
bool allprime[1000001] = {0};

vector<int> sieve_of_primes_upto_10e6() {
  int n = 1000007;
  vector<int> set_of_primes;
  set_of_primes.push_back(2);
  int p;
  for (p = 3; p * p <= n; p += 2) {
    if (allprime[p] == 0) {
      set_of_primes.push_back(p);
      for (int i = p * p; i <= n; i += p) {
        allprime[i] = 1;
      }
    }
  }
  for (p; p <= n; p += 2) {
    if (allprime[p] == 0) {
      set_of_primes.push_back(p);
    }
  }
  return set_of_primes;
}
void solve1();
void solve() {
  int T;
  cin >> T;
  for (int z = 1; z <= T; z++) {
    solve1();
  }
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve1();
  return 0;
}
int vis[1000007] = {0};
void solve1() {
  int n;
  cin >> n;
  int arr[n];
  cin >> arr[0];
  int c = arr[0];
  for (int i = 1; i < n; i++) {
    cin >> arr[i];
    c = gcd(c, arr[i]);
  }
  if (c > 1) {
    cout << "not coprime" << endl;
    return;
  }
  vi vec = sieve_of_primes_upto_10e6();
  for (int i = 0; i < n; i++) {
    if (arr[i] != 1 && vis[arr[i]] > 0) {
      cout << "setwise coprime" << endl;
      return;
    }
    int j = 0;
    while (arr[i] >= vec[j] * vec[j]) {
      if (vis[vec[j]] == 0) {
        while (arr[i] % vec[j] == 0) {
          arr[i] /= vec[j];
          vis[vec[j]]++;
        }
      } else if (arr[i] % vec[j] == 0) {
        cout << "setwise coprime" << endl;
        return;
      }
      j++;
    }
    if (vis[arr[i]] == 0)
      vis[arr[i]]++;
    else if (arr[i] != 1) {
      cout << "setwise coprime" << endl;
      return;
    }
  }
  /*for(int i=0;i<10;i++)
  {
      cout<<vis[i]<<" ";
  }*/
  cout << "pairwise coprime" << endl;
}