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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int gcd(int a, int b) {
  int mi = std::min(a, b);
  int ma = std::max(a, b);
  if (mi == 0) {
    return ma;
  } else {
    return gcd(ma % mi, mi);
  }
}

std::set<int> get_prime_factors(int n) {
  std::set<int> result;
  while (n % 2 == 0) {
    n = n / 2;
    result.insert(2);
  }
  for (int i = 3; i <= sqrt(n) + 1; i = i + 2) {
    while (n % i == 0) {
      n = n / i;
      result.insert(i);
    }
  }
  if (n > 2) {
    result.insert(n);
  }
  return result;
}

int main() {
  int n = 0;
  long long base = 1e9 + 7;
  scanf("%d", &n);
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  std::map<int, int> primes;
  for (int i = 0; i < n; i++) {
    std::set<int> current_factors = get_prime_factors(a[i]);
    for (std::set<int>::iterator it = current_factors.begin();
         it != current_factors.end(); ++it) {
      if (primes.find(*it) != primes.end()) {
        primes[*it] += 1;
      } else {
        primes[*it] = 1;
      }
    }
  }
  int max_number = 1;
  for (std::map<int, int>::iterator it = primes.begin(); it != primes.end();
       ++it) {
    max_number = std::max(it->second, max_number);
  }
  if (max_number == 1) {
    printf("pairwise coprime\\n");
  } else {
    if (max_number < n) {
      printf("setwise coprime\\n");
    } else {
      printf("not coprime\\n");
    }
  }

  //  printf("%d\\n", gcd(150, 155));

  //  for(int i=0; i<n; i++){
  //    for(int j=0; j<k+1; j++){
  //      printf("%lld ", arr[i][j]);
  //    }
  //    printf("\\n");
  //  }
  //  printf("%lld\\n", (arr[n-1][k] - arr[n-1][k-1] + base) % base);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

int gcd(int a, int b) {
  int mi = std::min(a, b);
  int ma = std::max(a, b);
  if (mi == 0) {
    return ma;
  } else {
    return gcd(ma % mi, mi);
  }
}

std::set<int> get_prime_factors(int n) {
  std::set<int> result;
  while (n % 2 == 0) {
    n = n / 2;
    result.insert(2);
  }
  for (int i = 3; i < sqrt(n); i = i + 2) {
    while (n % i == 0) {
      n = n / i;
      result.insert(i);
    }
  }
  if (n > 2) {
    result.insert(n);
  }
  return result;
}

int main() {
  int n = 0;
  long long base = 1e9 + 7;
  scanf("%d", &n);
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  std::set<int> cumulative_factors = get_prime_factors(a[0]);
  std::set<int> current_factors;
  std::set<int> intersect;
  bool found_noncomp = false;
  int current_d = a[0];
  for (int i = 1; i < n; i++) {
    current_d = gcd(current_d, a[i]);
    if (!found_noncomp) {
      current_factors = get_prime_factors(a[i]);
      std::set<int> intersect;
      std::set_intersection(cumulative_factors.begin(),
                            cumulative_factors.end(), current_factors.begin(),
                            current_factors.end(),
                            std::inserter(intersect, intersect.begin()));
      if (intersect.size() > 0) {
        found_noncomp = true;
      } else {
        set<int>::iterator it;
        for (it = current_factors.begin(); it != current_factors.end(); ++it) {
          cumulative_factors.insert(*it);
        }
        // std::set<int> my_union;
        // std::set_union(cumulative_factors.begin(), cumulative_factors.end(),
        // current_factors.begin(), current_factors.end(),
        // std::inserter(my_union, my_union.begin())); cumulative_factors =
        // my_union;
      }
    }
  }

  if (current_d > 1) {
    printf("not coprime\\n");
  } else {
    if (found_noncomp) {
      printf("setwise coprime\\n");
    } else {
      printf("pairwise coprime\\n");
    }
  }

  //  printf("%d\\n", gcd(150, 155));

  //  for(int i=0; i<n; i++){
  //    for(int j=0; j<k+1; j++){
  //      printf("%lld ", arr[i][j]);
  //    }
  //    printf("\\n");
  //  }
  //  printf("%lld\\n", (arr[n-1][k] - arr[n-1][k-1] + base) % base);
}