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

#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int B[1000001];
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int d = 0;
  bool c = false;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    d = gcd(d, A[i]);
    B[A[i]]++;
  }
  for (int i = 2; i < 1000001; i++) {
    int count = 0;
    for (int j = 1; j * i < 1000001; j++) {
      count += B[i * j];
    }
    if (count > 1)
      c = true;
  }
  if (d != 1)
    puts("not coprime");
  else if (c)
    puts("setwise coprime");
  else
    puts("pairwise coprime");
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
int INF = 1e9 + 7;
int mod = 998244353;
long double PI =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998626034825342117;
struct Edge {
  long long to;
  long long cost;
}; // to...辺 cost...重み これをそれぞれ隣接グラフとして格納
void dijkstra(
    const vector<vector<Edge>> &G, long long s,
    vector<long long> &dis) { // Gは隣接グラフ,sは始点,距離の結果をdisに格納
  long long N = G.size();
  dis.resize(N, INF);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq; // 「仮の最短距離, 頂点」が小さい順に並ぶ
  dis[s] = 0;
  pq.emplace(dis[s], s);
  while (!pq.empty()) { //
    pair<int, int> p = pq.top();
    pq.pop();
    int v = p.second;
    if (dis[v] < p.first) { // 最短距離で無ければ無視
      continue;
    }
    for (auto &e : G[v]) {
      if (dis[e.to] >
          dis[v] + e.cost) { // 最短距離候補なら priority_queue に追加
        dis[e.to] = dis[v] + e.cost;
        pq.emplace(dis[e.to], e.to);
      }
    }
  }
  return;
}
long double kodohou(long double N) { return N * 180.0 / PI; }
struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
bool IsPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}
signed gcd(long long x, long long y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
signed lcm(long long x, long long y) { return x / gcd(x, y) * y; }
long long RepeatSquaring(long long N, long long P, long long M) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    long long t = RepeatSquaring(N, P / 2, M);
    return t * t % M;
  }
  return N * RepeatSquaring(N, P - 1, M);
}
long long Fibonacci(long long A) {
  if (A == 1)
    return 1;
  vector<long long> B(A + 1);
  B[0] = 1;
  B[1] = 1;
  for (int i = 2; i <= A; i++) {
    B[i] = B[i - 1] + B[i - 2];
    B[i] %= mod;
  }
  return B[A];
}
int mod_pow(int x, int y) {
  int res = 1;
  while (y > 0) {
    if (y % 2) {
      res = res * x % mod;
    }
    x = x * x % mod;
    y /= 2;
  }
  return res;
}
int fac[100005], finv[100005], inv[100005];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < 100005; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
int COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
vector<int> sieve(int max) {
  vector<bool> prime(max + 1, true);
  vector<int> A;
  for (int i = 2; i <= max; ++i)
    if (prime[i]) {
      A.push_back(i);
      for (int j = 2; i * j <= max; ++j)
        prime[i * j] = false;
    }
  return A;
}
void prime_factorize(vector<int> &prime, map<int, int> &B, long long N,
                     bool &b) {
  for (int a : prime) {
    if (N % a != 0)
      continue;
    while (N % a == 0)
      N /= a;
    if (B[a] > 0) {
      puts("setwise coprime");
      b = false;
      break;
    }
    B[a]++;
  }
}
signed main() {
  vector<int> prime = sieve(1000000);
  int N;
  cin >> N;
  vector<int> A(N);
  int count = 0;
  map<int, int> B;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i == 0)
      count = A[i];
    count = gcd(count, A[i]);
  }
  if (count != 1) {
    puts("not coprime");
    return 0;
  }
  bool b = true;
  for (int i = 0; i < N; i++) {
    prime_factorize(prime, B, A[i], b);
    if (!b)
      return 0;
  }
  puts("pairwise coprime");
}