prithivMLmods/Coder-Stat huggingface dataset:
Problem p02814 (C++)

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

using namespace std;

using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll gcd(ll a, ll b) {
  if (a < b) {
    ll x;
    x = a;
    a = b;
    b = x;
  }
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
template <typename T> T lcm(T x, T y) {
  return x / gcd(max(x, y), min(x, y)) * y;
}

int main() {
  ll n, m, i, j, k;
  cin >> n >> m;
  vector<ll> a(n);
  ll l = 1;
  for (auto &x : a) {
    cin >> x;
    l = lcm(l, x / 2);
  }
  ll ret = 0;
  // cout << l << endl;
  for (auto &x : a) {
    if (l % x == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << (m / l + 1) / 2 << endl;
  return 0;
}


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

using namespace std;

using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll gcd(ll a, ll b) {
  if (a < b) {
    ll x;
    x = a;
    a = b;
  }
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
template <typename T> T lcm(T x, T y) {
  return x / gcd(max(x, y), min(x, y)) * y;
}

int main() {
  ll n, m, i, j, k;
  cin >> n >> m;
  vector<ll> a(n);
  ll l = 1;
  for (auto &x : a) {
    cin >> x;
    l = lcm(l, x / 2);
  }
  ll ret = 0;
  // cout << l << endl;
  for (auto &x : a) {
    if (l % x == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << (m / l + 1) / 2 << endl;
  return 0;
}
Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

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

int f(int x) {
  int res = 0;
  while (x % 2 == 0) {
    x /= 2;
    res++;
  }
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  // a -> a'にしている
  REP(i, n) {
    if (a[i] % 2 == 1) {
      cout << 0 << endl;
      return 0;
    }
    a[i] /= 2;
  }

  /* a' -> a''、つまり上記で言う所の2^tで割ってる
     これをやると一々a'の最大に最小公倍数を
     合わせるとか考えることがなくなるので楽
  */
  int t = f(a[0]);
  REP(i, n) {
    if (f(a[i] != t)) {
      cout << 0 << endl;
      return 0;
    }
    a[i] >>= t; // a[i] /= 2^t
  }
  m >>= t;

  ll l = 1;
  REP(i, n) {
    l = lcm(l, a[i]);
    if (l > m) {
      cout << 0 << endl;
      return 0;
    }
  }
  // m -> m'
  m /= l;

  /*
    これはm'以下の奇数の数、つまり
    mを最小公倍数で割った時の奇数倍を表す
    (奇数倍の数=倍数中の奇数の数を求めれば良い)
  */
  ll ans = (m + 1) / 2;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

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

int f(int x) {
  int res = 0;
  while (x % 2 == 0) {
    x /= 2;
    res++;
  }
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  // a -> a'にしている
  REP(i, n) {
    if (a[i] % 2 == 1) {
      cout << 0 << endl;
      return 0;
    }
    a[i] /= 2;
  }

  /* a' -> a''、つまり上記で言う所の2^tで割ってる
     これをやると一々a'の最大に最小公倍数を
     合わせるとか考えることがなくなるので楽
  */
  int t = f(a[0]);
  REP(i, n) {
    if (f(a[i]) != t) {
      cout << 0 << endl;
      return 0;
    }
    a[i] >>= t; // a[i] /= 2^t
  }
  m >>= t;

  ll l = 1;
  REP(i, n) {
    l = lcm(l, a[i]);
    if (l > m) {
      cout << 0 << endl;
      return 0;
    }
  }
  // m -> m'
  m /= l;

  /*
    これはm'以下の奇数の数、つまり
    mを最小公倍数で割った時の奇数倍を表す
    (奇数倍の数=倍数中の奇数の数を求めれば良い)
  */
  ll ans = (m + 1) / 2;
  cout << ans << endl;

  return 0;
}
Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

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

int f(int x) {
  int res = 0;

  while (x % 2 == 0) {
    x /= 2;
    res++;
  }

  return res;
}

int main(void) {
  // Your code here!
  int n, m;
  cin >> n >> m;

  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {

    if (a[i] % 2 == 1) {
      cout << 0 << endl;
      return 0;
    }

    a[i] /= 2;
  }

  int t = f(a[0]);

  for (int i = 0; i < n; i++) {

    if (f(a[i] != t)) {
      cout << 0 << endl;
      return 0;
    }

    a[i] = a[i] >> t;
  }

  m = m >> t;

  long long l = 1;

  for (int i = 0; i < n; i++) {
    l = lcm(l, a[i]);

    if (l > m) {
      cout << 0 << endl;
      return 0;
    }
  }

  m /= l;

  int ans = (m + 1) / 2;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

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

int f(int x) {
  int res = 0;

  while (x % 2 == 0) {
    x /= 2;
    res++;
  }

  return res;
}

int main(void) {
  // Your code here!
  int n, m;
  cin >> n >> m;

  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {

    if (a[i] % 2 == 1) {
      cout << 0 << endl;
      return 0;
    }

    a[i] /= 2;
  }

  int t = f(a[0]);

  for (int i = 0; i < n; i++) {

    if (f(a[i]) != t) {
      cout << 0 << endl;
      return 0;
    }

    a[i] = a[i] >> t;
  }

  m = m >> t;

  long long l = 1;

  for (int i = 0; i < n; i++) {
    l = lcm(l, a[i]);

    if (l > m) {
      cout << 0 << endl;
      return 0;
    }
  }

  m /= l;

  int ans = (m + 1) / 2;
  cout << ans << endl;
  return 0;
}
Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

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

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> b(N);
  for (ll i = 0; i < N; i++) {
    cin >> b[i];
    b[i] = b[i] / 2;
  }

  ll L = lcm(b[0], b[1]);
  for (ll i = 2; i < N; i++) {
    L = lcm(L, b[i]);
    if (L > M) {
      printf("0\n");
      return 0;
    }
  }

  ll C = M / L;

  ll D = M / (2 * L);

  for (ll i = 0; i < N; i++) {
    if ((L / b[i]) % 2 == 0) {
      D = C;
    }
  }

  printf("%lld\n", C - D);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

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

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> b(N);
  for (ll i = 0; i < N; i++) {
    cin >> b[i];
    b[i] = b[i] / 2;
  }

  if (N == 1) {
    printf("%lld\n", M / b[0] - M / (2 * b[0]));
    return 0;
  }

  ll L = lcm(b[0], b[1]);
  for (ll i = 2; i < N; i++) {
    L = lcm(L, b[i]);
    if (L > M) {
      printf("0\n");
      return 0;
    }
  }

  ll C = M / L;

  ll D = M / (2 * L);

  for (ll i = 0; i < N; i++) {
    if ((L / b[i]) % 2 == 0) {
      D = C;
    }
  }

  printf("%lld\n", C - D);
}
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

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

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll x = a[0];
  ll y = 0;
  while (x % 2 == 0) {
    x /= 2;
    y++;
  }
  ll shift = 1ll << y;
  for (ll i = 0; i < n; i++) {
    if (a[i] % shift != 0 || a[i] % (shift << 1) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (ll i = 0; i < n; i++) {
    a[i] >>= 1;
  }
  vector<ll> ruiseki(n);
  ruiseki[0] = a[0];
  for (ll i = 1; i < n; i++) {
    ruiseki[i] = ruiseki[i - 1] / gcd(ruiseki[i - 1], a[i]) * a[i];
    // if(ruiseki[i] > m) {
    //     cout << 0 << endl;
    //     return 0;
    // }
  }

  printf("%lld\n", ll(ceil(m / ruiseki[n - 1] / 2.0)));
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

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

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll x = a[0];
  ll y = 0;
  while (x % 2 == 0) {
    x /= 2;
    y++;
  }
  ll shift = 1ll << y;
  for (ll i = 0; i < n; i++) {
    if (a[i] % shift != 0 || a[i] % (shift << 1) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (ll i = 0; i < n; i++) {
    a[i] >>= 1;
  }
  vector<ll> ruiseki(n);
  ruiseki[0] = a[0];
  for (ll i = 1; i < n; i++) {
    ruiseki[i] = ruiseki[i - 1] / gcd(ruiseki[i - 1], a[i]) * a[i];
    if (ruiseki[i] > m) {
      cout << 0 << endl;
      return 0;
    }
  }

  printf("%lld\n", ll(ceil(m / ruiseki[n - 1] / 2.0)));
}
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

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

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll x = a[0];
  ll y = 0;
  while (x % 2 == 0) {
    x /= 2;
    y++;
  }
  ll shift = 1ll << y;
  for (ll i = 0; i < n; i++) {
    if (a[i] % shift != 0 || a[i] % (shift << 1) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (ll i = 0; i < n; i++) {
    a[i] >>= 1;
  }
  vector<ll> ruiseki(n);
  ruiseki[0] = a[0];
  for (ll i = 1; i < n; i++) {
    ruiseki[i] = ruiseki[i - 1] * a[i] / gcd(ruiseki[i - 1], a[i]);
  }

  printf("%lld\n", ll(ceil(m / ruiseki[n - 1] / 2.0)));
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

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

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll x = a[0];
  ll y = 0;
  while (x % 2 == 0) {
    x /= 2;
    y++;
  }
  ll shift = 1ll << y;
  for (ll i = 0; i < n; i++) {
    if (a[i] % shift != 0 || a[i] % (shift << 1) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (ll i = 0; i < n; i++) {
    a[i] >>= 1;
  }
  vector<ll> ruiseki(n);
  ruiseki[0] = a[0];
  for (ll i = 1; i < n; i++) {
    ruiseki[i] = ruiseki[i - 1] * a[i] / gcd(ruiseki[i - 1], a[i]);
    if (ruiseki[i] > m) {
      cout << 0 << endl;
      return 0;
    }
  }

  printf("%lld\n", ll(ceil(m / ruiseki[n - 1] / 2.0)));
}
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // for next_permutation
#include <bits/stl_numeric.h>
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>

#define DEBUG 0
#define REP(i, n) for (long long i = 0; i < (n); i++)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;

unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

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

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  cin >> N >> M;

  vector<ll> a(N);
  ll amax = 0;

  REP(i, N) {
    cin >> a[i];
    amax = max(amax, a[i]);
  }
  ll wa = a[0];
  int wa2 = 0;

  for (int i = 1; i < N; ++i) {
    wa = lcm(wa, a[i]);
    if (wa >= MOD * 2) {
      cout << 0 << endl;
      return 0;
    }
  }

  // lcmの2が複数含まれてたらおわり
  // それぞれの2の個数が一致していないといけない
  int cnt1 = 0;
  while (a[0] % 2 == 0) {
    ++cnt1;
    a[0] /= 2;
  }
  for (int i = 1; i < N; ++i) {
    int cnt2 = 0;
    while (a[i] % 2 == 0) {
      ++cnt2;
      a[i] %= 2;
    }

    if (cnt1 != cnt2) {
      cout << 0 << endl;
      return 0;
    }
  }

  ll res;

  M += (wa / 2);
  res = M / wa;

  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm> // for next_permutation
#include <bits/stl_numeric.h>
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>

#define DEBUG 0
#define REP(i, n) for (long long i = 0; i < (n); i++)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;

unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

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

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  cin >> N >> M;

  vector<ll> a(N);
  ll amax = 0;

  REP(i, N) {
    cin >> a[i];
    amax = max(amax, a[i]);
  }
  ll wa = a[0];
  int wa2 = 0;

  for (int i = 1; i < N; ++i) {
    wa = lcm(wa, a[i]);
    if (wa >= MOD * 2) {
      cout << 0 << endl;
      return 0;
    }
  }

  // lcmの2が複数含まれてたらおわり
  // それぞれの2の個数が一致していないといけない
  int cnt1 = 0;
  while (a[0] % 2 == 0) {
    ++cnt1;
    a[0] /= 2;
  }
  for (int i = 1; i < N; ++i) {
    int cnt2 = 0;
    while (a[i] % 2 == 0) {
      ++cnt2;
      a[i] /= 2;
    }

    if (cnt1 != cnt2) {
      cout << 0 << endl;
      return 0;
    }
  }

  ll res;

  M += (wa / 2);
  res = M / wa;

  cout << res << endl;

  return 0;
}
Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1e15;
using Graph = vector<vector<int>>;

int gcd(int a, int b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

int lcm(int x, int y) {
  if (x == 0 || y == 0)
    return 0;
  return x / gcd(x, y) * y;
}

signed main() {
  int N, M;
  cin >> N >> M;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] /= 2;
  }

  int cnt = 0;
  int B = A[0];
  while (B % 2 == 0) {
    B /= 2;
    cnt++;
  }
  for (int i = 1; i < N; i++) {
    B = A[i];
    int now = 0;
    while (B % 2 == 0) {
      B /= 2;
      now++;
    }
    // 2で割れる回数が異なったら
    if (cnt != now) {
      cout << 0 << endl;
      return 0;
    }
  }

  int LCM = lcm(A[0], A[1]);
  for (int i = 2; i < N; i++) {
    LCM = lcm(LCM, A[i]);
    if (LCM > M) {
      cout << 0 << endl;
      return 0;
    }
  }

  cout << (M / LCM + 1) / 2 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1e15;
using Graph = vector<vector<int>>;

int gcd(int a, int b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

int lcm(int x, int y) {
  if (x == 0 || y == 0)
    return 0;
  return x / gcd(x, y) * y;
}

signed main() {
  int N, M;
  cin >> N >> M;

  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] /= 2;
  }

  int cnt = 0;
  int B = A[0];
  while (B % 2 == 0) {
    B /= 2;
    cnt++;
  }
  for (int i = 1; i < N; i++) {
    B = A[i];
    int now = 0;
    while (B % 2 == 0) {
      B /= 2;
      now++;
    }
    // 2で割れる回数が異なったら
    if (cnt != now) {
      cout << 0 << endl;
      return 0;
    }
  }

  int LCM;
  if (N >= 2)
    LCM = lcm(A[0], A[1]);
  else
    LCM = A[0];

  for (int i = 2; i < N; i++) {
    LCM = lcm(LCM, A[i]);
    if (LCM > M) {
      cout << 0 << endl;
      return 0;
    }
  }

  cout << (M / LCM + 1) / 2 << endl;
}
Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef long long ll;
const char sp = ' ';

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

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

using namespace std;

int main(void) {
  ll N, M, amax = 1;
  cin >> N >> M;
  ll a[100005], d = 1;
  rep(i, N) {
    cin >> a[i];
    // amax=max(amax, a[i]);
    if (i == 0) {
      while (true) {
        if (a[i] % 2 != 0)
          break;
        a[i] /= 2;
        d *= 2;
      }
    } else {
      if (a[i] % d != 0) {
        cout << 0 << endl;
        return 0;
      }
      a[i] /= d;
      if (a[i] % 2 == 0) {
        cout << 0 << endl;
        return 0;
      }
    }
  }
  d /= 2;
  rep(i, N) { a[i] *= d; }
  ll LCM = 1;
  rep(i, N) { LCM = lcm(LCM, a[i]); }
  // cout<<lcm<<endl;
  ll ans = (ll)((M / LCM) + 1) / 2;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
typedef unsigned long long ll;
const char sp = ' ';

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

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

using namespace std;

int main(void) {
  ll N, M, amax = 1;
  cin >> N >> M;
  ll a[100005], d = 1;
  rep(i, N) {
    cin >> a[i];
    // amax=max(amax, a[i]);
    if (i == 0) {
      while (true) {
        if (a[i] % 2 != 0)
          break;
        a[i] /= 2;
        d *= 2;
      }
    } else {
      if (a[i] % d != 0) {
        cout << 0 << endl;
        return 0;
      }
      a[i] /= d;
      if (a[i] % 2 == 0) {
        cout << 0 << endl;
        return 0;
      }
    }
  }
  d /= 2;
  rep(i, N) { a[i] *= d; }
  ll LCM = 1;
  rep(i, N) { LCM = lcm(LCM, a[i]); }
  // cout<<lcm<<endl;
  ll ans = (ll)((M / LCM) + 1) / 2;
  cout << ans << endl;
  return 0;
}
Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const long long maxn = 10;

long long n, m;
long long a[maxn];
long long b[maxn];
long long ans;

void in();
void solve();
void out();

int main() {
  in();
  solve();
  out();
}

void in() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    a[i] /= 2;
  }
}

void solve() {
  long long chek = __builtin_ctz(a[0]);
  long long lcm = a[0];
  for (long long i = 0; i < n; i++) {
    lcm = lcm * a[i] / __gcd(lcm, a[i]);
    if (__builtin_ctzll(a[i]) != chek || lcm > m)
      return;
  }
  ans = (m / lcm + 1) / 2;
}

void out() { cout << ans; }


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const long long maxn = 1e5 + 10;

long long n, m;
long long a[maxn];
long long b[maxn];
long long ans;

void in();
void solve();
void out();

int main() {
  in();
  solve();
  out();
}

void in() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    a[i] /= 2;
  }
}

void solve() {
  long long chek = __builtin_ctz(a[0]);
  long long lcm = a[0];
  for (long long i = 0; i < n; i++) {
    lcm = lcm * a[i] / __gcd(lcm, a[i]);
    if (__builtin_ctzll(a[i]) != chek || lcm > m)
      return;
  }
  ans = (m / lcm + 1) / 2;
}

void out() { cout << ans; }
Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#pragma region Macros
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
#pragma endregion

long long gcd(long long a, long long b) {
  if (a < b)
    std::swap(a, b);
  if (b == 0)
    return a;

  while (a % b > 0) {
    long long buf = b;
    b = a % b;
    a = buf;
  }
  return b;
}

void solve() {
  int n;
  ll m;
  cin >> n >> m;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int cnt2 = 0;
  for (int i = 0; i < n; i++) {
    int tmp = 0;
    while (a.at(i) % 2 == 0) {
      a.at(i) /= 2;
      tmp++;
    }
    if (i == 0)
      cnt2 = tmp;
    else {
      if (cnt2 != tmp) {
        cout << 0 << "\n";
        return;
      }
    }
  }
  ll odd = 1;
  for (int i = 0; i < n; i++) {
    odd = odd * a.at(i) / gcd(odd, a.at(i));
  }
  odd *= (1ll << (cnt2 - 1));
  cout << (m / odd) / 2 + (m / odd) % 2 << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


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

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
#pragma endregion

long long gcd(long long a, long long b) {
  if (a < b)
    std::swap(a, b);
  if (b == 0)
    return a;

  while (a % b > 0) {
    long long buf = b;
    b = a % b;
    a = buf;
  }
  return b;
}

void solve() {
  int n;
  ll m;
  cin >> n >> m;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int cnt2 = 0;
  for (int i = 0; i < n; i++) {
    int tmp = 0;
    while (a.at(i) % 2 == 0) {
      a.at(i) /= 2;
      tmp++;
    }
    if (i == 0)
      cnt2 = tmp;
    else {
      if (cnt2 != tmp) {
        cout << 0 << "\n";
        return;
      }
    }
  }
  ll odd = 1;
  for (int i = 0; i < n; i++) {
    odd = odd * a.at(i) / gcd(odd, a.at(i));
  }
  if (cnt2 >= 2)
    odd *= pow(2, cnt2 - 1);
  cout << (m / odd) / 2 + (m / odd) % 2 << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, (n)-1, 0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll gcd(ll x, ll y) {
  if (x > y)
    return gcd(y, x);
  else if (x == 0)
    return y;
  else
    return gcd(y % x, x);
}
ll lcm(ll x, ll y) {
  ll g = gcd(x, y);
  return g * (x / g) * (y / g);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  ll m;
  cin >> n >> m;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  auto count = [&](ll n) {
    int ret = 0;
    while (n % 2 == 0) {
      ret++;
      n /= 2;
    }
    return ret;
  };
  int cnt = count(A[0]);
  rep(i, n) {
    if (count(A[i]) != cnt) {
      cout << 0 << endk;
      return 0;
    }
  }
  ll l = A[0];
  rep(i, n) l = lcm(A[i], l);
  cout << (m < l / 2 ? 0 : (m - l / 2) / l + 1) << endk;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, (n)-1, 0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll gcd(ll x, ll y) {
  if (x > y)
    return gcd(y, x);
  else if (x == 0)
    return y;
  else
    return gcd(y % x, x);
}
ll lcm(ll x, ll y) {
  ll g = gcd(x, y);
  return g * (x / g) * (y / g);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  ll m;
  cin >> n >> m;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  auto count = [&](ll n) {
    int ret = 0;
    while (n % 2 == 0) {
      ret++;
      n /= 2;
    }
    return ret;
  };
  int cnt = count(A[0]);
  rep(i, n) {
    if (count(A[i]) != cnt) {
      cout << 0 << endk;
      return 0;
    }
  }
  ll l = A[0];
  rep(i, n) {
    l = lcm(A[i], l);
    if (l / 2 > m) {
      cout << 0 << endk;
      return 0;
    }
  }
  cout << (m - l / 2) / l + 1 << endk;
  return 0;
}
Problem p02814 (Python)

Buggy submission (status: Runtime Error):
# from fractions import gcd
from math import gcd
from functools import reduce

N, M = map(int, input().split())
nums = set([num // 2 for num in list(map(int, input().split()))])
# 1<=X<=Mの範囲で、半公倍数の数を求める。
# 但し、Xは、num*(p+0.5)でなければならない
# Mが10**9なので、全部やるのは無理。
# n.5倍なので、numsのあたいは全て偶数になっている。

# nums一つ一つ、答えになりうるXの配列を作って、重複したものを残せばそれが答えになる。
# Nが10**5なので、どうだろう


def half_multiple(x, y):
    return (x * y) // gcd(x, y)


def run():
    half_multiple_num = reduce(half_multiple, nums, 1)

    # print(half_multiple_num)
    for num in nums:
        if (half_multiple_num // num) % 2 == 0:
            print(0)
            return

    pmax = M // half_multiple_num
    pmax = pmax - 1 if pmax % 2 == 0 else pmax

    print((pmax + 1) // 2)


run()


Fixed submission:
from fractions import gcd

# from math import gcd
from functools import reduce

N, M = map(int, input().split())
nums = set([num // 2 for num in list(map(int, input().split()))])
# 1<=X<=Mの範囲で、半公倍数の数を求める。
# 但し、Xは、num*(p+0.5)でなければならない
# Mが10**9なので、全部やるのは無理。
# n.5倍なので、numsのあたいは全て偶数になっている。

# nums一つ一つ、答えになりうるXの配列を作って、重複したものを残せばそれが答えになる。
# Nが10**5なので、どうだろう


def half_multiple(x, y):
    return (x * y) // gcd(x, y)


def run():
    half_multiple_num = reduce(half_multiple, nums, 1)

    # print(half_multiple_num)
    for num in nums:
        if (half_multiple_num // num) % 2 == 0:
            print(0)
            return

    pmax = M // half_multiple_num
    pmax = pmax - 1 if pmax % 2 == 0 else pmax

    print((pmax + 1) // 2)


run()
Problem p02814 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
int main() {
  long long n, m;
  cin >> n >> m;
  long long a[n];
  long long ans = 1;
  long long ok = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] /= 2;
    ok = ans * a[i];
    ans = __gcd(ans, a[i]);
    ans = ok / ans;
  }
  // cout<<ans<<endl;
  long long ammat = m / ans;
  for (int i = 0; i < n; i++) {
    if ((ans / (a[i] / 2)) % 2 == 0) {
      cout << 0;
      return 0;
    }
  }
  if (ammat % 2) {
    cout << ammat / 2 + 1;
  } else {
    cout << ammat / 2;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
int main() {
  long long n, m;
  cin >> n >> m;
  long long a[n];
  long long ans = 1;
  long long ok = 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] /= 2;
    ok = ans * a[i];
    ans = __gcd(ans, a[i]);
    ans = ok / ans;
  }
  // cout<<ans<<endl;
  long long ammat = m / ans;
  for (int i = 0; i < n; i++) {
    if ((ans / (a[i])) % 2 == 0) {
      cout << 0;
      return 0;
    }
  }
  if (ammat % 2) {
    cout << ammat / 2 + 1;
  } else {
    cout << ammat / 2;
  }
}
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

int gcd(int a, int b) {
  if (b > a)
    return gcd(b, a);
  else if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  int a[100010];
  rep(i, N) cin >> a[i];
  int k = 0;
  while (((a[0] >> k) << k) == a[0])
    k++;
  rep(i, N) {
    int tmp = 0;
    while (((a[i] >> tmp) << tmp) == a[i])
      tmp++;
    if (k != tmp) {
      cout << 0 << endl;
      return 0;
    }
  }
  int L = a[0];
  rep(i, N) { L = L * a[i] / gcd(L, a[i]); }
  cout << (M + L / 2) / L << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

int gcd(int a, int b) {
  if (b > a)
    return gcd(b, a);
  else if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  int a[100010];
  rep(i, N) cin >> a[i];
  int k = 0;
  while (((a[0] >> k) << k) == a[0])
    k++;
  rep(i, N) {
    int tmp = 0;
    while (((a[i] >> tmp) << tmp) == a[i])
      tmp++;
    if (k != tmp) {
      cout << 0 << endl;
      return 0;
    }
  }
  int L = a[0];
  rep(i, N) {
    L = L * a[i] / gcd(L, a[i]);
    if ((M + L / 2) / L == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << (M + L / 2) / L << endl;

  return 0;
}
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

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

int main() {
  ll n, m;
  cin >> n >> m;

  ll a[n];
  rep(i, n) cin >> a[i];
  ll g = 1;
  rep(i, n) g = (g * a[i]) / gcd(g, a[i]);

  g /= 2;

  rep(i, n) if (g % a[i] == 0) {
    cout << 0;
    return 0;
  }

  ll ans = m / g + 1;
  cout << ans / 2 << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

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

int main() {
  ll n, m;
  cin >> n >> m;

  ll a[n];
  rep(i, n) cin >> a[i];
  ll g = 1;
  rep(i, n) {
    g = (g * a[i]) / gcd(g, a[i]);
    if (g / 2 > m) {
      cout << 0;
      return 0;
    }
  }

  g /= 2;

  rep(i, n) if (g % a[i] == 0) {
    cout << 0;
    return 0;
  }

  ll ans = m / g + 1;
  cout << ans / 2 << endl;
}
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

  if (a < b)
    return gcd(b, a);

  return gcd(b, a % b);
}

ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  bool ok = true;
  for (int i = 1; i < n; i++) {
    int cntX = 0, cntY = 0;

    int x = a[i];
    int y = a[i - 1];
    while (x % 2 == 0) {
      x /= 2;
      cntX++;
    }

    while (y % 2 == 0) {
      y /= 2;
      cntY++;
    }

    if (cntX != cntY) {
      ok = false;
      break;
    }
  }

  ll vlcm = a[0] / 2;
  for (int i = 1; i < n; i++) {
    a[i] /= 2;
    vlcm = lcm(a[i], vlcm);
  }

  ll ans = 0;

  if (ok) {
    ll mul = 1;
    while (vlcm * mul <= m) {
      ans++;
      mul += 2;
    }

    cout << ans << endl;
  } else {
    cout << 0 << endl;
  }

  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

  if (a < b)
    return gcd(b, a);

  return gcd(b, a % b);
}

ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  bool ok = true;
  for (int i = 1; i < n; i++) {
    int cntX = 0, cntY = 0;

    int x = a[i];
    int y = a[i - 1];
    while (x % 2 == 0) {
      x /= 2;
      cntX++;
    }

    while (y % 2 == 0) {
      y /= 2;
      cntY++;
    }

    if (cntX != cntY) {
      ok = false;
      break;
    }
  }

  ll vlcm = a[0] / 2;
  for (int i = 1; i < n; i++) {
    a[i] /= 2;
    vlcm = lcm(a[i], vlcm);
    if (vlcm > m) {
      ok = false;
      break;
    }
  }

  ll ans = 0;

  if (ok) {
    ll mul = 1;
    while (vlcm * mul <= m) {
      ans++;
      mul += 2;
    }

    cout << ans << endl;
  } else {
    cout << 0 << endl;
  }

  return 0;
}
Problem p02814 (Python)

Buggy submission (status: Runtime Error):
def main():
    from functools import reduce
    from math import gcd

    def lcm(a, b):
        return (a // gcd(a, b)) * b

    N, M = map(int, input().split())
    (*a,) = map(int, input().split())

    p = -1
    b = []
    for x in a:
        cnt = 0
        while x % 2 == 0:
            x //= 2
            cnt += 1
        if ~p and p != cnt:
            print(0)
            return
        p = cnt
        b.append(x)

    if p == 0:
        print(0)
        return

    t = pow(2, p - 1) * reduce(lcm, b)
    ans = (M // t) - (M // (t * 2))

    print(ans)


if __name__ == "__main__":
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())


Fixed submission:
def main():
    from functools import reduce

    def gcd(a, b):
        return a if b == 0 else gcd(b, a % b)

    def lcm(a, b):
        return (a // gcd(a, b)) * b

    N, M = map(int, input().split())
    (*a,) = map(int, input().split())

    p = -1
    b = []
    for x in a:
        cnt = 0
        while x % 2 == 0:
            x //= 2
            cnt += 1
        if ~p and p != cnt:
            print(0)
            return
        p = cnt
        b.append(x)

    if p == 0:
        print(0)
        return

    t = pow(2, p - 1) * reduce(lcm, b)
    ans = (M // t) - (M // (t * 2))

    print(ans)


if __name__ == "__main__":
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())
Problem p02814 (C++)

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

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)

lli gcd(lli x, lli y) {
  if (x < y)
    swap(x, y);
  if (y == 0) {
    return x;
  } else {
    return gcd(y, x % y);
  }
}

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

void func(long long N, long long M, std::vector<long long> a) {
  sort(a.begin(), a.end());
  REP(i, 0, N) a[i] /= 2;
  lli GCD = a[0];
  REP(i, 1, N) GCD = lcm(GCD, a[i]);

  REP(i, 0, N) {
    if ((GCD / a[i]) % 2 == 0) {
      cout << 0 << endl;
      return;
    }
  }

  cout << (M + GCD) / (2 * GCD) << endl;
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  func(N, M, std::move(a));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)

lli gcd(lli x, lli y) {
  if (x < y)
    swap(x, y);
  if (y == 0) {
    return x;
  } else {
    return gcd(y, x % y);
  }
}

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

void func(long long N, long long M, std::vector<long long> a) {
  sort(a.begin(), a.end());
  REP(i, 0, N) a[i] /= 2;
  lli GCD = a[0];
  REP(i, 1, N) {
    GCD = lcm(GCD, a[i]);
    if (GCD > M) {
      cout << 0 << endl;
      return;
    }
  }

  REP(i, 0, N) {
    if ((GCD / a[i]) % 2 == 0) {
      cout << 0 << endl;
      return;
    }
  }

  cout << (M + GCD) / (2 * GCD) << endl;
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  func(N, M, std::move(a));
  return 0;
}
Problem p02814 (Python)

Buggy submission (status: Runtime Error):
def main():
    from math import gcd, ceil

    n, m, *a = map(int, open(0).read().split())
    b = [0 for _ in range(n)]
    a.sort()

    for i, j in enumerate(a):
        c = 0
        while j % 2 == 0:
            c += 1
            j = j // 2
        a[i] = j
        b[i] = c

    if len(set(b)) > 1:
        print(0)
        exit()

    lcm = 1
    for i in a:
        lcm = (lcm * i) // gcd(lcm, i)

    k = b[0] - 1
    ans = ceil(m // 2**k // lcm / 2)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    from fractions import gcd
    from math import ceil

    n, m, *a = map(int, open(0).read().split())
    b = [0 for _ in range(n)]
    a.sort()

    for i, j in enumerate(a):
        c = 0
        while j % 2 == 0:
            c += 1
            j = j // 2
        a[i] = j
        b[i] = c

    if len(set(b)) > 1:
        print(0)
        exit()

    lcm = 1
    for i in a:
        lcm = (lcm * i) // gcd(lcm, i)

    k = b[0] - 1
    ans = ceil(m // 2**k // lcm / 2)
    print(ans)


if __name__ == "__main__":
    main()
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int64_t n, m;
  cin >> n >> m;
  vector<int64_t> as(n);
  for (auto &a : as) {
    cin >> a;
  }

  int64_t lcm_v = 1;
  FOR_LT(i, 0, n) { lcm_v = lcm(lcm_v, as[i]); }

  FOR_LT(i, 0, n) {
    if (((lcm_v / as[i]) & 1) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  int64_t fv = lcm_v / 2;
  if (m < fv) {
    cout << 0 << endl;
  } else {
    cout << 1 + (m - fv) / (2 * fv) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int64_t n, m;
  cin >> n >> m;
  vector<int64_t> as(n);
  for (auto &a : as) {
    cin >> a;
  }

  int64_t lcm_v = 1;
  FOR_LT(i, 0, n) {
    lcm_v = lcm(lcm_v, as[i]);
    if (m < lcm_v / 2) {
      cout << 0 << endl;
      return 0;
    }
  }

  FOR_LT(i, 0, n) {
    if (((lcm_v / as[i]) & 1) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  int64_t fv = lcm_v / 2;
  if (m < fv) {
    cout << 0 << endl;
  } else {
    cout << 1 + (m - fv) / (2 * fv) << endl;
  }

  return 0;
}
Problem p02814 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

s64 gcd(s64 x, s64 y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

s64 cmul(s64 x, s64 y) {
  s64 g = gcd(x, y);
  return x * y / g;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  s64 N, M;
  cin >> N >> M;

  vs64 A(N);
  s64 g = 0;
  rep(i, N) {
    cin >> A[i];
    A[i] /= 2;
    g = gcd(A[i], g);
  }

  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  s64 d = 1;
  rep(i, A.size()) {
    d = cmul(d, A[i]);
    if ((A[i] / g) % (s64)2 == 0) {
      d = M + 1;
      break;
    }
  }

  s64 ans = (M + d) / ((s64)2 * d);

  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

s64 gcd(s64 x, s64 y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

s64 cmul(s64 x, s64 y) {
  s64 g = gcd(x, y);
  return x * y / g;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  s64 N, M;
  cin >> N >> M;

  vs64 A(N);
  s64 g = 0;
  rep(i, N) {
    cin >> A[i];
    A[i] /= 2;
    g = gcd(A[i], g);
  }

  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  s64 d = 1;
  rep(i, A.size()) {
    d = cmul(d, A[i]);
    if (d > M)
      break;
    if ((A[i] / g) % (s64)2 == 0) {
      d = M + 1;
      break;
    }
  }

  s64 ans = (M + d) / ((s64)2 * d);

  cout << ans << "\n";
  return 0;
}