3

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

duplex143
  • 619
  • 2
  • 9
  • 25
  • 1
    *This is O(N). Since n is up to 10^9, I need to come up with an O(NlogN) or O(logN) approach* Note that O(NlogN) is actually a *worse* time complexity than O(N). – Telescope Jul 10 '20 at 17:16
  • I'm not sure this is an exact duplicate, since the accepted answer to the previous question only provides an O(n) solution. However, my gut feeling is that no O(log n) solution exists. Even if there is an analytical solution, it would probably require using the base 2 Champernowne constant with a billion bits of accuracy. In any case, in C, I can compute f(10^9) in about 4 seconds. With a precomputed list of 100 equally spaced values, you should be able to reduce the average compute time to about 20 ms for 0 – r3mainer Jul 10 '20 at 20:46
  • @r3mainer f(10^9) in about 4 seconds: You just had a for loop till 1e9 or had any optimization? Also, I don't understand what you mean by precomputed list of 100 equally spaced values. You mean to store values of f(1),f(1e7),f(2e7),f(3e7),...f(99e7) and use them? Eg: we'll use f(1e7) for computing f(1.5e7)?? How do we compute them in the first place under the time constraint? – duplex143 Jul 11 '20 at 09:51
  • @duplex143 Nothing special, I just enabled compiler optimization. I've posted some code [here](https://github.com/squeamish-ossifrage/stackoverflow/blob/master/binary_concatenation.c) that basically uses the above approach, but it was a bit easier to implement with steps of 2^24 instead of 10^7. It's not very well commented and might have one or 2 bugs, but it seems to work OK. – r3mainer Jul 11 '20 at 12:38
  • @r3mainer I was able to come up with a O(logN) approach. f(N) can be thought of sum of multiple AGP series. Pls reopen the question. – duplex143 Jul 11 '20 at 17:14
  • I already voted to reopen. Give it a while longer, I think only one more vote will do it. – r3mainer Jul 11 '20 at 18:42
  • Or if not, you could just post your solution as an answer to the earlier question – r3mainer Jul 12 '20 at 09:38
  • I have upvoted the question hoping that it would reopen. – Satyendra Kumar Sep 13 '20 at 16:06
  • I've added O(logN) solution here : https://stackoverflow.com/a/67252483/5524175 – duplex143 Apr 25 '21 at 10:50

0 Answers0