All problems

Problem 643

Out of Boundary Paths

Medium · 20 pts · Dynamic Programming

Problem

A ball sits in cell (startRow, startColumn) of an M × N grid. In one move it goes one cell up, down, left or right, and it may leave the grid. Count the move sequences of at most maxMove moves that end with the ball leaving the grid (a sequence stops as soon as the ball leaves). Print the count modulo 1000000007. Keep the number of ways to be in each cell after k moves; every move from a border cell across the edge adds to the answer. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).

Input

A single line with M, N, maxMove, startRow and startColumn (rows and columns counted from 0).

Output

Print the number of paths modulo 1000000007.

Constraints

1 <= M, N <= 50; 0 <= maxMove <= 50; 0 <= startRow < M; 0 <= startColumn < N

Example input

2 2 2 0 0

Example output

6

Explanation

In a 2 × 2 grid starting at (0, 0) with 2 moves, the ball can leave directly up or left (2 ways), or move to one of the two neighbours first and then leave in one of 2 directions (4 ways). That gives 6 paths.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Knight Dialer Next: Number of Dice Rolls With Target Sum