結果

問題 No.3 ビットすごろく
ユーザー tokutoku
提出日時 2019-03-18 22:25:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3,053 ms / 5,000 ms
コード長 1,474 bytes
コンパイル時間 1,917 ms
コンパイル使用メモリ 151,792 KB
実行使用メモリ 37,916 KB
最終ジャッジ日時 2023-09-14 01:15:15
合計ジャッジ時間 26,716 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 66 ms
4,376 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 25 ms
4,376 KB
testcase_09 AC 440 ms
7,668 KB
testcase_10 AC 1,155 ms
12,172 KB
testcase_11 AC 256 ms
6,856 KB
testcase_12 AC 54 ms
4,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 955 ms
12,200 KB
testcase_15 AC 2,611 ms
30,292 KB
testcase_16 AC 1,488 ms
12,080 KB
testcase_17 AC 2,381 ms
29,516 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 3,031 ms
37,916 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1,018 ms
12,108 KB
testcase_23 AC 3,053 ms
37,800 KB
testcase_24 AC 3,026 ms
37,700 KB
testcase_25 AC 2,631 ms
30,104 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 1,382 ms
12,076 KB
testcase_29 AC 271 ms
7,004 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 165 ms
5,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<int(n); i++)
#define FOR(i,m,n) for(int i=int(m); i<int(n); i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i,a) for(auto &i:a)

typedef pair <int, int> P;
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
const int  INF = 1 << 30;
const ll INFL = 1LL<<60;
const ll mod = 1000000007;		





int main() {
	vector<bool> used(10050, false);
	VI v(10050, 0);
	VI ans(10050, INF);
	int n;

	cin >> n;


	FOR(i, 1, 10001) {
		int j = i;
		while (j) {
			v[i] += j % 2;
			j /= 2;
		}
	}
	
	queue<pair<int,int>> q;

	q.push({ 1, 1 });

	while (!q.empty()) {
		pair<int, int> p = q.front();
		used[p.first] = true;
		q.pop();
		ans[p.first] = min(ans[p.first], p.second);
		if (p.first - v[p.first] > 0) {
			if (!used[p.first - v[p.first]]) {
				q.push({ p.first - v[p.first],p.second + 1 });
			}
		}
		if (p.first + v[p.first] <= n) {
			if (!used[p.first + v[p.first]]) {
				q.push({ p.first + v[p.first],p.second + 1 });
			}
		}
	}


	if (ans[n] == INF)cout << -1 << endl;
	else cout << ans[n] << endl;

	return 0;
}
0