Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  int c = -1;
  while (c != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}

int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  int N, M;
  cin >> N >> M;
  vi a(N);
  rep(i, N) { cin >> a[i]; }
  int n2 = -1;
  rep(i, N) {
    int k = 0;
    int d = 2;
    while (a[i] % d == 0) {
      d <<= 1;
      k++;
    }
    if (n2 < 0) {
      n2 = k;
    } else if (n2 != k) {
      cout << 0 << endl;
      return 0;
    }
  }
  n2--;
  rep(i, N) { a[i] /= 2; }
  rep(i, n2) {
    M /= 2;
    rep(j, N) { a[i] /= 2; }
  }
  ll l = a[0];
  rep(i, N) {
    l = lcm(l, a[i]);
    if (l > M) {
      cout << 0 << endl;
      return 0;
    }
  }
  ll s = M / l;
  ll ans = (s + 1) / 2;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  int c = -1;
  while (c != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}

int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  int N, M;
  cin >> N >> M;
  vi a(N);
  rep(i, N) { cin >> a[i]; }
  int n2 = -1;
  rep(i, N) {
    int k = 0;
    int d = 2;
    while (a[i] % d == 0) {
      d <<= 1;
      k++;
    }
    if (n2 < 0) {
      n2 = k;
    } else if (n2 != k) {
      cout << 0 << endl;
      return 0;
    }
  }
  n2--;
  rep(i, N) { a[i] /= 2; }
  rep(i, n2) {
    M /= 2;
    rep(j, N) { a[j] /= 2; }
  }
  ll l = a[0];
  rep(i, N) {
    l = lcm(l, a[i]);
    if (l > M) {
      cout << 0 << endl;
      return 0;
    }
  }
  ll s = M / l;
  ll ans = (s + 1) / 2;
  cout << ans << endl;
  return 0;
}