#include #include #include using namespace std; const int INTMAX = numeric_limits::max(); unsigned long bitcount(int bits){ bits = (bits & 0x55555555) + ((bits >> 1) & 0x55555555); //cout << "[bits]" << bits << endl; bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333); //cout << "[bits]" << bits << endl; bits = (bits & 0x0f0f0f0f) + ((bits >> 4) & 0x0f0f0f0f); //cout << "[bits]" << bits << endl; bits = (bits & 0x00ff00ff) + ((bits >> 8) & 0x00ff00ff); //cout << "[bits]" << bits << endl; bits = (bits & 0x0000ffff) + ((bits >> 16) & 0x0000ffff); //cout << "[bits]" << bits << endl; return bits; } int main() { int N; cin >> N; queue q; q.push(1); vector dist( N + 1, INTMAX ); dist[1] = 1; //cout << "[1]" << dist[1] << endl; while(!q.empty()){ int now = q.front(); int bit = bitcount(now); q.pop(); if(now - bit > 0 && dist[now - bit] == INTMAX){ dist[now - bit] = dist[now] + 1; q.emplace(now - bit); //cout << "[" << dist[now] + 1 << "]" <