CF1695C Solution
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
Problem links: (CF, Luogu) | I strongly recommend reading it on the blog.
Problem Statement
Given an () grid, the value of each cell is either or . Determine whether there is a path from to such that the sum of the values of the cells traversed by the path is . On a path, you can only move from to or (move right or down).
Approach
Seeing the constraint (), we know that brute-force search certainly will not work (do not learn from me), so we need to think of another method.
First, if the path passes through an odd number of cells—in other words, if is odd—then such a path certainly does not exist (the numbers of and cells traversed cannot be equal).
Directly determining whether a grid satisfies the requirement is too troublesome. We can instead consider whether, given any path, we can make some changes according to the path’s value (that is, the sum of the cells it passes through) and finally make the path’s value equal to .
The figure below shows one change to a path (only one cell differs before and after the change). This ultimately changes the value of the path.

In one change, the path’s value changes by , , or . Then, if the path’s initial value is even, can we transform the path in this manner into one whose value is …?
Obviously not. It does not work if the entire grid consists only of or only of , so we still need to improve the method.
First, we must ensure that the grid does not contain only paths with especially extreme values. If only paths with especially extreme values exist, no matter how we change them, we cannot produce a path with value .
Therefore, we need to find the path with the maximum value and the path with the minimum value.
Let the value of the maximum-value path be and the value of the minimum-value path be .
Then, if:
we can certainly use such changes to turn an even-valued path into a path whose value is .
Alternatively, we can understand it this way: if the condition above holds, we can gradually transform the minimum-value path into the maximum-value path. During this process, there must be a path whose value equals .
As for finding the maximum- and minimum-sum paths in such a grid, that is a very standard problem (using DP), so I will not elaborate here. If you are unfamiliar with it, see Luogu P1004.
Code
1 |
|





![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)