Problem p03651 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdbool>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<ll, ll> P;
ll ma = 1000000000 + 7;
ll mx = 1000003;
ll h, w, n, m, k;
string s, t;
int dis[101][101];
int graph[52][52];
bool vis[52];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
void comb(vector<vector<ll>> &v) {
  for (ll i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (ll k = 1; k < v.size(); k++) {
    for (int j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % ma;
    }
  }
}

ll GetDigit(ll num) { return log10(num) + 1; }
int sy, sx;
char maze[1010][1010];
P nums[10];
ll bfs(ll goal) {
  queue<P> que;
  rep(i, h) {
    rep(j, w) { dis[i][j] = -1; }
  }
  que.push(P(sy, sx));
  dis[sy][sx] = 0;

  while (que.size() > 0) {
    P p = que.front();
    que.pop();
    if (p.first == nums[goal].first && p.second == nums[goal].second)
      break;
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && maze[ny][nx] != 'X' &&
          dis[ny][nx] == -1) {
        que.push(P(ny, nx));
        dis[ny][nx] = dis[p.first][p.second] + 1;
      }
    }
  }
  sy = nums[goal].first;
  sx = nums[goal].second;
  return dis[nums[goal].first][nums[goal].second];
}

int main() {
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end(), greater<ll>());
  if (k > a[0]) {
    cout << "IMPOSSIBLE" << endl;
    return 0;
  }
  rep(i, n) {
    if (k == a[i]) {
      cout << "POSSIBLE" << endl;
      return 0;
    }
  }
  rep(i, n - 1) {
    for (ll j = i + 1; j < n; j++) {
      ll tmp = abs(a[i] - a[j]);
      if (tmp == 0) {
        continue;
      }
      if (k % tmp == 0) {
        cout << "POSSIBLE" << endl;
        return 0;
      }
    }
  }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdbool>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<ll, ll> P;
ll ma = 1000000000 + 7;
ll mx = 1000003;
ll h, w, n, m, k;
string s, t;
int dis[101][101];
int graph[52][52];
bool vis[52];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
void comb(vector<vector<ll>> &v) {
  for (ll i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (ll k = 1; k < v.size(); k++) {
    for (int j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]) % ma;
    }
  }
}

ll GetDigit(ll num) { return log10(num) + 1; }
int sy, sx;
char maze[1010][1010];
P nums[10];
ll bfs(ll goal) {
  queue<P> que;
  rep(i, h) {
    rep(j, w) { dis[i][j] = -1; }
  }
  que.push(P(sy, sx));
  dis[sy][sx] = 0;

  while (que.size() > 0) {
    P p = que.front();
    que.pop();
    if (p.first == nums[goal].first && p.second == nums[goal].second)
      break;
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && maze[ny][nx] != 'X' &&
          dis[ny][nx] == -1) {
        que.push(P(ny, nx));
        dis[ny][nx] = dis[p.first][p.second] + 1;
      }
    }
  }
  sy = nums[goal].first;
  sx = nums[goal].second;
  return dis[nums[goal].first][nums[goal].second];
}

int main() {
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end(), greater<ll>());
  if (k > a[0]) {
    cout << "IMPOSSIBLE" << endl;
    return 0;
  }
  rep(i, n) {
    if (k == a[i]) {
      cout << "POSSIBLE" << endl;
      return 0;
    }
  }
  ll gc = gcd(a[0], a[1]);
  for (ll i = 2; i < n; i++) {
    gc = gcd(gc, a[i]);
  }
  if (k % gc == 0) {
    cout << "POSSIBLE" << endl;
    return 0;
  }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}