I've seen this question in a programming contest. Given a number n, find the decimal value of the number formed by concatenating the binary representations of first n natural numbers. Print answer modulo 10^9+7. Also, n can be as big as 10^9.
Eg: n=4. Number formed=11011100(1=1,10=2,11=3,100=4). Decimal value of 11011100=220.
I was able to come up with the recurrence relation
If size of n in decimal representation = k
Then f(n)=f(n-1)*pow(2,k)+n , where k= 1+step(logn/log2)
This is O(N). Since n is upto 10^9, need to come up with O(logN) approach.
This is basically the A047778 sequence.
1,6,27,220,1765,14126,113015,1808248,28931977, 462911642,7406586283,118505380540,1896086088653, 30337377418462,485398038695407,15532737238253040, 497047591624097297,15905522931971113522