This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: STANDALONE
#include <iostream>
#include "tools/assert_that.hpp"
#include "tools/ceil_sqrt.hpp"
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
assert_that(tools::ceil_sqrt(0) == 0);
assert_that(tools::ceil_sqrt(1) == 1);
assert_that(tools::ceil_sqrt(2) == 2);
assert_that(tools::ceil_sqrt(3) == 2);
assert_that(tools::ceil_sqrt(4) == 2);
assert_that(tools::ceil_sqrt(5) == 3);
assert_that(tools::ceil_sqrt(6) == 3);
assert_that(tools::ceil_sqrt(7) == 3);
assert_that(tools::ceil_sqrt(8) == 3);
assert_that(tools::ceil_sqrt(9) == 3);
assert_that(tools::ceil_sqrt(10) == 4);
assert_that(tools::ceil_sqrt(9223372030926249000) == 3037000499);
assert_that(tools::ceil_sqrt(9223372030926249001) == 3037000499);
assert_that(tools::ceil_sqrt(9223372030926249002) == 3037000500);
assert_that(tools::ceil_sqrt(9223372036854775807) == 3037000500);
return 0;
}
#line 1 "tests/ceil_sqrt.test.cpp"
// competitive-verifier: STANDALONE
#include <iostream>
#line 1 "tools/assert_that.hpp"
#line 5 "tools/assert_that.hpp"
#include <cstdlib>
#define assert_that_impl(cond, file, line, func) do {\
if (!cond) {\
::std::cerr << file << ':' << line << ": " << func << ": Assertion `" << #cond << "' failed." << '\n';\
::std::exit(EXIT_FAILURE);\
}\
} while (false)
#define assert_that(...) assert_that_impl((__VA_ARGS__), __FILE__, __LINE__, __func__)
#line 1 "tools/ceil_sqrt.hpp"
#include <cassert>
#line 1 "tools/ceil.hpp"
#line 5 "tools/ceil.hpp"
#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 1 "tools/is_unsigned.hpp"
#line 5 "tools/is_unsigned.hpp"
namespace tools {
template <typename T>
struct is_unsigned : ::std::is_unsigned<T> {};
template <typename T>
inline constexpr bool is_unsigned_v = ::tools::is_unsigned<T>::value;
}
#line 8 "tools/ceil.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> ceil(const M x, const N y) noexcept {
assert(y != 0);
if (y >= 0) {
if (x > 0) {
return (x - 1) / y + 1;
} else {
if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
return 0;
} else {
return x / y;
}
}
} else {
if (x >= 0) {
if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
return 0;
} else {
return x / y;
}
} else {
return (x + 1) / y + 1;
}
}
}
}
#line 6 "tools/ceil_sqrt.hpp"
namespace tools {
template <typename T>
T ceil_sqrt(const T n) {
assert(n >= 0);
if (n == 0) return 0;
T ok = 1;
T ng;
for (ng = 2; ng - 1 < tools::ceil(n, ng - 1); ng *= 2);
while (ng - ok > 1) {
const T mid = ok + (ng - ok) / 2;
if (mid - 1 < tools::ceil(n, mid - 1)) {
ok = mid;
} else {
ng = mid;
}
}
return ok;
}
}
#line 6 "tests/ceil_sqrt.test.cpp"
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
assert_that(tools::ceil_sqrt(0) == 0);
assert_that(tools::ceil_sqrt(1) == 1);
assert_that(tools::ceil_sqrt(2) == 2);
assert_that(tools::ceil_sqrt(3) == 2);
assert_that(tools::ceil_sqrt(4) == 2);
assert_that(tools::ceil_sqrt(5) == 3);
assert_that(tools::ceil_sqrt(6) == 3);
assert_that(tools::ceil_sqrt(7) == 3);
assert_that(tools::ceil_sqrt(8) == 3);
assert_that(tools::ceil_sqrt(9) == 3);
assert_that(tools::ceil_sqrt(10) == 4);
assert_that(tools::ceil_sqrt(9223372030926249000) == 3037000499);
assert_that(tools::ceil_sqrt(9223372030926249001) == 3037000499);
assert_that(tools::ceil_sqrt(9223372030926249002) == 3037000500);
assert_that(tools::ceil_sqrt(9223372036854775807) == 3037000500);
return 0;
}