結果

問題 No.3 ビットすごろく
ユーザー prog470prog470
提出日時 2016-09-12 23:26:06
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,133 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 69,220 KB
実行使用メモリ 20,224 KB
最終ジャッジ日時 2024-04-28 12:38:54
合計ジャッジ時間 7,253 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

long long dp[10005] = { -1 };
int N;

int f(int num) {
	int ret = 0;
	for (int i = 0; i < 32; i++) {
		if (num >> i & 1 == 1) ret++;
	}
	return ret;
}

int dfs(int num) { //numまで何歩でいけるかかえす。
	if (num == 1) {
		//cout << "dp[" << num << "]: " << 1 << endl;
		return dp[1] = 1;
	}
	if (dp[num] != -1) {
		return dp[num];
	}
	for (int i = 1; i <= N; i++) {	//from i to num
		if (i + f(i) == num || i - f(i) == num) {
			//cout << "from i:" << i<<" to "<<num<<endl;
			long long tmp = dp[num];
			if (dp[num] == -1) tmp = (1<<30);
			if(dfs(i) != -1) dp[num] = min(tmp, dp[num] = dfs(i) + 1);
			//cout << "dp[" << num << "]: " << dp[num] << endl;
		}
	}
	return dp[num];
}


int main() {
	cin >> N;

	for (int i = 1; i <= N; i++) dp[i] = -1;
	dfs(N);
	cout << dp[N] << endl;
	/*
	for (int i = 0; i < 64; i++) {
		cout <<"i:"<<i<<" : "<< (1 << i) << endl;
	}
	*/

	return 0;
}
0