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

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i, a, b) for (ll i = a; i < (b); i++)
#define range(a) a.begin(), a.end()
#define endl "\\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
using Graph = vector<vector<int>>;
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL << 60;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}
void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}

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

const int A = 1000005;

int main() {
  int N;
  cin >> N;
  vector<ll> data(N);
  vector<ll> number(A, 0);
  FOR(i, 0, N) {
    cin >> data.at(i);
    number.at(data.at(i))++;
  }

  bool p = true;
  FOR(i, 2, A) {
    int cnt = 0;
    for (int j = i; j < A; j += i) {
      cnt += number.at(j);
      // cout<<cnt<<" ";
      if (cnt >= 2) {
        p = false;
        break;
      }
    }
    if (!p)
      break;
  }

  if (p) {
    cout << "pairwise coprime" << endl;
    return 0;
  }

  int ans = data.at(0);

  FOR(i, 1, N) { ans = gcd(ans, data.at(i)); }

  if (ans == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i, a, b) for (ll i = a; i < (b); i++)
#define range(a) a.begin(), a.end()
#define endl "\\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
using Graph = vector<vector<int>>;
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL << 60;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}
void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}

vector<ll> prime_factorize(long long N) {
  vector<ll> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;

    // 割れる限り割り続ける
    while (N % a == 0) {
      N /= a;
    }

    // その結果を push
    res.push_back(a);
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back(N);
  return res;
}

int vector_finder(vector<ll> vec, ll number) {
  auto itr = find(vec.begin(), vec.end(), number);
  int index = distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return index + 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  int N, max = 1;
  cin >> N;
  vector<ll> number = {1};
  vector<pair<ll, ll>> count = {make_pair(1, 1)};
  FOR(k, 0, N) {
    long long A;
    cin >> A;
    const auto &s = prime_factorize(A);

    FOR(i, 0, s.size()) {

      if (vector_finder(number, s.at(i)) != 0) {
        count.at(vector_finder(number, s.at(i)) - 1).second++;
        if (count.at(vector_finder(number, s.at(i)) - 1).second > max)
          max = count.at(vector_finder(number, s.at(i)) - 1).second;
      } else {
        count.push_back(make_pair(s.at(i), 1));
        number.push_back(s.at(i));
      }
    }
  }

  if (max == 1)
    cout << "pairwise coprime" << endl;
  else if (max == N)
    cout << "not coprime" << endl;
  else
    cout << "setwise coprime" << endl;

  return 0;
}