#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Util { std::vector Split(const std::string& str, const char delim); std::string Itos(const int value); std::string Ftos(const double value); int Stoi(const std::string& buf); double Stof(const std::string& buf); std::vector ConvertInt(const std::vector& buffers); std::vector ConvertDouble(const std::vector& buffers); }// namespace Util namespace Node { enum NodeIndex { Front = 0, Back, Size, }; } struct Element { std::array node = { {-1, -1} }; }; void Search(const std::vector& mass, const std::vector& root, std::vector& memo) { std::list box; box.emplace_back(0); for(int step = 1; step <= static_cast(mass.size()); ++step) { std::list process = std::move(box); for(auto& target : process) { if(memo[target] == -1 || step < memo[target]) { memo[target] = step; } for(int cur = 0; cur < Node::Size; ++cur) { if(root[target].node[cur] != -1) { box.emplace_back(root[target].node[cur]); } } } } } int main() { // 入出力の速度向上 std::cin.tie(0); std::ios::sync_with_stdio(false); std::string buf; std::vector buffers; std::vector mass; std::vector root; std::vector memo; std::getline(std::cin, buf); mass.resize(Util::Stoi(buf)); for(int cur = 0; cur < static_cast(mass.size()); ++cur) { mass[cur] = std::bitset<32>(cur + 1).count(); } root.resize(mass.size()); for(int cur = 0; cur < static_cast(root.size()); ++cur) { if(cur + mass[cur] < static_cast(mass.size())) { root[cur].node[Node::Front] = cur + mass[cur]; } if(0 <= cur - mass[cur]) { root[cur].node[Node::Back] = cur - mass[cur]; } } memo.resize(mass.size(), -1); Search(mass, root, memo); printf("%d\n", memo.back()); // printf("mass-----\n"); // for(int cur = 0; cur < static_cast(mass.size()); ++cur) // { // printf("[%5d]%5d\n", cur, mass[cur]); // } // printf("root-----\n"); // for(int cur = 0; cur < static_cast(root.size()); ++cur) // { // printf("[%5d]%5d+ %5d-\n", cur, root[cur].node[Node::Front], root[cur].node[Node::Back]); // } // printf("memo-----\n"); // for(int cur = 0; cur < static_cast(memo.size()); ++cur) // { // printf("[%5d]%5d\n", cur, memo[cur]); // } return 0; } namespace Util { std::vector Split(const std::string& str, const char delim) { std::istringstream iss(str); std::vector ret; std::string tmp; while(std::getline(iss, tmp, delim)) { ret.push_back(tmp); } return ret; } std::string Itos(const int value) { std::ostringstream ostream; ostream << value; return ostream.str(); } std::string Itof(const double value) { std::ostringstream ostream; ostream << value; return ostream.str(); } int Stoi(const std::string& buf) { return std::atoi(buf.c_str()); } double Stof(const std::string& buf) { return std::atof(buf.c_str()); } std::vector ConvertInt(const std::vector& buffers) { std::vector ret(buffers.size()); for(int cur = 0; cur < static_cast(ret.size()); ++cur) { ret[cur] = Stoi(buffers[cur]); } return ret; } std::vector ConvertDouble(const std::vector& buffers) { std::vector ret(buffers.size()); for(int cur = 0; cur < static_cast(ret.size()); ++cur) { ret[cur] = Stof(buffers[cur]); } return ret; } }// namespace Util