結果

問題 No.3 ビットすごろく
ユーザー yukiteru
提出日時 2018-02-10 10:27:21
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,221 bytes
コンパイル時間 823 ms
コンパイル使用メモリ 88,156 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 08:53:57
合計ジャッジ時間 1,764 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<vector>
#include<utility>
#include<stack>
#include<queue>
#include<list>
#include<bitset>
#include<functional>

#define FOR(i, a, b) for(int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000
#define PI 3.14159265358979

using namespace std;
typedef pair<int, int> P;

int main(void) {
	int n;
	int dp[10001] = {};
	queue<P>que;

	cin >> n;

	que.push(P(1,1));
	dp[1] = 1;
	if (n == 1) {
		cout << 1 << endl;
	}
	else {
		while (1) {
			int a, deep;
			int b;
			int flag = 0;
			a = que.front().first;
			deep = que.front().second;
			b = a;
			que.pop();
			while (1) {
				if (b % 2 == 1) {
					flag++;
				}
				b /= 2;
				if (b == 0) {
					break;
				}
			}
			if (a + flag == n) {
				cout << deep + 1 << endl;
				break;
			}
			if (a - flag > 0 && dp[a - flag] == 0) {
				que.push(P(a - flag, deep + 1));
				dp[a - flag] = 1;
			}
			if (a + flag < n&&dp[a + flag] == 0) {
				que.push(P(a + flag, deep + 1));
				dp[a + flag] = 1;
			}
			if (que.empty() == 1) {
				cout << -1 << endl;
				break;
			}
		}
	}
	


	
	return 0;
}
0