#P10236. [yLCPC2024] D. 排卡
[yLCPC2024] D. 排卡
Background
After going through countless hardships, Fusu finally arrived at the arcade. But she soon found that there was a huge line (not the NOi Qualifier’s Team B).
When there are many people, maimai players often decide who plays next by “queueing cards”. Because there are simply too many people, while queueing the cards, they noticed the ID numbers on the cards.
They asked Fusu a question. Can you solve it?
Problem Description
Fusu has a deque . This deque is similar to the queue concept in computer science, but the difference is that it can read and pop elements from the front, and also read and pop elements from the back, so it is called a “double-ended queue (deque)”.
There are numbers in this deque. Fusu will construct a sequence of length through operations. In the -th operation (), she can perform one of the following two processes:
- Let be the front element of , and pop one element from the front of .
- Let be the back element of , and pop one element from the back of .
We define the score of a pair as:
That is, the result of to the power of modulo . In particular, in this problem we define .
Now, Fusu wants to construct the sequence using the best strategy, to maximize the value of the following expression:
$$\sum_{i = 1}^{n - 1} \mathrm{score}(b_i, b_{i + 1})$$That is, the sum of the scores of all adjacent pairs in , computed in the original order.
Note that we only take modulo when computing the score of a pair. When computing the sum, we do not take the sum modulo anything.
Input Format
This problem contains multiple test cases in a single test file. The first line is a positive integer , representing the number of test cases. For each test case:
The first line is an integer (), representing the length of the sequence.
The second line contains integers (), representing the values in deque from front to back.
It is guaranteed that the sum of over all test cases in a single test file does not exceed .
Output Format
For each test case, output one line with one integer representing the answer.
2
5
5 3 1 4 2
6
6 5 1 4 2 3
1168
15655
Hint
Translated by ChatGPT 5