📌  相关文章
📜  计算将M个对象放置在N个盒子的不同分区中的方法

📅  最后修改于: 2021-04-17 15:08:19             🧑  作者: Mango

给定两个正整数NM ,任务是找到将M个不同的对象放置在偶数索引框的分区中的方式的数量,这些索引框被依次编号为[1,N],并且每个iBox都有i个不同的分区。由于答案可能非常大,请以模1000000007为模。

例子:

方法:请按照以下步骤解决问题:

  • M个对象将被放置在甚至索引框的分区中。令SN个框中的总偶数索引框的分区
  • 分区的数目等于所有偶数的总和达到N.因此,点心,S = X *(X + 1),其中X =地板(N / 2)。
  • 每个对象可以占据S个不同的位置之一。因此,总数为S * S * S ..(M倍)= S M。

下面是上述方法的实现:

C++
// C++ implementation of the
// above Aapproach
 
#include 
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
        // becauuse 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 paritions 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 Aapproach
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
        // becauuse 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 paritions 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 Aapproach
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
        # becauuse 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 paritions 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 Aapproach
 
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
        // becauuse 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 paritions 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


输出:
36

时间复杂度: O(log M)
辅助空间: O(1)