#pragma once
#include "../../bit/lowbit/lib.hpp"
#include "../../util/alias/num/lib.hpp"
namespace tifa_libs {
template <u32 = 0>
class gosper {
static inline u32 n_, k_;
u64 now_;
static CEXP u64 num_begin() NE { return (1_u64 << k_) - 1; }
static CEXP u64 num_end() NE { return 1_u64 << n_; }
public:
static CEXP void set(u32 n, u32 k) NE {
assert(k && k <= n && n < 64);
n_ = n, k_ = k;
}
static CEXP gosper begin() NE { return num_begin(); }
static CEXP gosper end() NE { return num_end(); }
CEXP gosper(u64 now = num_begin()) NE : now_(min(now, num_end())) { assert(now == num_end() || (u32)std::popcount(now) == k_); }
CEXP u64 operator*() CNE { return now_; }
CEXP bool operator!=(gosper CR x) CNE { return **this != *x; }
CEXP void operator++() NE {
u64 c = lowbit(now_), r = now_ + c;
now_ = min(((r ^ now_) / 4 / c) | r, num_end());
}
};
} // namespace tifa_libs
#line 2 "src/gen/gosper/lib.hpp"
#line 2 "src/bit/lowbit/lib.hpp"
#line 2 "src/util/util/lib.hpp"
// https://github.com/Tiphereth-A/CP-lib
#include <bits/extc++.h>
// clang-format off
namespace tifa_libs {
#define CEXP constexpr
#define CEXPE constexpr explicit
#define CR const&
#define CP const*
#define PC *const
#define CPC const*const
#define TPN typename
#define NE noexcept
#define CNE const noexcept
#define ND [[nodiscard]]
#define cT_(...) std::conditional_t<sizeof(__VA_ARGS__) <= sizeof(size_t) * 2, __VA_ARGS__, __VA_ARGS__ CR>
// NOLINTNEXTLINE(misc-const-correctness)
#define flt_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i < i##e; ++i)
#define retif_(cond, if_true, ...) if cond return if_true __VA_OPT__(; else return __VA_ARGS__)
#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif
using namespace std::ranges;
using namespace std::literals;
template <class T>
CEXP T abs(T x) NE { retif_((x < 0), -x, x); }
} // namespace tifa_libs
// clang-format on
#line 4 "src/bit/lowbit/lib.hpp"
namespace tifa_libs {
template <class T>
CEXP T lowbit(T x) NE { return T(1) << std::countr_zero(x); }
} // namespace tifa_libs
#line 2 "src/util/alias/num/lib.hpp"
#line 4 "src/util/alias/num/lib.hpp"
// clang-format off
namespace tifa_libs {
#define mk0_(w, t) using w = t; using c##w = const t
#define mk_(w, t) mk0_(w, t); CEXP w operator""_##w(unsigned long long x) NE { return (w)x; }
mk_(i8, int8_t) mk_(u8, uint8_t) mk_(i16, int16_t) mk_(u16, uint16_t) mk_(i32, int32_t) mk_(u32, uint32_t) mk_(i64, int64_t) mk_(u64, uint64_t) mk_(isz, ssize_t) mk_(usz, size_t) mk_(chr, char) mk_(schr, signed char) mk_(uchr, unsigned char) mk_(sint, signed) mk_(uint, unsigned);
mk0_(i128, __int128_t); mk0_(u128, __uint128_t); mk0_(f32, float); mk0_(f64, double); mk0_(f128, long double);
#undef mk0_
#undef mk_
} // namespace tifa_libs
// clang-format on
#line 5 "src/gen/gosper/lib.hpp"
namespace tifa_libs {
template <u32 = 0>
class gosper {
static inline u32 n_, k_;
u64 now_;
static CEXP u64 num_begin() NE { return (1_u64 << k_) - 1; }
static CEXP u64 num_end() NE { return 1_u64 << n_; }
public:
static CEXP void set(u32 n, u32 k) NE {
assert(k && k <= n && n < 64);
n_ = n, k_ = k;
}
static CEXP gosper begin() NE { return num_begin(); }
static CEXP gosper end() NE { return num_end(); }
CEXP gosper(u64 now = num_begin()) NE : now_(min(now, num_end())) { assert(now == num_end() || (u32)std::popcount(now) == k_); }
CEXP u64 operator*() CNE { return now_; }
CEXP bool operator!=(gosper CR x) CNE { return **this != *x; }
CEXP void operator++() NE {
u64 c = lowbit(now_), r = now_ + c;
now_ = min(((r ^ now_) / 4 / c) | r, num_end());
}
};
} // namespace tifa_libs