Counting Ways

4

2 votes
Dynamic Programming, Applications of Dynamic Programming, Algorithms
Problem

The universe is a 2D array of dimensions NM. You are standing in cell (1,1) at the start.
You can make a move in three different directions:

The biggest length of a move the man can make is K. If the current position of the man is (x,y), in a one-step he can go to:

  • (x+w,y) where wK and x+wN.
  • (x,y+w) where wK and y+wM.
  • (x+w,y+w) where wK and x+wN and y+wM.

In how many ways can you reach the cell (N,M)?

Since the solution can be very large, compute it modulo 109+7.

Input format

  • The first line contains Q denoting the number of test cases.
  • Each of the next Q lines contains three integers, NM and K.

Output format

For each test case, output on a new line the count of ways to reach the cell (N,M) modulo 109+7.

Constraints

1Q,N,M,K1000
The sum of NM over all test cases won't exceed 108.

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the first query paths will be

(1,1)(1,2)(2,2)(1,1)(2,1)(2,2)(1,1)(2,2)

Editor Image

?