Partitioning Number (Large)

Count non-decreasing partitions of N whose first part is divisible by D and whose parts span a range of at most 2.

Medium6Dynamic programmingCombinatoricsMathImplementationNo 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. Reading the buckets from left to right, the numbers of balls are in non-decreasing order.
  2. The leftmost bucket is not empty, and the number of balls in it is divisible by DD.
  3. For any two buckets, not only for two adjacent ones, the difference between their numbers of balls is at most 2.

In how many different ways can Shekhu do this? Two ways are different if the lists of ball counts, read from left to right, differ.

Input

The first line contains the number of test cases TT.

Each of the next TT lines contains two integers NN and DD.

Limits

  • 1T1001 \le T \le 100
  • 1D1001 \le D \le 100
  • 1N1051 \le N \le 10^5

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. The answer can exceed the range of a 32-bit integer.

Note

In the first test case of example 1 (N=7N = 7, 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 is not a valid distribution, because the difference between 1 and 4 is greater than 2.

In the second test case of example 1 (N=7N = 7, D=2D = 2) the only possible distribution is 2 2 3. 3 4 fails because its first term is not divisible by 2.

In the third test case of example 1 (N=2N = 2, D=4D = 4) no distribution exists.