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

#include <bits/stdc++.h>
#include <math.h>

using namespace std;

template <typename T> long long modpow(const T n, const T p, const T mod);
template <typename T> long long modinv(const T n, const T mod);
template <typename T> bool chmax(T &a, const T &b);
template <typename T> bool chmin(T &a, const T &b);

long long inf = 1000000007;

int main() {
  vector<vector<long long>> memo(1000001);
  vector<bool> sosu(1000001, true);
  sosu.at(0) = false;
  sosu.at(1) = false;
  for (long long i = 2; i <= 1000000; i++) {
    if (sosu.at(i) == false)
      continue;

    for (long long j = i * 2; j <= 1000000; j += i) {
      sosu.at(j) = false;
    }
  }

  long long n;
  cin >> n;
  vector<long long> v(n);
  set<long long> soinsu;
  map<long long, long long> zisyo;

  string ans = "pairwise coprime";

  for (long long i = 0; i < n; i++)
    cin >> v.at(i);
  sort(v.begin(), v.end());

  for (auto a : v) { // cout<<endl<<a<<"の素因数たち: ";
    long long now = a;
    if (sosu.at(now)) { // cout<<now;
      soinsu.insert(now);
      zisyo[now]++;
      memo.at(now).push_back(now);
      continue;
    }

    for (long long i = 2; i * i <= now; i++) {
      //			if(memo.at(now).size()!=0){
      //				for(auto b:memo.at(now)){//cout<<b<<" ";
      //					zisyo[b]++;
      //					memo.at(a).push_back(b);
      //				}
      //				break;
      //			}

      if (now % i == 0) { // cout<<i<<" ";
        soinsu.insert(i);
        zisyo[i]++;
        memo.at(a).push_back(i);

        while (now % i == 0)
          now /= i;
      }
    }

    if (now != 1) { // cout<<now<<" ";
      soinsu.insert(now);
      zisyo[now]++;
      memo.at(a).push_back(now);
    }
  }

  for (auto it = soinsu.begin(); it != soinsu.end(); it++) {
    if (zisyo.at(*it) >= 2)
      ans = "setwise coprime";
  }

  long long hoge = v.at(0);
  for (long long i = 1; i < n; i++) {
    long long a = hoge;
    long long b = v.at(i);

    while (a % b != 0) {
      long long c = a % b;
      a = b;
      b = c;
    }

    hoge = b;
  }

  if (hoge != 1)
    ans = "not coprime";

  cout << ans << endl;

  return 0;
}

template <typename T> long long modpow(const T n, const T p, const T mod) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    long long a = modpow(n, p / 2, mod);
    return a * a % mod;
  }
  if (p % 2 == 1)
    return (modpow(n, p - 1, mod) * n) % mod;
  cerr << "ERROR" << endl;
  return 1;
}

template <typename T> long long modinv(const T n, const T mod) {
  return modpow(n, mod - 2, mod);
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>

using namespace std;

template <typename T> long long modpow(const T n, const T p, const T mod);
template <typename T> long long modinv(const T n, const T mod);
template <typename T> bool chmax(T &a, const T &b);
template <typename T> bool chmin(T &a, const T &b);

long long inf = 1000000007;

int main() {
  vector<vector<long long>> memo(1000001);
  vector<bool> sosu(1000001, true);
  sosu.at(0) = false;
  sosu.at(1) = false;
  for (long long i = 2; i <= 1000000; i++) {
    if (sosu.at(i) == false)
      continue;

    for (long long j = i * 2; j <= 1000000; j += i) {
      sosu.at(j) = false;
    }
  }

  long long n;
  cin >> n;
  vector<long long> v(n);
  set<long long> soinsu;
  map<long long, long long> zisyo;

  string ans = "pairwise coprime";

  for (long long i = 0; i < n; i++)
    cin >> v.at(i);
  sort(v.begin(), v.end());

  for (auto a : v) { // cout<<endl<<a<<"の素因数たち: ";
    long long now = a;
    if (sosu.at(now)) { // cout<<now;
      soinsu.insert(now);
      zisyo[now]++;
      memo.at(now).push_back(now);
      continue;
    }

    for (long long i = 2; i * i <= now; i++) {
      if (memo.at(now).size() != 0) {
        for (auto b : memo.at(now)) { // cout<<b<<" ";
          zisyo[b]++;
          memo.at(a).push_back(b);
        }
        break;
      }

      if (now % i == 0) { // cout<<i<<" ";
        soinsu.insert(i);
        zisyo[i]++;
        memo.at(a).push_back(i);

        now %= i;
      }
    }

    if (now != 1) { // cout<<now<<" ";
      soinsu.insert(now);
      zisyo[now]++;
      memo.at(a).push_back(now);
    }
  }

  for (auto it = soinsu.begin(); it != soinsu.end(); it++) {
    if (zisyo.at(*it) >= 2)
      ans = "setwise coprime";
    if (zisyo.at(*it) >= n)
      ans = "not coprime";
  }

  cout << ans << endl;

  return 0;
}

template <typename T> long long modpow(const T n, const T p, const T mod) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    long long a = modpow(n, p / 2, mod);
    return a * a % mod;
  }
  if (p % 2 == 1)
    return (modpow(n, p - 1, mod) * n) % mod;
  cerr << "ERROR" << endl;
  return 1;
}

template <typename T> long long modinv(const T n, const T mod) {
  return modpow(n, mod - 2, mod);
}

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}