Bus Stops (Small)

Count the ways K buses starting at the first K stops can cover all N stops and end at the last K, with gaps of at most P.

Hard8Dynamic programmingBit manipulationCombinatoricsNo attempts yetTime limit5sMemory limit512 MB

Problem

The First City of Mars has NN bus stops, all placed on one straight road of length N1N-1 km. The mayor likes simple things, so he numbered the stops from 1 to NN starting at the left end and put adjacent stops exactly 1 km apart.

The city has KK buses. The mayor wants to know how many ways there are to plan one day of service. A plan has to satisfy all of the following conditions.

  • At the beginning of the day the KK buses stand at the first KK stops, one bus per stop.
  • Buses only move to the right. Stop 1 is the leftmost stop.
  • At the end of the day the KK buses must stand at the last KK stops, one bus per stop.
  • Exactly one bus stops at every bus stop.
  • For a single bus, the distance between two consecutive stops it serves is at most PP km.

The route of a bus is the list of stops it serves, ordered by increasing stop number. Two plans are different if some stop is served by a different bus. The number of plans can be very large, so print it modulo 30031.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains three integers NN, KK and PP separated by one space.

Limits

  • 1<T301 < T \le 30
  • 1<P101 < P \le 10
  • 1<KP1 < K \le P
  • K<NK < N
  • 1<N<10001 < N < 1000

Output

For each test case, print the number of plans modulo 30031 on its own line, in the format Case #t: X, where tt is the test case number starting from 1 and X is the remainder.

Hint

For N=10N = 10, K=3K = 3, P=3P = 3 there is only one plan. Name the buses A, B and C. Then A serves stops 1, 4, 7, 10, bus B serves 2, 5, 8, and bus C serves 3, 6, 9.

For N=5N = 5, K=2K = 2, P=3P = 3 there are three plans.

  • A serves 1, 3, 5 and B serves 2, 4
  • A serves 1, 3, 4 and B serves 2, 5
  • A serves 1, 4 and B serves 2, 3, 5