#P4105. [HEOI2014] 南园满地堆轻絮
[HEOI2014] 南园满地堆轻絮
Description
Xiao Z is a poetry enthusiast in the ZRP (Zombies’ Republic of Poetry). Recently, he has been studying the prosody of classical poetry.
In the past, poems were meant to be sung to melodies. For example, consider the following tune of “Bodhisattva Man.” When sung, the corresponding notes are:
南 园 满 地 堆 轻 絮, 愁 闻 一 霎 清 明 雨
1 1 5 5 6 6 5 4 4 3 3 2 2 1
Therefore, one can see that the sequence of notes 1 1 5 5 6 6 5 4 4 3 3 2 2 1 becomes the key to studying prosody.
From many historical sources, Xiao Z found that the pitch of an ancient tune is non-decreasing. He wants to know, for a given piece, how to raise or lower its notes to make the sequence non-decreasing, while making the largest adjustment among all notes as small as possible. That is, if a piece with notes is viewed as a positive integer sequence , the goal is to find another positive integer sequence such that for every we have , and $\texttt{Ans} = \max\{\lvert A_j - B_j \rvert, 1 \leq j \leq n\}$ is minimized.
Xiao Z quickly figured out a method, but since he is busy writing poetry, the task is handed over to you.
Input Format
Since the testdata size may be large, the data are generated as follows.
Each test consists of numbers: , meaning there are notes and the first note is .
The generation rules are: define $F(x) = S_a \times x^3 + S_b \times x^2 + S_c \times x + S_d$; then the recurrence is $A_i = \bigl( F(A_{i-1}) + F(A_{i-2}) \bigr) \bmod \texttt{Mod}$, with . Since intermediate values may be very large, at each step and for every number in , take modulo .
Output Format
Output one line containing a positive integer .
3 815 6901 3839 178 199 10007
1334
Hint
Constraints
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, , $S_a, S_b, S_c, S_d, A_1 \leq \min\{10^4, \texttt{Mod}\}$, , and all input numbers are positive integers.
Friendly hint
In the sample, the generated sequence is . If you change to and also change to , the cost is .
Translated by ChatGPT 5
京公网安备 11011102002149号