結果

問題 No.3554 Rurumaru Function Problem 2
コンテスト
ユーザー risujiroh
提出日時 2026-05-23 00:14:13
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,953 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,331 ms
コンパイル使用メモリ 347,344 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-23 00:14:22
合計ジャッジ時間 3,207 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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<uint32_t> 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<array<int64_t, 2>, 2> f{};
  f[1][1] = 1;
  for (int k : Rev(Rep(0, 30))) {
    array<array<int64_t, 2>, 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 <bits/stdc++.h>

template <class T>
concept MyRange =
    std::ranges::range<T> &&
    !std::convertible_to<T, std::string_view> &&
    !std::convertible_to<T, std::filesystem::path>;

template <class T>
concept MyTuple = std::__is_tuple_like<T>::value && !MyRange<T>;

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<decltype(e)>(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 T>
class OneBased {
 public:
  explicit OneBased(T&& x) : ref_(std::forward<T>(x)) {}

  template <class... Ts>
  requires(sizeof...(Ts) > 1)
      OneBased(Ts&&... xs) : ref_(std::forward_as_tuple(std::forward<Ts>(xs)...)) {}

  friend std::istream& operator>>(std::istream& is, OneBased x) {
    if constexpr (MyRange<T>) {
      for (auto&& e : x.ref_) {
        is >> ::OneBased(e);
      }
    } else if constexpr (MyTuple<T>) {
      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<T>) {
      auto f = [](auto&& e) { return ::OneBased(std::forward<decltype(e)>(e)); };
      os << (x.ref_ | std::views::transform(f));
    } else if constexpr (MyTuple<T>) {
      std::apply([&](auto&... xs) { os << std::tuple(::OneBased(xs)...); }, x.ref_);
    } else {
      os << ++x.ref_;
      --x.ref_;
    }
    return os;
  }

 private:
  T ref_;
};

template <class T>
OneBased(T&&) -> OneBased<T>;

template <class... Ts>
OneBased(Ts&&...) -> OneBased<std::tuple<Ts...>>;

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
0