```
vector<int> countBits(int num) {
int sum = 0, mask = 1;
vector<int> res(num + 1, 0);
while (sum < num)
{
int count = min(num - sum, mask);
for (int i = 0; i < count; i++)
res[sum + i + 1] = res[i] + 1;
sum += count;
mask <<= 1;
}
return res;
}
```