結果

問題 No.3789 Binary 0-count Sequence
コンテスト
ユーザー risujiroh
提出日時 2026-11-06 23:17:02
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 3,665 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,643 ms
コンパイル使用メモリ 367,796 KB
実行使用メモリ 28,360 KB
最終ジャッジ日時 2026-11-06 23:17:14
合計ジャッジ時間 5,681 ms
ジャッジサーバーID
(参考情報)
judge7_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

void Solve() {
  int n;
  IN(n);
  vector<int> a(n);
  IN(a);

  a.push_back(1);  // f(0)
  for (int sz = 1;; sz *= 2) {
    for (uint32_t i : Rep(sz, 2 * sz))
      a.push_back(bit_width(i) - popcount(i));
    if (sz >= n)
      break;
  }

  auto z = atcoder::z_algorithm(a);
  for (int L : Rep(0, Len(z) - n))
    if (z[n + L] >= n) {
      OUT(L);
      return;
    }

  if (n == 1) {
    OUT(int64_t(1) << a[0]);
    return;
  }

  a.resize(n);

  int mn = ranges::min(a);

  for (int& e : a)
    e -= mn;

  int x = int(ranges::find(a, 0) - a.begin());
  int64_t L;
  if (x + 1 == n) {
    int k = 1;
    while (true) {
      L = (int64_t(1) << k) - n;
      if (L >= 0) {
        a.push_back(k);
        break;
      }
      ++k;
    }
  }
  L = (int64_t(1) << a[x + 1]) - (x + 1);

  if (mn > 0) {
    L += int64_t(1) << a[x + 1];
    L += int64_t(1) << (a[x + 1] + mn + 1);
  }

  for (int& e : a)
    e += mn;

  for (int i : Rep(0, n)) {
    uint64_t x = uint64_t(L + i);
    if (a[i] != bit_width(x) - popcount(x)) {
      OUT(-1);
      return;
    }
  }

  OUT(L);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t;
  IN(t);
  while (t--) {
    Solve();
  }
}

#elif __INCLUDE_LEVEL__ == 1

#include <bits/stdc++.h>

#include <atcoder/string.hpp>

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 Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__)
#define Len(r) int(size(r))
#define IN(...) (cin >> forward_as_tuple(__VA_ARGS__))
#define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n')

#endif  // __INCLUDE_LEVEL__ == 1
0