結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  _oba23_ | 
| 提出日時 | 2018-08-18 20:44:08 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 708 bytes | 
| コンパイル時間 | 533 ms | 
| コンパイル使用メモリ | 60,908 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:05:36 | 
| 合計ジャッジ時間 | 1,493 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <iostream>
#include <vector>
using namespace std;
vector<int> popcnt_cache;
vector<unsigned int> cnt_cache;
int n;
int popcnt(int n){
    if(popcnt_cache[n]){
        return popcnt_cache[n];
    }
    int x=0;
    while(n){
        x+=n & 1;
        n >>= 1;
    }
    popcnt_cache[n]=x;
    return x;
}
void calc(int i, unsigned int cnt){
    if(cnt_cache[i]<=cnt) return;
    cnt_cache[i]=cnt;
    int p=popcnt(i);
    if(i+p<=n){
        calc(i+p, cnt+1);
    }
    if(i-p>0){
        calc(i-p, cnt+1);
    }
}
int main(){
    cin >> n;
    popcnt_cache=vector<int>(n+1, 0);
    cnt_cache=vector<unsigned int>(n+1, -1);
    calc(1, 1);
    cout << (int)cnt_cache[n] << endl;
    return 0;
}
            
            
            
        