結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2019-07-24 11:00:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 850 bytes |
コンパイル時間 | 1,861 ms |
コンパイル使用メモリ | 171,792 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 09:26:17 |
合計ジャッジ時間 | 2,581 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i)) using ll = long long; using P = pair<int, int>; using namespace std; bool visited[101010]; int main() { int N; cin >> N; queue<P> que; que.push({1, 1}); visited[1] = true; while (que.size()) { P p = que.front(); que.pop(); if (p.first == N) { cout << p.second << endl; return 0; } int b = p.first; int x = b + __builtin_popcount(p.first); if (x <= N and !visited[x]) { visited[x] = true; que.push({x, p.second+1}); } int y = b - __builtin_popcount(p.first); if (y >= 1 and !visited[y]) { visited[y] = true; que.push({y, p.second+1}); } } cout << -1 << endl; }