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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define debug(x) cout << #x << " " << x << endl;

const int N = 1e6 + 5;
const double eps = 1e-9;
const int INF = 1e18;
const int P = 1e9 + 7;

int n;
int a[N];
int cnt[N];

int p[N];          // 质数数组
bool mpf[N];       // vis数组
int ind[N], d[N];  // 最小质因子指数、因数个数
int phi[N], mu[N]; // 欧拉函数、莫比乌斯函数
int g[N], f[N];    // 最小质因子的等比数列求和、因数和

void sieve(int n) {
  mpf[1] = 1;
  ind[1] = 1;
  d[1] = 1;
  phi[1] = 1;
  mu[1] = 1;
  g[1] = 1;
  f[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (!mpf[i]) {
      mpf[i] = p[++p[0]] = i;
      ind[i] = 1;
      d[i] = 2;
      phi[i] = i - 1;
      mu[i] = -1;
      g[i] = i + 1, f[i] = i + 1;
    }
    for (int j = 1, k; j <= p[0] && (k = i * p[j]) <= n; j++) {
      mpf[k] = p[j];
      if (!(i % p[j])) {
        ind[k] = ind[i] + 1;
        d[k] = d[i] / (ind[i] + 1) * (ind[k] + 1);
        phi[k] = phi[i] * p[j];
        mu[k] = 0;
        g[k] = g[i] * p[j] + 1;
        f[k] = f[i] / g[i] * g[k];
        break;
      }
      ind[k] = 1;
      d[k] = d[i] * d[p[j]];
      phi[k] = phi[i] * (p[j] - 1);
      mu[k] = -mu[i];
      g[k] = p[j] + 1;
      f[k] = f[i] * f[p[j]];
    }
  }
}

void fj(int x) {
  for (int i = 1; p[i] * p[i] <= x; i++) {
    if (x % p[i] == 0) {
      cnt[p[i]]++;
      while (x % p[i] == 0) {
        x /= p[i];
      }
    }
  }
  if (x != 1)
    cnt[x]++;
}

signed main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  cin >> n;
  sieve(1000000);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  int g = a[1];
  for (int i = 2; i <= n; i++)
    g = __gcd(g, a[i]);
  for (int i = 1; i <= n; i++) {
    fj(a[i]);
  }
  int flag = 1;
  for (int i = 1; i <= 1000000; i++) {
    if (cnt[i] >= 2)
      flag = 0;
  }
  if (flag) {
    puts("pairwise coprime");
  } else if (g == 1) {
    puts("setwise coprime");
  } else
    puts("not coprime");
  return 0;
}

// Created Time  :2020/8/29 20:00:20


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define debug(x) cout << #x << " " << x << endl;

const int N = 1e6 + 5;
const double eps = 1e-9;
const int INF = 1e18;
const int P = 1e9 + 7;

int n;
int a[N];
int cnt[N];

int p[N];          // 质数数组
bool mpf[N];       // vis数组
int ind[N], d[N];  // 最小质因子指数、因数个数
int phi[N], mu[N]; // 欧拉函数、莫比乌斯函数
int g[N], f[N];    // 最小质因子的等比数列求和、因数和

void sieve(int n) {
  mpf[1] = 1;
  ind[1] = 1;
  d[1] = 1;
  phi[1] = 1;
  mu[1] = 1;
  g[1] = 1;
  f[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (!mpf[i]) {
      mpf[i] = p[++p[0]] = i;
      ind[i] = 1;
      d[i] = 2;
      phi[i] = i - 1;
      mu[i] = -1;
      g[i] = i + 1, f[i] = i + 1;
    }
    for (int j = 1, k; j <= p[0] && (k = i * p[j]) <= n; j++) {
      mpf[k] = p[j];
      if (!(i % p[j])) {
        ind[k] = ind[i] + 1;
        d[k] = d[i] / (ind[i] + 1) * (ind[k] + 1);
        phi[k] = phi[i] * p[j];
        mu[k] = 0;
        g[k] = g[i] * p[j] + 1;
        f[k] = f[i] / g[i] * g[k];
        break;
      }
      ind[k] = 1;
      d[k] = d[i] * d[p[j]];
      phi[k] = phi[i] * (p[j] - 1);
      mu[k] = -mu[i];
      g[k] = p[j] + 1;
      f[k] = f[i] * f[p[j]];
    }
  }
}

void fj(int x) {
  for (int i = 1; i <= p[0]; i++) {
    if (x % p[i] == 0) {
      cnt[i]++;
      while (x % p[i] == 0) {
        x /= p[i];
      }
    }
  }
  if (x != 1)
    cnt[x]++;
}

signed main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  cin >> n;
  sieve(1000000);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  int g = a[1];
  for (int i = 2; i <= n; i++)
    g = __gcd(g, a[i]);
  for (int i = 1; i <= n; i++) {
    fj(a[i]);
  }
  int flag = 1;
  for (int i = 1; i <= 1000000; i++) {
    if (cnt[i] >= 2)
      flag = 0;
  }
  if (flag) {
    puts("pairwise coprime");
  } else if (g == 1) {
    puts("setwise coprime");
  } else
    puts("not coprime");
  return 0;
}

// Created Time  :2020/8/29 20:00:20