cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:warning: 切り捨て除算 (floor division)・切り上げ除算
(include/emthrm/math/division.hpp)

仕様

名前 戻り値
template <typename T, typename U>
std::common_type_t<T, U> floor_div(const T a, const U b);
$\left\lfloor \frac{A}{B} \right\rfloor$
template <typename T, typename U>
std::common_type_t<T, U> ceil_div(const T a, const U b);
$\left\lceil \frac{A}{B} \right\rceil$

参考文献

Code

#ifndef EMTHRM_MATH_DIVISION_HPP_
#define EMTHRM_MATH_DIVISION_HPP_

#include <cassert>
#include <type_traits>

namespace emthrm {

template <typename T, typename U>
std::common_type_t<T, U> floor_div(const T a, const U b) {
  assert(b != 0);
  return a / b - ((int{a < 0} ^ int{b < 0}) & int{a % b != 0});
}

template <typename T, typename U>
std::common_type_t<T, U> ceil_div(const T a, const U b) {
  assert(b != 0);
  return a / b + int{(a < 0) == (b < 0) && a % b != 0};
}

}  // namespace emthrm

#endif  // EMTHRM_MATH_DIVISION_HPP_
#line 1 "include/emthrm/math/division.hpp"



#include <cassert>
#include <type_traits>

namespace emthrm {

template <typename T, typename U>
std::common_type_t<T, U> floor_div(const T a, const U b) {
  assert(b != 0);
  return a / b - ((int{a < 0} ^ int{b < 0}) & int{a % b != 0});
}

template <typename T, typename U>
std::common_type_t<T, U> ceil_div(const T a, const U b) {
  assert(b != 0);
  return a / b + int{(a < 0) == (b < 0) && a % b != 0};
}

}  // namespace emthrm
Back to top page