Problem p03651 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define POW(n) ((n) * (n))
#define ALL(a) (a).begin(), (a).end()
#define dump(v) (cerr << #v << ": " << v << endl)
#define cerr                                                                   \
  if (true)                                                                    \
  cerr

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<unsigned long long> vull;

// ll n = to_T<ll>("114514")
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}
template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  ll r = a % b;

  while (r) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

void solve(long long N, long long K, vector<long long> a) {
  sort(ALL(a));

  ll p = gcd(a[0], a[1]);
  FOR(i, 1, N) { p = gcd(p, a[i]); }

  if (K % p == 0 && a.back() >= K) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  long long K;
  long long N;
  cin >> N;
  vector<long long> A(N - 1 + 1);
  cin >> K;
  for (int i = 0; i <= N - 1; i++) {
    cin >> A[i];
  }
  solve(N, K, A);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define POW(n) ((n) * (n))
#define ALL(a) (a).begin(), (a).end()
#define dump(v) (cerr << #v << ": " << v << endl)
#define cerr                                                                   \
  if (true)                                                                    \
  cerr

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<unsigned long long> vull;

// ll n = to_T<ll>("114514")
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}
template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  ll r = a % b;

  while (r) {
    a = b;
    b = r;
    r = a % b;
  }

  return b;
}

void solve(long long N, long long K, vector<long long> a) {
  a.push_back(a[0]);
  sort(ALL(a));

  ll p = gcd(a[0], a[1]);
  FOR(i, 1, N) { p = gcd(p, a[i]); }

  if (K % p == 0 && a.back() >= K) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  long long K;
  long long N;
  cin >> N;
  vector<long long> A(N - 1 + 1);
  cin >> K;
  for (int i = 0; i <= N - 1; i++) {
    cin >> A[i];
  }
  solve(N, K, A);
  return 0;
}