📌  相关文章
📜  获得n个m面骰子的给定总和的方式数量

📅  最后修改于: 2021-05-06 08:00:27             🧑  作者: Mango

给定n个骰子,每个骰子有m个面(从1到m编号),找到获得给定总和X的方法的数量。X是抛出所有骰子时每个面的值之和。
例子:

方法:
基本上,要求使用[1…m]范围内的值实现n个操作的总和。
使用动态编程自上而下的方法来解决此问题。这些步骤是:

  • 基本案例:
    1. 如果(sum == 0且noofthrowsleft == 0)返回1 。这意味着总和x有
      实现了。
    2. 如果(sum <0并且noofthrowsleft == 0)返回0 ,则意味着sum x没有
      在所有方面都取得了成就。
  • 如果已经实现了具有当前noofthrowsleft的存在总和,则从表中将其返回而不是重新计算。
  • 然后,我们将遍历i = [1..m]中所有面的值,然后递归移动以得出sum-i,并将剩余的罚球数减少1。
  • 最后,我们将当前值存储在dp数组中

下面是上述方法的实现:

C++
// C++ function to calculate the number of
// ways to achieve sum x in n no of throws
#include 
using namespace std;
#define mod 1000000007
int dp[55][55];
 
// Function to calculate recursively the
// number of ways to get sum in given
// throws and [1..m] values
int NoofWays(int face, int throws, int sum)
{
    // Base condition 1
    if (sum == 0 && throws == 0)
        return 1;
 
    // Base condition 2
    if (sum < 0 || throws == 0)
        return 0;
 
    // If value already calculated dont
    // move into re-computation
    if (dp[throws][sum] != -1)
        return dp[throws][sum];
 
    int ans = 0;
    for (int i = 1; i <= face; i++) {
 
        // Recusively moving for sum-i in
        // throws-1 no of throws left
        ans += NoofWays(face, throws - 1, sum - i);
    }
 
    // Inserting present values in dp
    return dp[throws][sum] = ans;
}
 
// Driver function
int main()
{
    int faces = 6, throws = 3, sum = 12;
 
    memset(dp, -1, sizeof dp);
 
    cout << NoofWays(faces, throws, sum) << endl;
 
    return 0;
}


Java
// Java function to calculate the number of
// ways to achieve sum x in n no of throwsVal
class GFG
{
 
    static int mod = 1000000007;
    static int[][] dp = new int[55][55];
 
    // Function to calculate recursively the
    // number of ways to get sum in given
    // throwsVal and [1..m] values
    static int NoofWays(int face, int throwsVal, int sum)
    {
        // Base condition 1
        if (sum == 0 && throwsVal == 0)
        {
            return 1;
        }
 
        // Base condition 2
        if (sum < 0 || throwsVal == 0)
        {
            return 0;
        }
 
        // If value already calculated dont
        // move into re-computation
        if (dp[throwsVal][sum] != -1)
        {
            return dp[throwsVal][sum];
        }
 
        int ans = 0;
        for (int i = 1; i <= face; i++)
        {
 
            // Recusively moving for sum-i in
            // throwsVal-1 no of throwsVal left
            ans += NoofWays(face, throwsVal - 1, sum - i);
        }
 
        // Inserting present values in dp
        return dp[throwsVal][sum] = ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int faces = 6, throwsVal = 3, sum = 12;
        for (int i = 0; i < 55; i++)
        {
            for (int j = 0; j < 55; j++)
            {
                dp[i][j] = -1;
            }
        }
 
        System.out.println(NoofWays(faces, throwsVal, sum));
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 function to calculate the number of
# ways to achieve sum x in n no of throws
import numpy as np
 
mod = 1000000007;
 
dp = np.zeros((55,55));
 
# Function to calculate recursively the
# number of ways to get sum in given
# throws and [1..m] values
def NoofWays(face, throws, sum) :
 
    # Base condition 1
    if (sum == 0 and throws == 0) :
        return 1;
 
    # Base condition 2
    if (sum < 0 or throws == 0) :
        return 0;
 
    # If value already calculated dont
    # move into re-computation
    if (dp[throws][sum] != -1) :
        return dp[throws][sum];
 
    ans = 0;
    for i in range(1, face + 1) :
 
        # Recusively moving for sum-i in
        # throws-1 no of throws left
        ans += NoofWays(face, throws - 1, sum - i);
 
    # Inserting present values in dp
    dp[throws][sum] = ans;
     
    return ans;
 
 
# Driver function
if __name__ == "__main__" :
 
    faces = 6; throws = 3; sum = 12;
 
    for i in range(55) :
        for j in range(55) :
            dp[i][j] = -1
 
    print(NoofWays(faces, throws, sum)) ;
     
# This code is contributed by AnkitRai01


C#
// C# function to calculate the number of
// ways to achieve sum x in n no of throwsVal
using System;
 
class GFG
{
     
    static int[,]dp = new int[55,55];
 
    // Function to calculate recursively the
    // number of ways to get sum in given
    // throwsVal and [1..m] values
    static int NoofWays(int face, int throwsVal, int sum)
    {
        // Base condition 1
        if (sum == 0 && throwsVal == 0)
        {
            return 1;
        }
 
        // Base condition 2
        if (sum < 0 || throwsVal == 0)
        {
            return 0;
        }
 
        // If value already calculated dont
        // move into re-computation
        if (dp[throwsVal,sum] != -1)
        {
            return dp[throwsVal,sum];
        }
 
        int ans = 0;
        for (int i = 1; i <= face; i++)
        {
 
            // Recusively moving for sum-i in
            // throwsVal-1 no of throwsVal left
            ans += NoofWays(face, throwsVal - 1, sum - i);
        }
 
        // Inserting present values in dp
        return dp[throwsVal,sum] = ans;
    }
 
    // Driver code
    static public void Main ()
    {
        int faces = 6, throwsVal = 3, sum = 12;
        for (int i = 0; i < 55; i++)
        {
            for (int j = 0; j < 55; j++)
            {
                dp[i,j] = -1;
            }
        }
 
    Console.WriteLine(NoofWays(faces, throwsVal, sum));
    }
}
 
// This code is contributed by ajit.


输出:
25

时间复杂度: O(投掷*面孔*总和)
空间复杂度: O(faces * sum)