結果

問題 No.3 ビットすごろく
ユーザー newlife171128
提出日時 2017-12-21 22:20:11
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 152 ms / 5,000 ms
コード長 972 bytes
コンパイル時間 587 ms
コンパイル使用メモリ 64,584 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 08:53:19
合計ジャッジ時間 2,748 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#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