結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2015-10-19 15:25:19 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 773 bytes |
コンパイル時間 | 494 ms |
コンパイル使用メモリ | 64,556 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 07:33:57 |
合計ジャッジ時間 | 1,479 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <iostream> #include <cstdio> #include <queue> using namespace std; const int INF = 1e+9; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int ans = -1; queue<int> que; que.push(1); bool visited[10001] = {0}; int d[10001]; for(int i = 0; i <= n; ++i) d[i] = INF; d[1] = 1; while(!que.empty()){ int p = que.front(); que.pop(); if(n == p){ ans = d[p]; break; } if(visited[p]){ continue; } visited[p] = true; int next = __builtin_popcount(p); if(next + p <= n){ d[next + p] = min(d[next + p], d[p] + 1); que.push(next + p); } if(p - next >= 1){ d[p - next] = min(d[p - next], d[p] + 1); que.push(p - next); } } cout << ans << endl; return 0; }