結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2019-12-15 16:17:40 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 836 bytes |
コンパイル時間 | 1,795 ms |
コンパイル使用メモリ | 172,972 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 09:34:09 |
合計ジャッジ時間 | 2,858 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <bits/stdc++.h> using namespace std; const int INF = numeric_limits<int>::max(); int bitcnt(int num) { int c = 0; while (num > 0) { if (num % 2 == 1) { c++; } num /= 2; } return c; } int main() { int n; cin >> n; queue<int> q; q.push(1); vector<int> dist(n + 1, INF); dist[1] = 1; while (!q.empty()) { int now = q.front(); q.pop(); int bit = bitcnt(now); if (now - bit > 0 && dist[now - bit] == INF) { dist[now - bit] = dist[now] + 1; q.emplace(now - bit); } if (now + bit <= n && dist[now + bit] == INF) { dist[now + bit] = dist[now] + 1; q.emplace(now + bit); } } cout << (dist[n] < INF ? dist[n] : -1) << endl; return 0; }