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

#include <bits/stdc++.h>

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  std::vector<bool> PM(1e6 + 1, true);
  std::vector<int> PM_list;
  std::vector<int> PM_list_sqrt;
  std::map<int, int> PM_map;
  for (int i = 2; i <= 1e6; i++) {
    if (PM[i]) {
      PM_list.push_back(i);
      if (i <= 1000) {
        PM_list_sqrt.push_back(i);
      }
      PM_map[i] = 0;
      for (int j = i + i; j <= 1e6; j += i) {
        PM[j] = false;
      }
    }
  }

  // std::vector< int > PM_cnt(PM_list.size(), 0);

  for (int i = 0; i < N; i++) {
    while (A[i] != 1) {
      auto find_itr = PM_map.find(A[i]);
      if (find_itr != PM_map.end()) {
        PM_map[find_itr->first]++;
        A[i] = 1;
      } else {
        for (int j = 0; j < PM_list_sqrt.size(); j++) {
          if (A[i] % PM_list_sqrt[j] == 0) {
            while (A[i] % PM_list_sqrt[j] == 0) {
              A[i] /= PM_list_sqrt[j];
            }
            PM_map[PM_list_sqrt[j]]++;
            break;
          }
        }
      }
    }
  }

  int f = 2;
  for (int j = 0; j < PM_list.size(); j++) {
    if (PM_map[PM_list[j]] == N) {
      f = 0;
      break;
    } else if (PM_map[PM_list[j]] > 1) {
      f = 1;
    }
  }

  if (f == 2) {
    std::cout << "pairwise coprime" << std::endl;
  } else if (f == 1) {
    std::cout << "setwise coprime" << std::endl;
  } else {
    std::cout << "not coprime" << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  std::vector<bool> PM(1e6 + 1, true);
  std::vector<int> PM_list;
  std::vector<int> PM_list_sqrt;
  std::map<int, int> PM_map;
  for (int i = 2; i <= 1e6; i++) {
    if (PM[i]) {
      PM_list.push_back(i);
      if (i <= 1000) {
        PM_list_sqrt.push_back(i);
      }
      PM_map[i] = 1;
      for (int j = i + i; j <= 1e6; j += i) {
        PM[j] = false;
      }
    }
  }

  std::vector<int> PM_cnt(PM_list.size(), 0);

  for (int i = 0; i < N; i++) {
    std::map<int, int> cur_map;
    while (A[i] != 1) {
      auto find_itr = PM_map.find(A[i]);
      if (find_itr != PM_map.end()) {
        auto find2_itr = cur_map.find(find_itr->first);
        if (find2_itr == cur_map.end()) {
          PM_cnt[find_itr->first]++;
        }
        A[i] = 1;
      } else {
        for (int j = 0; j < PM_list_sqrt.size(); j++) {
          if (A[i] % PM_list_sqrt[j] == 0) {
            auto find2_itr = cur_map.find(PM_list_sqrt[j]);
            if (find2_itr == cur_map.end()) {
              cur_map[PM_list_sqrt[j]] = 1;
              PM_cnt[PM_list_sqrt[j]]++;
              A[i] /= PM_list_sqrt[j];
            } else {
              A[i] /= PM_list_sqrt[j];
            }
            break;
          }
        }
      }
    }
  }

  int f = 2;
  for (int j = 0; j < PM_list.size(); j++) {
    if (PM_cnt[j] == N) {
      f = 0;
    } else if (PM_cnt[j] > 1) {
      f = 1;
    }
  }

  if (f == 2) {
    std::cout << "pairwise coprime" << std::endl;
  } else if (f == 1) {
    std::cout << "setwise coprime" << std::endl;
  } else {
    std::cout << "not coprime" << std::endl;
  }

  return 0;
}