Problem Statement
Bob is playing with his ball destroyer robot. Initially, Bob had r red balls, g green balls and b blue balls. The robot repeated the following 3-step program until there were no balls left:
- If there is at least one red ball available, destroy one red ball.
- If there is at least one green ball available, destroy one green ball.
- If there is at least one blue ball available, destroy one blue ball.
Definition
- Class:
- AlternateColors2
- Method:
- countWays
- Parameters:
- int, int
- Returns:
- long
- Method signature:
- long countWays(int n, int k)
- (be sure your method is public)
Notes
- It follows from the constraints that the return value will always fit into a long.
Constraints
- n will be between 1 and 100000, inclusive.
- k will be between 1 and n, inclusive.
Examples
1
1
Returns: 1
There was only one ball. This ball was necessarily the first ball destroyed. Therefore, it had to be red.3
3
Returns: 3
There are three cases in which the third ball to be destroyed is red: r = 3, b = 0, g = 0. r = 2, b = 1, g = 0. r = 2, b = 0, g = 1.6
4
Returns: 9
6
1
Returns: 21
1000
2
Returns: 1
In order for the second destroyed ball to be red, there would have to be zero balls of the other colors.100000
100000
Returns: 1666700000