結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-08-30 18:03:20 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,290 bytes | 
| コンパイル時間 | 1,598 ms | 
| コンパイル使用メモリ | 173,184 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:03:58 | 
| 合計ジャッジ時間 | 2,730 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(),(a).end()
#define ifnot(a) if(not a)
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;
// #define int ll
bool test = 0;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX (int)1e6 + 5
int bit_count(int n) {
	int cnt = 0;
	while (n != 0) {
		if (n & 1) cnt++;
		n >>= 1;
	}
	return cnt;
}
int N;
bool visited[MAX];
int bfs(int start) {
	queue<pair<int,int>> q;
	visited[0] = true;
	q.push({ start,0 });
	while (q.size()) {
		pair<int,int> now = q.front(); q.pop();
		if (now.first == N) return now.second + 1;
		auto next = now;
		next.second++;
		next.first = now.first - bit_count(now.first);
		if (next.first > 0 && not visited[next.first]) {
			visited[next.first] = true;
			q.push(next);
		}
		next.first = now.first + bit_count(now.first);
		if (next.first <= N && not visited[next.first]) {
			visited[next.first] = true;
			q.push(next);
		}
	}
	return -1;
}
signed main() {
	ll i, j, k;
	cin >> N;
	cout << bfs(1) << endl;
}
            
            
            
        