#include <iostream> using namespace std; int c[1000000]; int s = 0; int moc(int l) { int m = 0; while (l > 0) { m += l % 2; l /= 2; } return m; } int main() { int n; cin >> n; int i = 1; while (s <= n) { c[i - 1] = i; s += moc(i); i++; } for (int j = n ; j >= 0; j--) { if (s > n) { if ((s - moc(c[j])) >= n) { s -= moc(c[j]); c[j] = -1; } } if (s == n) break; } int il = 0; for(int l = n; l >= 0; l--) { if(c[l] != -1) il ++; } cout << il << "\n"; for(int l = n; l >= 0; l--) { if(c[l] != -1) cout << c[l] << " "; } cout << "\n"; }
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 | #include <iostream> using namespace std; int c[1000000]; int s = 0; int moc(int l) { int m = 0; while (l > 0) { m += l % 2; l /= 2; } return m; } int main() { int n; cin >> n; int i = 1; while (s <= n) { c[i - 1] = i; s += moc(i); i++; } for (int j = n ; j >= 0; j--) { if (s > n) { if ((s - moc(c[j])) >= n) { s -= moc(c[j]); c[j] = -1; } } if (s == n) break; } int il = 0; for(int l = n; l >= 0; l--) { if(c[l] != -1) il ++; } cout << il << "\n"; for(int l = n; l >= 0; l--) { if(c[l] != -1) cout << c[l] << " "; } cout << "\n"; } |