Number of pairs of numbers with xor equal to X

Revision en2, by Ewwa, 2017-04-21 17:31:30

Hello ! Can somebody explain to me how to solve this problem in O(logn) ?

Given N ( N <= 10^18 ) find the number of ordered pairs (a, b) for which a ^ b = X. ( 1 <= a, b <= N )

X is the first number >= N which has all it's bits equal to 1. For example if N = 5(101) then X = 7(111) and if N = 7 then X = 7.

Additional challenge : X is any number between 1 and 10^18, independent of N.

Problem link if somebody wants to submit : https://www.hackerearth.com/problem/algorithm/roy-and-maximum-xor/

Tags bit manipulation, math

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English Ewwa 2017-04-21 17:31:30 8
en1 English Ewwa 2017-04-21 17:30:18 544 Initial revision (published)