#include <iostream>
#include <cassert>
using uchar = unsigned char;
using uint = unsigned int;
static int iTotal, iOutCnt, iNonZeroCnt;
static uchar aBitCnt[1024];
static uint aOut[1 << 20];
static inline uint GetIterativeBitCnt(uint i)
{
uint iCnt = 0;
while (i != 0)
{
i = i & (i - 1);
iCnt++;
}
return iCnt;
}
static void InitBitCnt()
{
for (uint i = 0; i < 1024; ++i)
{
aBitCnt[i] = GetIterativeBitCnt(i);
}
}
static inline uint GetBitCnt(uint i)
{
return aBitCnt[i & 1023] + aBitCnt[(i >> 10) & 1023];
}
static void Solve()
{
iOutCnt = 0;
int iBitsLeft = iTotal;
for (uint iOut = 1; iBitsLeft > 0; ++iOut)
{
uint iBits = GetBitCnt(iOut);
aOut[iOutCnt++] = iOut;
iBitsLeft -= iBits;
}
iNonZeroCnt = iOutCnt;
if (iBitsLeft == 0)
return;
iBitsLeft = -iBitsLeft;
for (int i = iOutCnt - 2; i >= 0 && iBitsLeft > 0; --i)
{
int iBits2Discard = GetBitCnt(aOut[i]);
if (iBits2Discard <= iBitsLeft)
{
aOut[i] = 0;
iNonZeroCnt--;
iBitsLeft -= iBits2Discard;
}
}
}
static void Output()
{
std::cout << iNonZeroCnt << std::endl;
for (int i = iOutCnt - 1; i >= 0; --i)
{
if (aOut[i] == 0)
continue;
std::cout << aOut[i];
iNonZeroCnt--;
if (iNonZeroCnt > 0)
{
std::cout << " ";
}
}
}
int main()
{
std::cin >> iTotal;
InitBitCnt();
Solve();
Output();
}
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 106 107 108 109 | #include <iostream> #include <cassert> using uchar = unsigned char; using uint = unsigned int; static int iTotal, iOutCnt, iNonZeroCnt; static uchar aBitCnt[1024]; static uint aOut[1 << 20]; static inline uint GetIterativeBitCnt(uint i) { uint iCnt = 0; while (i != 0) { i = i & (i - 1); iCnt++; } return iCnt; } static void InitBitCnt() { for (uint i = 0; i < 1024; ++i) { aBitCnt[i] = GetIterativeBitCnt(i); } } static inline uint GetBitCnt(uint i) { return aBitCnt[i & 1023] + aBitCnt[(i >> 10) & 1023]; } static void Solve() { iOutCnt = 0; int iBitsLeft = iTotal; for (uint iOut = 1; iBitsLeft > 0; ++iOut) { uint iBits = GetBitCnt(iOut); aOut[iOutCnt++] = iOut; iBitsLeft -= iBits; } iNonZeroCnt = iOutCnt; if (iBitsLeft == 0) return; iBitsLeft = -iBitsLeft; for (int i = iOutCnt - 2; i >= 0 && iBitsLeft > 0; --i) { int iBits2Discard = GetBitCnt(aOut[i]); if (iBits2Discard <= iBitsLeft) { aOut[i] = 0; iNonZeroCnt--; iBitsLeft -= iBits2Discard; } } } static void Output() { std::cout << iNonZeroCnt << std::endl; for (int i = iOutCnt - 1; i >= 0; --i) { if (aOut[i] == 0) continue; std::cout << aOut[i]; iNonZeroCnt--; if (iNonZeroCnt > 0) { std::cout << " "; } } } int main() { std::cin >> iTotal; InitBitCnt(); Solve(); Output(); } |
English