shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/utility/binary-search.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// binary-search.hpp の整数版を、型の全範囲を含む境界で検証する
// ok < ng と ok > ng の両方で、答え・f に渡す値が ok と ng の間にあること・f の呼び出し回数を確かめる
// あふれないことは -fsanitize=undefined でも確かめた
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/utility/binary-search.hpp"

#include <iostream>
#include <limits>
#include <random>

bool ok = true;

void check(bool cond, const char* what) {
    if (!cond) {
        if (ok) std::cerr << "failed: " << what << std::endl;
        ok = false;
    }
}

// lo <= t < hi として、f(x) = x <= t (ok = lo, ng = hi) と f(x) = x > t (ok = hi, ng = lo) を調べる
template <typename T>
void test(T lo, T hi, T t, const char* what) {
    long long calls = 0;
    bool inside = true;
    T r1 = binary_search(lo, hi, [&](T x) {
        calls++;
        if (!(lo < x && x < hi)) inside = false;
        return x <= t;
    });
    check(r1 == t, what);
    check(inside && calls <= std::numeric_limits<T>::digits + 1, what);
    calls = 0;
    T r2 = binary_search(hi, lo, [&](T x) {
        calls++;
        if (!(lo < x && x < hi)) inside = false;
        return x > t;
    });
    check(r2 == t + 1, what);
    check(inside && calls <= std::numeric_limits<T>::digits + 1, what);
}

// 型の両端と 0 の付近を境界にして調べる
template <typename T>
void test_extreme(const char* what) {
    constexpr T mn = std::numeric_limits<T>::min(), mx = std::numeric_limits<T>::max();
    for (T t : {mn, static_cast<T>(mn + 1), static_cast<T>(mn / 2), static_cast<T>(0), static_cast<T>(1), static_cast<T>(mx / 2), static_cast<T>(mx - 2), static_cast<T>(mx - 1)}) {
        test<T>(mn, mx, t, what);
    }
    if (std::numeric_limits<T>::is_signed) {
        for (T t : {static_cast<T>(-2), static_cast<T>(-1)}) test<T>(mn, mx, t, what);
        test<T>(mn, static_cast<T>(0), static_cast<T>(-1), what);
        test<T>(static_cast<T>(-1), mx, static_cast<T>(0), what);
    }
}

// 差が 1 なら f を呼ばずに ok を返す。差が 2 なら真ん中を 1 回だけ調べる
template <typename T>
void test_small(T a, const char* what) {
    long long calls = 0;
    check(binary_search(a, static_cast<T>(a + 1), [&](T) { return calls++, false; }) == a && calls == 0, what);
    check(binary_search(static_cast<T>(a + 1), a, [&](T) { return calls++, false; }) == static_cast<T>(a + 1) && calls == 0, what);
    T m = static_cast<T>(a + 1), b = static_cast<T>(a + 2);
    check(binary_search(a, b, [&](T x) { return calls++, x != m; }) == a && calls == 1, what);
    check(binary_search(a, b, [&](T x) { return calls++, x == m; }) == m && calls == 2, what);
    check(binary_search(b, a, [&](T x) { return calls++, x != m; }) == b && calls == 3, what);
    check(binary_search(b, a, [&](T x) { return calls++, x == m; }) == m && calls == 4, what);
}

// constexpr で使える
static_assert(binary_search(0, 1000, [](long long m) { return m * m <= 500; }) == 22);
static_assert(binary_search(std::numeric_limits<long long>::max(), std::numeric_limits<long long>::min(), [](long long x) { return x >= 0; }) == 0);

