Count RGB triples with each component between 0 and K whose largest and smallest values differ by at most V.
Medium6CombinatoricsMathNo attempts yetTime limit5sMemory limit512 MBA color is a triple of component values giving the levels of red, green, and blue. Each component is an integer from 0 to K, inclusive. For example, in the color system with K=3, the colors (0,2,3) and (0,3,2) are two of the distinct colors available.
A color is bland if every pair of its component values differs by at most V. For example, in a system with K=2 and V=1, the color (2,1,1) is bland: the red and green components differ by 1, the red and blue components differ by 1, the green and blue components differ by 0, and none of those differences exceeds 1. The color (2,1,0) is not bland, because its red and blue components differ by more than 1.
Mr. Turner paints gloomy landscapes and wants to design a color system with many bland colors in it. Given K and V, count the distinct bland colors.
The first line contains the number of test cases T. Each of the next T lines contains two integers K and V separated by a space.
For each test case, print one line of the form Case #x: y, where x is the test case number starting from 1 and y is the number of distinct bland colors.
With K=1 and V=1 there are eight possible colors: (0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0), and (1,1,1). All eight are bland, because no two components differ by more than 1.
With K=1 and V=0 the same eight colors are possible, but only (0,0,0) and (1,1,1) are bland.