Problem p03651 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

// greatest common divisor and least common multiple
// gcd is calculated by Euclidean Algorithm
// lcm = m * n / gcd(m,n)
template <typename T = int> T gcd(T a, T b) {
  if (a < b)
    return gcd(b, a);
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  ll maxi = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    chmax(maxi, a[i]);
  }
  if (maxi < k) {
    cout << "IMPOSSIBLE"
         << "\n";
    return 0;
  }
  if (n == 1) {
    if (k == a[0])
      cout << "POSSIBLE"
           << "\n";
    else
      cout << "IMPOSSIBLE"
           << "\n";
  }
  ll gcds = gcd(a[0], a[1]);
  for (int i = 2; i < n; ++i) {
    gcds = gcd(gcds, a[i]);
  }
  for (int i = 0; i < n; ++i) {
    if (abs(a[0] - k) % gcds == 0) {
      cout << "POSSIBLE"
           << "\n";
      return 0;
    }
  }

  cout << "IMPOSSIBLE"
       << "\n";
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

// greatest common divisor and least common multiple
// gcd is calculated by Euclidean Algorithm
// lcm = m * n / gcd(m,n)
template <typename T = int> T gcd(T a, T b) {
  if (a < b)
    return gcd(b, a);
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  ll maxi = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    chmax(maxi, a[i]);
  }
  if (maxi < k) {
    cout << "IMPOSSIBLE"
         << "\n";
    return 0;
  }
  if (n == 1) {
    if (k == a[0])
      cout << "POSSIBLE"
           << "\n";
    else
      cout << "IMPOSSIBLE"
           << "\n";
    return 0;
  }
  ll gcds = gcd(a[0], a[1]);
  for (int i = 2; i < n; ++i) {
    gcds = gcd(gcds, a[i]);
  }
  for (int i = 0; i < n; ++i) {
    if (abs(a[0] - k) % gcds == 0) {
      cout << "POSSIBLE"
           << "\n";
      return 0;
    }
  }

  cout << "IMPOSSIBLE"
       << "\n";
  return 0;
}