1
Farmer John's N () cows are standing in a line, with cow 1 at the front of the line and cow N at the back of the line. FJ's cows also come in many different species. He denotes each species with an integer from 1 to N. The i'th cow from the front of the line is of species ().
FJ is taking his cows to a checkup at a local bovine hospital. However, the bovine veterinarian is very picky and wants to perform a checkup on the i'th cow in the line, only if it is species ().
FJ is lazy and does not want to completely reorder his cows. He will perform the following operation exactly once.
- Select two integers and such that . Reverse the order of the cows that are between the -th cow and the -th cow in the line, inclusive.
FJ wants to measure how effective this approach is. Find the sum of the number of cows that are checked by the veterinarian over all possible operations.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains an integer .
The second line contains .
The third line contains .
OUTPUT FORMAT (print output to the terminal / stdout):
Output one line with the sum of the number of cows that are checked by the veterinarian over all possible operations.
SAMPLE INPUT:
3
1 3 2
3 2 1
SAMPLE OUTPUT:
3
SAMPLE INPUT:
3
1 2 3
1 2 3
SAMPLE OUTPUT:
12
SAMPLE INPUT:
7
1 3 2 2 1 3 2
3 2 2 1 2 3 1
SAMPLE OUTPUT:
60
SCORING:
- Input 4:
- Input 5:
- Inputs 6-9: are all generated uniformly at random in the range
- Inputs 10-15: are all generated uniformly at random in the range [1,2]
- Inputs 16-23: No additional constraints.
2
It is another cold and boring day on Farmer John's farm. To pass the time, Farmer John has invented a fun leisure activity involving performing operations on an integer array.
Farmer John has an array of () non-negative integers and an integer (). Then, FJ will ask Bessie for an integer . In one operation, FJ can pick an index i and subtract or add 1 to . FJ's boredom value is the minimum number of operations he must perform so that is divisible by M for all .
Among all possible , output FJ's minimum possible boredom value.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains (), the number of independent test cases to solve.
The first line of each test case contains and .
The second line of each test case contains ().
It is guaranteed that the sum of over all test cases does not exceed .
OUTPUT FORMAT (print output to the terminal / stdout):
For each test case, output an integer on a new line containing FJ's minimum possible boredom value among all possible values of .
SAMPLE INPUT:
2
5 9
15 12 18 3 8
3 69
1 988244353 998244853
SAMPLE OUTPUT:
10
21
In the first test case, one optimal choice of x is 3. FJ can perform 10 operations to make a=[12,12,21,3,12].
SCORING:
- Input 2: and
- Input 3:
- Inputs 4-5: .
- Inputs 6-16: No additional constraints.
3
Bessie has an () addition table where the integer in the cell at row r and column c is r+c, for all . For example, for N=3, the table would look like this:
2 3 4
3 4 5
4 5 6
Unfortunately, Elsie got ahold of the table and permuted it by performing the following three types of operations as many times as she wanted.
- Swap two rows
- Swap two columns
- Select two values a and b that are both present in the table, then simultaneously change every occurrence of a to b and every occurrence of b to a.
Elsie will always perform operations in increasing order of type; that is, she performs as many operations as she likes (possibly none) first of type 1, then of type 2, and finally of type 3.
Help Bessie recover a possible state of the table after Elsie finished applying all of her operations of types 1
and 2, but before applying any operations of type 3. There may be multiple possible answers, in which case you should output the lexicographically smallest one.
To compare two tables lexicographically, compare the first entries at which they differ, when reading both tables in the natural order (rows from top to bottom, left to right within a row).
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains .
The next lines each contain integers, representing Bessie's addition table after Elsie has permuted it.
OUTPUT FORMAT (print output to the terminal / stdout):
The lexicographically minimum possible state of the table after all operations of types 1 and 2, but before any operations of type 3. It is guaranteed that the answer exists.
SAMPLE INPUT:
3
3 4 2
5 2 3
6 3 5
SAMPLE OUTPUT:
4 2 3
5 3 4
6 4 5
Here is a possible sequence of operations Elsie might have performed.
2 3 4
3 4 5
4 5 6
-> (op 1: swap columns 2 and 3)
2 4 3
3 5 4
4 6 5
-> (op 1: swap columns 1 and 2)
4 2 3
5 3 4
6 4 5
-> (op 3: swap values 2 and 3)
4 3 2
5 2 4
6 4 5
-> (op 3: swap values 3 and 4)
3 4 2
5 2 3
6 3 5
Note: the following is also a possible state of the table after operations of types 1 and 2, but it is not the lexicographically smallest because the second entry of the first row is larger than in the correct answer.
4 6 5
3 5 4
2 4 3
SAMPLE INPUT:
6
8 10 5 6 7 4
12 11 10 4 8 2
5 4 6 7 9 8
10 2 4 8 5 12
6 8 7 9 3 5
4 12 8 5 6 10
SAMPLE OUTPUT:
7 5 8 9 10 6
4 2 5 6 7 3
8 6 9 10 11 7
5 3 6 7 8 4
9 7 10 11 12 8
6 4 7 8 9 5
SCORING:
- Inputs 4-5:
- Inputs 6-7:
- Inputs 8-11:
- Inputs 12-15: No additional constraints.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?