Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int INF = 1e9;

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

bool IsPrime(long long n) {
  if (n == 1)
    return false;
  for (long long i = 2; i * i < n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

vector<long long> EnumDivisors(long long n) {
  vector<long long> res;

  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (n / i != i)
        res.push_back(n / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

vector<pair<long long, long long>> PrimeFactorize(long long n) {
  vector<pair<long long, long long>> res;

  long long a = 0;
  long long b = 0;

  for (long long i = 2; i * i <= n; i++) {
    a = 0;
    if (n % i == 0) {
      if (IsPrime(i)) {
        while (n % i == 0) {
          n /= i;
          a++;
        }
        res.push_back(make_pair(i, a));
      }
    }
  }

  if (n != 1)
    res.push_back(make_pair(n, 1));

  return res;
}

int main() {
  int n;
  const int max_n = 1e6;
  long long a[max_n + 1];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<int> list(max_n + 1, 0);
  bool flag1 = 0;
  bool flag2 = 0;
  for (int i = 0; i < n; i++) {
    vector<pair<long long, long long>> res;
    res = PrimeFactorize(a[i]);
    for (int j = 0; j < res.size(); j++) {
      int num = res[j].first;
      if (list[num] > 0)
        flag1 = 1;
      list[num]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (list[i] == n)
      flag2 = 1;
  }

  string ans = "not coprime";
  if (flag2 == 0)
    ans = "setwise coprime";
  if (flag1 == 0)
    ans = "pairwise coprime";

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int INF = 1e9;

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

bool IsPrime(long long n) {
  if (n == 1)
    return false;
  for (long long i = 2; i * i < n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

vector<long long> EnumDivisors(long long n) {
  vector<long long> res;

  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.push_back(i);
      if (n / i != i)
        res.push_back(n / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

vector<pair<long long, long long>> PrimeFactorize(long long n) {
  vector<pair<long long, long long>> res;

  long long a = 0;
  long long b = 0;

  for (long long i = 2; i * i <= n; i++) {
    a = 0;
    if (n % i == 0) {
      if (IsPrime(i)) {
        while (n % i == 0) {
          n /= i;
          a++;
        }
        res.push_back(make_pair(i, a));
      }
    }
  }

  if (n != 1)
    res.push_back(make_pair(n, 1));

  return res;
}

int main() {
  int n;
  const int max_n = 1e6;
  long long a[max_n + 1];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<int> list(n, 0);
  bool flag1 = 0;
  bool flag2 = 0;
  for (int i = 0; i < n; i++) {
    vector<pair<long long, long long>> res;
    res = PrimeFactorize(a[i]);
    for (int j = 0; j < res.size(); j++) {
      int num = res[j].first;
      if (list[num] > 0)
        flag1 = 1;
      list[num]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (list[i] == n)
      flag2 = 1;
  }

  string ans = "not coprime";
  if (flag2 == 0)
    ans = "setwise coprime";
  if (flag1 == 0)
    ans = "pairwise coprime";

  cout << ans << endl;

  return 0;
}