結果

問題 No.3 ビットすごろく
ユーザー Unbakedbread
提出日時 2025-05-29 00:14:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 465 bytes
コンパイル時間 2,278 ms
コンパイル使用メモリ 198,112 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-05-29 00:14:42
合計ジャッジ時間 3,491 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
	auto popcount=[](int n){int res=0;for(int i=0;i<30;i++) if((n>>i)&1) res++;return res;};
	int n;cin>>n;
	vector<int> ans(n+1,-1);
	queue<int> que;
	ans[1]=1; que.push(1);
	while(!que.empty()){
		int v=que.front(); que.pop();
		int t=popcount(v);
		for(auto d:{-t,t}){
			int nv=v+d;
			if(!(1<=nv&&nv<=n&&ans[nv]==-1)) continue;
			que.push(nv);
			ans[nv]=ans[v]+1;
		}
	}cout<<ans[n]<<"\n";
	return 0;
}
0