結果

問題 No.3 ビットすごろく
ユーザー soupesuteakasoupesuteaka
提出日時 2014-12-29 01:07:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,090 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 90,828 KB
実行使用メモリ 6,680 KB
最終ジャッジ日時 2023-09-13 22:58:28
合計ジャッジ時間 2,136 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,164 KB
testcase_01 AC 3 ms
6,148 KB
testcase_02 AC 4 ms
6,096 KB
testcase_03 AC 3 ms
6,176 KB
testcase_04 AC 3 ms
6,120 KB
testcase_05 AC 4 ms
6,364 KB
testcase_06 AC 3 ms
6,180 KB
testcase_07 AC 3 ms
6,212 KB
testcase_08 AC 4 ms
6,260 KB
testcase_09 AC 5 ms
6,396 KB
testcase_10 AC 4 ms
6,396 KB
testcase_11 AC 4 ms
6,344 KB
testcase_12 AC 4 ms
6,288 KB
testcase_13 AC 3 ms
6,424 KB
testcase_14 AC 5 ms
6,384 KB
testcase_15 AC 5 ms
6,680 KB
testcase_16 AC 5 ms
6,376 KB
testcase_17 AC 4 ms
6,416 KB
testcase_18 AC 4 ms
6,148 KB
testcase_19 AC 5 ms
6,436 KB
testcase_20 AC 4 ms
6,260 KB
testcase_21 AC 3 ms
6,252 KB
testcase_22 AC 4 ms
6,360 KB
testcase_23 AC 4 ms
6,516 KB
testcase_24 AC 5 ms
6,544 KB
testcase_25 AC 5 ms
6,404 KB
testcase_26 AC 3 ms
6,292 KB
testcase_27 AC 4 ms
6,188 KB
testcase_28 AC 4 ms
6,404 KB
testcase_29 AC 5 ms
6,420 KB
testcase_30 AC 3 ms
6,100 KB
testcase_31 AC 4 ms
6,164 KB
testcase_32 AC 4 ms
6,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <iomanip>

#define INF 1000000001
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const double PI = acos(-1.0);

int N;
vector<int> G[100001];
int dist[100001];

int main()
{
	ios::sync_with_stdio(false);

	cin >> N;
	FOR(i,1,N+1) {
		int bc=0, x=i;
		while (x) {
			if (x&1) bc++;
			x >>= 1;
		}
		if (i-bc>0) G[i].push_back(i-bc);
		if (i+bc<=N) G[i].push_back(i+bc);
	}
	queue< pii > q;
	q.push( pii(1, 1) );
	memset(dist, 0, sizeof(dist));
	while (!q.empty()) {
		pii p = q.front(); q.pop();
		if (dist[p.first]) continue;
		dist[p.first] = p.second;
		FOR(i,0,G[p.first].size()) {
			q.push(pii(G[p.first][i], p.second+1));
		}
	}
	if (dist[N]) cout << dist[N] << endl;
	else cout << -1 << endl;
	
	return 0;
}
0