結果

問題 No.3 ビットすごろく
ユーザー Shawn stayC
提出日時 2023-01-16 22:11:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 455 bytes
コンパイル時間 1,891 ms
コンパイル使用メモリ 198,440 KB
最終ジャッジ日時 2025-02-10 03:59:12
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
	int n; cin>>n;
	queue<int> q;
	q.push(1);
	vector<int> dis(n+1,n+1);
	dis[1]=1;  
	while(!q.empty()){
		int now=q.front(); q.pop();
		int bit=__builtin_popcount(now);
		if(now-bit>0 && dis[now-bit]==n+1){
			dis[now-bit]=dis[now]+1;
			q.emplace(now-bit);
		}
		if(now+bit<=n && dis[now+bit]==n+1){
			dis[now+bit]=dis[now]+1;
			q.emplace(now+bit);
		}
	}
	cout<<(dis[n]<n+1?dis[n]:-1)<<endl;
}
0