Count RGB triples with each component in 0 to K whose largest and smallest values differ by at most V.
Easy2Brute forceMathNo attempts yetTime limit5sMemory limit512 MBA color is a triple of component values giving the red, green, and blue levels. Each component is an integer from 0 to K, inclusive. For example, in the color system with K=3, the triples (0,2,3) and (0,3,2) are two of the distinct colors.
A color is bland when every pair of its component values differs by at most V. In a system with K=2 and V=1, the color (2,1,1) is bland: red and green differ by 1, red and blue differ by 1, green and blue differ by 0, and no difference exceeds 1. The color (2,1,0) is not bland, because red and blue differ by more than 1.
Turner paints gloomy landscapes and wants 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 space separated integers K and V.
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 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), and all eight are bland, so the answer is 8.
With K=1 and V=0 the same eight colors exist, but only (0,0,0) and (1,1,1) are bland, so the answer is 2.