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

#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
// #define endl "\\n"
#define print_array(name, n)                                                   \\
  for (ll o = 0; o < n; o++)                                                   \\
  cout << name[o] << " "
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 2e18
#define pll pair<ll, ll>
#define pii pair<int, int>
bool sortbysecond(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.ss > b.ss);
}

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

template <typename T> string NumberTostring(T Number) {
  ostringstream ss;
  ss << Number;
  return ss.str();
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  ll tgcd = 0;
  // cout<<"yo"<<endl;
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
    tgcd = gcd(vec[i], tgcd);
  }
  if (tgcd != 1) {
    cout << "not coprime" << endl;
  } else {
    int f1 = 0;
    int mxn = 1000001;
    vector<ll> spf(mxn);
    // cout<<"he"<<endl;
    spf[1] = 1;
    for (int i = 2; i < mxn; i++) {
      spf[i] = i;
    }
    for (int i = 2; i < mxn; i += 2) {
      spf[i] = 2;
    }
    for (int i = 3; i * i < mxn; i++) {
      if (spf[i] == i) {
        for (int j = i * i; j < mxn; j += i) {
          if (spf[j] == j) {
            spf[j] = i;
          }
        }
      }
    }
    // cout<<"hell"<<endl;
    bool prime[mxn];
    memset(prime, false, sizeof(prime));
    for (int i = n - 1; i >= 0; i--) {
      ll x = vec[i];
      while (x != 1) {
        // cout<<x<<endl;
        // cout<<spf[x]<<endl;
        if (prime[spf[x]] == true) {
          f1 = 1;
          break;
        } else {
          prime[spf[x]] = true;
          ll tt = spf[x];
          while (x % tt == 0) {
            x = x / tt;
          }
          // x = x/spf[x];
          // cout<<x<<"----"<<endl;
        }
      }
      if (f1 == 1) {
        break;
      }
    }
    if (f1 == 0) {
      cout << "pairwise coprime" << endl;
    } else {
      cout << "setwise coprime" << endl;
    }
    // cout<<"let"<<endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define endl "\\n"
#define print_array(name, n)                                                   \\
  for (ll o = 0; o < n; o++)                                                   \\
  cout << name[o] << " "
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 2e18
#define pll pair<ll, ll>
#define pii pair<int, int>
bool sortbysecond(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.ss > b.ss);
}

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

template <typename T> string NumberTostring(T Number) {
  ostringstream ss;
  ss << Number;
  return ss.str();
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> vec(n);
  ll tgcd = 0;
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
    tgcd = gcd(vec[i], tgcd);
  }
  if (tgcd != 1) {
    cout << "not coprime" << endl;
  } else {
    int f1 = 0;
    bool prime[1000001];
    memset(prime, true, sizeof(prime));
    for (int p = 2; p * p <= 1000001; p++) {
      if (prime[p] == true) {
        for (int i = p * p; i <= 1000001; i += p) {
          prime[i] = false;
        }
      }
    }
    vector<ll> op;
    ll cnt = 0;
    for (int i = 2; i < 1000001; i++) {
      if (prime[i] == true) {
        cnt++;
        op.pb(i);
      }
    }
    // cout<<cnt<<endl;1000001
    // print_array(op,500);
    // cout<<op.size()<<endl;
    bool pr[1000001];
    memset(pr, false, sizeof(pr));
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < op.size(); j++) {
        if (vec[i] % op[j] == 0) {
          if (pr[op[j]] == true) {
            f1 = 1;
            break;
          } else {
            pr[op[j]] = true;
          }
        }
      }
      if (f1 == 1) {
        break;
      }
    }
    if (f1 == 0) {
      cout << "pairwise coprime" << endl;
    } else {
      cout << "setwise coprime" << endl;
    }
    // cout<<"let"<<endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}