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

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int, int> pii;
typedef pair<long long int, long long int> plli;
typedef vector<vi> vvi;
typedef vector<vlli> vvlli;
#define fi(i, a, b) for (int i = a; i <= b; i++)
#define flli(i, a, b) for (long long int i = a; i <= b; i++)
#define bi(i, a, b) for (int i = a; i >= b; i--)
#define blli(i, a, b) for (long long int i = a; i >= b; i--)
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pi 2 * acos(0.0)
#define pb push_back
#define tr(v, it) for (decltype(v.begin()) it = v.begin(); it != v.end(); it++)
#define present(v, num) (v.find(num) != v.end())
#define cpresent(v, num) (find(v.begin(), v.end(), num) != v.end())
#define pq priority_queue
#define mp make_pair
const int inf = INT_MAX;
const lli INF = LLONG_MAX;
const lli mod = 1e9 + 7;
lli gcd(lli a, lli 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);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  lli n;
  scanf("%lld", &n);
  vlli a(n);
  flli(i, 0, n - 1) scanf("%lld", &a[i]);
  // check if its setwise coprime

  vector<bool> vis(1000001, true);
  vis[0] = false;
  vis[1] = false;
  vvlli ans(1000001);
  flli(i, 2, 1000000) {
    if (vis[i]) {
      ans[i].pb(i);
      for (lli j = 2 * i; j <= 1000000; j += i) {
        vis[j] = false;
        ans[j].pb(i);
      }
    }
  }
  vlli pcount(1000000, 0);
  bool flag = true;
  flli(i, 0, n - 1) {
    tr(ans[a[i]], it) {
      if (pcount[*it] != 0) {
        flag = false;
      }
      pcount[*it]++;
    }
  }
  lli setwise = 1;
  if (!flag) {
    flli(i, 2, 1000000) if (pcount[i] == n) setwise = setwise * i;
  }
  if (flag)
    printf("pairwise coprime");
  else if (setwise == 1)
    printf("setwise coprime");
  else
    printf("not coprime");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int, int> pii;
typedef pair<long long int, long long int> plli;
typedef vector<vi> vvi;
typedef vector<vlli> vvlli;
#define fi(i, a, b) for (int i = a; i <= b; i++)
#define flli(i, a, b) for (long long int i = a; i <= b; i++)
#define bi(i, a, b) for (int i = a; i >= b; i--)
#define blli(i, a, b) for (long long int i = a; i >= b; i--)
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pi 2 * acos(0.0)
#define pb push_back
#define tr(v, it) for (decltype(v.begin()) it = v.begin(); it != v.end(); it++)
#define present(v, num) (v.find(num) != v.end())
#define cpresent(v, num) (find(v.begin(), v.end(), num) != v.end())
#define pq priority_queue
#define mp make_pair
const int inf = INT_MAX;
const lli INF = LLONG_MAX;
const lli mod = 1e9 + 7;
lli gcd(lli a, lli 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);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast;
  lli n;
  cin >> n;
  vlli a(n);
  flli(i, 0, n - 1) cin >> a[i];
  // check if its setwise coprime
  lli setwise = a[0];
  flli(i, 1, n - 1) setwise = gcd(setwise, a[i]);

  vector<bool> vis(1000001, true);
  vis[0] = false;
  vis[1] = false;
  vvlli ans(1000001);
  flli(i, 2, 1000000) {
    if (vis[i]) {
      ans[i].pb(i);
      for (lli j = 2 * i; j <= 1000000; j += i) {
        vis[j] = false;
        ans[j].pb(i);
      }
    }
  }
  vlli pcount(1000000, 0);
  bool flag = true;
  flli(i, 0, n - 1) {
    tr(ans[a[i]], it) {
      if (pcount[*it] != 0) {
        flag = false;
        break;
      } else
        pcount[*it] = 1;
    }
    if (!flag)
      break;
  }
  if (flag)
    cout << "pairwise coprime";
  else if (setwise == 1)
    cout << "setwise coprime";
  else
    cout << "not coprime";
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
  return 0;
}