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

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

bool pairwise = true, setwise = false;
int pot[1000001], N, arr[1000000];
bool vis[1000001];
bool chk[1000001];
void sieve() {
  int i, j;
  pot[0] = 1;
  pot[1] = 1;
  for (i = 2; i <= 1000000; i += 2) {
    pot[i] = 2;
  }
  for (i = 3; i <= 1000000; i += 2) {
    if (!pot[i]) {
      for (j = 1; i * j <= 1000000; j++) {
        if (!pot[i * j]) {
          pot[i * j] = i;
        }
      }
    }
  }
}
int main(void) {
  sieve();
  scanf("%d", &N);
  int i;
  for (i = 0; i < N; ++i) {
    scanf("%d", arr + i);
  }
  int pairgcd = gcd(arr[0], arr[1]);
  for (i = 2; i < N; i++) {
    pairgcd = gcd(pairgcd, arr[i]);
  }
  if (pairgcd == 1)
    setwise = true;
  // struct Vector vec[N];
  unordered_set<int> st[N];
  for (i = 0; i < N; i++) {
    int x = *(arr + i);
    // memset(chk, false, sizeof(chk));
    // init(&vec[i], 1000001);
    while (x > 1) {
      //  if (!chk[pot[x]]) {
      //      chk[pot[x]] = true;
      //      push_back(&vec[i], pot[x]);// check this whole & vs !& thing
      //}
      st[i].insert(pot[x]);
      x /= pot[x];
    }
    for (int y : st[i]) {
      if (vis[y]) {
        pairwise = false;
      }
      vis[y] = true;
    }
    // delete(&vec[i]);
  }
  if (pairwise) {
    printf("pairwise coprime\\n");
  } else if (setwise) {
    printf("setwise coprime\\n");
  } else {
    printf("not coprime\\n");
  }
  return 0;
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 * CHECK IF DEBUGGING OUTPUT BLOCKS ANSWER
 */


Error: TLE

Fixed submission:

#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_set>

struct Vector {
  int size, lastptr, *array;
};
void init(struct Vector *vector, int n) {
  vector->array = (int *)calloc(n, sizeof(int));
  vector->size = n;
  vector->lastptr = 0;
  assert(vector->array && "init broken");
}
void resize(struct Vector *vector) {
  vector->array = (int *)realloc(vector->array, 2 * vector->size);
  vector->size *= 2;
}
void push_back(struct Vector *vector, int a) {
  if (vector->lastptr == vector->size) {
    resize(vector);
    push_back(vector, a);
  } else {
    vector->array[vector->lastptr++] = a;
  }
}
// void delete(struct Vector* vector) {free(vector->array);}
int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

bool pairwise = true, setwise = false;
int pot[1000001], N, arr[1000000];
bool vis[1000001];
bool chk[1000001];
void sieve() {
  int i, j;
  pot[0] = 1;
  pot[1] = 1;
  for (i = 2; i <= 1000000; i += 2) {
    pot[i] = 2;
  }
  for (i = 3; i <= 1000000; i += 2) {
    if (!pot[i]) {
      for (j = 1; i * j <= 1000000; j++) {
        if (!pot[i * j]) {
          pot[i * j] = i;
        }
      }
    }
  }
}
int main(void) {
  sieve();
  scanf("%d", &N);
  int i;
  for (i = 0; i < N; ++i) {
    scanf("%d", arr + i);
  }
  int pairgcd = gcd(arr[0], arr[1]);
  for (i = 2; i < N; i++) {
    pairgcd = gcd(pairgcd, arr[i]);
  }
  if (pairgcd == 1)
    setwise = true;
  // struct Vector vec[N];
  std::unordered_set<int> st[N];
  for (i = 0; i < N; i++) {
    int x = *(arr + i);
    memset(chk, false, sizeof(chk));
    // init(&vec[i], 1000001);
    while (x > 1) {
      st[i].insert(pot[x]);
      x /= pot[x];
    }
    int j = 0;
    /*for (j=0; j < vec[i].lastptr; j++) {
        if (vis[vec[i].array[j]]) {
            pairwise = false;
        }
        vis[vec[i].array[j]] = true;
    }*/
    for (int num : st[i]) {
      if (vis[num]) {
        pairwise = false;
      }
      vis[num] = true;
    }
    // delete(&vec[i]);
  }
  if (pairwise) {
    printf("pairwise coprime\\n");
  } else if (setwise) {
    printf("setwise coprime\\n");
  } else {
    printf("not coprime\\n");
  }
  return 0;
}