#include #include #include using namespace std; class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x> que; // 点番号、手数 que.push(make_pair(1, 1)); vector memo(n+1, -1); int res = -1; while(!que.empty()) { pair cur = que.front(); que.pop(); int v = cur.first, cnt = cur.second; if(v == n) { res = cnt; break; } if(memo[v] != -1) { continue; } memo[v] = cnt; int x = __builtin_popcount(v); if(v+x <= n) { que.push(make_pair(v+x, cnt+1)); } if(v-x >= 1) { que.push(make_pair(v-x, cnt+1)); } } printf("%d\n", res); return 0; }