#include <bitset> #include <iostream> #include <vector> int main() { int need; std::cin >> need; std::bitset< 8 * sizeof(int) > bs; std::vector< int > series = { 1 }; int got = 1; // grow while(got < need) { series.push_back(series.back() + 1); bs = series.back(); got += bs.count(); } int overflow = got - need; // shrink int pos = series.size() - 1; while(0 < overflow) { bs = series[pos]; int cnt = bs.count(); if (cnt <= overflow) { overflow -= cnt; series.erase(series.begin() + pos); } else { --pos; } } size_t sz = series.size(); std::cout << sz << "\n"; for (int i = sz - 1; 0 < i; --i) { std::cout << series[i] << " "; } std::cout << series[0] << "\n"; 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 | #include <bitset> #include <iostream> #include <vector> int main() { int need; std::cin >> need; std::bitset< 8 * sizeof(int) > bs; std::vector< int > series = { 1 }; int got = 1; // grow while(got < need) { series.push_back(series.back() + 1); bs = series.back(); got += bs.count(); } int overflow = got - need; // shrink int pos = series.size() - 1; while(0 < overflow) { bs = series[pos]; int cnt = bs.count(); if (cnt <= overflow) { overflow -= cnt; series.erase(series.begin() + pos); } else { --pos; } } size_t sz = series.size(); std::cout << sz << "\n"; for (int i = sz - 1; 0 < i; --i) { std::cout << series[i] << " "; } std::cout << series[0] << "\n"; return 0; } |