Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
Last Updated :
26 Nov, 2021
Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].
Note: The product may be very large, take modulo to 109 + 7.
Examples:
Input: arr[] = {4, 7, 5, 9, 3}, M = 4
Output: 808556639
Explanation:
There are five possible sets. They are:
{4, 7, 5, 9}. In the sorted order, this set becomes {4, 5, 7, 9}. In this set, index 1, 2 and 4 divides M completely. Therefore, arr[1] * arr[2] * arr[4] = 4 * 5 * 9 = 180.
Similarly, the remaining four sets along with their products are:
{4, 7, 9, 3} -> 108
{4, 5, 9, 3} -> 108
{4, 7, 5, 3} -> 84
{7, 5, 9, 3} -> 135
The total value = ((180 * 108 * 108 * 84 * 135) % (10^9+7)) = 808556639
Input:arr[] = {7, 8, 9}, M = 2
Output: 254016
Approach: Since it is not possible to find all the sets, the idea is to count the total number of times every element occurs in the required position. If the count is found, then:
product = (arr[1]count of arr[1]) * (arr[2]count of arr[2])* ….. *(arr[N]count of arr[N])
- In order to find the count of the arr[i], we have to find the total number of the different sets that can be formed by placing arr[i] at every possible index(that divides M completely).
- Therefore, the number of sets formed by placing arr[i] at jth(j divides m completely) index will be:
(Number of elements lesser than arr[i])Cj-1 * (Number of elements greater than arr[i]) CM-j
- As the count of any element may be very large, so for finding (arr[i]count of arr[i]) % (109 + 7), Fermat’s little theorem is used.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
const int m = 4;
long long int power(lli x, lli y, lli p)
{
lli res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void nCr(lli n, lli p, lli f[][m + 1])
{
for (lli i = 0; i <= n; i++) {
for (lli j = 0; j <= m; j++) {
if (j > i)
f[i][j] = 0;
else if (j == 0 || j == i)
f[i][j] = 1;
else
f[i][j] = (f[i - 1][j]
+ f[i - 1][j - 1])
% p;
}
}
}
void operations(lli arr[], lli n, lli f[][m + 1])
{
lli p = 1000000007;
nCr(n, p - 1, f);
sort(arr, arr + n);
lli ans = 1;
for (lli i = 0; i < n; i++) {
long long int x = 0;
for (lli j = 1; j <= m; j++) {
if (m % j == 0)
x = (x
+ (f[n - i - 1][m - j]
* f[i][j - 1])
% (p - 1))
% (p - 1);
}
ans = ((ans * power(arr[i],
x, p))
% p);
}
cout << ans << endl;
}
int main()
{
lli arr[] = { 4, 5, 7, 9, 3 };
lli n = sizeof (arr) / sizeof (arr[0]);
lli f[n + 1][m + 1];
operations(arr, n, f);
}
|
Java
import java.util.*;
class GFG{
static int m = 4 ;
static long power( long x, long y, long p)
{
long res = 1 ;
x = x % p;
while (y > 0 )
{
if (y % 2 == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static void nCr( int n, long p, int f[][])
{
for ( int i = 0 ; i <= n; i++)
{
for ( int j = 0 ; j <= m; j++)
{
if (j > i)
f[i][j] = 0 ;
else if (j == 0 || j == i)
f[i][j] = 1 ;
else
f[i][j] = (f[i - 1 ][j] +
f[i - 1 ][j - 1 ]) % ( int )p;
}
}
}
static void operations( int arr[], int n, int f[][])
{
long p = 1000000007 ;
nCr(n, p - 1 , f);
Arrays.sort(arr);
long ans = 1 ;
for ( int i = 0 ; i < n; i++)
{
long x = 0 ;
for ( int j = 1 ; j <= m; j++)
{
if (m % j == 0 )
x = (x + (f[n - i - 1 ][m - j] *
f[i][j - 1 ]) %
(p - 1 )) %
(p - 1 );
}
ans = ((ans * power(arr[i], x, p)) % p);
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 7 , 9 , 3 };
int n = arr.length;
int [][]f = new int [n + 1 ][m + 1 ];
operations(arr, n, f);
}
}
|
Python3
m = 4
def power(x, y, p):
res = 1
x = x % p
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def nCr(n, p, f):
for i in range (n):
for j in range (m + 1 ):
if (j > i):
f[i][j] = 0
elif (j = = 0 or j = = i):
f[i][j] = 1
else :
f[i][j] = ((f[i - 1 ][j] +
f[i - 1 ][j - 1 ]) % p)
def operations(arr, n, f):
p = 1000000007
nCr(n, p - 1 , f)
arr.sort()
ans = 1
for i in range (n):
x = 0
for j in range ( 1 , m + 1 ):
if (m % j = = 0 ):
x = ((x + (f[n - i - 1 ][m - j] *
f[i][j - 1 ]) % (p - 1 )) %
(p - 1 ))
ans = ((ans * power(arr[i],
x, p)) % p)
print (ans)
if __name__ = = "__main__" :
arr = [ 4 , 5 , 7 , 9 , 3 ]
n = len (arr)
f = [[ 0 for x in range (m + 1 )]
for y in range (n + 1 )]
operations(arr, n, f)
|
C#
using System;
class GFG{
static int m = 4;
static long power( long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static void nCr( int n, long p, int [,]f)
{
for ( int i = 0; i <= n; i++)
{
for ( int j = 0; j <= m; j++)
{
if (j > i)
f[i, j] = 0;
else if (j == 0 || j == i)
f[i, j] = 1;
else
f[i, j] = (f[i - 1, j] +
f[i - 1, j - 1]) % ( int )p;
}
}
}
static void operations( int []arr, int n, int [,]f)
{
long p = 1000000007;
nCr(n, p - 1, f);
Array.Sort(arr);
long ans = 1;
for ( int i = 0; i < n; i++)
{
long x = 0;
for ( int j = 1; j <= m; j++)
{
if (m % j == 0)
x = (x + (f[n - i - 1, m - j] *
f[i, j - 1]) %
(p - 1)) %
(p - 1);
}
ans = ((ans * power(arr[i], x, p)) % p);
}
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 4, 5, 7, 9, 3 };
int n = arr.Length;
int [,]f = new int [n + 1, m + 1];
operations(arr, n, f);
}
}
|
Javascript
<script>
let m = 4;
function power(x, y, p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function nCr(n, p, f)
{
for (let i = 0; i <= n; i++)
{
for (let j = 0; j <= m; j++)
{
if (j > i)
f[i][j] = 0;
else if (j == 0 || j == i)
f[i][j] = 1;
else
f[i][j] = (f[i - 1][j] +
f[i - 1][j - 1]) % p;
}
}
}
function operations(arr, n, f)
{
let p = 1000000007;
nCr(n, p - 1, f);
arr.sort();
let ans = 1;
for (let i = 0; i < n; i++)
{
let x = 0;
for (let j = 1; j <= m; j++)
{
if (m % j == 0)
x = (x + (f[n - i - 1][m - j] *
f[i][j - 1]) %
(p - 1)) %
(p - 1);
}
ans = ((ans * power(arr[i], x, p)) % p);
}
document.write(ans + "\n" );
}
let arr = [ 4, 5, 7, 9, 3 ];
let n = arr.length;
let f = new Array(n + 1);
for ( var i = 0; i < f.length; i++)
{
f[i] = new Array(2);
}
operations(arr, n, f);
</script>
|
Time Complexity: O(N * M) where N is the length of the array and M is given by the user.
Auxiliary Space: O(N * M)
Share your thoughts in the comments
Please Login to comment...