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

#include <bits/stdc++.h>
using namespace std;

// template {{{  0
// using {{{ 1
using ll = long long int;
using llu = long long unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x, y) scanf("%lld%lld", &x, &y)
#define Scll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x, y, z) for (ll x = z; x < y; x++)
#define Rep(x, y) Repe(x, y, 0)
#define RRepe(x, y, z) for (ll x = y - z - 1; x >= 0; x--)
#define RRep(x, y) RRepe(x, y, 0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\\n") : printf("No\\n")
#define YESNO(a) (a) ? printf("YES\\n") : printf("NO\\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  int t;
  while (b) {
    t = b;
    b = a % b;
    a = t;
  }
  return a;
}

int main() {

  int N;
  Scd(N);

  vi a(N);
  Rep(i, N) Scd(a[i]);

  int ans = 2;
  char *anss[3] = {"not", "setwise", "pairwise"};
  vi ex(1e6 + 1);
  vi te;

  Rep(i, N) {
    int ta = a[i];
    te.clear();
    for (int d = 2; d * d <= a[i]; d++) {
      if (ta % d == 0) {
        te.push_back(d);
        ta /= d;
        d--;
      }
    }
    if (ta != 1)
      te.push_back(ta);
    for (int d : te) {
      if (ex[d]) {
        ans = 1;
      }
    }
    for (int d : te) {
      ex[d] = 1;
    }
    if (ans != 2)
      break;
  }

  int gg = 0;
  Rep(i, N) { gg = gcd(gg, a[i]); }
  if (gg != 1)
    ans = 0;

  printf("%s coprime\\n", anss[ans]);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// template {{{  0
// using {{{ 1
using ll = long long int;
using llu = long long unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x, y) scanf("%lld%lld", &x, &y)
#define Scll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x, y, z) for (ll x = z; x < y; x++)
#define Rep(x, y) Repe(x, y, 0)
#define RRepe(x, y, z) for (ll x = y - z - 1; x >= 0; x--)
#define RRep(x, y) RRepe(x, y, 0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\\n") : printf("No\\n")
#define YESNO(a) (a) ? printf("YES\\n") : printf("NO\\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  int t;
  while (b) {
    t = b;
    b = a % b;
    a = t;
  }
  return a;
}

int main() {

  int N;
  Scd(N);

  vi a(N);
  Rep(i, N) Scd(a[i]);

  int ans = 2;
  char *anss[3] = {"not", "setwise", "pairwise"};
  vi ex(1e6 + 1);
  vi te;

  Rep(i, N) {
    int ta = a[i];
    te.clear();
    for (int d = 2; d * d <= a[i]; d++) {
      while (ta % d == 0) {
        te.push_back(d);
        ta /= d;
      }
    }
    if (ta != 1)
      te.push_back(ta);
    for (int d : te) {
      if (ex[d])
        ans = 1;
    }
    for (int d : te) {
      ex[d] = 1;
    }
  }

  int gg = 0;
  Rep(i, N) { gg = gcd(gg, a[i]); }
  if (gg != 1)
    ans = 0;

  printf("%s coprime\\n", anss[ans]);

  return 0;
}