結果

問題 No.3 ビットすごろく
ユーザー amtgjwamtgjw
提出日時 2018-04-29 13:53:31
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 843 bytes
コンパイル時間 417 ms
コンパイル使用メモリ 61,228 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 08:02:25
合計ジャッジ時間 3,426 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define INF 		2000000007

int bitcount32(int dw32) // 32 bits 値)
{
	// 32 bits 限定アルゴリズムを利用している
	int cnt = 0;

	while(dw32){
		if(dw32 & 1)
			cnt += 1;
		dw32 >>= 1;
	}
	return cnt;
}//bitcount32



int main(){
	int N;
	scanf("%d",&N);

	//int cnt = 1;

	vector<bool> flag(N+1,false);
	vector<int> DP(N+1,INF);

	DP[0] = -1;
	DP[1] = 0;
	for(int i=1;i<N;++i){
		if(DP[i]==INF) continue;
		//cout << "i : " << i ;
		int cnt = bitcount32(i);
		//printf("[%d-%d-%d]\n",i-cnt,i,i+cnt);
		//printf("%d:%d:%d\n",DP[i-cnt],DP[i],DP[i+cnt]);
		DP[i+cnt] = min(DP[i+cnt],DP[i]+1);
		if(DP[i-cnt] > DP[i]+1){
			DP[i-cnt] = DP[i]+1;
			i -= cnt+1;
			//printf("Back : %d[%d]\n",i+1,DP[i]);
		}	
	}
	printf("%d\n",DP[N]!=INF ? DP[N]+1 : -1);
}
0