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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

#define INF pow(10, 9)
// typedef priority_queue<tuple<int,int,int>, vector< tuple<int, int, int> >,
// greater< tuple<int, int, int> > > PQ;

typedef long long int ll;

ll calc_gcd(ll v1, ll v2) {
  if (v1 < v2)
    swap(v1, v2);
  if (v2 == 0) {
    return v1;
  } else {
    return calc_gcd(v2, v1 % v2);
  }
}

/*
def calc_lcm(value1:int, value2:int):
    gcd = calc_gcd(value1, value2)
    return value1 // gcd * value2

*/

ll pow_mod(ll base, ll pow, ll div) {
  ll res = 1;
  while (pow > 1) {
    if (pow & 1) {
      res = res * base % div;
    }
    base = base * base % div;
    pow /= 2;
  }

  return res * base % div;
}

void sieve(vector<int> &sieves, int num_max) {
  for (int i = 2; i <= num_max; i++) {
    int num = i;
    while (num <= num_max) {
      sieves[num] = min(sieves[num], i);
      num += i;
    }
  }
}

bool judge_and_add(vector<bool> &flag_sieve_not, vector<int> &sieves,
                   int value) {
  int v = value;
  int prime = 1;
  while (v > 1) {
    if (flag_sieve_not[sieves[v]] == true && sieves[v] > prime)
      return false;
    flag_sieve_not[sieves[v]] = true;
    prime = sieves[v];
    v /= sieves[v];
  }
  return true;
}

int main(void) {

  int N;
  cin >> N;
  vector<int> Array(N);

  int A_max = 0;
  for (int i = 0; i < N; i++) {
    cin >> Array[i];
    if (A_max < Array[i])
      A_max = Array[i];
  }

  ll gcds = Array[0];
  bool flag_prime = true;

  vector<int> sieves(A_max + 1, INF);
  sieve(sieves, A_max);

  vector<bool> flag_sieve_not(A_max + 1);

  judge_and_add(flag_sieve_not, sieves, Array[0]);

  for (int i = 0; i < N - 1; i++) {
    gcds = calc_gcd(gcds, Array[i + 1]);

    if (flag_prime == true) {
      flag_prime = judge_and_add(flag_sieve_not, sieves, Array[i + 1]);
    }
  }

  if (flag_prime == true) {
    cout << "pairwise coprime" << endl;
  } else if (gcds > 1) {
    cout << "not coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

#define INF pow(10, 9)
// typedef priority_queue<tuple<int,int,int>, vector< tuple<int, int, int> >,
// greater< tuple<int, int, int> > > PQ;

typedef long long int ll;

int calc_gcd(int v1, int v2) {
  if (v1 < v2)
    swap(v1, v2);
  if (v2 == 0) {
    return v1;
  } else {
    return calc_gcd(v2, v1 % v2);
  }
}

/*
def calc_lcm(value1:int, value2:int):
    gcd = calc_gcd(value1, value2)
    return value1 // gcd * value2

*/

int main(void) {

  int N;
  cin >> N;
  vector<int> Array(N);
  for (int i = 0; i < N; i++) {
    cin >> Array[i];
  }

  int gcd_1 = 0;

  int gcds = Array[0];
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (calc_gcd(Array[i], Array[j]) == 1) {
        gcd_1++;
      }
    }

    gcds = calc_gcd(gcds, Array[i + 1]);
  }
  long long int pair_tot = N * (N - 1) / 2;

  if (gcd_1 == pair_tot) {
    cout << "pairwise coprime" << endl;
  } else if (gcds > 1) {
    cout << "not coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }

  return 0;
}