Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \
  std::sort(v.begin(), v.end(),                                                \
            std::greater<decltype(v[0])>()) // 降順でVをソート
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

ll n, m;
Vi aa;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout<<std::fixed<<std::setprecision(30);
  cin >> n >> m;
  // rep(i,10000)rep(j,10000)dp[i][j]=0;
  // memset(dp,0,sizeof(dp));
  aa.resize(n);
  rep(i, n) cin >> aa[i];
  ll tmp = 1;
  bool flg = true;
  rep(i, n) {
    tmp = lcm(tmp, aa[i] / 2);
    if ((tmp * 2 / aa[i]) % 2 == 0) {
      flg = false;
      break;
    }
  }
  ll ans = m / tmp;
  if (!flg) {
    print(0);
  } else if (ans % 2 == 1) {
    print((ans - 1) / 2 + 1);
  } else {
    print(ans / 2);
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \
  std::sort(v.begin(), v.end(),                                                \
            std::greater<decltype(v[0])>()) // 降順でVをソート
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

ll n, m;
Vi aa;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout<<std::fixed<<std::setprecision(30);
  cin >> n >> m;
  // rep(i,10000)rep(j,10000)dp[i][j]=0;
  // memset(dp,0,sizeof(dp));
  aa.resize(n);
  rep(i, n) cin >> aa[i];
  ll tmp = 1;
  bool flg = true;
  rep(i, n) {
    tmp = lcm(tmp, aa[i] / 2);
    if ((tmp * 2 / aa[i]) % 2 == 0) {
      flg = false;
      break;
    }
  }
  ll ans = m / tmp;
  if (!flg) {
    print(0);
  } else if (ans % 2 == 1) {
    print((ans - 1) / 2 + 1);
  } else {
    print(ans / 2);
  }
  return 0;
}