Find the sum of power of bit count raised to the power B | Function to calculate a ^ b mod m using fast - exponentiation method ; * Function to get no of set bits in binary representation of positive integer n ; Function to calculate sum ; Itereate for all values of array A ; Calling fast - exponentiation and appending ans to sum ; Driver code
function fastmod ( base , exp , mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { var ans = fastmod ( base , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; } function countSetBits ( n ) { var count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function findPowerSum ( n , ar ) { var mod = parseInt ( 1e9 + 7 ) ; var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { var base = countSetBits ( ar [ i ] ) ; var exp = ar [ i ] ; sum += fastmod ( base , exp , mod ) ; sum %= mod ; } return sum ; } var n = 3 ; var ar = [ 1 , 2 , 3 ] ; document . write ( findPowerSum ( n , ar ) ) ;