Problem p02814 (C++)

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

template <typename InputIterator>
typename InputIterator::value_type summation(InputIterator first,
                                             InputIterator last) {
  return std::accumulate(first, last, typename InputIterator::value_type());
}
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v);
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p);
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v) {
  for (auto &i : v) {
    stream >> i;
  }
  return stream;
}
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p) {
  stream >> p.first >> p.second;
  return stream;
}
template <typename T> T gcd(const T x, const T y) {
  if (x == 0) {
    return y;
  } else if (y == 0) {
    return x;
  } else if (x < y) {
    return gcd(x, y % x);
  } else if (y < x) {
    return gcd(y, x % y);
  } else {
    return x;
  }
}

template <typename T> T lcm(const T x, const T y) {
  if (x == 0 || y == 0) {
    return 0;
  } else {
    return x / gcd(x, y) * y;
  }
}
int main() {
  int64_t n, m;
  std::cin >> n >> m;

  std::vector<int64_t> a(n);
  std::cin >> a;
  std::sort(a.begin(), a.end());

  std::set<int64_t> s;

  for (int64_t i = 0; i < n; i++) {
    a[i] /= 2;
    s.insert(a[i]);
  }

  if (s.size() == 1) {
    if (m < a[0]) {
      std::cout << 0 << std::endl;
    } else {
      m -= a[0];
      std::cout << 1 + m / (2 * a[0]) << std::endl;
    }
  } else {
    std::vector<int64_t> b(a.cbegin(), a.cend());
    auto g = gcd(b[0], b[1]);
    auto l = lcm(b[0], b[1]);
    for (int64_t i = 2; i < b.size(); i++) {
      l = lcm(l, b[i]);
      g = gcd(g, b[i]);
    }

    for (const auto j : b) {
      if ((j / g) % 2 == 0) {
        std::cout << 0 << std::endl;
        return 0;
      }
    }

    if (m < l) {
      std::cout << 0 << std::endl;
    } else {
      m -= l;
      std::cout << 1 + m / (2 * l) << std::endl;
    }
  }

  return 0;
}


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

template <typename InputIterator>
typename InputIterator::value_type summation(InputIterator first,
                                             InputIterator last) {
  return std::accumulate(first, last, typename InputIterator::value_type());
}
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v);
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p);
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v) {
  for (auto &i : v) {
    stream >> i;
  }
  return stream;
}
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p) {
  stream >> p.first >> p.second;
  return stream;
}
template <typename T> T gcd(const T x, const T y) {
  if (x == 0) {
    return y;
  } else if (y == 0) {
    return x;
  } else if (x < y) {
    return gcd(x, y % x);
  } else if (y < x) {
    return gcd(y, x % y);
  } else {
    return x;
  }
}

template <typename T> T lcm(const T x, const T y) {
  if (x == 0 || y == 0) {
    return 0;
  } else {
    return x / gcd(x, y) * y;
  }
}
int main() {
  int64_t n, m;
  std::cin >> n >> m;

  std::vector<int64_t> a(n);
  std::cin >> a;
  std::sort(a.begin(), a.end());

  std::set<int64_t> s;

  for (int64_t i = 0; i < n; i++) {
    a[i] /= 2;
    s.insert(a[i]);
  }

  if (s.size() == 1) {
    if (m < a[0]) {
      std::cout << 0 << std::endl;
    } else {
      m -= a[0];
      std::cout << 1 + m / (2 * a[0]) << std::endl;
    }
  } else {
    std::vector<int64_t> b(a.cbegin(), a.cend());
    auto g = gcd(b[0], b[1]);
    auto l = lcm(b[0], b[1]);
    for (int64_t i = 2; i < b.size(); i++) {
      l = lcm(l, b[i]);
      g = gcd(g, b[i]);
      if (m < l) {
        std::cout << 0 << std::endl;
        return 0;
      }
    }

    for (const auto j : b) {
      if ((j / g) % 2 == 0) {
        std::cout << 0 << std::endl;
        return 0;
      }
    }

    if (m < l) {
      std::cout << 0 << std::endl;
    } else {
      m -= l;
      std::cout << 1 + m / (2 * l) << std::endl;
    }
  }

  return 0;
}