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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

const int n = 1000000;
void eratosthenes(vector<int> &prime) {
  rep(0, i, n) prime.at(i) = 1;
  prime.at(0) = prime.at(1) = 0;
  rep(0, i, n) {
    if (prime.at(i)) {
      for (int j = i + i; j < n; j += i) {
        prime.at(j) = 0;
      }
    }
  }
}

bool IsPrime(ll num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

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

  // 素数である
  return true;
}

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(0, i, N) { cin >> A.at(i); }
  sort(all(A));

  vector<int> prime(1000000);
  eratosthenes(prime);
  vector<ll> value;

  rep(0, i, 1000000) {
    if (prime.at(i) == 1) {
      value.push_back(i);
    }
  }

  vector<ll> memo(1000010, 0);

  ll flag1 = 1;
  ll flag2 = 0;
  ll a_gcd = A.at(0);
  rep(0, i, N) {
    ll a = A.at(i);
    if (IsPrime(a)) {
      memo.at(a) = 1;
      a = 1;
    }
    for (auto v : value) {
      if (a < v) {
        break;
      }
      if (a % v == 0) {
        if (memo.at(v) != 0) {
          flag1 = 0;
        }
        memo.at(v) = 1;
        a /= v;
      }
      if (flag1 == 0) {
        break;
      }
    }
    a_gcd = gcd(a_gcd, A.at(i));
  }
  if (a_gcd == 1) {
    flag2 = 1;
  }

  if (flag1 == 1) {
    cout << "pairwise coprime" << endl;
  } else if (flag2 == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

const int n = 1000000;
void eratosthenes(vector<int> &prime) {
  rep(0, i, n) prime.at(i) = 1;
  prime.at(0) = prime.at(1) = 0;
  rep(0, i, n) {
    if (prime.at(i)) {
      for (int j = i + i; j < n; j += i) {
        prime.at(j) = 0;
      }
    }
  }
}

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(0, i, N) { cin >> A.at(i); }
  sort(all(A));

  vector<int> prime(1000000);
  eratosthenes(prime);
  vector<ll> value;

  rep(0, i, 1000000) {
    if (prime.at(i) == 1) {
      value.push_back(i);
    }
  }

  vector<ll> memo(1000010, 0);

  ll flag1 = 1;
  ll flag2 = 0;
  ll a_gcd = A.at(0);
  rep(0, i, N) {
    ll a = A.at(i);
    for (auto v : value) {
      if (a < v) {
        break;
      }
      if (a % v == 0) {
        if (memo.at(v) != 0) {
          flag1 = 0;
        }
        memo.at(v) = 1;
        a /= v;
        if (flag1 == 0) {
          break;
        }
      }
    }
    a_gcd = gcd(a_gcd, A.at(i));
  }
  if (a_gcd == 1) {
    flag2 = 1;
  }

  if (flag1 == 1) {
    cout << "pairwise coprime" << endl;
  } else if (flag2 == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}