Problem p03651 (C++)

Buggy submission (status: Runtime Error):
// include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int MOD = 1000000007;

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// main code

// 条件
// 0. 目標より大きい数字が存在する
// 1. 互いに素な組が存在する
// 2. なかった時、目標より大きい数字

LL gcd(LL bigger, LL smaller) {
  if (bigger % smaller != 0) {
    return gcd(smaller, (bigger % smaller));
  }
  return smaller;
}

int main(int argc, char const *argv[]) {
  int N, K;
  string ok = "POSSIBLE", ng = "IMPOSSIBLE";
  cin >> N >> K;
  VI a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  SORT(a);

  if (N == 1 and a[0] != K) {
    cout << ng << endl;
    return 0;
  }

  LL whole_gcd = gcd(a[0], a[1]);
  for (int i = 2; i < N; i++) {
    whole_gcd = gcd(whole_gcd, a[i]);
  }

  for (int i = a.back(); i >= K; i -= whole_gcd) {
    if (i == K) {
      cout << ok << endl;
      return 0;
    }
  }

  cout << ng << endl;
  return 0;
}


Fixed submission:
// include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int MOD = 1000000007;

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// main code

// 条件
// 0. 目標より大きい数字が存在する
// 1. 互いに素な組が存在する
// 2. なかった時、目標より大きい数字

LL gcd(LL bigger, LL smaller) {
  if (bigger % smaller != 0) {
    return gcd(smaller, (bigger % smaller));
  }
  return smaller;
}

int main(int argc, char const *argv[]) {
  int N, K;
  string ok = "POSSIBLE", ng = "IMPOSSIBLE";
  cin >> N >> K;
  VI a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  SORT(a);

  if (N == 1) {
    if (a[0] != K) {
      cout << ng << endl;
      return 0;
    } else {
      cout << ok << endl;
      return 0;
    }
  }

  LL whole_gcd = gcd(a[0], a[1]);
  for (int i = 2; i < N; i++) {
    whole_gcd = gcd(whole_gcd, a[i]);
  }

  for (int i = a.back(); i >= K; i -= whole_gcd) {
    if (i == K) {
      cout << ok << endl;
      return 0;
    }
  }

  cout << ng << endl;
  return 0;
}