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

#include <bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
#define zero(v) memset(v, 0, sizeof(v))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define foreach(it, c)                                                         \\
  for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define all(v) (v).begin(), (v).end()
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
typedef long long ll;
typedef unsigned uint;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
typedef pair<int, int> pii;
int inline in() {
  int x = 0, c;
  for (; (uint)((c = getchar()) - '0') >= 10;) {
    if (c == '-')
      return -in();
    if (!~c)
      throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((uint)((c = getchar()) - '0') < 10);
  return x;
}

const int Maxv = 1e3 + 5;
bool is_prime[Maxv];
vector<int> prime;
void init() {
  for (int i = 0; i < Maxv; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < Maxv; i++) {
    if (is_prime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j < Maxv; j += i)
        is_prime[j] = false;
    }
  }
}
// mii prime_factor(int n){
//	mii res;
//	for(int i=2;i*i<=n;i++){
//		while(n%i==0){
//			++res[i];
//			n/=i;
//		}
//	}
//	if(n!=1)res[n]=1;
//	return res;
// }
int n;
const int Maxn = 1e6 + 5;
int a[Maxn];
int cnt[Maxn];

int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  bool ver1 = true;
  int fac = a[1];
  for (int i = 1; i <= n; i++) {
    if (i > 1) {
      fac = __gcd(fac, a[i]);
    }
    int x = a[i];
    for (int j = 0; j < SZ(prime); j++) {
      int pi = prime[j];
      if (pi > x)
        break;
      if (x % pi == 0) {
        if (cnt[pi] > 0) {
          ver1 = false;
        }
        cnt[pi]++;
        while (x % pi == 0) {
          x /= pi;
        }
      }
    }
    if (x != 1) {
      if (cnt[x] > 0) {
        ver1 = false;
      }
      cnt[x]++;
    }
  }
  if (fac != 1) {
    printf("not coprime\\n");
    return 0;
  }
  if (!ver1)
    printf("setwise coprime\\n");
  else
    printf("pairwise coprime\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
#define zero(v) memset(v, 0, sizeof(v))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define foreach(it, c)                                                         \\
  for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define all(v) (v).begin(), (v).end()
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
typedef long long ll;
typedef unsigned uint;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
typedef pair<int, int> pii;
int inline in() {
  int x = 0, c;
  for (; (uint)((c = getchar()) - '0') >= 10;) {
    if (c == '-')
      return -in();
    if (!~c)
      throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((uint)((c = getchar()) - '0') < 10);
  return x;
}

const int Maxv = 1e3 + 5;
bool is_prime[Maxv];
vector<int> prime;
void init() {
  for (int i = 0; i < Maxv; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < Maxv; i++) {
    if (is_prime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j < Maxv; j += i)
        is_prime[j] = false;
    }
  }
}
// mii prime_factor(int n){
//	mii res;
//	for(int i=2;i*i<=n;i++){
//		while(n%i==0){
//			++res[i];
//			n/=i;
//		}
//	}
//	if(n!=1)res[n]=1;
//	return res;
// }
int n;
const int Maxn = 1e6 + 5;
int a[Maxn];
int ok_cnt[Maxn];

int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  //	for(int i=0;i<SZ(prime);i++)cout<<prime[i]<<' ';cout<<endl;
  bool ver1 = true;
  for (int i = 1; i <= n; i++) {
    int x = a[i];
    for (int j = 1; j * j <= x; j++) {
      if (x % j == 0) {
        if (ok_cnt[j] > 0 || ok_cnt[x / j] > 0) {
          //					cout<<i<<' '<<j<<endl;
          ver1 = false;
        }
        if (j * j == x) {
          if (j != 1)
            ok_cnt[j]++;
        } else {
          if (j != 1)
            ok_cnt[j]++;
          ok_cnt[x / j]++;
        }
      }
    }
    //		for(int j=0;j<SZ(prime);j++){
    //			int pi=prime[j];
    //			if(pi*pi>x)break;
    //			if(x%pi==0){
    //
    //			}
    //		}
  }
  //	cout<<ok_cnt[2]<<endl;
  for (int i = 2; i < Maxn; i++) {
    if (ok_cnt[i] == n) {
      printf("not coprime\\n");
      return 0;
    }
  }
  if (ver1)
    printf("pairwise coprime\\n");
  else
    printf("setwise coprime\\n");
  return 0;
}