結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  sqrt_3 | 
| 提出日時 | 2017-03-03 00:02:13 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3 ms / 5,000 ms | 
| コード長 | 625 bytes | 
| コンパイル時間 | 650 ms | 
| コンパイル使用メモリ | 78,540 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:35:05 | 
| 合計ジャッジ時間 | 1,586 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<iomanip>
#include<queue>
using namespace std;
int bitcount(int n){
    if(n==0)return 0;
    else return bitcount(n/2)+n%2;
}
//typedef __int64 LL;
typedef pair<int,int>P;
int a[10010];
int main(){
    int n; cin>>n; fill(a,a+n+1,-1);
    queue<int> que;
    que.push(1),a[1]=1;
    while(que.size()>=1){
          int p=que.front(); que.pop();
          int x=bitcount(p);
          if(p+x<=n && a[p+x]==-1)que.push(p+x),a[p+x]=a[p]+1;
          if(p-x>=1 && a[p-x]==-1)que.push(p-x),a[p-x]=a[p]+1;
    } 
    cout<<a[n]<<endl;
    return 0;
}
            
            
            
        