#include <stdio.h> #include <iostream> using namespace std; const int N = 1000 * 1000; int tab[N]; bool niewyb[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int it = 1; int suma = 0; while (suma < n) { tab[it] = __builtin_popcount(it); suma += tab[it]; it++; } int dru = --it; int ilenie = 0; while (suma > n) { if (suma - tab[dru] >= n) { suma -= tab[dru]; niewyb[dru] = true; ilenie++; } dru--; } cout << it - ilenie << "\n"; while (it > 0) { if (!niewyb[it]) cout << it << " "; it--; } }
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 | #include <stdio.h> #include <iostream> using namespace std; const int N = 1000 * 1000; int tab[N]; bool niewyb[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int it = 1; int suma = 0; while (suma < n) { tab[it] = __builtin_popcount(it); suma += tab[it]; it++; } int dru = --it; int ilenie = 0; while (suma > n) { if (suma - tab[dru] >= n) { suma -= tab[dru]; niewyb[dru] = true; ilenie++; } dru--; } cout << it - ilenie << "\n"; while (it > 0) { if (!niewyb[it]) cout << it << " "; it--; } } |