Count pairs from a given range having equal Bitwise OR and XOR values | Function to calculate ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; The upper bound is 2 ^ N ; Stores the count of pairs ; Generate all possible pairs ; Find XOR of both integers ; Find OR of both integers ; If both are equal ; Increment count ; Print count % MOD ; Driver Code ; Function Call
def power ( x , y ) : NEW_LINE INDENT MOD = 1000000007 NEW_LINE res = 1 NEW_LINE x = x % MOD NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % MOD NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % MOD NEW_LINE DEDENT return res NEW_LINE DEDENT def countPairs ( N ) : NEW_LINE INDENT MOD = 1000000007 NEW_LINE high = power ( 2 , N ) NEW_LINE count = 0 NEW_LINE for i in range ( high ) : NEW_LINE INDENT for j in range ( high ) : NEW_LINE INDENT X = ( i ^ j ) NEW_LINE Y = ( i j ) NEW_LINE if ( X == Y ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count % MOD ) NEW_LINE DEDENT N = 10 NEW_LINE countPairs ( N ) NEW_LINE