#P5167. xtq 的神笔
xtq 的神笔
Description
The shape of each painting can be abstracted as cells in a row, where the -th cell has a weight .
xtq has enough paintbrushes of different colors. Each time he uses one brush, he can paint a continuous segment on the cells with length at least , and then switch to another brush and continue painting starting from the next cell, where .
To test xtq’s painting skills, the art teacher sets some challenges.
He may start painting from any cell between and (inclusive) and paint all the way to cell . If he starts from cell , he gains an initial score of .
Suppose xtq uses the same brush to paint continuously from cell to cell . Then he gains a score of $(a_i \mathbin{\mathrm{or}} a_{i+1} \mathbin{\mathrm{or}} a_{i+2} \mathbin{\mathrm{or}} \cdots \mathbin{\mathrm{or}} a_j) + (a_i \mathbin{\mathrm{and}} a_{i+1} \mathbin{\mathrm{and}} a_{i+2} \mathbin{\mathrm{and}} \cdots \mathbin{\mathrm{and}} a_j) + \gcd(a_i, a_{i+1}, a_{i+2}, \ldots, a_j)$, where denotes the greatest common divisor.
Now, xtq wants to find a plan for using the brushes so that for each painting he needs to copy, his total score is as large as possible.
Input Format
The first line contains a positive integer , representing the number of paintings.
For each painting:
The first line contains two positive integers .
The second line contains positive integers .
The third line contains integers .
Output Format
For each painting, output one integer, representing the maximum score xtq can obtain after completing this painting.
1
6 2
3 1 4 5 6 2
6 -2
31
Hint
Sample explanation:
xtq can start from and gain an initial score of points. The first segment is , gaining points; the second segment is , gaining points; the third segment is , gaining points. In total, points.
Constraints:
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, , , , .
The testdata has multiple levels, so it should not be too strict on constant factors.
Translated by ChatGPT 5
京公网安备 11011102002149号