結果

問題 No.3 ビットすごろく
ユーザー tai96tai96
提出日時 2015-02-20 23:46:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 944 ms / 5,000 ms
コード長 671 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 59,604 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-13 23:05:43
合計ジャッジ時間 15,952 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 196 ms
4,376 KB
testcase_04 AC 54 ms
4,376 KB
testcase_05 AC 468 ms
4,376 KB
testcase_06 AC 219 ms
4,380 KB
testcase_07 AC 121 ms
4,376 KB
testcase_08 AC 364 ms
4,380 KB
testcase_09 AC 621 ms
4,376 KB
testcase_10 AC 756 ms
4,376 KB
testcase_11 AC 565 ms
4,376 KB
testcase_12 AC 454 ms
4,380 KB
testcase_13 AC 162 ms
4,376 KB
testcase_14 AC 729 ms
4,380 KB
testcase_15 AC 930 ms
4,376 KB
testcase_16 AC 856 ms
4,376 KB
testcase_17 AC 913 ms
4,376 KB
testcase_18 AC 138 ms
4,376 KB
testcase_19 AC 941 ms
4,376 KB
testcase_20 AC 38 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 736 ms
4,380 KB
testcase_23 AC 942 ms
4,376 KB
testcase_24 AC 944 ms
4,376 KB
testcase_25 AC 928 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 178 ms
4,376 KB
testcase_28 AC 836 ms
4,384 KB
testcase_29 AC 574 ms
4,380 KB
testcase_30 AC 9 ms
4,384 KB
testcase_31 AC 12 ms
4,384 KB
testcase_32 AC 524 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <algorithm>

int n;
int dp[10001];

const int inf = 100000000;

int countBit(int x){
	int ret = 0;
	while (x > 0){
		if (x & 1)ret++;
		x >>= 1;
	}
	return ret;
}

int main(){
	std::cerr << "Hello" << std::endl;
	std::cin >> n;

	for (int i = 0; i < 10001; i++)dp[i] = inf;
	dp[1] = 1;

	for (int i = 0; i < 10001; i++){
		for (int j = 1; j <= n; j++){
			if (dp[j] == inf)continue;

			int cnt = countBit(j);
			if (j + cnt <= n)dp[j + cnt] = std::min(dp[j + cnt], dp[j] + 1);
			if (j - cnt >= 1)dp[j - cnt] = std::min(dp[j - cnt], dp[j] + 1);
		}
	}

	std::cout << (dp[n] == inf ? -1 : dp[n]) << std::endl;

	return 0;
}
0