// No.3 ビットすごろく // https://yukicoder.me/problems/no/3 // #include #include #include #include #include #include #include using namespace std; struct edge { int to; int cost; }; typedef pair P; const int INF = 999999999; int pop_count(unsigned int n); vector dijkstra(vector& adj, int s); int pop_count(unsigned int n) { int res; __asm__( "popcnt %1, %0" : "=r"(res) : "r"(n) ); return(res); } vector dijkstra(vector>& adj, int s) { vector d(adj.size()+1); priority_queue, greater

> que; fill(d.begin(), d.end(), INF ); d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < adj[v].size(); ++i) { edge e = adj[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } return d; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int N; cin >> N; vector> adj; adj.resize(N+1); for (auto i = 1; i <= N; ++i) { int b = pop_count(i); if (i-b >= 1) adj[i].push_back({i-b, 1}); if (i+b <= N) adj[i].push_back({i+b, 1}); } vector dist = dijkstra(adj, 1); if (dist[N] != INF) cout << dist[N] +1 << endl; else cout << -1 << endl; }