Let g(n)=k. Then f(n)=2k−1, and so we just need to determine all k such that (2k−1)k has k 1's in its binary representation. To do this, we can simply expand
(2k−1)k=i=0∑k(−1)i(ik)2k2−ki=i=0∑⌊2k−1⌋(2ik)2k2−2ki−(2i+1k)2k2−2ki−k+((k+1)mod2).
Here (k+1)mod2 is 1 if k is even and 0 if k is odd. Write s(k,i) for
(2ik)2k2−2ki−(2i+1k)2k2−2ki−k
Then we know that
s(k,i)<2k2k2−2ki=2k2−(2i−1)k,
and we also know that 2k2−(2i+1)k divides s(k,i). Therefore
f((2k−1)k)=i=0∑⌊2k−1⌋f(s(k,i))+((k+1)mod2).
Since s(k,i)/2k2−(2i+1)k≡−(2i+1k)mod2k, we know that f(s(k,i))=f(s(k,i)/2k2−(2i+1)k) is at least
k−⌊log2(2i+1k)⌋.
Now we can proceed in two ways.
Approach 1. If k≥3, then we get
s(k,0)+s(k,1)≥2k−⌊log2k⌋−⌊log2(3k)⌋>2k−4log2k
Therefore 4log2k>k and so k<16. Now, plugging in
k≥2k−⌊log2k⌋−⌊log2(3k)⌋
for k<16, we get k=4,5,6,7,8,9 and the equality holds in all cases.
However, when k≥5 we have
f((2k−1)k)>f(s(k,0))+f(s(k,1))+f(s(k,2))≥k+1,
which is a contradiction. When k=4, we have f(154)≥s(4,0)+s(4,1)+1>5. Therefore, we reach a contradiction from k≥3, showing that k<3 must hold. It is clear that k=1,2 both work and we get n=1,9. □
Approach 2. It is clear that k=1,2 both work, so we will assume that k≥3 from now on. If k is odd, then we have s(k,(k−1)/2)=k⋅2k−1, showing that f(s(k,(k−1)/2))≥k. We also have f(s,0)≥1, so we already get a contradiction. Therefore, k must be even, and we have f(s(k,0)),f(s(k,k/2−1))≥k−⌊log2k⌋. Thus, 2⌊log2k⌋≥k, and we get k=4. However, it is easy to compute that f(154)=4, showing that k≥3 does not work at all. □