#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll max_log = 30;
ll cnt_bits(ll x) {
ll res = 0;
for(ll i = 0; i < max_log; i++)
if(x & (1LL<<i))
res += 1;
return res;
}
ll sum_bits_in_leq(ll x) {
ll res = 0, above = 0;
for(ll i = max_log; i >= 0; i--)
if(x & (1<<i)) {
res += above * (1LL<<i);
if(i > 0) {
res += i * (1LL<<(i - 1));
}
above += 1;
}
res += above;
return res;
}
void test_proc() {
for(ll n = 0; n < 10000; n++) {
ll fast_res = sum_bits_in_leq(n);
ll slow_res = 0;
for(ll i = 0; i <= n; i++)
slow_res += cnt_bits(i);
if(slow_res != fast_res) {
cout << "HERE " << n << "!?!?!?" << endl;
}
}
}
int main() {
std::ios::sync_with_stdio(false);
//test_proc();
ll n;
cin >> n;
vector<ll> A;
while(n != 0) {
ll a = 0, b = 1.1e6, c;
while(a < b) {
c = (a + b) / 2;
if(sum_bits_in_leq(c) < n)
a = c + 1;
else
b = c;
}
A.push_back(a);
n -= cnt_bits(a);
}
cout << A.size() << "\n";
for(auto& x : A)
cout << x << " ";
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 | #include <bits/stdc++.h> #define ll long long using namespace std; const ll max_log = 30; ll cnt_bits(ll x) { ll res = 0; for(ll i = 0; i < max_log; i++) if(x & (1LL<<i)) res += 1; return res; } ll sum_bits_in_leq(ll x) { ll res = 0, above = 0; for(ll i = max_log; i >= 0; i--) if(x & (1<<i)) { res += above * (1LL<<i); if(i > 0) { res += i * (1LL<<(i - 1)); } above += 1; } res += above; return res; } void test_proc() { for(ll n = 0; n < 10000; n++) { ll fast_res = sum_bits_in_leq(n); ll slow_res = 0; for(ll i = 0; i <= n; i++) slow_res += cnt_bits(i); if(slow_res != fast_res) { cout << "HERE " << n << "!?!?!?" << endl; } } } int main() { std::ios::sync_with_stdio(false); //test_proc(); ll n; cin >> n; vector<ll> A; while(n != 0) { ll a = 0, b = 1.1e6, c; while(a < b) { c = (a + b) / 2; if(sum_bits_in_leq(c) < n) a = c + 1; else b = c; } A.push_back(a); n -= cnt_bits(a); } cout << A.size() << "\n"; for(auto& x : A) cout << x << " "; return 0; } |
English