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

#include <bits/stdc++.h>
using namespace std;
int n, a[1000100], vis[1000100];
int v[1000100], pws, primes[78498], tot;
int ihateabc[100100], too;
void linear(int N) {
  memset(v, 0, sizeof(v));
  tot = 0;
  for (int i = 2; i <= N; i++) {
    if (v[i] == 0) {
      v[i] = i;
      primes[tot] = i;
      tot++;
    }
    for (int j = 0; j < tot; j++) {
      if (primes[j] > v[i] || primes[j] > (N / i))
        break;
      v[i * primes[j]] = primes[j];
    }
  }
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  linear(1000);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int go = a[1];
  for (int i = 1; i <= n; i++)
    go = __gcd(a[i], go);
  if (go != 1) {
    puts("not coprime");
    return 0;
  }
  pws = 0;
  sort(a + 1, a + n + 1);
  for (int i = 2; i <= n; i++) {
    if (a[i] == a[i - 1] && a[i] != 1) {
      puts("setwise coprime");
      return 0;
    }
  }
  too = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != a[i - 1] && a[i] != 1) {
      too++;
      ihateabc[too] = a[i];
    }
  }
  n = too;
  for (int i = 1; i <= too; i++)
    a[i] = ihateabc[i];
  reverse(a + 1, a + n + 1);
  if (n > 78498) {
    puts("setwise coprime");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < tot; j++) {
      if (primes[j] * primes[j] > a[i])
        continue;
      if (a[i] % primes[j] == 0) {
        if (vis[primes[j]]) {
          puts("setwise coprime");
          return 0;
        }
        vis[primes[j]] = 1;
        while (a[i] % primes[j] == 0)
          a[i] /= primes[j];
      }
    }
    if (a[i] != 1) {
      if (vis[a[i]]) {
        puts("setwise coprime");
        return 0;
      }
      vis[a[i]] = 1;
    }
  }
  puts("pairwise coprime");
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, a[1000100], vis;
int v[1000100], pws, primes[78498], tot;
int ihateabc[100100], too;
void linear(int N) {
  memset(v, 0, sizeof(v));
  tot = 0;
  for (int i = 2; i <= N; i++) {
    if (v[i] == 0) {
      v[i] = i;
      primes[tot] = i;
      tot++;
    }
    for (int j = 0; j < tot; j++) {
      if (primes[j] > v[i] || primes[j] > (N / i))
        break;
      v[i * primes[j]] = primes[j];
    }
  }
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  linear(1000000);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int go = a[1];
  for (int i = 1; i <= n; i++)
    go = __gcd(a[i], go);
  if (go != 1) {
    puts("not coprime");
    return 0;
  }
  pws = 0;
  sort(a + 1, a + n + 1);
  for (int i = 2; i <= n; i++) {
    if (a[i] == a[i - 1] && a[i] != 1) {
      puts("setwise coprime");
      return 0;
    }
  }
  too = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != a[i - 1] && a[i] != 1) {
      too++;
      ihateabc[too] = a[i];
    }
  }
  n = too;
  for (int i = 1; i <= too; i++)
    a[i] = ihateabc[i];
  reverse(a + 1, a + n + 1);
  if (n > 78498) {
    puts("setwise coprime");
    return 0;
  }
  for (int j = 0; j < tot; j++) {
    if (primes[j] > a[1])
      break;
    vis = 0;
    for (int i = 1; i <= n; i++) {
      if (primes[j] > a[i])
        break;
      if (a[i] % primes[j] == 0) {
        if (vis) {
          puts("setwise coprime");
          return 0;
        }
        vis = 1;
      }
    }
  }
  puts("pairwise coprime");
  return 0;
}