#if __INCLUDE_LEVEL__ == 0 #include __BASE_FILE__ uint32_t f(uint32_t x, uint32_t y) { uint32_t x_even = x & 0x55555555; uint32_t x_odd = x & 0xaaaaaaaa; uint32_t y_even = y & 0x55555555; uint32_t y_odd = y & 0xaaaaaaaa; return (x_even & y_even) | x_odd | y_odd; } uint32_t g(int n) { vector v; v.reserve(n * n); for (uint32_t x : Rep(0, n)) for (uint32_t y : Rep(0, n)) { v.push_back(f(x, y)); } ranges::sort(v); int max_len = -INF; uint32_t ret = -1; for (int l = 0; l < Len(v);) { int len = 1; while (l + len < Len(v) && v[l] == v[l + len]) ++len; if (SetMax(max_len, len)) ret = v[l]; l += len; } return ret; } int64_t Count(int n, uint32_t target) { array, 2> f{}; f[1][1] = 1; for (int k : Rev(Rep(0, 30))) { array, 2> nf{}; int nk = n >> k & 1; int tk = target >> k & 1; for (int ex : Rep(0, 2)) for (int ey : Rep(0, 2)) { if (f[ex][ey] == 0) continue; for (int x : Rep(0, 2)) { if (ex && x > nk) break; int nex = ex && x == nk; for (int y : Rep(0, 2)) { if (ey && y > nk) break; int ney = ey && y == nk; if (k % 2 == 0) { if ((x & y) != tk) continue; } else { if ((x | y) != tk) continue; } nf[nex][ney] += f[ex][ey]; } } } f = nf; } return f[0][0]; } void Solve() { int64_t N; IN(N); if (N <= 2) { OUT(0); return; } int64_t ans = 0; int64_t val = 2; for (int l = 3;;) { int64_t new_val = val << 2 | 2; auto check = [&](int n) -> bool { return Count(n, val) < Count(n, new_val); }; if (!check(N)) { ans += (N - l + 1) * val; break; } int ok = l; int ng = l + 1; while (!check(ng)) { ng += ng - l; SetMin(ng, N); } while (ok + 1 < ng) { int mid = midpoint(ok, ng); (!check(mid) ? ok : ng) = mid; } ans += int64_t(ok - l + 1) * val; l = ok + 1; val = new_val; } OUT(ans); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); Solve(); } #elif __INCLUDE_LEVEL__ == 1 #include template concept MyRange = std::ranges::range && !std::convertible_to && !std::convertible_to; template concept MyTuple = std::__is_tuple_like::value && !MyRange; namespace std { istream& operator>>(istream& is, MyRange auto&& r) { for (auto&& e : r) { is >> e; } return is; } istream& operator>>(istream& is, MyTuple auto&& t) { apply([&](auto&... xs) { (is >> ... >> xs); }, t); return is; } ostream& operator<<(ostream& os, MyRange auto&& r) { auto sep = ""; for (auto&& e : r) { os << exchange(sep, " ") << forward(e); } return os; } ostream& operator<<(ostream& os, MyTuple auto&& t) { auto sep = ""; apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t); return os; } } // namespace std template class OneBased { public: explicit OneBased(T&& x) : ref_(std::forward(x)) {} template requires(sizeof...(Ts) > 1) OneBased(Ts&&... xs) : ref_(std::forward_as_tuple(std::forward(xs)...)) {} friend std::istream& operator>>(std::istream& is, OneBased x) { if constexpr (MyRange) { for (auto&& e : x.ref_) { is >> ::OneBased(e); } } else if constexpr (MyTuple) { std::apply([&](auto&... xs) { (is >> ... >> ::OneBased(xs)); }, x.ref_); } else { is >> x.ref_; --x.ref_; } return is; } friend std::ostream& operator<<(std::ostream& os, OneBased x) { if constexpr (MyRange) { auto f = [](auto&& e) { return ::OneBased(std::forward(e)); }; os << (x.ref_ | std::views::transform(f)); } else if constexpr (MyTuple) { std::apply([&](auto&... xs) { os << std::tuple(::OneBased(xs)...); }, x.ref_); } else { os << ++x.ref_; --x.ref_; } return os; } private: T ref_; }; template OneBased(T&&) -> OneBased; template OneBased(Ts&&...) -> OneBased>; using namespace std; #define LAMBDA2(x, y, ...) ([&](auto&& x, auto&& y) -> decltype(auto) { return __VA_ARGS__; }) #define Rev views::reverse #define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__) #define Len(r) int(size(r)) #define SetMin(...) LAMBDA2(x, y, y < x && (x = y, 1))(__VA_ARGS__) #define SetMax(...) LAMBDA2(x, y, x < y && (x = y, 1))(__VA_ARGS__) #define INF (INT_MAX / 2) #define IN(...) (cin >> forward_as_tuple(__VA_ARGS__)) #define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n') #endif // __INCLUDE_LEVEL__ == 1