結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2017-11-23 00:57:25 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 944 bytes | 
| コンパイル時間 | 1,860 ms | 
| コンパイル使用メモリ | 163,440 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:50:28 | 
| 合計ジャッジ時間 | 2,365 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define print(x) cout<<x<<endl;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a) for(int i=0;i<a;i++)
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<int, PI> V;
typedef vector<int> VE;
const ll mod = 1000000007;
int d[10001];
int binally_count(int n){
  string str(std::bitset<32>(n).to_string<char, std::char_traits<char>, std::allocator<char> >());
  int cnt=0;
  REP(i,32){
    if(str[i]=='1')cnt++;
  }
  return cnt;
}
int main(){
  int n;
  cin>>n;
  queue<int> que;
  REP(i,n+1)d[i]=10001;
  que.push(1);
  d[1]=1;
  while(que.size()){
    int p=que.front();que.pop();
    if(p==n)break;
    int pp=p+binally_count(p);
    if(0<pp&&pp<=n&&d[pp]==10001){
      que.push(pp);
      d[pp]=d[p]+1;
    }
    int pm=p-binally_count(p);
    if(0<pm&&pm<=n&&d[pm]==10001){
      que.push(pm);
      d[pm]=d[p]+1;
    }
  }
  int ans=d[n]!=10001?d[n]:-1;
  print(ans);
}
            
            
            
        