結果

問題 No.3 ビットすごろく
ユーザー rhincodon66rhincodon66
提出日時 2018-09-07 22:28:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 561 bytes
コンパイル時間 1,729 ms
コンパイル使用メモリ 174,352 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-29 15:36:09
合計ジャッジ時間 2,876 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<int> a;
vector<bool> b;

int bitsum(int n){
	int ans=0;
	while(n>0){
		if(n&1) ans++;
		n>>=1;
	}
	return ans;
}

int sugoroku(int i,int n){
	if(i<=0 || i>n) return 100000000;
	if(b.at(i)) return 100000000;
	b.at(i)=true;
	if(i==n) return 1;
	return min(sugoroku(i+a.at(i),n)+1,sugoroku(i-a.at(i),n)+1);
}

int main(){
	int n;cin >> n;
	a.resize(n+1);
	b.resize(n+1,false);
	for(int i=1;i<=n;++i){
		a.at(i)=bitsum(i);
	}
	int ans=sugoroku(1,n);
	if(b.at(n)) cout << ans << endl;
	else cout << -1 << endl;
}
0