Product of divisors of a number from a given list of its prime factors | Java Program to implement the above approach ; Function to calculate ( a ^ b ) % m ; Function to calculate and return the product of divisors ; Stores the frequencies of prime divisors ; Iterate over the prime divisors ; Update the product ; Update the count of divisors ; Driver Code
import java . util . * ; class GFG { static int MOD = 1000000007 ; static int power ( int a , int b , int m ) { a %= m ; int res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( ( res % m ) * ( a % m ) ) % m ; a = ( ( a % m ) * ( a % m ) ) % m ; b >>= 1 ; } return res % m ; } static int productOfDivisors ( int p [ ] , int n ) { HashMap < Integer , Integer > prime = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime . containsKey ( p [ i ] ) ) prime . put ( p [ i ] , prime . get ( p [ i ] ) + 1 ) ; else prime . put ( p [ i ] , 1 ) ; } int product = 1 , d = 1 ; for ( Map . Entry < Integer , Integer > itr : prime . entrySet ( ) ) { int val = power ( itr . getKey ( ) , ( itr . getValue ( ) ) * ( itr . getValue ( ) + 1 ) / 2 , MOD ) ; product = ( power ( product , itr . getValue ( ) + 1 , MOD ) * power ( val , d , MOD ) ) % MOD ; d = ( d * ( itr . getValue ( ) + 1 ) ) % ( MOD - 1 ) ; } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 11 } ; int n = arr . length ; System . out . println ( productOfDivisors ( arr , n ) ) ; } }