#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define ar array
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
int onesinbin(int x){
int counter =0;
while(x){
if(x%2==1) counter++;
x/=2;
}
return counter;
}
void solve(int n){
int i=1;
int sum =0;
vector<int> v;
v.push_back(0);
/*while(n>0){
/*if(n-onesinbin(i)==1 && log2(i+1)==floor(log2(i+1))){
n-=onesinbin(i);
n-= onesinbin(i+1);
v.push_back(i);
v.push_back(i+1);
length+=2;
break;
}
else if(n-onesinbin(i)==1 && log2(i+1)!=floor(log2(i+1))){
i++;
}
else if(n-onesinbin(i)<0){
i++;
}
else if(n-onesinbin(i)>=0){
n-=onesinbin(i);
v.push_back(i);
length++;
i++;
}
/*else{
i++;
}
}*/
while(sum<n){
sum+=onesinbin(i);
v.push_back(i);
i++;
v[0]++;
}
i=v[0]-1;
while(sum!=n){
//cout << v[i] << onesinbin(i) << "\n";
if(sum-onesinbin(v[i])>=n){
sum-=onesinbin(v[i]);
v[i]=0;
v[0]--;
}
i--;
}
cout << v[0] << "\n";
for(int i=v.size()-1; i>0; i--){
if(v[i]!=0)cout<< v[i] << " ";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
int n;
/*for(int i=1; i<300; i++){
if(log2(i)==floor(log2(i))){
cout << "\n";
}
cout << onesinbin(i) << " ";
if(onesinbin(i+1)<onesinbin(i)) cout << " ";
}*/
//cout << "\n";
// cin >> tc;
for (int t = 1; t <= tc; t++) {
cin >> n;
solve(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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif #define ar array #define ll long long #define ld long double #define sza(x) ((int)x.size()) #define all(a) (a).begin(), (a).end() const int MAX_N = 1e5 + 5; const ll MOD = 1e9 + 7; const ll INF = 1e9; const ld EPS = 1e-9; int onesinbin(int x){ int counter =0; while(x){ if(x%2==1) counter++; x/=2; } return counter; } void solve(int n){ int i=1; int sum =0; vector<int> v; v.push_back(0); /*while(n>0){ /*if(n-onesinbin(i)==1 && log2(i+1)==floor(log2(i+1))){ n-=onesinbin(i); n-= onesinbin(i+1); v.push_back(i); v.push_back(i+1); length+=2; break; } else if(n-onesinbin(i)==1 && log2(i+1)!=floor(log2(i+1))){ i++; } else if(n-onesinbin(i)<0){ i++; } else if(n-onesinbin(i)>=0){ n-=onesinbin(i); v.push_back(i); length++; i++; } /*else{ i++; } }*/ while(sum<n){ sum+=onesinbin(i); v.push_back(i); i++; v[0]++; } i=v[0]-1; while(sum!=n){ //cout << v[i] << onesinbin(i) << "\n"; if(sum-onesinbin(v[i])>=n){ sum-=onesinbin(v[i]); v[i]=0; v[0]--; } i--; } cout << v[0] << "\n"; for(int i=v.size()-1; i>0; i--){ if(v[i]!=0)cout<< v[i] << " "; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; int n; /*for(int i=1; i<300; i++){ if(log2(i)==floor(log2(i))){ cout << "\n"; } cout << onesinbin(i) << " "; if(onesinbin(i+1)<onesinbin(i)) cout << " "; }*/ //cout << "\n"; // cin >> tc; for (int t = 1; t <= tc; t++) { cin >> n; solve(n); } return 0; } |
English