Compositions Missing a Sequence

Count the ordered compositions of n that use no part from the arithmetic progression starting at m with step k.

Easy3Dynamic programmingCombinatoricsInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

A composition of an integer nn is an ordered list of positive integers whose sum is nn. Two compositions that use the same numbers in a different order count as different, which is what separates compositions from partitions. Written out in full, the compositions of the first few integers are:

1: {1}
2: {1+1, 2}
3: {1+1+1, 1+2, 2+1, 3}
4: {1+1+1+1, 1+1+2, 1+2+1, 1+3, 2+1+1, 2+2, 3+1, 4}

For 3, the compositions 1+2 and 2+1 are counted separately. As you may have guessed, an integer nn has 2n12^{n-1} compositions.

This problem puts a condition on the numbers a composition uses. A composition misses a set SS when no number in the composition belongs to SS. The compositions of the first few integers that miss the set of even integers are:

1: {1}
2: {1+1}
3: {1+1+1, 3}
4: {1+1+1+1, 1+3, 3+1}

No odd integer has a composition that misses the set of odd integers, and every composition of an even integer nn into even numbers alone pairs off with a composition of n/2n/2 multiplied by 2.

Write a program that counts the compositions of nn which miss the arithmetic sequence {m+iki=0,1,2,}\{m + ik \mid i = 0, 1, 2, \dots\}.

Input

The first line contains one integer PP, the number of data sets (1P100001 \le P \le 10000). Each data set is processed the same way and independently of the others.

Each of the next PP lines holds one data set. A line contains the data set number KK, followed by the three space separated integers nn, mm and kk (1n301 \le n \le 30, 0m<k<300 \le m < k < 30).

Output

Print one line for each data set. The line contains the data set number KK, a single space, then the number of compositions of nn which miss the given sequence.