#include <iostream> #include <iomanip> #include <bitset> const uint64_t m1 = 0x5555555555555555; //binary: 0101... const uint64_t m2 = 0x3333333333333333; //binary: 00110011.. const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ... const uint64_t m8 = 0x00ff00ff00ff00ff; //binary: 8 zeros, 8 ones ... const uint64_t m16 = 0x0000ffff0000ffff; //binary: 16 zeros, 16 ones ... const uint64_t m32 = 0x00000000ffffffff; //binary: 32 zeros, 32 ones const uint64_t h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3... //This is a naive implementation, shown for comparison, //and to help in understanding the better functions. //This algorithm uses 24 arithmetic operations (shift, add, and). uint64_t popcount64(uint64_t x) { x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits return (x * h01) >> 56; //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); uint64_t n; std::cin >> n; uint64_t count = 1; uint64_t total = 0; while (total < n) { total += popcount64(count); // std::cout << std::setfill('0') << std::setw(5) << count; //std::cout << " " << std::bitset<8>{count} << " " << total << std::endl; ++count; } --count; uint64_t cut = std::max(total - n, 0LU); std::cout << (cut > 0 ? count - 1 : count) << std::endl << count; for (auto i = count - 1; i > 0; --i) { if (cut > 0 && popcount64(i) == cut) { cut = 0; continue; } std::cout << " " << i; } std::cout << std::endl; //std::cout << n << " " << count << std::endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream> #include <iomanip> #include <bitset> const uint64_t m1 = 0x5555555555555555; //binary: 0101... const uint64_t m2 = 0x3333333333333333; //binary: 00110011.. const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ... const uint64_t m8 = 0x00ff00ff00ff00ff; //binary: 8 zeros, 8 ones ... const uint64_t m16 = 0x0000ffff0000ffff; //binary: 16 zeros, 16 ones ... const uint64_t m32 = 0x00000000ffffffff; //binary: 32 zeros, 32 ones const uint64_t h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3... //This is a naive implementation, shown for comparison, //and to help in understanding the better functions. //This algorithm uses 24 arithmetic operations (shift, add, and). uint64_t popcount64(uint64_t x) { x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits return (x * h01) >> 56; //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); uint64_t n; std::cin >> n; uint64_t count = 1; uint64_t total = 0; while (total < n) { total += popcount64(count); // std::cout << std::setfill('0') << std::setw(5) << count; //std::cout << " " << std::bitset<8>{count} << " " << total << std::endl; ++count; } --count; uint64_t cut = std::max(total - n, 0LU); std::cout << (cut > 0 ? count - 1 : count) << std::endl << count; for (auto i = count - 1; i > 0; --i) { if (cut > 0 && popcount64(i) == cut) { cut = 0; continue; } std::cout << " " << i; } std::cout << std::endl; //std::cout << n << " " << count << std::endl; return 0; } |