結果
問題 | No.3 ビットすごろく |
ユーザー | soylio |
提出日時 | 2020-01-17 18:35:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 691 bytes |
コンパイル時間 | 679 ms |
コンパイル使用メモリ | 74,320 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 09:36:58 |
合計ジャッジ時間 | 1,659 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> #include <queue> using namespace std; int count(int n) { int t = 0; while (n > 0) { if (n & 1) t++; n >>= 1; } return t; } int dist[10005]; int main() { int n; queue<int> q; cin >> n; for (int i = 1; i <= n; i++) { dist[i] = -1; } dist[1] = 1; q.push(1); while (!q.empty()) { int p = q.front(); q.pop(); if (p - count(p) > 0) { if (dist[p - count(p)] == -1) { q.push(p - count(p)); dist[p - count(p)] = dist[p] + 1; } } if (p + count(p) <= n) { if (dist[p + count(p)] == -1) { q.push(p + count(p)); dist[p + count(p)] = dist[p] + 1; } } if (dist[n] != -1) break; } cout << dist[n] << endl; return 0; }