結果

問題 No.3 ビットすごろく
ユーザー silopysilopy
提出日時 2019-06-28 23:56:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 710 bytes
コンパイル時間 619 ms
コンパイル使用メモリ 66,412 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2023-09-14 22:48:03
合計ジャッジ時間 7,692 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;
using lint=int64_t;

int N;
int ans;

int bit[10010];
int bitcnt(int n)
{
	if(bit[n]!=-1)
		return bit[n];
	
	int ret=0;
	while(1)
	{
		if(n==0)break;
		ret+=n%2;
		n/=2;
	}
	bit[n]=ret;
	return ret;
}

bool visited[10010];
void dfs(int d,int cur)
{
	if(cur==N)
	{
		ans=min(ans,d);
		return;
	}
	
	visited[cur]=true;
	int cnt=bitcnt(cur);
	if(cur+cnt<=N && !visited[cur+cnt])
		dfs(d+1,cur+cnt);
	if(1<=cur-cnt && !visited[cur-cnt])
		dfs(d+1,cur-cnt);

	visited[cur]=false;
	return;
}

int main()
{
	ans=1e9;
	for(int i=0;i<10010;i++)
		bit[i]=-1;

	cin >> N;
	dfs(1,1);
	if(ans==1e9)
		cout << -1;
	else
		cout << ans;
	cout << endl;
	return 0;
}
0