#P6297. 替换
替换
Problem Description
Daniel13265 has a necklace made of various beautiful shells. For various reasons, this necklace is not a ring. Instead, the shells are simply threaded on an ordinary string. Each shell on the necklace has a beauty value . Shells of the same type have the same beauty value, while shells of different types have different beauty values.
Daniel13265 defines that the segment of shells from the -th to the -th is symmetric if and only if
Daniel13265 often takes out a segment of shells. If the segment is symmetric, he will be very happy. If the segment is not symmetric, he will replace some shells in it with new ones so that the segment becomes symmetric. One replacement can arbitrarily change the beauty value of the shell at any one position. However, too many replacements will make the segment lose its original look, so Daniel13265 will perform at most replacements. If a segment can become symmetric after at most replacements, then Daniel13265 calls this segment “viewable”.
Daniel13265 simply defines the “viewing index” of the “viewable” shell segment from the -th to the -th as
where denotes the original beauty value of the -th shell.
Now he is curious about the maximum “viewing index” among all “viewable” shell segments in this necklace. Since this value may be very large, you only need to output the result modulo .
Input Format
The input consists of lines.
The first line contains two positive integers , denoting the number of shells on the necklace and the maximum number of replacements Daniel13265 may perform on a segment.
The second line contains positive integers separated by single spaces. The -th number denotes the beauty value of the -th shell on the necklace.
Output Format
Output one line containing one non-negative integer, representing the maximum “viewing index” among all “viewable” shell segments modulo .
7 1
1 2 4 2 3 3 4
288
6 1
3 1 2 250000002 1 2
1
Hint
Sample Explanation #1
The “viewable” shell segments are $[1],[2],[3],[4],[1,2],[2,3],[2,4],[3,3],[3,4],[4,2],[1,2,4],[2,3,3],[2,4,2],[3,3,4],[4,2,3],[2,3,3,4],[4,2,3,3,4]$. Among them, the segment with the maximum “viewing index” is .
Sample Explanation #2
Among the “viewable” shell segments, the one with the maximum “viewing index” is . Its value is , and after taking modulo , the result is .
Constraints
This problem uses bundled testdata. For each subtask, you will get the full score of that subtask only if you pass all test cases in it.
| Subtask ID | Score | ||
|---|---|---|---|
For of the testdata, , , and .
Translated by ChatGPT 5