Count ways to place M objects in distinct partitions of N boxes
Given two positive integers N and M, the task is to find the number of ways to place M distinct objects in partitions of even indexed boxes which are numbered [1, N] sequentially, and every ith Box has i distinct partitions. Since the answer can be very large, print modulo 1000000007.
Examples:
Input: N = 2, M = 1
Output: 2
Explanation: Since, N = 2. There is only one even indexed box i.e box 2, having 2 partitions. Therefore, there are two positions to place an object. Therefore, number of ways = 2.
Input: N = 5, M = 2
Output: 32
Approach: Follow the steps below to solve the problem:
- M objects are to be placed in even indexed box’s partitions. Let S be the total even indexed box’s partitions in N boxes.
- The number of partitions is equal to the summation of all even numbers up to N. Therefore, Sum, S = X * (X + 1), where X = floor(N / 2).
- Each object can occupy one of S different positions. Therefore, the total number of ways = S*S*S..(M times) = SM.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int power( int x, unsigned int y, int p = MOD)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * 1LL * x) % p;
y = y >> 1;
x = (x * 1LL * x) % p;
}
return res;
}
void totalWays( int N, int M)
{
int X = N / 2;
int S = (X * 1LL * (X + 1)) % MOD;
cout << power(S, M, MOD) << "\n" ;
}
int main()
{
int N = 5, M = 2;
totalWays(N, M);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int MOD = 1000000007 ;
static int power( int x, int y, int p)
{
p = MOD;
int res = 1 ;
x = x % p;
while (y > 0 )
{
if ((y & 1 ) != 0 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static void totalWays( int N, int M)
{
int X = N / 2 ;
int S = (X * (X + 1 )) % MOD;
System.out.println(power(S, M, MOD));
}
public static void main (String[] args)
{
int N = 5 , M = 2 ;
totalWays(N, M);
}
}
|
Python3
MOD = 1000000007
def power(x, y, p = MOD):
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 totalWays(N, M):
X = N / / 2
S = (X * (X + 1 )) % MOD
print (power(S, M, MOD))
if __name__ = = '__main__' :
N, M = 5 , 2
totalWays(N, M)
|
C#
using System;
public class GFG{
public static int MOD = 1000000007;
static int power( int x, int y, int p)
{
p = MOD;
int res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static void totalWays( int N, int M)
{
int X = N / 2;
int S = (X * (X + 1)) % MOD;
Console.WriteLine(power(S, M, MOD));
}
static public void Main ()
{
int N = 5, M = 2;
totalWays(N, M);
}
}
|
Javascript
<script>
var MOD = 1000000007;
function power(x, y, p = MOD)
{
var res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * 1 * x) % p;
y = y >> 1;
x = (x * 1 * x) % p;
}
return res;
}
function totalWays(N, M)
{
var X = parseInt(N / 2);
var S = (X * 1 * (X + 1)) % MOD;
document.write( power(S, M, MOD) << "<br>" );
}
var N = 5, M = 2;
totalWays(N, M);
</script>
|
Time Complexity: O(log M)
Auxiliary Space: O(1)
Last Updated :
22 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...