Partitioning Balls into Buckets

Count the ways to split N balls into buckets whose sizes are non-decreasing, span at most 2, and start with a value divisible by D.

Medium6Dynamic programmingMathCombinatoricsImplementationNo attempts yetTime limit5sMemory limit512 MB

Problem

Shekhu has NN balls. She wants to distribute them among one or more buckets so that all of the following conditions hold.

  1. The numbers of balls in the buckets are non-decreasing when read from left to right.
  2. The leftmost bucket is not empty, and the number of balls in it is divisible by DD.
  3. The difference in the number of balls between any two buckets, not only between adjacent ones, is at most 2.

How many ways are there? Two ways count as different when the lists of bucket sizes, read from left to right, differ.

Input

The first line contains the number of test cases TT.

Each of the next TT lines contains one test case: two integers NN and DD separated by a space.

Limits

  • 1T1001 \le T \le 100
  • 1D1001 \le D \le 100
  • 1N20001 \le N \le 2000

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the number of ways.

Hint

For N=7N = 7 and D=1D = 1 the 10 possible distributions are:

  • 1 1 1 1 1 1 1
  • 1 1 1 1 1 2
  • 1 1 1 1 3
  • 1 1 1 2 2
  • 1 2 2 2
  • 1 1 2 3
  • 1 3 3
  • 2 2 3
  • 3 4
  • 7

1 2 4 does not count, because the difference between 1 and 4 is more than 2.

For N=7N = 7 and D=2D = 2 the only possible distribution is 2 2 3. The distribution 3 4 is excluded because its first bucket holds 3 balls, which is not divisible by 2.

For N=2N = 2 and D=4D = 4 no distribution exists.