Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define MOD_1_000_000_007 (1000000007LL)
#define LINF (1LL << 60)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep1(i, n) for (long long i = 1; i < (long long)(n); i++)
#define rep2(i, n) for (long long i = 2; i < (long long)(n); i++)
#define rep0c(i, n) for (long long i = 0; i <= (long long)(n); i++)
#define rep1c(i, n) for (long long i = 1; i <= (long long)(n); i++)
#define rep2c(i, n) for (long long i = 2; i <= (long long)(n); i++)
#define repc0(n, i) for (long long i = (long long)(n); i >= 0; i--)
#define repc1(n, i) for (long long i = (long long)(n); i >= 1; i--)
#define repc2(n, i) for (long long i = (long long)(n); i >= 2; i--)

namespace solver {
using namespace std;
typedef long long ll;

ll N, M, A[100010];

ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

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

void init() {
  scanf("%lld %lld", &N, &M);
  rep(i, N) {
    scanf("%lld", A + i);
    A[i] /= 2;
  }
}

void solve() {
  while (A[0] % 2 == 0) {
    rep(i, N) if (A[i] % 2 == 0) { A[i] /= 2; }
    else {
      printf("0\n");
      return;
    }
    M /= 2;
  }
  rep(i, N) if (A[i] % 2 == 0) {
    printf("0\n");
    return;
  }
  ll acc = A[0];
  rep1(i, N) acc = lcm(A[i], acc);
  ll ans = 0;
  ll a = M / acc;
  if (a % 2)
    ans = (a + 1) / 2;
  else
    ans = a / 2;
  printf("%lld\n", ans);
}
} // namespace solver

int main() {
  solver::init();
  solver::solve();
  return 0;
}

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

#define MOD_1_000_000_007 (1000000007LL)
#define LINF (1LL << 60)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep1(i, n) for (long long i = 1; i < (long long)(n); i++)
#define rep2(i, n) for (long long i = 2; i < (long long)(n); i++)
#define rep0c(i, n) for (long long i = 0; i <= (long long)(n); i++)
#define rep1c(i, n) for (long long i = 1; i <= (long long)(n); i++)
#define rep2c(i, n) for (long long i = 2; i <= (long long)(n); i++)
#define repc0(n, i) for (long long i = (long long)(n); i >= 0; i--)
#define repc1(n, i) for (long long i = (long long)(n); i >= 1; i--)
#define repc2(n, i) for (long long i = (long long)(n); i >= 2; i--)

namespace solver {
using namespace std;
typedef long long ll;

ll N, M, A[100010];

ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

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

void init() {
  scanf("%lld %lld", &N, &M);
  rep(i, N) {
    scanf("%lld", A + i);
    A[i] /= 2;
  }
}

void solve() {
  while (A[0] % 2 == 0) {
    rep(i, N) if (A[i] % 2 == 0) { A[i] /= 2; }
    else {
      printf("0\n");
      return;
    }
    M /= 2;
  }
  rep(i, N) if (A[i] % 2 == 0) {
    printf("0\n");
    return;
  }
  ll acc = A[0];
  rep1(i, N) {
    acc = lcm(A[i], acc);
    if (acc > M) {
      printf("0\n");
      return;
    }
  }
  ll ans = 0;
  ll a = M / acc;
  if (a % 2)
    ans = (a + 1) / 2;
  else
    ans = a / 2;
  printf("%lld\n", ans);
}
} // namespace solver

int main() {
  solver::init();
  solver::solve();
  return 0;
}