#P9743. 「KDOI-06-J」旅行

    ID: 10973 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>动态规划 DP2023洛谷原创O2优化前缀和洛谷月赛

「KDOI-06-J」旅行

Problem Description

Little C is traveling in Country C.

Country C has n×mn\times m cities, which can be viewed as an n×mn\times m grid. Define (i,j)(i,j) as the city in the ii-th row and the jj-th column of the grid.

There are 22 transportation systems in this country:

  • For all 1≤i<n,1≤j≤m1\leq i<n,1\leq j\leq m, there is a one-way railway built by Company L from (i,j)(i,j) to (i+1,j)(i+1,j).
  • For all 1≤i≤n,1≤j<m1\leq i\leq n,1\leq j<m, there is a one-way railway built by Company Z from (i,j)(i,j) to (i,j+1)(i,j+1).

In each city, there is a ticket office. At the ticket office in city (i,j)(i,j), you can buy one railway ticket of Company L for ai,ja_{i,j} yuan, and buy one railway ticket of Company Z for bi,jb_{i,j} yuan. When you have one railway ticket of a company, you can take any railway segment of that company and consume that ticket. Note that one railway ticket can be used once and only once.

Little C initially is in city (1,1)(1,1). He wants to travel in Country C, but he does not want to waste any money (that is, when his trip ends, he should not have any extra unused tickets in hand). For all 1≤x≤n,1≤y≤m1\leq x\leq n,1\leq y\leq m, find the number of ways for him to spend kk yuan and end the trip at city (x,y)(x,y), modulo 998 244 353998\ 244\ 353.

Two travel plans are different if and only if the cities Little C passes through are different, or the numbers of railway tickets of some company that he buys in some city are different.

Input Format

Read data from standard input.

The first line contains three positive integers n,m,kn,m,k, representing the grid size and the amount of money.

The next nn lines each contain mm positive integers; the jj-th integer in the ii-th line represents ai,ja_{i,j}.

The next nn lines each contain mm positive integers; the jj-th integer in the ii-th line represents bi,jb_{i,j}.

Output Format

Output to standard output.

Output a total of nn lines, each containing mm integers, representing the number of ways to end the trip at each point with exactly all the money spent, modulo 998 244 353998\ 244\ 353.

3 3 5
3 2 1
2 1 3
1 3 2
1 2 3
2 3 1
3 1 2
0 0 0
0 1 5
1 3 5

Hint

[Sample Explanation #1]

The plans to reach (3,1)(3,1) are:

  • Buy 11 Company L railway ticket at (1,1)(1,1); take Company L's railway to (2,1)(2,1); buy 11 Company L railway ticket at (2,1)(2,1); take Company L's railway to (3,1)(3,1).

The plans to reach (2,2)(2,2) are:

  • Buy 11 Company L railway ticket at (1,1)(1,1); take Company L's railway to (2,1)(2,1); buy 11 Company Z railway ticket at (2,1)(2,1); take Company Z's railway to (2,2)(2,2).

The plans to reach (3,2)(3,2) are:

  • Buy 11 Company Z railway ticket at (1,1)(1,1); take Company Z's railway to (1,2)(1,2); buy 22 Company L railway tickets at (1,2)(1,2); take Company L's railway to (2,2)(2,2); take Company L's railway to (3,2)(3,2).
  • Buy 11 Company L railway ticket and 11 Company Z railway ticket at (1,1)(1,1); take Company Z's railway to (1,2)(1,2); take Company L's railway to (2,2)(2,2); buy 11 Company L railway ticket at (2,2)(2,2); take Company L's railway to (3,2)(3,2).
  • Buy 11 Company L railway ticket and 11 Company Z railway ticket at (1,1)(1,1); take Company L's railway to (2,1)(2,1); take Company Z's railway to (2,2)(2,2); buy 11 Company L railway ticket at (2,2)(2,2); take Company L's railway to (3,2)(3,2).

The plans to reach (2,3)(2,3) are:

  • Buy 11 Company L railway ticket and 22 Company Z railway tickets at (1,1)(1,1). After that, there are 33 ways to take railways of the two companies from (1,1)(1,1) to (2,3)(2,3).
  • Buy 11 Company Z railway ticket at (1,1)(1,1); take Company Z's railway to (1,2)(1,2); buy 11 Company L railway ticket and 11 Company Z railway ticket at (1,2)(1,2). After that, there are 22 ways to take railways of the two companies from (1,2)(1,2) to (2,3)(2,3).

[Sample #2]

See travel/travel2.in and travel/travel2.ans in the contestant directory. This sample satisfies the constraints of test points 7∼87\sim 8.

[Sample #3]

See travel/travel3.in and travel/travel3.ans in the contestant directory. This sample satisfies the constraints of test point 1111.

[Constraints]

For all data, it is guaranteed that: 1≤n,m≤451\leq n,m\leq45, 1≤k,ai,j,bi,j≤901\leq k,a_{i,j},b_{i,j}\leq90.

Test Point ID n,mn,m kk ai,ja_{i,j} bi,jb_{i,j}
1∼31\sim3 ≤3\leq3 ≤5\leq5 =1=1 =1=1
4∼64\sim6 ≤10\leq10 =40=40
7∼87\sim8 ≤40\leq40 ≤30\leq30 =45=45
9∼109\sim10 ≤15\leq15 ≤15\leq15
1111 ≤30\leq30
1212 ≤20\leq20 ≤40\leq40
13∼1513\sim15 ≤25\leq25 ≤50\leq50
1616 ≤30\leq30 ≤60\leq60
1717 ≤35\leq35 ≤70\leq70
18∼1918\sim19 ≤40\leq40 ≤80\leq80
2020 ≤45\leq45 ≤90\leq90

Translated by ChatGPT 5