Given three integers R, B, and W which denote the number of runs, balls, and wickets. One can score 0, 1, 2, 3, 4, 6, or a wicket in a single ball in a cricket match. The task is to count the number of ways in which a team can score exactly R runs in exactly B balls with at-most W wickets. Since the number of ways will be large, print the answer modulo 1000000007.Â
Examples:Â
Â
Input: R = 4, B = 2, W = 2Â
Output: 7Â
The 7 ways are:Â
0, 4Â
4, 0Â
Wicket, 4Â
4, WicketÂ
1, 3Â
3, 1Â
2, 2Â
Input: R = 40, B = 10, W = 4Â
Output: 653263Â
Â
Â
Approach: The problem can be solved using Dynamic Programming and Combinatorics. The recurrence will have 6 states, we initially start with runs = 0, balls = 0 and wickets = 0. The states will be:Â
Â
- If a team scores 1 run off a ball then runs = runs + 1 and balls = balls + 1.
- If a team scores 2 runs off a ball then runs = runs + 2 and balls = balls + 1.
- If a team scores 3 runs off a ball then runs = runs + 3 and balls = balls + 1.
- If a team scores 4 runs off a ball then runs = runs + 4 and balls = balls + 1.
- If a team scores 6 runs off a ball then runs = runs + 6 and balls = balls + 1.
- If a team scores no run off a ball then runs = runs and balls = balls + 1.
- If a team loses 1 wicket off a ball then runs = runs and balls = balls + 1 and wickets = wickets + 1.
The DP will consist of three stages, with the run state being a maximum of 6 * Balls, since it is the maximum possible. Hence dp[i][j][k] denotes the number of ways in which i runs can be scored in exactly j balls with losing k wickets.Â
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>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 wicketsint 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 codeint 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 wicketsstatic 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 codepublic 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 approachmod = 1000000007RUNMAX = 300BALLMAX = 50WICKETMAX = 10Â
# Function to return the number of ways# to score R runs in B balls with# at most W wicketsdef 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 approachusing 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 wicketsstatic 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 codestatic 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
<script>// javascript implementation of the approach    var mod = 1000000007;    var RUNMAX = 300;    var BALLMAX = 50;    var WICKETMAX = 10;Â
    // Function to return the number of ways    // to score R runs in B balls with    // at most W wickets    function 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 && 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];Â
        var 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           var R = 40, B = 10, W = 4;Â
        var dp = Array(RUNMAX).fill().map(()=>Array(BALLMAX).fill().map(()=>Array(WICKETMAX).fill(0)));        for (i = 0; i < RUNMAX; i++)            for (j = 0; j < BALLMAX; j++)                for (k = 0; k < WICKETMAX; k++)                    dp[i][j][k] = -1;Â
        document.write(CountWays(0, 0, 0, R, B, W, dp));Â
// This code is contributed by umadevi9616</script> |
653263
Â
Time Complexity: O(r*b*w), as we are using recursion with memorization, so we will avoid redundant function calls and the complexity will be O(r*b*w)
Auxiliary Space: O(300*50*10), as we are using extra space for the dp matrix.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
