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 calculate sum ; Itereate for all values of array A ; Calling fast - exponentiation and appending ans to sum ; Driver code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fastmod ( int base , int exp , int mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { int ans = fastmod ( base , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; } int findPowerSum ( int n , int ar [ ] ) { const int mod = 1e9 + 7 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int base = __builtin_popcount ( ar [ i ] ) ; int exp = ar [ i ] ; sum += fastmod ( base , exp , mod ) ; sum %= mod ; } return sum ; } int main ( ) { int n = 3 ; int ar [ ] = { 1 , 2 , 3 } ; cout << findPowerSum ( n , ar ) ; return 0 ; }