Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
template <typename T>
using posteriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx[] = {0, -1, -1, -1, 0, 1, 1,
// 1};
template <typename T, typename U> inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}
template <typename T, typename U> inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}
template <typename T> void unique(vector<T> &a) {
  a.erase(unique(ALL(a)), a.end());
}
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

ll __lcm(ll a, ll b) { return a / __gcd(a, b) * b; }

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  REP(i, n) cin >> a[i];
  sort(ALL(a));
  unique(a);
  n = a.size();
  vector<int> cnt(n);
  REP(i, n) {
    a[i] >>= 1;
    int tmp = a[i];
    while (a[i] % 2 == 0) {
      ++cnt[i];
      tmp >>= 1;
    }
  }
  sort(ALL(cnt));
  if (cnt.front() < cnt.back()) {
    cout << "0\n";
    return 0;
  }
  ll l = a[0];
  FOR(i, 1, n) {
    l = __lcm(l, a[i]);
    if (l > m) {
      cout << "0\n";
      return 0;
    }
  }
  m -= l;
  cout << m / (l * 2) + 1 << '\n';
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
template <typename T>
using posteriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx[] = {0, -1, -1, -1, 0, 1, 1,
// 1};
template <typename T, typename U> inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}
template <typename T, typename U> inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}
template <typename T> void unique(vector<T> &a) {
  a.erase(unique(ALL(a)), a.end());
}
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

ll __lcm(ll a, ll b) { return a / __gcd(a, b) * b; }

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  REP(i, n) cin >> a[i];
  sort(ALL(a));
  unique(a);
  n = a.size();
  vector<int> cnt(n);
  REP(i, n) {
    a[i] >>= 1;
    int tmp = a[i];
    while (tmp % 2 == 0) {
      ++cnt[i];
      tmp >>= 1;
    }
  }
  sort(ALL(cnt));
  if (cnt.front() < cnt.back()) {
    cout << "0\n";
    return 0;
  }
  ll l = a[0];
  FOR(i, 1, n) {
    l = __lcm(l, a[i]);
    if (l > m) {
      cout << "0\n";
      return 0;
    }
  }
  m -= l;
  cout << m / (l * 2) + 1 << '\n';
  return 0;
}