結果

問題 No.3 ビットすごろく
ユーザー tai96tai96
提出日時 2015-02-20 23:33:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 879 ms / 5,000 ms
コード長 635 bytes
コンパイル時間 453 ms
コンパイル使用メモリ 59,392 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 23:03:49
合計ジャッジ時間 14,995 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 186 ms
4,380 KB
testcase_04 AC 51 ms
4,380 KB
testcase_05 AC 441 ms
4,376 KB
testcase_06 AC 200 ms
4,376 KB
testcase_07 AC 110 ms
4,380 KB
testcase_08 AC 341 ms
4,376 KB
testcase_09 AC 573 ms
4,380 KB
testcase_10 AC 715 ms
4,380 KB
testcase_11 AC 530 ms
4,376 KB
testcase_12 AC 423 ms
4,376 KB
testcase_13 AC 149 ms
4,380 KB
testcase_14 AC 678 ms
4,376 KB
testcase_15 AC 878 ms
4,376 KB
testcase_16 AC 810 ms
4,376 KB
testcase_17 AC 852 ms
4,376 KB
testcase_18 AC 125 ms
4,380 KB
testcase_19 AC 870 ms
4,380 KB
testcase_20 AC 34 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 674 ms
4,376 KB
testcase_23 AC 867 ms
4,376 KB
testcase_24 AC 879 ms
4,376 KB
testcase_25 AC 867 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 163 ms
4,376 KB
testcase_28 AC 766 ms
4,376 KB
testcase_29 AC 538 ms
4,380 KB
testcase_30 AC 8 ms
4,376 KB
testcase_31 AC 11 ms
4,380 KB
testcase_32 AC 487 ms
4,376 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::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