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

#include <bits/stdc++.h>

#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define ss(a) scanf("%s", a)
#define pi(a) printf("%d\\n", a)
#define pl(a) printf("%lld\\n", a)
#define pc(a) putchar(a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (register int i = head[s]; i; i = Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int, int>
#define mii unordered_map<int, int>
#define msi unordered_map<string, int>
#define lowbit(x) ((x) & (-(x)))
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout << #x << ": " << x << endl
using namespace std;

inline int qr() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}

const int N = 1e6 + 10;

/*
素数存在prime中 下标1 - cntp
MAX_N若开到1e7 prime表注意除10
*/
const ll MAX_N = 1e6 + 1;
bool is_prime[MAX_N];
int prime[MAX_N / 10], cntp = 0, mndiv[MAX_N]; // mndiv最小质因数
void init() {
  for (ll i = 2; i < MAX_N; ++i)
    is_prime[i] = 1;
  mndiv[1] = 1;
  for (ll i = 2; i < MAX_N; ++i) {
    if (is_prime[i])
      prime[++cntp] = i, mndiv[i] = i;
    for (int j = 1; j <= cntp && i * prime[j] < MAX_N; j++) {
      is_prime[i * prime[j]] = false, mndiv[i * prime[j]] = prime[j];
      if (i % prime[j] == 0)
        break;
    }
  }
}

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

int main() {
  init(), n = qr();
  repi(i, 1, n) a[i] = qr();
  repi(t, 1, n) {
    int x = a[t];
    repi(i, 1, cntp) {
      if (1ll * prime[i] * prime[i] > x)
        break;
      if (x % prime[i] == 0)
        cnt[prime[i]]++;
      while (x % prime[i] == 0)
        x /= prime[i];
    }
    if (x > 1)
      cnt[x]++;
  }
  int mx = 0;
  repi(i, 2, MAX_N - 1) mx = max(mx, cnt[i]);
  if (mx <= 1)
    puts("pairwise coprime");
  else if (mx < n)
    puts("setwise coprime");
  else
    puts("not coprime");
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define ss(a) scanf("%s", a)
#define pi(a) printf("%d\\n", a)
#define pl(a) printf("%lld\\n", a)
#define pc(a) putchar(a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (register int i = head[s]; i; i = Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int, int>
#define mii unordered_map<int, int>
#define msi unordered_map<string, int>
#define lowbit(x) ((x) & (-(x)))
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout << #x << ": " << x << endl
using namespace std;

inline int qr() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}

const int N = 1e6 + 10;

typedef pair<ll, int> P;
vector<ll> tmp;
/*
素数存在prime中 下标1 - cntp
MAX_N若开到1e7 prime表注意除10
*/
const ll MAX_N = 1e6 + 1;
bool is_prime[MAX_N];
int prime[MAX_N / 10], cntp = 0, mndiv[MAX_N]; // mndiv最小质因数
void init() {
  for (ll i = 2; i < MAX_N; ++i)
    is_prime[i] = 1;
  mndiv[1] = 1;
  for (ll i = 2; i < MAX_N; ++i) {
    if (is_prime[i])
      prime[++cntp] = i, mndiv[i] = i;
    for (int j = 1; j <= cntp && i * prime[j] < MAX_N; j++) {
      is_prime[i * prime[j]] = false, mndiv[i * prime[j]] = prime[j];
      if (i % prime[j] == 0)
        break;
    }
  }
}
ll mul(ll a, ll b, ll mod) {
  ll res = 0;
  a %= mod, b %= mod;
  while (b) {
    if (b & 1) {
      res += a;
      if (res >= mod)
        res -= mod;
    }
    b >>= 1, a <<= 1;
    if (a >= mod)
      a -= mod;
  }
  return res;
}

ll qpow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = mul(res, x, mod);
    x = mul(x, x, mod), n >>= 1;
  }
  return res;
}

void Pollard_Rho(ll n) {
  if (is_prime[n]) {
    tmp.pb(n);
    return;
  }
  ll x, y, z, c, d;
  int i, j;
  while (true) {
    x = rand() * rand() % (n - 1) + 1, c = rand() * rand() % (n - 1) + 1;
    for (y = x, i = j = 2;; i++) {
      x = (mul(x, x, n) + c) % n, z = x - y;
      if (z < 0)
        z = -z;
      d = __gcd(z, n);
      if (d > 1 && d < n) {
        Pollard_Rho(d), Pollard_Rho(n / d);
        return;
      }
      if (x == y)
        break;
      if (i == j)
        y = x, j <<= 1;
    }
  }
}

vector<P> divi;
map<ll, int> mp;

void Prime_Factorization(ll n) {
  tmp.clear(), divi.clear(), mp.clear();
  if (n == 1) {
    divi.pb(P(1, 1));
    return;
  }
  Pollard_Rho(n);
  for (auto x : tmp)
    mp[x]++;
  for (auto x : mp)
    divi.pb(P(x.fi, x.se));
}

int n, cnt[N];

int main() {
  n = qr(), init();
  bool flag1 = true, flag2 = true;
  repi(i, 1, n) {
    int x = qr();
    Prime_Factorization(x);
    for (auto it : divi) {
      if (it.fi == 1)
        continue;
      if (cnt[it.fi])
        flag1 = false;
      cnt[it.fi]++;
    }
  }
  if (flag1)
    puts("pairwise coprime");
  else {
    repi(i, 2, 1000000) if (cnt[i] == n) {
      flag2 = false;
      break;
    }
    puts(flag2 ? "setwise coprime" : "not coprime");
  }
  return 0;
}