結果

問題 No.3 ビットすごろく
ユーザー Shawn stayCShawn stayC
提出日時 2023-01-16 22:11:17
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 455 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 205,908 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-31 12:30:18
合計ジャッジ時間 3,702 ms
ジャッジサーバーID
(参考情報)
judge3 / 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