結果

問題 No.3 ビットすごろく
コンテスト
ユーザー newlife171128
提出日時 2017-12-21 22:20:11
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 972 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 390 ms
コンパイル使用メモリ 92,268 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2026-03-22 05:45:46
合計ジャッジ時間 1,976 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cmath>

using namespace std;

int nums[10001] = { 10000 };
int bit_time[10001] = { };
int n = 0;
void dfs(int x, int times) {
	if (x > n || x < 1 || times + 1 > nums[x]) {
		return;
	}

	/*
	 if (nums[x] != 0 && ++times < nums[x]) {
	 nums[x] = ++times;
	 }
	 if (nums[x] == 0) {
	 nums[x] = ++times;
	 }
	 */

	nums[x] = min(nums[x], ++times);
	/*


	 cout << "nums:" << x << " " << times << endl;
	 */

	/*	 cout<<nums[x]<<endl;*/

	int count = 0;
	if (bit_time[x] == 0) {
		for (int k = 0; k <= /* ceil(sqrt(i))*/log2(x); k++) {
			if ((x >> k) % 2 == 1) {
				count++;
			}
		}
		bit_time[x] = count;
	}
	dfs(x + bit_time[x], times);

	dfs(x - bit_time[x], times);

}
int main() {

	cin >> n;

	for (int i = 1; i <= n; i++) {
		nums[i] = 10000;
	}

	dfs(1, 0);

	if (nums[n] != 10000) {
		cout << nums[n] << endl;
	} else {
		cout << -1 << endl;
	}
	return 0;
}
0