結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
dazy
|
| 提出日時 | 2017-11-24 14:52:29 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,340 bytes |
| コンパイル時間 | 531 ms |
| コンパイル使用メモリ | 65,216 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 08:50:40 |
| 合計ジャッジ時間 | 1,514 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <queue>
using namespace std;
#define fastcin {\
cin.tie(0);\
ios::sync_with_stdio(false);\
}
#define REP(i, a) for(int i = 0; i < a; i++)
#define scan(x) cin >> n;
#define print(x) cout << x << "\n"
#define INF 999999999
int countbit(int n) {
int count = 0;
for (int i = 1; i < n + 1; i <<= 1) if (n & i) count++;
return count;
}
int main() {
fastcin;
int n;
scan(n);
int masu[10001];
queue<int> q;
REP(i, 10001) masu[i] = INF;
int ans = -1;
masu[1] = 1;
q.push(1);
int move, p ,m, v;
if (n != 1) {
while (q.size()) {
v = q.front();
q.pop();
if (masu[v] != INF && masu[v] != -1) {
move = countbit(v);
p = v + move;
m = v - move;
if (p == n || m == n) {
print(masu[v] + 1);
exit(0);
}
if (p < n && masu[p] == INF) {
q.push(p);
masu[p] = masu[v] + 1;
}
if (m > 0 && masu[m] == INF) {
q.push(m);
masu[m] = masu[v] + 1;
}
masu[v] = -1;
}
}
}
else ans = 1;
print(ans);
return 0;
}
dazy