int main() {
    // 通常の境界探索
    check(binary_search(0, 1000, [](long long m) { return m * m <= 500; }) == 22, "sqrt(500)");
    check(binary_search(0, 1000000000, [](int x) { return static_cast<long long>(x) * x <= 1000000000; }) == 31622, "sqrt(1e9)");
    check(binary_search(1000, 0, [](long long m) { return m * m >= 500; }) == 23, "ok > ng");

    // 負数を含む範囲を愚直と比べる
    std::mt19937_64 rng(314);
    for (int rep = 0; rep < 10000; rep++) {
        long long lo = static_cast<long long>(rng() % 2001) - 1000;
        long long hi = lo + static_cast<long long>(rng() % 1000) + 1;
        long long t = lo + static_cast<long long>(rng() % (hi - lo));
        test<long long>(lo, hi, t, "負数を含む long long");
        test<int>(static_cast<int>(lo), static_cast<int>(hi), static_cast<int>(t), "負数を含む int");
    }

    // 型の全範囲
    test_extreme<int>("int の全範囲");
    test_extreme<long long>("long long の全範囲 (LLONG_MIN, LLONG_MAX)");
    test_extreme<unsigned long long>("unsigned long long の全範囲 (0, ULLONG_MAX)");
    test_extreme<unsigned int>("unsigned int の全範囲");
    test_extreme<short>("short の全範囲");

    // 両端の差が 1 または 2
    for (long long a : {std::numeric_limits<long long>::min(), -2LL, -1LL, 0LL, std::numeric_limits<long long>::max() - 2}) test_small<long long>(a, "差が 1, 2 の long long");
    for (int a : {std::numeric_limits<int>::min(), -1, 0, std::numeric_limits<int>::max() - 2}) test_small<int>(a, "差が 1, 2 の int");
    for (unsigned long long a : {0ULL, 1ULL, std::numeric_limits<unsigned long long>::max() - 2}) test_small<unsigned long long>(a, "差が 1, 2 の unsigned long long");

    // 型が違うときは common_type にそろえる
    check(binary_search(0, std::numeric_limits<long long>::max(), [](long long x) { return x <= 3000000000LL; }) == 3000000000LL, "int と long long");
    check(binary_search(0ULL, std::numeric_limits<unsigned long long>::max(), [](unsigned long long x) { return x <= (1ULL << 63); }) == (1ULL << 63), "unsigned long long の上半分");

    if (ok) std::cout << "Hello World" << std::endl;
}
#line 1 "test/unittest/utility/binary-search.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// binary-search.hpp の整数版を、型の全範囲を含む境界で検証する
// ok < ng と ok > ng の両方で、答え・f に渡す値が ok と ng の間にあること・f の呼び出し回数を確かめる
// あふれないことは -fsanitize=undefined でも確かめた
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/utility/binary-search.hpp"

/**
 * @file binary-search.hpp
 * @brief 二分探索
 */

#include <cmath>
#include <type_traits>

#line 2 "src/base/functions.hpp"

/**
 * @file functions.hpp
 * @brief 汎用関数 (chmin, chmax など)
 */

#include <algorithm>
#line 10 "src/base/functions.hpp"
#include <iterator>
#include <limits>
#include <numeric>
#if __cplusplus >= 202002L
#include <ranges>
#endif
#line 17 "src/base/functions.hpp"
#include <utility>

template <typename T1, typename T2>
inline constexpr bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T1, typename T2>
inline constexpr bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

inline constexpr long long max(const long long& t1, const long long& t2) {
    return std::max<long long>(t1, t2);
}

inline constexpr long long min(const long long& t1, const long long& t2) {
    return std::min<long long>(t1, t2);
}

using std::abs;
using std::gcd;
using std::lcm;
using std::size;

#if __cplusplus >= 202002L
using std::midpoint;
#else
// C++20 の std::midpoint と同じ (整数は a の側に丸め、あふれない)
template <typename T>
inline constexpr std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, T> midpoint(T a, T b) noexcept {
    using U = std::make_unsigned_t<T>;
    if (a <= b) return static_cast<T>(a + static_cast<T>(static_cast<U>(static_cast<U>(b) - static_cast<U>(a)) >> 1));
    return static_cast<T>(a - static_cast<T>(static_cast<U>(static_cast<U>(a) - static_cast<U>(b)) >> 1));
}
template <typename T>
inline constexpr std::enable_if_t<std::is_floating_point_v<T>, T> midpoint(T a, T b) noexcept {
    constexpr T lo = std::numeric_limits<T>::min() * 2;
    constexpr T hi = std::numeric_limits<T>::max() / 2;
    const T abs_a = a < 0 ? -a : a, abs_b = b < 0 ? -b : b;
    if (abs_a <= hi && abs_b <= hi) return (a + b) / 2;
    if (abs_a < lo) return a + b / 2;
    if (abs_b < lo) return a / 2 + b;
    return a / 2 + b / 2;
}
template <typename T>
inline constexpr std::enable_if_t<std::is_object_v<T>, T*> midpoint(T* a, T* b) noexcept {
    return a + (b - a) / 2;
}
#endif

