結果

問題 No.3 ビットすごろく
ユーザー aim_cpoaim_cpo
提出日時 2017-05-04 03:37:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 5,000 ms
コード長 1,638 bytes
コンパイル時間 790 ms
コンパイル使用メモリ 95,592 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2023-09-14 00:41:30
合計ジャッジ時間 4,717 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 49 ms
4,376 KB
testcase_10 AC 127 ms
4,376 KB
testcase_11 AC 31 ms
4,380 KB
testcase_12 AC 8 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 108 ms
4,376 KB
testcase_15 AC 284 ms
5,084 KB
testcase_16 AC 161 ms
4,376 KB
testcase_17 AC 252 ms
5,044 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 328 ms
5,596 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 113 ms
4,376 KB
testcase_23 AC 326 ms
6,144 KB
testcase_24 AC 325 ms
6,044 KB
testcase_25 AC 284 ms
5,092 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 152 ms
4,380 KB
testcase_29 AC 32 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 20 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//--------------------------------------------------template----------------------------------------//
//c
#include <stdio.h>
#include <time.h>
#include <math.h>
//c++
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <complex>
#include <cmath>
//c++11
#include <tuple>
#define all(c) ((c).begin(),(c).end())
#define rall(c) (c).rbegin(),(c).rend()
//#define sort(v,n) sort(v,v+n)
//#define vsort(v) sort(v.begin(),v.end())
//#define vvsort(v) sort(v.begin(),v.end(),greater<int>())
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
inline int contbit(int a) {
	int res = 0;
	while (a) {
		if (a & 1)res++;
		a >>= 1;
	}
	return res;
}
int n;
int ans = inf;
bool b[10001];
inline void bfs() {
	queue<pair<int,int>> q;
	q.push(pair<int,int>(1,1));
	b[1] = 1;
	while (!q.empty()) {
		int now = q.front().first;
		int nowcont = q.front().second;
		q.pop();
		b[now] = 1;
		if (now == n) { ans = min(ans, nowcont); break; }
		int m = contbit(now);
		if (now + m <= n && b[now+m]==0 )q.push(pair<int, int>(now + m, nowcont+1));
		if (now - m > 0 && b[now-m]==0)q.push(pair<int, int>(now - m, nowcont+1));
	}
}

int main() {
	cin >> n;
	bfs();
	if (ans!=inf) {
		cout << ans << endl;
	}
	else {
		cout << -1 << endl;
	}
}
0