結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
cormoran
|
| 提出日時 | 2016-11-14 02:54:37 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 855 bytes |
| コンパイル時間 | 1,786 ms |
| コンパイル使用メモリ | 172,928 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 08:10:32 |
| 合計ジャッジ時間 | 2,640 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template<class T>
int bitcount(T n){
int ret = 0;
while (n > 0) ret++, n &= n - 1;
return ret;
}
int main() {
int N; cin >> N;
vector<int> visited(N);
queue<int> que;
que.push(0);
int ans = 1;
for(; que.size(); ans++) {
queue<int> nxt_que;
while(que.size()) {
int a = que.front(); que.pop();
if(visited[a]) continue;
visited[a] = true;
if(a == N - 1) goto OK;
// int b = __builtin_popcount(a + 1);
int b = bitcount(a + 1);
if(a + b < N and not visited[a + b]) nxt_que.push(a + b);
if(a - b >= 0 and not visited[a - b]) nxt_que.push(a - b);
}
swap(que, nxt_que);
}
cout << -1 << endl;
return 0;
OK:
cout << ans << endl;
return 0;
}
cormoran