/**
 * 要素数を long long で返す (std::ssize の long long 版)
 * size() を持つ型なら、begin() と end() を持たない自作の型にも使える
 */
#if __cplusplus >= 202002L
template <typename R>
    requires requires(R& r) { std::ranges::size(r); }
inline constexpr long long len(R&& r) {
    return static_cast<long long>(std::ranges::size(r));
}
#else
template <typename C>
inline constexpr auto len(const C& c) -> decltype(static_cast<long long>(std::size(c))) {
    return static_cast<long long>(std::size(c));
}
#endif

template <typename T>
constexpr T extgcd(const T& a, const T& b, T& x, T& y) {
    T d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1;
        y = 0;
    }
    return d;
}

/**
 * floor(a / b) (b != 0、負の数も可)
 */
template <typename T1, typename T2>
inline constexpr std::common_type_t<T1, T2> floordiv(T1 a, T2 b) {
    return a / b - (a % b != 0 && (a < 0) != (b < 0));
}
/**
 * ceil(a / b) (b != 0、負の数も可)
 */
template <typename T1, typename T2>
inline constexpr std::common_type_t<T1, T2> ceildiv(T1 a, T2 b) {
    return a / b + (a % b != 0 && (a < 0) == (b < 0));
}
/**
 * {floor(a / b), a - b * floor(a / b)} (b != 0、負の数も可)
 * 余りは 0 または b と同じ符号になる
 */
template <typename T1, typename T2>
inline constexpr std::pair<std::common_type_t<T1, T2>, std::common_type_t<T1, T2>> divmod(T1 a, T2 b) {
    std::common_type_t<T1, T2> q = floordiv(a, b);
    return {q, a - q * b};
}

/**
 * 0 <= x < a
 */
inline constexpr bool inrange(long long x, long long a) {
    return 0 <= x && x < a;
}
/**
 * a <= x < b
 */
inline constexpr bool inrange(long long x, long long a, long long b) {
    return a <= x && x < b;
}
/**
 * 0 <= x < a and 0 <= y < b
 */
inline constexpr bool inrect(long long x, long long y, long long a, long long b) {
    return 0 <= x && x < a && 0 <= y && y < b;
}
#line 2 "src/utility/traits.hpp"

/**
 * @file traits.hpp
 * @brief 型特性
 */

#include <cstddef>
#include <functional>
#line 11 "src/utility/traits.hpp"
#include <set>
#line 14 "src/utility/traits.hpp"

#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif

namespace internal_traits {
using std::begin, std::end;

#if __cplusplus >= 202002L
template <class T>
    struct is_range_impl : std::bool_constant < requires(T& t) {
    begin(t);
    end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif

// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
                                    std::is_constructible_v<S, std::ranges::range_reference_t<R>>;

template <class R>
auto range_size(R& r) {
    return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
    return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
    return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
    return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;

template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
    : std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
}  // namespace internal_traits

template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;

#if __cplusplus >= 202002L
#define HAS_METHOD(func_name)                                                                   \
    namespace internal_traits {                                                                 \
    template <class T>                                                                          \
    concept has_##func_name##_impl = requires { std::declval<T>().func_name(); };               \
    }                                                                                           \
    template <class T>                                                                          \
    struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
    template <class T>                                                                          \
    inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;

#define HAS_METHOD_ARG(func_name)                                                                  \
    namespace internal_traits {                                                                    \
    template <class T, class U>                                                                    \
    concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
    }                                                                                              \
    template <class T, class U>                                                                    \
    struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
    template <class T, class U>                                                                    \
    inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name)                                                              \
    namespace internal_traits {                                                            \
    template <class T, class = void>                                                       \
    struct has_##func_name##_impl : std::false_type {};                                    \
    template <class T>                                                                     \
    struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
        : std::true_type {};                                                               \
    }                                                                                      \
    template <class T>                                                                     \
    struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {};          \
    template <class T>                                                                     \
    inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;

#define HAS_METHOD_ARG(func_name)                                                                              \
    namespace internal_traits {                                                                                \
    template <class T, typename U, class = void>                                                               \
    struct has_##func_name##_impl : std::false_type {};                                                        \
    template <class T, typename U>                                                                             \
    struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
        : std::true_type {};                                                                                   \
    }                                                                                                          \
    template <class T, typename U>                                                                             \
    struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {};                           \
    template <class T, typename U>                                                                             \
    inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif

HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)

