#include <bits/stdc++.h> using namespace std; constexpr int LIM = 1e6; int s[LIM + 10]; int bs(int l, int r, int x) { while(l < r) { int mid = (l + r) / 2; if(s[mid] < x) { l = mid + 1; } else { r = mid; } } return l; } int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { s[i] = s[i - 1] + __popcount(i); } vector<int>res; while(n > 0) { int x = bs(1, n, n); n -= __popcount(x); res.push_back(x); } printf("%ld\n", res.size()); for(auto x : res) { printf("%d ", x); } }
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 | #include <bits/stdc++.h> using namespace std; constexpr int LIM = 1e6; int s[LIM + 10]; int bs(int l, int r, int x) { while(l < r) { int mid = (l + r) / 2; if(s[mid] < x) { l = mid + 1; } else { r = mid; } } return l; } int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { s[i] = s[i - 1] + __popcount(i); } vector<int>res; while(n > 0) { int x = bs(1, n, n); n -= __popcount(x); res.push_back(x); } printf("%ld\n", res.size()); for(auto x : res) { printf("%d ", x); } } |