結果

問題 No.3 ビットすごろく
ユーザー TaichiK12TaichiK12
提出日時 2019-03-01 13:12:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 811 bytes
コンパイル時間 463 ms
コンパイル使用メモリ 66,416 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-05 16:32:13
合計ジャッジ時間 1,737 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	long long n,a,o=0;
	cin>>n;
	long long dp[n],b[n],c[n];
	for(int i=0;i<n;i++){
		dp[i]=10000;
		b[i]=0;
		c[i]=0;
		a=i+1;
		while(a>0){
			b[i]+=a%2;
			a/=2;
		}
	}
	dp[0]=1;
	for(int i=0;i<n-1;i++){
		if(dp[i]!=10000){
			if(i+b[i]<n){
				dp[i+b[i]]=dp[i]+1;
				c[i+b[i]]=1;
			}
		}
	}
	if(dp[n-1]!=10000)cout<<dp[n-1]<<endl;
	else{
		while(o!=0){
			o=0;
			for(int i=0;i<n;i++){
				if(c[i]==1){
					c[i]=0;
					if(i+b[i]<n){
						if(dp[i+b[i]]>dp[i]+1){
							dp[i+b[i]]=dp[i]+1;
							c[i+b[i]]=1;
							o++;
						}
					}
					if(i+b[i]>0){
						if(dp[i-b[i]]>dp[i]+1){
							dp[i-b[i]]=dp[i]+1;
							c[i-b[i]]=1;
							o++;
						}
					}
				}
			}
		}
		if(dp[n-1]==10000)dp[n-1]=-1;
		cout<<dp[n-1]<<endl;
	}
}
0