結果
問題 | No.3 ビットすごろく |
ユーザー | GOTKAKO |
提出日時 | 2024-05-06 23:35:26 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 707 bytes |
コンパイル時間 | 2,210 ms |
コンパイル使用メモリ | 205,420 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-29 10:28:41 |
合計ジャッジ時間 | 3,362 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,inf = 1e9; cin >> N; vector<int> dist(N+1,inf); dist.at(1) = 1; queue<int> Q; Q.push(1); while(Q.size()){ int num = Q.front(); int one = 0; while(num){ if(num&1) one++; num >>= 1; } num = Q.front(); Q.pop(); if(num+one <= N && dist.at(num+one) == inf) dist.at(num+one) = dist.at(num)+1,Q.push(num+one); if(num-one >= 1 && dist.at(num-one) == inf) dist.at(num-one) = dist.at(num)+1,Q.push(num-one); } if(dist.at(N) == inf) dist.at(N) = -1; cout << dist.at(N) << endl; }