Problem p03651 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>

#ifdef _MSC_VER
#include <Windows.h>
#else
#include <unistd.h>
#endif

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = (LL)1e9 + 7;
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

#define INF 1LL << 50

LL A[(int)1e5 + 3];
int main() {
  LL N, K;
  cin >> N >> K;
  set<LL> st;
  REP(i, N) { cin >> A[i]; }
  sort(A, A + N);
  bool flag = false;
  if (N == 1) {
    if (A[0] == K)
      flag = true;
    if (flag) {
      cout << "POSSIBLE" << endl;
    } else {
      cout << "IMPOSSIBLE" << endl;
    }
    return 0;
  }
  REP(i, N - 1) st.insert(abs(A[i + 1] - A[i]));

  REP(i, N) {
    if (A[i] == K) {
      flag = true;
      break;
    } else if (A[i] > K) {
      for (auto s : st) {
        if ((A[i] - K) % s == 0) {
          flag = true;
          break;
        }
      }
      if (flag)
        break;
    }
  }
  if (flag) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>

#ifdef _MSC_VER
#include <Windows.h>
#else
#include <unistd.h>
#endif

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = (LL)1e9 + 7;
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

#define INF 1LL << 50

LL A[(int)1e5 + 3];
int main() {
  LL N, K;
  cin >> N >> K;
  set<LL> st;
  REP(i, N) { cin >> A[i]; }
  sort(A, A + N);
  bool flag = false;
  if (N == 1) {
    if (A[0] == K)
      flag = true;
    if (flag) {
      cout << "POSSIBLE" << endl;
    } else {
      cout << "IMPOSSIBLE" << endl;
    }
    return 0;
  }
  REP(i, N - 1) {
    if (A[i + 1] != A[i])
      st.insert(abs(A[i + 1] - A[i]));
  }
  REP(i, N) {
    if (A[i] == K) {
      flag = true;
      break;
    } else if (A[i] > K) {
      for (auto s : st) {
        if ((A[i] - K) % s == 0) {
          flag = true;
          break;
        }
      }
      if (flag)
        break;
    }
  }
  if (flag) {
    cout << "POSSIBLE" << endl;
  } else {
    cout << "IMPOSSIBLE" << endl;
  }
  return 0;
}