#include <iostream>
const int MAX_N = 1000000;
const int MAX_K = 1000000;
int N;
int Bits[MAX_K];
int BitsTotal[MAX_K];
int M;
int Rezultaty[MAX_K];
/*
1 1 1
2 1 2
3 2 4
4 1 5
5 2 7
6 2 9
7 3 11
8 1 12
9 2 14
10 2 16
11 3 19
*/
int popcount(int x)
{
int Wynik = 0;
int Liczba = x;
while (Liczba != 0)
{
Wynik += Liczba & 1;
Liczba >>= 1;
}
return Wynik;
}
int main() {
std::cin >> N;
int K = 0;
int Total = 0;
while (Total < N)
{
K++;
int Bity = popcount(K);
Total += Bity;
Bits[K] = Bity;
BitsTotal[K] = Total;
}
{
K++;
int Bity = popcount(K);
Total += Bity;
Bits[K] = Bity;
BitsTotal[K] = Total;
}
M = 0;
int BitsLeft = N;
int Ostatni = K;
while (BitsLeft > 0) {
int i = Ostatni;
while (i > 0 && BitsTotal[i] >= BitsLeft)
{
i--;
}
int Rezultat = i+1;
Rezultaty[M] = Rezultat;
M++;
BitsLeft -= Bits[Rezultat];
Ostatni = Rezultat;
}
std::cout << M << "\n";
for (int i = 0; i < M; i++) {
if (i != 0) std::cout << ' ';
std::cout << Rezultaty[i];
}
std::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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <iostream> const int MAX_N = 1000000; const int MAX_K = 1000000; int N; int Bits[MAX_K]; int BitsTotal[MAX_K]; int M; int Rezultaty[MAX_K]; /* 1 1 1 2 1 2 3 2 4 4 1 5 5 2 7 6 2 9 7 3 11 8 1 12 9 2 14 10 2 16 11 3 19 */ int popcount(int x) { int Wynik = 0; int Liczba = x; while (Liczba != 0) { Wynik += Liczba & 1; Liczba >>= 1; } return Wynik; } int main() { std::cin >> N; int K = 0; int Total = 0; while (Total < N) { K++; int Bity = popcount(K); Total += Bity; Bits[K] = Bity; BitsTotal[K] = Total; } { K++; int Bity = popcount(K); Total += Bity; Bits[K] = Bity; BitsTotal[K] = Total; } M = 0; int BitsLeft = N; int Ostatni = K; while (BitsLeft > 0) { int i = Ostatni; while (i > 0 && BitsTotal[i] >= BitsLeft) { i--; } int Rezultat = i+1; Rezultaty[M] = Rezultat; M++; BitsLeft -= Bits[Rezultat]; Ostatni = Rezultat; } std::cout << M << "\n"; for (int i = 0; i < M; i++) { if (i != 0) std::cout << ' '; std::cout << Rezultaty[i]; } std::cout << '\n'; return 0; } |
English