#include <iostream>
#include <vector>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define LOG(s) cerr << s << '\n'
const bool debug = true;
#else
#define LOG(s)
const bool debug = false;
#endif
int moc(int n) {
int res = 0;
while(n > 0) {
res += n % 2;
n /= 2;
}
return res;
}
int sumaMocy(int n) {
n++;
int p = 1;
int res = 0;
while(p <= n - 1) {
int intervals = n / (2 * p);
res += (intervals * p) + max((n - (intervals * 2 * p) - p), 0);
p *= 2;
}
return res;
}
int next(int n, int b) {
b--;
while(sumaMocy(b) >= n) {
b--;
}
return b + 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n;
cin >> n;
// prev można w czasie O(logn), ale mi wystarczy oszacowanie przez n.
int t = n + 1;
vector<int> v;
while(n > 0) {
if(debug) {
LOG("N:");
LOG(n);
LOG(moc(n));
LOG(sumaMocy(n));
LOG(next(n, t));
}
t = next(n, t);
n -= moc(t);
v.push_back(t);
}
cout << v.size() << '\n';
for(auto e : v) {
cout << e << ' ';
}
cout << '\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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include <iostream> #include <vector> using namespace std; // #define DEBUG #ifdef DEBUG #define LOG(s) cerr << s << '\n' const bool debug = true; #else #define LOG(s) const bool debug = false; #endif int moc(int n) { int res = 0; while(n > 0) { res += n % 2; n /= 2; } return res; } int sumaMocy(int n) { n++; int p = 1; int res = 0; while(p <= n - 1) { int intervals = n / (2 * p); res += (intervals * p) + max((n - (intervals * 2 * p) - p), 0); p *= 2; } return res; } int next(int n, int b) { b--; while(sumaMocy(b) >= n) { b--; } return b + 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n; cin >> n; // prev można w czasie O(logn), ale mi wystarczy oszacowanie przez n. int t = n + 1; vector<int> v; while(n > 0) { if(debug) { LOG("N:"); LOG(n); LOG(moc(n)); LOG(sumaMocy(n)); LOG(next(n, t)); } t = next(n, t); n -= moc(t); v.push_back(t); } cout << v.size() << '\n'; for(auto e : v) { cout << e << ' '; } cout << '\n'; return 0; } |
English