結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-07-10 15:36:52 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 611 bytes | 
| コンパイル時間 | 1,370 ms | 
| コンパイル使用メモリ | 163,740 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:24:21 | 
| 合計ジャッジ時間 | 2,367 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define MAX 9999999
int bitcount(int a){
	int ret = 0;
	while (a > 0){
		ret++;
		a &= a - 1;
	}
	return ret;
}
int main() {
	int N;
	cin >> N;
	vector<int> dp(N + 1, MAX);
	dp[1] = 1;
	queue<int> q;
	q.push(1);
	while (!q.empty()){
		int now = q.front(); q.pop();
		int move = bitcount(now);
		for (int i = -move; i <= move; i += move * 2)
		{
			int next = now + i;
			if (next < 1 || next > N) continue;
			if (dp[next] != MAX) continue;
			dp[next] = dp[now] + 1;
			q.push(next);
		}
	}
	if (dp[N] == MAX) cout << -1 << endl;
	else cout << dp[N] << endl;
}
            
            
            
        