// // main.cpp // Q11 // // Created by AkihiroKOBAYASHI on 7/5/15. // Copyright (c) 2015 Akhr5884. All rights reserved. // #include using namespace std; string to_binString(unsigned int val){ if( !val ) { return string("0"); } string str; while( val != 0 ) { if( (val & 1) == 0 ) // val は偶数か? str.insert(str.begin(), '0'); // 偶数の場合 else str.insert(str.begin(), '1'); // 奇数の場合 val >>= 1; } return str; } int canGoValue(string binString) { int i; int value = 0; for(i = 0; i < binString.size(); i++) { if(binString[i] == '1') { value++; } } return value; } int main(int argc, const char * argv[]) { using namespace std; int goalvalue; int *goalvalueArray; int i = 0; int add; int count; cin >> goalvalue; goalvalueArray = (int*)malloc(sizeof(goalvalueArray) * (goalvalue + 1)); while(i <= goalvalue) { goalvalueArray[i] = 0; i++; } count = 1; i = 1; goalvalueArray[1] = 1; while (1) { if(i == goalvalue) { cout << count << "\n"; return 0; } add = canGoValue(to_binString(i)); count++; if(i + add <= goalvalue){ i += add; goalvalueArray[i] = 1; } else if(i + add > goalvalue) { i -= add; if(goalvalueArray[i] == 1) { count = -1; cout << count << "\n"; return 0; } else { goalvalueArray[i] = 1; } } } return 0; }