#FT0000096. Nhảy ô của đàn bò (Cow Hopscotch)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Nhảy ô của đàn bò (Cow Hopscotch)
Cow Hopscotch
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
Cow Hopscotch takes place on a grid of rows and columns. Cell has an integer label between and . A cow starts at the top-left cell and wants to reach the bottom-right cell .
From cell , the cow may jump directly to cell if all three conditions hold:
- The destination is on a strictly lower row: .
- The destination is in a strictly later column: .
- The two cells have different labels: .
A jump may skip any number of rows or columns; adjacent cells are not required. Two jump sequences are distinct if they visit different sequences of cells.
Count the ways to reach , modulo .
Input
The first line contains integers . Each of the next lines contains exactly integers; the -th value on the -th grid line is .
Output
Print one integer between and : the number of valid jump sequences modulo .
Subtasks
- Subtask 1 (20 points): ; ; for every cell.
- Subtask 2 (30 points): ; ; for every cell.
- Subtask 3 (50 points): ; ; for every cell.
Examples
Example 1
Input
4 4 4
1 1 1 1
1 3 2 1
1 2 4 1
1 1 1 1
Output
5
Explanation
The start and destination both have label , so a direct jump is invalid. The reachable intermediate cells include with label , with label , with label , and with label . There are five valid complete jump sequences: via , via , via , via , and via followed by . Therefore, the output is .
Example 2
Input
2 2 2
1 1
1 2
Output
1
Explanation
The direct jump from to moves strictly down and right and changes the label from to . It is the only possible path to the destination, giving answer .