結果

問題 No.3 ビットすごろく
ユーザー chacoder1
提出日時 2021-06-27 17:28:31
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 799 bytes
コンパイル時間 2,666 ms
コンパイル使用メモリ 198,800 KB
最終ジャッジ日時 2025-01-22 14:41:47
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
static const int INF=100100100;

int f(int x){
  int ans=0;
  while(x>0){
    if(x%2==1) ans++;
    x/=2;
  }
  return ans; 
}

signed main(){
  vector<int>cb(10100,INF);  
  int n;
  cin>>n;
  if(n==1){
    cout<<1<<endl;
    return 0;
  }
  cb[1]=1;
  queue<int>que;
  que.push(1);
  while(que.size()>0){
    int temp=que.front();
    que.pop();
    if(temp+f(temp)<=n && cb[temp+f(temp)]==INF){
      que.push(temp+f(temp));
      cb[temp+f(temp)]=cb[temp]+1;
    }
    if(cb[temp-f(temp)]==INF){
      que.push(temp-f(temp));
      cb[temp-f(temp)]=cb[temp]+1;
    }
  }
  
  if(cb[n]==INF){
    cout<<-1<<endl;
  }
  else{
    cout<<cb[n]<<endl;
  }
  return 0;
}


0