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

#pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int, int>
#define mod 1000000007
#define MAX 10000003
#define F first
#define S second
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, a, n) for (int i = a; i < n; i++)
#define all(v) v.begin(), v.end()
#define input(arr, n)                                                          \\
  for (ll i1 = 0; i1 < n; i1++)                                                \\
  cin >> arr[i1]
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update> // s.order_of_key(val)
                                          // *s.find_by_order(ind)
int1024_t gcd(int1024_t a, int1024_t b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int1024_t lcm(int1024_t a, int1024_t b) { return (a * b) / gcd(a, b); }
int prime[MAX];
unordered_map<int, int> max_map;

// Function to return a^n
int power(int a, int n) {
  if (n == 0)
    return 1;
  int p = power(a, n / 2) % mod;
  p = (p * p) % mod;
  if (n & 1)
    p = (p * a) % mod;
  return p;
}

// Function to find the smallest prime factors
// of numbers upto MAX
void sieve() {
  prime[0] = prime[1] = 1;
  for (int i = 2; i < MAX; i++) {
    if (prime[i] == 0) {
      for (int j = i * 2; j < MAX; j += i) {
        if (prime[j] == 0) {
          prime[j] = i;
        }
      }
      prime[i] = i;
    }
  }
}

// Function to return the LCM modulo M
ll lcmModuloM(const int *ar, int n) {

  for (int i = 0; i < n; i++) {
    int num = ar[i];
    unordered_map<int, int> temp;

    // Temp stores mapping of prime factor to
    // its power for the current element
    while (num > 1) {

      // Factor is the smallest prime factor of num
      int factor = prime[num];

      // Increase count of factor in temp
      temp[factor]++;

      // Reduce num by its prime factor
      num /= factor;
    }

    for (auto it : temp) {

      // Store the highest power of every prime
      // found till now in a new map max_map
      max_map[it.first] = max(max_map[it.first], it.second);
    }
  }

  ll ans = 1;

  for (auto it : max_map) {

    // LCM is product of primes to their highest powers modulo M
    ans = (ans * power(it.F, it.S)) % mod;
  }

  return ans;
}
int main() {
  fio;
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    rep(i, n) cin >> a[i];
    // sort(a,a+n);
    int1024_t g = a[0];
    int1024_t s = a[0];
    repp(i, 1, n) { g = gcd(g, a[i]); }
    int1024_t flag = 0;
    int1024_t y = a[0];
    sieve();
    s = lcmModuloM(a, n);
    repp(i, 1, n) {
      // s=lcm(s,a[i])%mod;;
      y = ((y % mod) * (a[i] % mod)) % mod;
    }
    if (s != y)
      flag = 1;
    if (flag == 0) {
      cout << "pairwise coprime" << endl;
    } else {
      if (g == 1)
        cout << "setwise coprime" << endl;
      else
        cout << "not coprime" << endl;
    }
  }
}

Error: 0

Fixed submission:

#pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int, int>
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, a, n) for (int i = a; i < n; i++)
#define all(v) v.begin(), v.end()
#define input(arr, n)                                                          \\
  for (ll i1 = 0; i1 < n; i1++)                                                \\
  cin >> arr[i1]
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update> // s.order_of_key(val)
                                          // *s.find_by_order(ind)
ll lcm(ll a, ll b) { return (a / __gcd(a, b)) * b; }
int main() {
  fio;
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    ll a[n];
    rep(i, n) cin >> a[i];
    // sort(a,a+n);
    ll g = a[0];
    ll s = a[0];
    repp(i, 1, n) { g = __gcd(g, a[i]); }
    ll flag = 0;
    ll y = a[0];
    repp(i, 1, n) {
      s = lcm(s, a[i]);
      y *= a[i];
      if (s != y) {
        flag = 1;
        break;
      }
    }
    if (flag == 0) {
      cout << "pairwise coprime" << endl;
    } else {
      if (g == 1)
        cout << "setwise coprime" << endl;
      else
        cout << "not coprime" << endl;
    }
  }
}