結果

問題 No.3 ビットすごろく
ユーザー トミー
提出日時 2024-11-27 15:44:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 717 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 200,608 KB
最終ジャッジ日時 2025-02-25 06:20:41
ジャッジサーバーID
(参考情報)
judge3 / judge2
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:41:28: warning: ignoring return value of ‘int system(const char*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   41 | int main() { solve();system("pause"); }
      |                      ~~~~~~^~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;

ll bitCount(ll n){
  ll res=0;
  while(n>0){
    res+=n&1;
    n>>=1;
  }
  return res;
}

void solve() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<bool> seen(n+1,false);
  deque<pair<ll,ll>> dq;
  dq.push_back({1,1});
  while(!dq.empty()){ 
    auto [a,b]=dq.front();
    dq.pop_front();
    if(a==n){
      cout << b << endl;
      return;
    }
    if(seen.at(a)) continue;
    seen.at(a)=true;
    if(a-bitCount(a)>=1){
      dq.push_back({a-bitCount(a),b+1});
    }
    if(a+bitCount(a)<=n){
      dq.push_back({a+bitCount(a),b+1});
    }
  }
  cout << -1 << endl;
}

int main() { solve();system("pause"); }
0