結果

問題 No.3 ビットすごろく
コンテスト
ユーザー abinull
提出日時 2025-06-29 17:21:56
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 797 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 339 ms
コンパイル使用メモリ 85,748 KB
最終ジャッジ日時 2026-07-12 23:16:33
合計ジャッジ時間 1,144 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_algobase.h:76,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/string:53,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/locale_classes.h:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/ios_base.h:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/ios:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/ostream.h:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/ostream:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/iostream:43,
                 from main.cpp:1:
/home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bit: In instantiation of 'constexpr int std::__popcount(_Tp) [with _Tp = int]':
main.cpp:30:26:   required from here
   30 |       bitcnt = __popcount(u);
      |                ~~~~~~~~~~^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bit:308:34: error: argument 1 in call to function '__builtin_popcountg' has signed type
  308 |       return __builtin_popcountg(__x);
      |                                  ^~~

ソースコード

diff #
raw source code

#include <iostream>
#include <bitset>
#include <vector>
#include <queue>

using namespace std;

int main() {

  int n;
  const int inf = 1000000;

  cin >> n;

  vector<int> w(n+1,inf);
  vector<bool> v(n+1, false);

  queue<int> q;

 int u, ub, uf;
  q.push(1);
  u = 1; w[1] = 1; v[1] = true;

  while (!q.empty()) {

    int bitcnt;

      u = q.front(); q.pop();

      bitcnt = __popcount(u);

      ub = u - bitcnt; uf = u + bitcnt;

      if (ub>0 && ub<=n && v[ub]==false) {
        q.push(ub);
        v[ub] = true;
        w[ub] = w[u] + 1; 
      }

      if (uf>0 && uf<=n && v[uf]==false) {
        q.push(uf);
        v[uf] = true;
        w[uf] = w[u] + 1;
      }
  } 
  cout << (v[n] == true ? w[n] : -1) << endl;

  return 0;
}

0