A subsequence is any sequence that you can get from another by deleting any amount of characters. The distinct non-empty subsequences of 100
are 0
, 1
, 00
, 10
, 100
. The distinct non-empty subsequences of 1010
are 0
, 1
, 00
, 01
, 10
, 11
, 010
, 100
, 101
, 110
, 1010
.
Write a program or function that given a positive integer n returns the number of distinct non-empty subsequences of the binary expansion of n.
Example: since 4
is 100
in binary, and we saw that it had five distinct non-empty subsequences above, so f(4) = 5
. Starting from n = 1, the sequence begins:
1, 3, 2, 5, 6, 5, 3, 7, 10, 11, 9, 8, 9, 7, 4, 9, 14, 17, 15, 16, 19, 17, 12
However, your program must work for any n < 250 in under a second on any modern machine. Some large examples:
f(1099511627775) = 40
f(1099511627776) = 81
f(911188917558917) = 728765543
f(109260951837875) = 447464738
f(43765644099) = 5941674