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++
// C++ implementation of the // above Approach Â
#include <bits/stdc++.h> using namespace std; Â
const int MOD = 1000000007; Â
// Iterative Function to calculate // (x^y)%p in O(log y) int power( int x, unsigned int y, int p = MOD) {     // Initialize Result     int res = 1; Â
    // Update x if x >= MOD     // to avoid multiplication overflow     x = x % p; Â
    while (y > 0) { Â
        // If y is odd, multiply x with result         if (y & 1)             res = (res * 1LL * x) % p; Â
        // multiplied by long long int,         // to avoid overflow         // because res * x <= 1e18, which is         // out of bounds for integer Â
        // n must be even now Â
        // y = y/2         y = y >> 1; Â
        // Change x to x^2         x = (x * 1LL * x) % p;     }     return res; } Â
// Utility function to find // the Total Number of Ways void totalWays( int N, int M) {     // Number of Even Indexed     // Boxes     int X = N / 2; Â
    // Number of partitions of     // Even Indexed Boxes     int S = (X * 1LL * (X + 1)) % MOD; Â
    // Number of ways to distribute     // objects     cout << power(S, M, MOD) << "\n" ; } Â
// Driver Code int main() {     // N = number of boxes     // M = number of distinct objects     int N = 5, M = 2; Â
    // Function call to     // get Total Number of Ways     totalWays(N, M); Â
    return 0; } |
Java
// Java implementation of the // above Approach import java.io.*; class GFG { Â Â Â public static int MOD = 1000000007 ; Â
// Iterative Function to calculate // (x^y)%p in O(log y) static int power( int x, int y, int p) {         p = MOD;        // Initialize Result     int res = 1 ; Â
    // Update x if x >= MOD     // to avoid multiplication overflow     x = x % p; Â
    while (y > 0 )     { Â
        // If y is odd, multiply x with result         if ((y & 1 ) != 0 )             res = (res * x) % p; Â
        // multiplied by long long int,         // to avoid overflow         // because res * x <= 1e18, which is         // out of bounds for integer Â
        // n must be even now Â
        // y = y/2         y = y >> 1 ; Â
        // Change x to x^2         x = (x * x) % p;     }     return res; } Â
// Utility function to find // the Total Number of Ways static void totalWays( int N, int M) {        // Number of Even Indexed     // Boxes     int X = N / 2 ; Â
    // Number of partitions of     // Even Indexed Boxes     int S = (X * (X + 1 )) % MOD; Â
    // Number of ways to distribute     // objects     System.out.println(power(S, M, MOD)); } Â
// Driver Code public static void main (String[] args) {            // N = number of boxes     // M = number of distinct objects     int N = 5 , M = 2 ; Â
    // Function call to     // get Total Number of Ways     totalWays(N, M); } } Â
// This code is contributed by Dharanendra L V |
Python3
# Python3 implementation of the # above Approach MOD = 1000000007 Â
# Iterative Function to calculate # (x^y)%p in O(log y) def power(x, y, p = MOD):        # Initialize Result     res = 1 Â
    # Update x if x >= MOD     # to avoid multiplication overflow     x = x % p     while (y > 0 ): Â
        # If y is odd, multiply x with result         if (y & 1 ):             res = (res * x) % p Â
        # multiplied by long long int,         # to avoid overflow         # because res * x <= 1e18, which is         # out of bounds for integer Â
        # n must be even now Â
        # y = y/2         y = y >> 1 Â
        # Change x to x^2         x = (x * x) % p     return res Â
# Utility function to find # the Total Number of Ways def totalWays(N, M):        # Number of Even Indexed     # Boxes     X = N / / 2 Â
    # Number of partitions of     # Even Indexed Boxes     S = (X * (X + 1 )) % MOD Â
    # Number of ways to distribute     # objects     print (power(S, M, MOD)) Â
# Driver Code if __name__ = = '__main__' : Â
    # N = number of boxes     # M = number of distinct objects     N, M = 5 , 2 Â
    # Function call to     # get Total Number of Ways     totalWays(N, M) Â
# This code is contributed by mohit kumar 29. |
C#
// C# implementation of the // above Approach Â
using System; Â
public class GFG{ Â
public static int MOD = 1000000007; Â
// Iterative Function to calculate // (x^y)%p in O(log y) static int power( int x, int y, int p) {              p = MOD;        // Initialize Result     int res = 1; Â
    // Update x if x >= MOD     // to avoid multiplication overflow     x = x % p; Â
    while (y > 0) { Â
        // If y is odd, multiply x with result         if ((y & 1) != 0)             res = (res * x) % p; Â
        // multiplied by long long int,         // to avoid overflow         // because res * x <= 1e18, which is         // out of bounds for integer Â
        // n must be even now Â
        // y = y/2         y = y >> 1; Â
        // Change x to x^2         x = (x * x) % p;     }     return res; } Â
// Utility function to find // the Total Number of Ways static void totalWays( int N, int M) {        // Number of Even Indexed     // Boxes     int X = N / 2; Â
    // Number of partitions of     // Even Indexed Boxes     int S = (X * (X + 1)) % MOD; Â
    // Number of ways to distribute     // objects     Console.WriteLine(power(S, M, MOD)); } Â
// Driver Code static public void Main () { Â
    // N = number of boxes     // M = number of distinct objects     int N = 5, M = 2; Â
    // Function call to     // get Total Number of Ways     totalWays(N, M); } } Â
// This code is contributed by Dharanendra L V |
Javascript
<script> Â
// Javascript implementation of the // above Approach Â
var MOD = 1000000007; Â
// Iterative Function to calculate // (x^y)%p in O(log y) function power(x, y, p = MOD) {     // Initialize Result     var res = 1; Â
    // Update x if x >= MOD     // to avoid multiplication overflow     x = x % p; Â
    while (y > 0) { Â
        // If y is odd, multiply x with result         if (y & 1)             res = (res * 1 * x) % p; Â
        // multiplied by long long int,         // to avoid overflow         // because res * x <= 1e18, which is         // out of bounds for integer Â
        // n must be even now Â
        // y = y/2         y = y >> 1; Â
        // Change x to x^2         x = (x * 1 * x) % p;     }     return res; } Â
// Utility function to find // the Total Number of Ways function totalWays(N, M) {     // Number of Even Indexed     // Boxes     var X = parseInt(N / 2); Â
    // Number of partitions of     // Even Indexed Boxes     var S = (X * 1 * (X + 1)) % MOD; Â
    // Number of ways to distribute     // objects     document.write( power(S, M, MOD) << "<br>" ); } Â
// Driver Code // N = number of boxes // M = number of distinct objects var N = 5, M = 2; // Function call to // get Total Number of Ways totalWays(N, M); Â
</script> |
36
Â
Time Complexity: O(log M)
Auxiliary Space: O(1)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!