Bus Stops (Large)

Count schedules assigning every stop to one of K left-to-right buses whose consecutive stops are at most P apart, modulo 30031.

Hard8Dynamic programmingBit manipulationMatrixNo attempts yetTime limit5sMemory limit512 MB

Problem

The First City of Mars has NN bus stops standing on one straight line of length N1N-1 km. The mayor likes to keep things simple, so the stops are numbered 1 to NN from the left, and two adjacent stops are exactly 1 km apart.

The city also runs KK buses. The mayor has to plan the bus schedule and wants to know how many ways there are to do it. That number can get very large. Luckily there are a few constraints:

  • At the start of the day every bus sits on one of the first KK stops, one bus per stop.
  • A bus only moves from left to right. Stop 1 is the leftmost stop.
  • At the end of the day every bus must sit on one of the last KK stops, one bus per stop.
  • Exactly one bus stops at each bus stop.
  • For one bus, the distance between two stops it makes in a row is at most PP km.

Count the schedules for the mayor. To avoid giving him very bad news (a lot of schedules), print the real number modulo 30031.

Input

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

Limits:

  • 1<T301 < T \le 30
  • 1<P101 < P \le 10
  • K<NK < N
  • 1<KP1 < K \le P
  • 1<N<1091 < N < 10^9

Output

For each case print one line in the format Case #t: x, where tt is the case number starting from 1 and xx is the number of schedules modulo 30031.

Hint

Name the buses A, B, C and so on, so that A starts on stop 1, B on stop 2, and so on.

For N=10N = 10, K=3K = 3, P=3P = 3 there is exactly one schedule. A stops at 1, 4, 7, 10. B stops at 2, 5, 8. C stops at 3, 6, 9.

For N=5N = 5, K=2K = 2, P=3P = 3 there are three schedules:

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