📜  在最多 W 个小门的 B 球中得分 R 的方法数

📅  最后修改于: 2021-09-22 10:05:29             🧑  作者: Mango

给定三个整数RBW ,它们表示运行小门的数量。在一场板球比赛中,一个人可以在一个球中得分0、1、2、3、4、6一个小门。任务是计算在其中一支球队能得分正好R运行在与最W¯¯小门正是乙球方式的数量。由于方式的数量会很大,打印答案模1000000007
例子:

方法:该问题可以使用动态规划和组合学来解决。循环将有 6 个状态,我们最初从run = 0balls = 0wickets = 0 开始。这些州将是:

  • 如果一个球队得分1 run off a ball然后running = running + 1balls = balls + 1
  • 如果一个球队得分2 run off a ball,那么running = running + 2balls = balls + 1
  • 如果一个球队得分3 run off a ball,那么running = running + 3balls = balls + 1
  • 如果一个球队得分4 run off a ball然后running = running + 4balls = balls + 1
  • 如果一个球队得分6 run off a ball,那么running = running + 6balls = balls + 1
  • 如果一支球队没有得分,那么runs = runningballs = balls + 1
  • 如果一支球队在一个球上失去了 1 个小门,那么run = runningballs = balls + 1wickets = wickets + 1

DP 将由三个状态组成,运行状态最多为6 * Balls ,因为它是最大的可能。因此dp[i][j][k]表示i 奔跑可以在恰好j 个球中得分的方式数量,而失去k 个小门
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define mod 1000000007
#define RUNMAX 300
#define BALLMAX 50
#define WICKETMAX 10
 
