結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  nayuta | 
| 提出日時 | 2019-10-07 14:17:38 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 798 bytes | 
| コンパイル時間 | 2,220 ms | 
| コンパイル使用メモリ | 177,412 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:31:40 | 
| 合計ジャッジ時間 | 3,078 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
int N;
int dist[10101];
vector<vector<int>> G;
int func(int x) {
  int ret = 0;
  while (x != 0) {
    ret += x % 2;
    x /= 2;
  }
  return ret;
}
int main() {
  cin >> N;
  G.assign(N + 1, vector<int>());
  for (int i = 1; i <= N; i++) {
    int to = func(i);
    if (0 < i - to) G[i].push_back(i - to);
    if (i + to <= N) G[i].push_back(i + to);
  }
  for (int i = 2; i <= N; i++) dist[i] = 1e9;
  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    int q = que.front();
    que.pop();
    for (auto to : G[q]) {
      if (dist[q] + 1 < dist[to]) {
        dist[to] = dist[q] + 1;
        que.push(to);
      }
    }
  }
  if (1e9 <= dist[N]) {
    cout << -1 << endl;
  } else {
    cout << dist[N] + 1 << endl;
  }
  return 0;
}
            
            
            
        