proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: Quotient as integer division (tools/quo.hpp)

template <typename M, typename N>
constexpr std::common_type_t<M, N> quo(M a, N b) noexcept;

Given $a$ and $b (\neq 0)$, a pair of values $(q, r)$ satisfying the following conditions is uniquely determined.

\[\begin{align*} \left\{\begin{array}{l} a = qb + r\\ q \in \mathbb{Z}\\ 0 \leq r < |b| \end{array}\right.& \end{align*}\]

It returns $q$. Note that $q$ is also written as follows.

\[\begin{align*} \left\{\begin{array}{ll} \lfloor \frac{a}{b} \rfloor & \text{(if $b > 0$)}\\ \lceil \frac{a}{b} \rceil & \text{(if $b < 0$)} \end{array}\right.& \end{align*}\]

Constraints

Time Complexity

License

Author

Depends on

Required by

Verified with

Code

#ifndef TOOLS_QUO_H
#define TOOLS_QUO_H

#include <cassert>
#include <type_traits>
#include "tools/is_integral.hpp"

namespace tools {

  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> quo(const M a, const N b) noexcept {
    assert(b != 0);

    if (a >= 0) {
      return a / b;
    } else {
      if (b >= 0) {
        return (a + 1) / b - 1;
      } else {
        return (a + 1) / b + 1;
      }
    }
  }
}

#endif
#line 1 "tools/quo.hpp"



#include <cassert>
#include <type_traits>
#line 1 "tools/is_integral.hpp"



#line 5 "tools/is_integral.hpp"

namespace tools {
  template <typename T>
  struct is_integral : ::std::is_integral<T> {};

  template <typename T>
  inline constexpr bool is_integral_v = ::tools::is_integral<T>::value;
}


#line 7 "tools/quo.hpp"

namespace tools {

  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> quo(const M a, const N b) noexcept {
    assert(b != 0);

    if (a >= 0) {
      return a / b;
    } else {
      if (b >= 0) {
        return (a + 1) / b - 1;
      } else {
        return (a + 1) / b + 1;
      }
    }
  }
}


Back to top page