#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))

namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
    using is_transparent = void;
    template <class T>
    constexpr T&& operator()(T&& x) const noexcept {
        return std::forward<T>(x);
    }
};
#endif
}  // namespace internal_traits

template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;

namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
}  // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 13 "src/utility/binary-search.hpp"

/**
 * f(ok) が真、f(ng) が偽として、境界で f が真になる側の値を返す
 * 整数なら ok と ng の差が 1 になるまで、浮動小数点数なら 100 回二分する
 */
template <typename M, typename N, class F, ENABLE_IF_T(std::is_integral_v<std::common_type_t<M, N>>and std::is_invocable_r_v<bool, F, std::common_type_t<M, N>>)>
inline constexpr std::common_type_t<M, N> binary_search(const M& ok, const N& ng, F f) {
    using T = std::common_type_t<M, N>;
    using U = std::make_unsigned_t<T>;
    T _ok = ok, _ng = ng;
    // 差は符号なしで求めるとあふれない (LLONG_MIN と LLONG_MAX の差も U に収まる)
    // 中点は midpoint で求める (あふれず、ok の側に丸める)
    while ((_ok < _ng ? static_cast<U>(static_cast<U>(_ng) - static_cast<U>(_ok)) : static_cast<U>(static_cast<U>(_ok) - static_cast<U>(_ng))) > 1) {
        T mid = midpoint(_ok, _ng);
        if (f(mid)) {
            _ok = mid;
        } else {
            _ng = mid;
        }
    }
    return _ok;
}

template <typename M, typename N, class F, ENABLE_IF_T(!std::is_integral_v<std::common_type_t<M, N>> && std::is_invocable_r_v<bool, F, std::common_type_t<M, N>>)>
inline constexpr std::common_type_t<M, N> binary_search(const M& ok, const N& ng, F f) {
    std::common_type_t<M, N> _ok = ok, _ng = ng;
    for (int i = 0; i < 100; i++) {
        std::common_type_t<M, N> mid = (_ok + _ng) / 2;
        if (f(mid)) {
            _ok = mid;
        } else {
            _ng = mid;
        }
    }
    return _ok;
}
#line 9 "test/unittest/utility/binary-search.test.cpp"

#include <iostream>
#line 12 "test/unittest/utility/binary-search.test.cpp"
#include <random>

bool ok = true;

void check(bool cond, const char* what) {
    if (!cond) {
        if (ok) std::cerr << "failed: " << what << std::endl;
        ok = false;
    }
}

// lo <= t < hi として、f(x) = x <= t (ok = lo, ng = hi) と f(x) = x > t (ok = hi, ng = lo) を調べる
template <typename T>
void test(T lo, T hi, T t, const char* what) {
    long long calls = 0;
    bool inside = true;
    T r1 = binary_search(lo, hi, [&](T x) {
        calls++;
        if (!(lo < x && x < hi)) inside = false;
        return x <= t;
    });
    check(r1 == t, what);
    check(inside && calls <= std::numeric_limits<T>::digits + 1, what);
    calls = 0;
    T r2 = binary_search(hi, lo, [&](T x) {
        calls++;
        if (!(lo < x && x < hi)) inside = false;
        return x > t;
    });
    check(r2 == t + 1, what);
    check(inside && calls <= std::numeric_limits<T>::digits + 1, what);
}

