結果

問題 No.3 ビットすごろく
ユーザー mannshi222mannshi222
提出日時 2022-03-27 20:43:12
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 703 bytes
コンパイル時間 1,025 ms
コンパイル使用メモリ 79,168 KB
最終ジャッジ日時 2025-01-28 13:07:59
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <bitset>

using namespace std;

int cnt( int n ) {
	int b = 0;
	while( n != 0 ) {
		if( n & 1 ) b++;
		n = n >> 1;
	}
	return b;
}
int main()
{
	int N;
	cin >> N;	

	vector<int> masu( N+1, -1 );
	queue<int> q;

	masu[1] = 1;
	q.push( 1 );
	while( !q.empty() ) {
		int now = q.front();
		int step = cnt( now );
		if( now - step >= 1 ) {
			if( masu[ now -step ] == -1 ) {
				q.push( now - step );
				masu[ now - step ] = masu[now]+1;
				
			}
		}
		if( now + step <= N ) {
			if( masu[ now + step ] == -1 ) {
				q.push( now + step );
				masu[ now + step ] = masu[now]+1;
			}
		}
		q.pop();
	}


	cout << masu[N] << endl;

	return 0;
}
0