結果

問題 No.3 ビットすごろく
ユーザー T.Stream
提出日時 2020-04-14 11:17:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 252 ms / 5,000 ms
コード長 626 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 194,084 KB
最終ジャッジ日時 2025-01-09 18:31:55
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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