#P5665. [CSP-S 2019] 划分
[CSP-S 2019] 划分
Description
In 2048, at the exam site of the 30th CSP certification, contestant Xiaoming opened the first problem. The sample of this problem has groups of testdata, numbered from , and the size of the -th testdata is .
Xiaoming designed a brute-force program for this problem. For a piece of testdata of size , the running time of the program is . However, after the program finishes running on a piece of testdata of size , it will produce wrong results on any piece of testdata whose size is less than . The in the sample are not necessarily increasing, but Xiaoming wants to run the sample correctly without modifying the program. So he decided to use a very primitive solution: divide all testdata into several segments, where the indices within each segment are continuous. Then, merge the testdata within the same segment into new testdata whose size equals the sum of sizes of the original testdata in that segment. Xiaoming will make the sizes of the new testdata non-decreasing.
That is, Xiaoming needs to find some split points such that
$$\sum_{i=1}^{k_1} a_i \leq \sum_{i=k_1+1}^{k_2} a_i \leq \cdots \leq \sum_{i=k_p+1}^{n} a_i$$Note that can be , and in this case , meaning Xiaoming can merge all testdata together and run it once.
Xiaoming hopes that, while running the sample correctly, the running time can be as small as possible, i.e., minimize:
$$\left(\sum_{i=1}^{k_1} a_i\right)^2 + \left(\sum_{i=k_1+1}^{k_2} a_i\right)^2 + \cdots + \left(\sum_{i=k_p+1}^{n} a_i\right)^2$$Xiaoming finds this problem very interesting and asks you for help: given and , please compute the minimum running time of Xiaoming's program under the optimal partition.
Input Format
Because the Constraints of this problem are large, for some test points, will be generated inside the program.
The first line contains two integers . The meaning of is described above, and indicates the input method.
- If , then for this test point are given directly. The input then contains: on the second line, integers separated by spaces, representing the size of each group of testdata.
- If , then for this test point will be generated specially; the generation method is given later. The input then contains: on the second line, six integers separated by spaces. In the next lines, the -th line contains three positive integers separated by spaces.
For test points with , are generated as follows:
Given integers , and triples .
It is guaranteed that . If , then for all , $b_i = (x \times b_{i−1} + y \times b_{i−2} + z) \bmod 2^{30}$.
It is guaranteed that and . Let , then also satisfies: for all , .
For all , if the index satisfies , then
$$a_i = \left(b_i \bmod \left( r_j − l_j + 1 \right) \right) + l_j$$The above data generation method is only to reduce the input size; the standard algorithm does not depend on this generation method.
Output Format
Output one integer in one line, representing the answer.
5 0
5 1 7 9 9
247
10 0
5 6 7 7 4 6 2 13 19 9
1256
10000000 1
123 456 789 12345 6789 3
2000000 123456789 987654321
7000000 234567891 876543219
10000000 456789123 567891234
4972194419293431240859891640
Hint
[Sample 1 Explanation]
The optimal partition is . Since , this partition is valid.
The answer is .
Although the partition has a running time smaller than , it is not valid because .
Although the partition is valid, its running time is , which is larger than .
[Sample 2 Explanation]
The optimal partition is $\{5\}, \{6\}, \{7\}, \{7\}, \{4,6,2\}, \{13\}, \{19,9\}$.
[Constraints]
::cute-table[]{tuack} | Test Point ID | | | | | :--: | :-: | :-: | :-: | | | | | | | | | | ^ | | | | | ^ | | | | | ^ | | | | | ^ | | | | | |
For all test points with , it is guaranteed that the final output answer .
All test points satisfy: , , , , , 。
Translated by ChatGPT 5
京公网安备 11011102002149号