結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-11-22 18:45:06 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 814 bytes |
| コンパイル時間 | 1,588 ms |
| コンパイル使用メモリ | 171,468 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 09:11:43 |
| 合計ジャッジ時間 | 2,620 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int cntbit(int x)
{
int ret = 0;
while(x > 0){
if(x % 2 == 1) ret++;
x /= 2;
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<int> cnt(n + 1);
fill(cnt.begin(), cnt.end(), 1e9);
queue<int> q;
q.push(1);
cnt[1] = 1;
while(!q.empty()){
int now = q.front(); q.pop();
int dist = cntbit(now);
if(now + dist >= 1 && now + dist <= n && cnt[now + dist] == 1e9) {
q.push(now + dist);
cnt[now + dist] = cnt[now] + 1;
}
if(now - dist >= 1 && now - dist <= n && cnt[now - dist] == 1e9) {
q.push(now - dist);
cnt[now - dist] = cnt[now] + 1;
}
}
if(cnt[n] != 1e9) cout << cnt[n] << endl;
else cout << -1 << endl;
return 0;
}