結果

問題 No.3 ビットすごろく
ユーザー T.StreamT.Stream
提出日時 2020-04-14 11:17:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 5,000 ms
コード長 626 bytes
コンパイル時間 1,953 ms
コンパイル使用メモリ 200,140 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 01:40:54
合計ジャッジ時間 5,876 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 11 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 50 ms
4,376 KB
testcase_06 AC 13 ms
4,376 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 42 ms
4,376 KB
testcase_09 AC 110 ms
4,376 KB
testcase_10 AC 155 ms
4,376 KB
testcase_11 AC 92 ms
4,380 KB
testcase_12 AC 47 ms
4,376 KB
testcase_13 AC 9 ms
4,380 KB
testcase_14 AC 119 ms
4,376 KB
testcase_15 AC 186 ms
4,380 KB
testcase_16 AC 194 ms
4,376 KB
testcase_17 AC 219 ms
4,376 KB
testcase_18 AC 8 ms
4,376 KB
testcase_19 AC 230 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 114 ms
4,376 KB
testcase_23 AC 184 ms
4,380 KB
testcase_24 AC 231 ms
4,376 KB
testcase_25 AC 225 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 12 ms
4,376 KB
testcase_28 AC 185 ms
4,376 KB
testcase_29 AC 95 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 81 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
	long n,i,j,c,inf=100000;
	cin >> n;
	vector<long> a(n,0),ans(n+1,inf);
	for(i=1; i<n; i++){
		c=i; j=0;
		while(c>=(1<<j)){
			if(c&(1<<j)){
				a[i]++;
			}
			j++;
		}
	}
	ans[n]=1;
	
	for(i=0; i<n; i++){
		for(j=n-1; j>=1; j--){
			if(j+a[j]<=n && ans[j+a[j]]!=inf){
				ans[j]=min(ans[j],ans[j+a[j]]+1);
			}
			if(j-a[j]>=1 && ans[j-a[j]]!=inf){
				ans[j]=min(ans[j],ans[j-a[j]]+1);
			}
		}
	}
	/*
	for(i=1; i<=n; i++){
		cout << ans[i] << ' ';
	}
	cout << endl;
	*/
	if(ans[1]==inf){
		cout << -1 << endl;
	}else{
		cout << ans[1] << endl;
	}
	
	return 0;
}
0