結果

問題 No.3 ビットすごろく
ユーザー memmemmi
提出日時 2018-06-22 15:45:17
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 979 bytes
コンパイル時間 872 ms
コンパイル使用メモリ 95,604 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 09:03:29
合計ジャッジ時間 1,844 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <iomanip>
using namespace std;
#define INF 1e9
#define PI acos(-1)
typedef long long ll;

int bitCnt(int x) {
	int cnt = 0;
	for (int i = 0; i < 14; i++) {
		if (x & 1 << i)cnt++;
	}
	return cnt;
}

int main() {
	
	int n; cin >> n;

	vector <int> check(n + 1, INT_MAX);
	queue<int> c;
	check[1] = 1;

	c.push(1);

	while (!c.empty()) {
		int p = c.front(); c.pop();
		int d = bitCnt(p);

		for (int i = -d; i <= d; i += 2 * d) {
			int np = p + i;
			if (np<1 || np>n)continue;
			if (check[np] != INT_MAX)continue;
			check[np] = check[p] + 1;
			c.push(np);
		}
	}

	cout << ((check[n] == INT_MAX) ? (-1) : check[n]) << endl;

	return 0;
}
0