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

#include <bits/stdc++.h>

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;

// status unsolved
long long GCD(long long a, long long b) {
  if (a < 0)
    a = -a;
  if (b < 0)
    b = -b;
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
set<int> prime_factorize(int n) {
  set<int> res;
  for (long long p = 2; p * p <= n; ++p) {
    if (n % p != 0)
      continue;
    int num = 0;
    while (n % p == 0) {
      ++num;
      n /= p;
    }
    res.insert(p);
  }
  if (n != 1)
    res.insert(n);
  return res;
}

int main() {
  long long n;
  cin >> n;
  vector<int> a(n);

  rep(i, n) cin >> a[i];

  // long long setwise=a[0];
  // rep2(i,n-1){
  //     setwise=GCD(a[i],setwise);
  // }
  // cout<<setwise<<endl;

  set<int> tree;
  bool p = true;
  // vector<int> cnt(1000010,0);
  rep(i, n) {
    // int k=1;
    // int tmp=a[i];
    /*
    while(a[i]>1){
        k++;
        if(a[i]%k==0){
            //見つかる
            if(tree.find(k)!=tree.end()){
                p=false;
                break;
            }
            else{
                //見つからない
                tree.insert(k);
            }
            while(a[i]%k==0)a[i]/=k;
        }
    }*/
    auto res = prime_factorize(a[i]);
    auto iter = res.begin();

    while (iter != res.end()) {
      // cnt[iter->first]++;
      // cnt[*iter]++;
      if (tree.find(*iter) != tree.end()) {
        p = false;
        break;
      } else {
        tree.emplace(*iter);
      }
      iter++;
    }
  }

  if (p) {
    cout << "pairwise coprime" << endl;
  } else {
    long long setwise = a[0];
    rep2(i, n - 1) { setwise = GCD(a[i], setwise); }
    if (setwise == 1) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "not coprime" << endl;
    }
  }

  // bool t=true;
  // rep2(i,1000010){
  //     if(cnt[i]==n){
  //         t=false;
  //         break;
  //     }
  // }
  // if(!t){
  //     cout<<"not coprime"<<endl;
  // }
  // else if(p){
  //     cout<<"pairwise coprime"<<endl;
  // }
  // else{
  //     cout<<"setwise coprime"<<endl;
  // }
  return 0;
}
/*


*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;

// status unsolved
long long GCD(long long a, long long b) {
  if (a < 0)
    a = -a;
  if (b < 0)
    b = -b;
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
set<int> prime_factorize(int n) {
  set<int> res;
  for (long long p = 2; p * p <= n; ++p) {
    if (n % p != 0)
      continue;
    int num = 0;
    while (n % p == 0) {
      ++num;
      n /= p;
    }
    res.insert(p);
  }
  if (n != 1)
    res.insert(n);
  return res;
}

int main() {
  long long n;
  cin >> n;
  vector<int> a(n);

  rep(i, n) cin >> a[i];

  // long long setwise=a[0];
  // rep2(i,n-1){
  //     setwise=GCD(a[i],setwise);
  // }
  // cout<<setwise<<endl;

  set<int> tree;
  bool p = true;
  vector<int> cnt(1000010, 0);
  rep(i, n) {
    // int k=1;
    // int tmp=a[i];
    /*
    while(a[i]>1){
        k++;
        if(a[i]%k==0){
            //見つかる
            if(tree.find(k)!=tree.end()){
                p=false;
                break;
            }
            else{
                //見つからない
                tree.insert(k);
            }
            while(a[i]%k==0)a[i]/=k;
        }
    }*/
    auto res = prime_factorize(a[i]);
    auto iter = res.begin();

    while (iter != res.end()) {
      // cnt[iter->first]++;
      cnt[*iter]++;
      if (tree.find(*iter) != tree.end()) {
        p = false;
      } else {
        tree.emplace(*iter);
      }
      iter++;
    }
  }
  bool t = true;
  rep2(i, 1000010) {
    if (cnt[i] == n) {
      t = false;
      break;
    }
  }
  if (!t) {
    cout << "not coprime" << endl;
  } else if (p) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  return 0;
}
/*


*/