#P72. [CSP-S 2019 Day 2] Brute Force Optimization
[CSP-S 2019 Day 2] Brute Force Optimization
Person in Charge
Attention
The data range of this problem is not exactly the same as the original problem.
Problem Description
In 2048, in the examination room of the 30th CSP Certification, Xiao Ming, a contestant, opened the first problem. This problem has groups of sample data, numbered from to , and the scale of the -th group of data is .
Xiao Ming designed a brute-force program for this problem. For a group of data with scale , the running time of the program is . However, after the program runs a group of data with scale , it will run incorrectly on any group of data with a scale smaller than . The in the sample are not necessarily increasing, but Xiao Ming wants to run the sample correctly without modifying the program, so he decides to use a very primitive solution: divide all data into several consecutive data segments (the numbers of data in a segment are consecutive), then merge the data in the same segment into new data whose scale is equal to the sum of the scales of the original data in the segment, and Xiao Ming will make the scales of the new data non-decreasing.
That is to say, Xiao Ming needs to find some dividing points such that:
$$\sum_{i=1}^{k_1} a_i \le \sum_{i=k_1+1}^{k_2} a_i \le \cdots \le \sum_{i=k_p+1}^{n} a_i$$Note that can be (in this case ), which means Xiao Ming can merge all data and run them together.
Xiao Ming hopes that under the condition that the program runs the sample correctly, the running time is as small as possible, that is, to minimize:
$$(\sum_{i=1}^{k_1} a_i)^2 + (\sum_{i=k_1+1}^{k_2} a_i)^2 + \cdots + (\sum_{i=k_p+1}^{n} a_i)^2$$Xiao Ming thinks this problem is very interesting and asks you for help: given and , please find the minimum running time of Xiao Ming's program under the optimal partitioning scheme.
Input Format
Due to the large data range of this problem, the of some test points will be generated within the program.
The first line contains two integers . has the meaning described in the problem, and indicates the input method:
- If , the of this test point are given directly. Next in the input file: the second line contains integers separated by spaces, representing the scale of each group of data.
- If , the of this test point will be generated specially, and the generation method is described later. Next in the input file: the second line contains six integers separated by spaces. The next lines each contain three positive integers separated by spaces (the -th line, ).
For test points with , the generation method of is as follows:
Given integers , and triples .
It is guaranteed that . If , then $\forall 3 \le i \le n, b_i = (x \times b_{i−1} + y \times b_{i−2} + z) \mod 2^{30}$.
It is guaranteed that and . Let , then also satisfies , .
For all , if the subscript satisfies , then:
$$a_i = \left(b_i \mod \left( r_j − l_j + 1 \right) \right) + l_j$$The above data generation method is only to reduce the size of the input file, and the standard algorithm does not depend on this generation method.
Output Format
Output a single integer on one line, representing the answer.
Samples
5 0
5 1 7 9 9
247
10 0
5 6 7 7 4 6 2 13 19 9
1256
Sample 1 Explanation
The optimal partitioning scheme is . It is a valid scheme because .
The answer is .
Although the partitioning scheme has a smaller running time than 247, it is not a valid scheme because .
Although the partitioning scheme is valid, its corresponding running time is 251, which is larger than 247.
Sample 2 Explanation
The optimal partitioning scheme is $\{5\}, \{6\}, \{7\}, \{7\}, \{4,6,2\}, \{13\}, \{19,9\}$.
Sample 3
See partition/partition3.in and partition/partition3.ans in the contestant's directory.
Data Range
| Test Case ID | |||
|---|---|---|---|
For all test points with , it is guaranteed that the final output answer .
For of the data: , , , , , .