結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  Yut176 | 
| 提出日時 | 2017-07-24 13:45:32 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 870 bytes | 
| コンパイル時間 | 919 ms | 
| コンパイル使用メモリ | 101,368 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:47:10 | 
| 合計ジャッジ時間 | 1,918 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<random>
#include<stack>
#include<queue>
#include<cassert>
using namespace std;
int inf = 1000000000;
int bitcount(int n){
  int cnt = 0;
  while( n != 0 ){
    if( n % 2 == 1 ) cnt++;
    n /= 2;
  }
  return cnt;
}
int main(void) {
  int n;
  cin >> n;
  vector<int> v(n+1, -1);
  queue<int> q;
  q.push(1);
  v[1] = 1;
  while( !q.empty() ){
    int now = q.front();
    q.pop();
    int cnt = bitcount(now);
    if( now + cnt <= n && v[now + cnt] == -1 ){
      v[now + cnt] = v[now] + 1;
      q.push( now + cnt );
    }
    if( now - cnt > 0 && v[now - cnt] == -1 ){
      v[now - cnt] = v[now] + 1;
      q.push( now - cnt );
    }
  }
  cout << v[n] << endl;
  return 0;
}
            
            
            
        