// Function to return the number of ways
// to score R runs in B balls with
// at most W wickets
int CountWays(int r, int b, int l, int R, int B, int W,
              int dp[RUNMAX][BALLMAX][WICKETMAX])
{
 
    // If the wickets lost are more
    if (l > W)
        return 0;
 
    // If runs scored are more
    if (r > R)
        return 0;
 
    // If condition is met
    if (b == B && r == R)
        return 1;
 
    // If no run got scored
    if (b == B)
        return 0;
 
    // Already visited state
    if (dp[r][b][l] != -1)
        return dp[r][b][l];
 
    int ans = 0;
 
    // If scored 0 run
    ans += CountWays(r, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 1 run
    ans += CountWays(r + 1, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 2 runs
    ans += CountWays(r + 2, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 3 runs
    ans += CountWays(r + 3, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 4 runs
    ans += CountWays(r + 4, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 6 runs
    ans += CountWays(r + 6, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored no run and lost a wicket
    ans += CountWays(r, b + 1, l + 1, R, B, W, dp);
    ans = ans % mod;
 
    // Memoize and return
    return dp[r][b][l] = ans;
}
 
// Driver code
int main()
{
    int R = 40, B = 10, W = 4;
 
    int dp[RUNMAX][BALLMAX][WICKETMAX];
    memset(dp, -1, sizeof dp);
 
    cout << CountWays(0, 0, 0, R, B, W, dp);
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
      
static int mod = 1000000007;
static int RUNMAX = 300;
static int BALLMAX = 50;
static int WICKETMAX = 10;
  
// Function to return the number of ways
// to score R runs in B balls with
// at most W wickets
static int CountWays(int r, int b, int l,
                    int R, int B, int W,
                            int [][][]dp)
{
  
    // If the wickets lost are more
    if (l > W)
        return 0;
  
    // If runs scored are more
    if (r > R)
        return 0;
  
    // If condition is met
    if (b == B && r == R)
        return 1;
  
    // If no run got scored
    if (b == B)
        return 0;
  
    // Already visited state
    if (dp[r][b][l] != -1)
        return dp[r][b][l];
  
    int ans = 0;
  
    // If scored 0 run
    ans += CountWays(r, b + 1, l, R, B, W, dp);
    ans = ans % mod;
  
    // If scored 1 run
    ans += CountWays(r + 1, b + 1, l, R, B, W, dp);
    ans = ans % mod;
  
    // If scored 2 runs
    ans += CountWays(r + 2, b + 1, l, R, B, W, dp);
    ans = ans % mod;
  
    // If scored 3 runs
    ans += CountWays(r + 3, b + 1, l, R, B, W, dp);
    ans = ans % mod;
  
    // If scored 4 runs
    ans += CountWays(r + 4, b + 1, l, R, B, W, dp);
    ans = ans % mod;
  
    // If scored 6 runs
    ans += CountWays(r + 6, b + 1, l, R, B, W, dp);
    ans = ans % mod;
  
    // If scored no run and lost a wicket
    ans += CountWays(r, b + 1, l + 1, R, B, W, dp);
    ans = ans % mod;
  
    // Memoize and return
    return dp[r][b][l] = ans;
}
  
// Driver code
public static void main(String[] args)
{
    int R = 40, B = 10, W = 4;
  
    int[][][] dp = new int[RUNMAX][BALLMAX][WICKETMAX];
    for(int i = 0; i < RUNMAX;i++)
        for(int j = 0; j < BALLMAX; j++)
            for(int k = 0; k < WICKETMAX; k++)
    dp[i][j][k]=-1;
  
    System.out.println(CountWays(0, 0, 0, R, B, W, dp));
}
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
mod = 1000000007
RUNMAX = 300
BALLMAX = 50
WICKETMAX = 10
 
# Function to return the number of ways
# to score R runs in B balls with
# at most W wickets
def CountWays(r, b, l, R, B, W, dp):
     
    # If the wickets lost are more
    if (l > W):
        return 0;
 
    # If runs scored are more
    if (r > R):
        return 0;
 
    # If condition is met
    if (b == B and r == R):
        return 1;
 
    # If no run got scored
    if (b == B):
        return 0;
 
    # Already visited state
    if (dp[r][b][l] != -1):
        return dp[r][b][l]
         
    ans = 0;
 
    # If scored 0 run
    ans += CountWays(r, b + 1, l,
                     R, B, W, dp);
    ans = ans % mod;
 
    # If scored 1 run
    ans += CountWays(r + 1, b + 1, l,
                     R, B, W, dp);
    ans = ans % mod;
 
    # If scored 2 runs
    ans += CountWays(r + 2, b + 1, l,
                     R, B, W, dp);
    ans = ans % mod;
 
    # If scored 3 runs
    ans += CountWays(r + 3, b + 1, l,
                     R, B, W, dp);
    ans = ans % mod;
 
    # If scored 4 runs
    ans += CountWays(r + 4, b + 1, l,
                     R, B, W, dp);
    ans = ans % mod;
 
    # If scored 6 runs
    ans += CountWays(r + 6, b + 1, l,
                     R, B, W, dp);
    ans = ans % mod;
 
    # If scored no run and lost a wicket
    ans += CountWays(r, b + 1, l + 1,
                     R, B, W, dp);
    ans = ans % mod;
     
    # Memoize and return
    dp[r][b][l] = ans
     
    return ans;
     
# Driver code   
if __name__=="__main__":
     
    R = 40
    B = 10
    W = 40
     
    dp = [[[-1 for k in range(WICKETMAX)]
               for j in range(BALLMAX)]
               for i in range(RUNMAX)]
     
    print(CountWays(0, 0, 0, R, B, W, dp))
 
# This code is contributed by rutvik_56


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG
{
     
static int mod = 1000000007;
static int RUNMAX = 300;
static int BALLMAX = 50;
static int WICKETMAX = 10;
 
// Function to return the number of ways
// to score R runs in B balls with
// at most W wickets
static int CountWays(int r, int b, int l,
                    int R, int B, int W,
                            int [,,]dp)
{
 
    // If the wickets lost are more
    if (l > W)
        return 0;
 
    // If runs scored are more
    if (r > R)
        return 0;
 
    // If condition is met
    if (b == B && r == R)
        return 1;
 
    // If no run got scored
    if (b == B)
        return 0;
 
    // Already visited state
    if (dp[r, b, l] != -1)
        return dp[r, b, l];
 
    int ans = 0;
 
    // If scored 0 run
    ans += CountWays(r, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 1 run
    ans += CountWays(r + 1, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 2 runs
    ans += CountWays(r + 2, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 3 runs
    ans += CountWays(r + 3, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 4 runs
    ans += CountWays(r + 4, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored 6 runs
    ans += CountWays(r + 6, b + 1, l, R, B, W, dp);
    ans = ans % mod;
 
    // If scored no run and lost a wicket
    ans += CountWays(r, b + 1, l + 1, R, B, W, dp);
    ans = ans % mod;
 
    // Memoize and return
    return dp[r, b, l] = ans;
}
 
// Driver code
static void Main()
{
    int R = 40, B = 10, W = 4;
 
    int[,,] dp = new int[RUNMAX, BALLMAX, WICKETMAX];
    for(int i = 0; i < RUNMAX;i++)
        for(int j = 0; j < BALLMAX; j++)
            for(int k = 0; k < WICKETMAX; k++)
    dp[i, j, k]=-1;
 
    Console.WriteLine(CountWays(0, 0, 0, R, B, W, dp));
}
}
 
// This code is contributed by mits


Javascript


输出:
653263