結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-01-17 17:24:48 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 822 bytes |
| コンパイル時間 | 552 ms |
| コンパイル使用メモリ | 68,188 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-01 07:10:13 |
| 合計ジャッジ時間 | 1,631 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int get_bits(int num) {
int count = 0;
int tmp = num;
while(tmp) {
if (tmp & 1) {
count++;
}
tmp >>= 1;
}
return count;
}
int main() {
int n;
cin >> n;
vector<int> move(n + 1, -1);
move[0] = 0;
queue <int> que;
que.push(1);
move[1] = 1;
while (!que.empty()) {
int now = que.front();
que.pop();
if (now == n) {
break;
}
int diff = get_bits(now);
int next1 = now + diff;
int next2 = now - diff;
if (move[next1] == -1 && 0 < next1 && next1 <= n) {
move[next1] = move[now] + 1;
que.push(next1);
}
if (move[next2] == -1 && 0 < next2 && next2 <= n) {
move[next2] = move[now] + 1;
que.push(next2);
}
}
cout << move[n] << endl;
return 0;
}