// 型の両端と 0 の付近を境界にして調べる
template <typename T>
void test_extreme(const char* what) {
    constexpr T mn = std::numeric_limits<T>::min(), mx = std::numeric_limits<T>::max();
    for (T t : {mn, static_cast<T>(mn + 1), static_cast<T>(mn / 2), static_cast<T>(0), static_cast<T>(1), static_cast<T>(mx / 2), static_cast<T>(mx - 2), static_cast<T>(mx - 1)}) {
        test<T>(mn, mx, t, what);
    }
    if (std::numeric_limits<T>::is_signed) {
        for (T t : {static_cast<T>(-2), static_cast<T>(-1)}) test<T>(mn, mx, t, what);
        test<T>(mn, static_cast<T>(0), static_cast<T>(-1), what);
        test<T>(static_cast<T>(-1), mx, static_cast<T>(0), what);
    }
}

// 差が 1 なら f を呼ばずに ok を返す。差が 2 なら真ん中を 1 回だけ調べる
template <typename T>
void test_small(T a, const char* what) {
    long long calls = 0;
    check(binary_search(a, static_cast<T>(a + 1), [&](T) { return calls++, false; }) == a && calls == 0, what);
    check(binary_search(static_cast<T>(a + 1), a, [&](T) { return calls++, false; }) == static_cast<T>(a + 1) && calls == 0, what);
    T m = static_cast<T>(a + 1), b = static_cast<T>(a + 2);
    check(binary_search(a, b, [&](T x) { return calls++, x != m; }) == a && calls == 1, what);
    check(binary_search(a, b, [&](T x) { return calls++, x == m; }) == m && calls == 2, what);
    check(binary_search(b, a, [&](T x) { return calls++, x != m; }) == b && calls == 3, what);
    check(binary_search(b, a, [&](T x) { return calls++, x == m; }) == m && calls == 4, what);
}

// constexpr で使える
static_assert(binary_search(0, 1000, [](long long m) { return m * m <= 500; }) == 22);
static_assert(binary_search(std::numeric_limits<long long>::max(), std::numeric_limits<long long>::min(), [](long long x) { return x >= 0; }) == 0);

int main() {
    // 通常の境界探索
    check(binary_search(0, 1000, [](long long m) { return m * m <= 500; }) == 22, "sqrt(500)");
    check(binary_search(0, 1000000000, [](int x) { return static_cast<long long>(x) * x <= 1000000000; }) == 31622, "sqrt(1e9)");
    check(binary_search(1000, 0, [](long long m) { return m * m >= 500; }) == 23, "ok > ng");

    // 負数を含む範囲を愚直と比べる
    std::mt19937_64 rng(314);
    for (int rep = 0; rep < 10000; rep++) {
        long long lo = static_cast<long long>(rng() % 2001) - 1000;
        long long hi = lo + static_cast<long long>(rng() % 1000) + 1;
        long long t = lo + static_cast<long long>(rng() % (hi - lo));
        test<long long>(lo, hi, t, "負数を含む long long");
        test<int>(static_cast<int>(lo), static_cast<int>(hi), static_cast<int>(t), "負数を含む int");
    }

    // 型の全範囲
    test_extreme<int>("int の全範囲");
    test_extreme<long long>("long long の全範囲 (LLONG_MIN, LLONG_MAX)");
    test_extreme<unsigned long long>("unsigned long long の全範囲 (0, ULLONG_MAX)");
    test_extreme<unsigned int>("unsigned int の全範囲");
    test_extreme<short>("short の全範囲");

    // 両端の差が 1 または 2
    for (long long a : {std::numeric_limits<long long>::min(), -2LL, -1LL, 0LL, std::numeric_limits<long long>::max() - 2}) test_small<long long>(a, "差が 1, 2 の long long");
    for (int a : {std::numeric_limits<int>::min(), -1, 0, std::numeric_limits<int>::max() - 2}) test_small<int>(a, "差が 1, 2 の int");
    for (unsigned long long a : {0ULL, 1ULL, std::numeric_limits<unsigned long long>::max() - 2}) test_small<unsigned long long>(a, "差が 1, 2 の unsigned long long");

    // 型が違うときは common_type にそろえる
    check(binary_search(0, std::numeric_limits<long long>::max(), [](long long x) { return x <= 3000000000LL; }) == 3000000000LL, "int と long long");
    check(binary_search(0ULL, std::numeric_limits<unsigned long long>::max(), [](unsigned long long x) { return x <= (1ULL << 63); }) == (1ULL << 63), "unsigned long long の上半分");

    if (ok) std::cout << "Hello World" << std::endl;
}
Back to top page