Problem 680
Concatenation of Consecutive Binary Numbers
Medium · 20 pts · Math, Bit Manipulation
Problem
Write the numbers 1, 2, ..., N in binary (without leading zeros) one after another to form one long binary string. Print its decimal value modulo 1000000007. Appending the binary form of i to a value V gives V × 2^len(i) + i, where len(i) is the number of bits of i; the length grows by one each time i reaches a power of two. Take the remainder after every step. Some values computed along the way can be larger than a 32-bit int can hold (about 2.1 × 10^9), even when the input looks small, so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
A single integer N.
Output
Print the decimal value of the concatenation modulo 1000000007.
Constraints
1 <= N <= 100000
Example input
3
Example output
27
Explanation
For N = 3 the binary forms are 1, 10 and 11, which concatenate to 11011. In decimal that is 16 + 8 + 2 + 1 = 27.