Volleyball Match

First, after swapping values if needed, we will assume that a >= b (since it’s easier to work under this assumption). Our solution then proceeds as follows:

  1. We first consider a number of cases that correspond to invalid scores, where the number of possible games is therefore 0.
  2. Then we consider the case where team A wins with a scoreline of the form 25:b where $b < 24$.
    In this case, we get to a scoreline of 24:b, and then team A scores the final point to reach 25:b. There were a total of $24+b$ points scored before Team A scored the final (winning) point. There are $C_{24+b,24}$ ways in which those $24+b$ points could have been scored, where $C_{x,y}$ is referring to the binomial coefficient ${x} \choose {y}$.
  3. There is one final case to consider, when team A wins with a scoreline of a:b with $a > 25$ where (in order to be a valid score) we must have $a-b = 2$. In this case, the score must have been tied 24:24 in order for the final scoreline to be reached. There are $C_{48,24}$ ways these first 48 points could have been scored. For each additional tie that was reached (like 25:25 or 26:26) it means that team A scored one point and Team B scored one point in some order. Then team A finally scored two points in a row to reach the winning scoreline. Hence, there were $b-24$ additional tied situations, giving rise to $2^{b-24}$ ways to achieve these additional tied situations. Overall, this means there are $C_{48,24}\cdot 2^{b-24}$ valid ways to reach the final scoreline in this case.

Putting this all together, we can code these cases as follows. Note we are using the math.comb method to compute the binomial coefficients.

import math

def n_games(a, b):
    if a < b:
        a, b = b, a

    # 3 cases of invalid scores
    if a < 25:
        return 0
    if a == 25 and b > 23:
        return 0
    if a > 25 and a - b != 2:
        return 0
    
    # base case, team A wins 25:b with b <= 23
    if a == 25:
        return math.comb(24 + b, 24)
        
    # last case, team A wins a:b with a > 25 and a-b == 2
    return math.comb(48, 24) * 2**(b - 24)

if __name__ == '__main__':
    import sys
    a = int(sys.stdin.readline())
    b = int(sys.stdin.readline())
    print(n_games(a, b) % 1_000_000_007)

Check the HackerRank editorial on this problem for another explanation for how one might compute intermediate scores.