結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  ei1333333 | 
| 提出日時 | 2014-10-29 22:17:33 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 705 bytes | 
| コンパイル時間 | 1,316 ms | 
| コンパイル使用メモリ | 164,596 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 07:05:14 | 
| 合計ジャッジ時間 | 2,293 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long int64;
const int INF = 1 << 30;
int main(){
  int N;
  cin >> N;
  queue< int > que; que.push(1);
  vector< int > min_cost(N + 1, INF);
  min_cost[1] = 1;
  while(!que.empty()){
    int p = que.front(); que.pop();
    int next = __builtin_popcount(p);
    if(p + next <= N && min_cost[p + next] > min_cost[p] + 1){
      que.push(p + next);
      min_cost[p + next] = min_cost[p] + 1;
    }
    if(p - next >= 1 && min_cost[p - next] > min_cost[p] + 1){
      que.push(p - next);
      min_cost[p - next] = min_cost[p] + 1;
    }
  }
  if(min_cost[N] == INF) cout << -1 << endl;
  else cout << min_cost[N] << endl;
}
            
            
            
        