Journey
Time limit2sMemory limit512 MB
Count binary strings of length N with no run of equal symbols longer than K, modulo 1e9+7.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Combinatorics, Math, Prefix sum
- Solved
- No attempts yet
Problem
One day, Homer was bored at home and decided to set out on a journey to discover the lands of Springfield. The lands of Springfield form an infinite grid. Homer's house is at cell , and his journey consists of steps, where each step is either a move one cell right or a move one cell down.
Already bored, Homer did not want his journey to be boring too. He decided he would not move in the same direction for more than consecutive steps. So a journey is interesting if, for every consecutive steps, Homer has moved in both directions.

Figure 1: Example with and (first test case)
Given and , count the interesting journeys Homer can make. Two journeys are different if, for some , the -th step of the first journey differs from the -th step of the second. The count can be large, so print it modulo .
Input
Your program is tested on one or more test cases. The first line of input holds a single integer , the number of test cases (), followed by test cases.
Each test case is given on a single line holding two integers separated by a single space. The first integer is the number of steps in Homer's journey, , and the second integer is the maximum number of consecutive steps Homer can take while moving in the same direction, where and .
Output
For each test case, print on a single line the number of different journeys Homer can make, modulo .