| ||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:|
Bob forgot how many balls of each color he initially had. He only remembers that there were n balls in total and that the k-th (1-based index) ball that was destroyed was red. Return the total number of different initial settings that match that description. Formally, return the number of different tuples (r, g, b) such that r + g + b = n and the k-th ball that was destroyed was red.
- 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.
|Method signature:||long countWays(int n, int k)|
|(be sure your method is public)|
|-||It follows from the constraints that the return value will always fit into a long.|
|-||n will be between 1 and 100000, inclusive.|
|-||k will be between 1 and n, inclusive.|
|There was only one ball. This ball was necessarily the first ball destroyed. Therefore, it had to be red.|
|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.
|In order for the second destroyed ball to be red, there would have to be zero balls of the other colors.|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.