結果

問題 No.3 ビットすごろく
コンテスト
ユーザー 777yuuki12323
提出日時 2017-06-19 18:26:25
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,296 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 548 ms
コンパイル使用メモリ 103,636 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-03-22 05:26:18
合計ジャッジ時間 1,556 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:79:15: warning: 'ans' may be used uninitialized [-Wmaybe-uninitialized]
   79 |         cout<<ans<<endl;
      |               ^~~
main.cpp:43:13: note: 'ans' was declared here
   43 |         int ans;
      |             ^~~

ソースコード

diff #
raw source code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <bitset>
#include <vector>
#include <queue>


typedef long long ll;
#define fi first
#define se second
const ll mod = 1000000007;
//              123456789

using namespace std;

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////


queue<int> Q;

int cost[11234];
int N;

int main(){
	
	fill(cost, cost+11234, mod);
	cost[1] = 1;
	
	Q.push( 1 );
	
	cin>>N;
	
	int ans;
	int num;
	int next;
	int flag = 0;
	int j[2] = { 1, -1 };
	int i;
	
	if( N > 1 ){
		while( !Q.empty() ){
			num = Q.front();
			for( i = 0; i < 2; i++ ){
				next = num + (j[i]*bitset<32>(num).count());
				//cout<<num<<" "<<bitset<32>(num).count()<<endl;
				if( 0 < next && next <= N ){
					if( cost[next] == (int)mod ){
						cost[next] = cost[num]+1;
						Q.push( next );
						//cout<<next<<endl;
					}
					if( next == N ) flag = 1;
				}
			}
			//cout<<num<<" "<<cost[num]<<endl;
			if( flag ){
				ans = cost[N];
				break;
			}
			
			Q.pop();
		}
	}
	else{
		ans = 1;
	}
	if( cost[N] == (int)mod ) ans = -1;
	
	cout<<ans<<endl;
	
	
	return 0;
}
0