Find the sum of power of bit count raised to the power B | C # program for the above approach ; 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 .
using System ; class GFG { static int fastmod ( int baseval , int exp , int mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { int ans = fastmod ( baseval , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( baseval , exp - 1 , mod ) % mod * baseval % mod ) % mod ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int findPowerSum ( int n , int [ ] ar ) { int mod = ( int ) 1e9 + 7 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int baseval = countSetBits ( ar [ i ] ) ; int exp = ar [ i ] ; sum += fastmod ( baseval , exp , mod ) ; sum %= mod ; } return sum ; } public static void Main ( ) { int n = 3 ; int [ ] ar = { 1 , 2 , 3 } ; Console . WriteLine ( findPowerSum ( n , ar ) ) ; } }