Given an integer, array A. Find the sum of set bits raised to the power A[i] for each element in A[i].
Example:
Input: N = 3, A[] = {1, 2, 3}
Output: 10
Explanation:
Set bit of each array element is
1 = 1 set bit,
2 = 1 set bit,
3 = 2 set bit
store each set bit in b[i].
Compute sum of power(b[i], i)
where i is ranging from 1 to n.
that is sum = power(1, 1)+
power(1, 2)+power(2, 3) = 10
Input: N = 4, A[] = {2, 4, 5, 3}
Output: 42
Approach:
We can use modular-exponentiation method to calculate a^b under mod m and inbuilt __builtin_popcount function to count number of set bits in binary representation of A[i].
Below is the implementation of the above approach.
C++
// C++ program for the // above approach #include <bits/stdc++.h> using namespace std; // Function to calculate a^b mod m // using fast-exponentiation method 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; } // Function to // calculate sum int findPowerSum(int n, int ar[]) { const int mod = 1e9 + 7; int sum = 0; // Itereate for all // values of array A for (int i = 0; i < n; i++) { int base = __builtin_popcount(ar[i]); int exp = ar[i]; // Calling fast-exponentiation and // appending ans to sum sum += fastmod(base, exp, mod); sum %= mod; } return sum; } // Driver code. int main() { int n = 3; int ar[] = { 1, 2, 3 }; cout << findPowerSum(n, ar); return 0; } |
chevron_right
filter_none
Java
// Java program for the above approach class GFG { // Function to calculate a^b mod m // using fast-exponentiation method static 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; } /* Function to get no of set bits in binary representation of positive integer n */ static int countSetBits(int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } // Function to calculate sum static int findPowerSum(int n, int ar[]) { final int mod = (int)1e9 + 7; int sum = 0; // Itereate for all // values of array A for (int i = 0; i < n; i++) { int base = countSetBits(ar[i]); int exp = ar[i]; // Calling fast-exponentiation and // appending ans to sum sum += fastmod(base, exp, mod); sum %= mod; } return sum; } // Driver code. public static void main (String[] args) { int n = 3; int ar[] = { 1, 2, 3 }; System.out.println(findPowerSum(n, ar)); } } // This code is contributed by AnkitRai01 |
chevron_right
filter_none
Python3
# Python3 program for the above approach # Function to calculate a^b mod m # using fast-exponentiation method def fastmod(base, exp, mod) : if (exp == 0) : return 1; elif (exp % 2 == 0) : ans = fastmod(base, exp / 2, mod); return (ans % mod * ans % mod) % mod; else : return (fastmod(base, exp - 1, mod) % mod * base % mod) % mod; # Function to # calculate sum def findPowerSum(n, ar) : mod = int(1e9) + 7; sum = 0; # Itereate for all values of array A for i in range(n) : base = bin(ar[i]).count('1'); exp = ar[i]; # Calling fast-exponentiation and # appending ans to sum sum += fastmod(base, exp, mod); sum %= mod; return sum; # Driver code. if __name__ == "__main__" : n = 3; ar = [ 1, 2, 3 ]; print(findPowerSum(n, ar)); # This code is contributed by AnkitRai01 |
chevron_right
filter_none
C#
// C# program for the above approach using System; class GFG { // Function to calculate a^b mod m // using fast-exponentiation method 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; } /* Function to get no of set bits in binary representation of positive integer n */ static int countSetBits(int n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } // Function to calculate sum static int findPowerSum(int n, int []ar) { int mod = (int)1e9 + 7; int sum = 0; // Itereate for all // values of array A for (int i = 0; i < n; i++) { int baseval = countSetBits(ar[i]); int exp = ar[i]; // Calling fast-exponentiation and // appending ans to sum sum += fastmod(baseval, exp, mod); sum %= mod; } return sum; } // Driver code. public static void Main () { int n = 3; int []ar = { 1, 2, 3 }; Console.WriteLine(findPowerSum(n, ar)); } } // This code is contributed by AnkitRai01 |
chevron_right
filter_none
Output:
10
Time Complexity: O(n*log(n))
Recommended Posts:
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Sum of absolute difference of all pairs raised to power K
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find unit digit of x raised to power y
- Find value of y mod (2 raised to power x)
- Find last five digits of a given five digit number raised to power five
- Check if a number can be expressed as x^y (x raised to power y)
- Print last k digits of a^b (a raised to power b)
- K-th digit in 'a' raised to power 'b'
- Number of digits in 2 raised to power n
- GCD of a number raised to some power and another number
- Minimum removals in a number to be divisible by 10 power raised to K
- Sum of each element raised to (prime-1) % prime
- Count of 1-bit and 2-bit characters in the given binary string
- Last digit of a number raised to last digit of N factorial
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Find power of power under mod of a prime
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01


