#include <unistd.h> #include <string> #include <vector> #include <map> #include <deque> #include <iostream> #include <algorithm> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; using VINT = vector<int>; using VLL = vector<LL>; using VULL = vector<ULL>; int countBits(int i) { int ret = 0; while (i) { if (i % 2) { ret++; } i = i/2; } return ret; } int main() { std::ios_base::sync_with_stdio(false); int n; std::cin >> n; VINT value; VINT accumulatedValue; value.reserve(1000000); accumulatedValue.reserve(1000000); int acc = 0; int i = 0; value.push_back(0); accumulatedValue.push_back(0); while(acc < n) { i++; int bits = countBits(i); value.push_back(bits); acc = accumulatedValue[i-1] + bits; accumulatedValue.push_back(acc); } VINT ret; for (auto i = accumulatedValue.size() - 1; i > 0; i--) { if (accumulatedValue[i] - value[i] >= n) { continue; } n -= value[i]; ret.push_back(i); } std::cout << ret.size() << std::endl; for (auto& a : ret) { std::cout << a << " "; } std::cout << 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 59 60 61 62 63 64 65 66 | #include <unistd.h> #include <string> #include <vector> #include <map> #include <deque> #include <iostream> #include <algorithm> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; using VINT = vector<int>; using VLL = vector<LL>; using VULL = vector<ULL>; int countBits(int i) { int ret = 0; while (i) { if (i % 2) { ret++; } i = i/2; } return ret; } int main() { std::ios_base::sync_with_stdio(false); int n; std::cin >> n; VINT value; VINT accumulatedValue; value.reserve(1000000); accumulatedValue.reserve(1000000); int acc = 0; int i = 0; value.push_back(0); accumulatedValue.push_back(0); while(acc < n) { i++; int bits = countBits(i); value.push_back(bits); acc = accumulatedValue[i-1] + bits; accumulatedValue.push_back(acc); } VINT ret; for (auto i = accumulatedValue.size() - 1; i > 0; i--) { if (accumulatedValue[i] - value[i] >= n) { continue; } n -= value[i]; ret.push_back(i); } std::cout << ret.size() << std::endl; for (auto& a : ret) { std::cout << a << " "; } std::cout << std::endl; return 0; } |