#P2671. [NOIP 2015 普及组] 求和
[NOIP 2015 普及组] 求和
Description
A long narrow strip of paper is evenly divided into cells, numbered from to . Each cell is colored with a color (represented by an integer from ), and a number is written on it.
| Index | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Color and number | ||||||
Define a special triplet , where , , are indices of cells on the strip, satisfying both of the following conditions:
- , , are integers, , .
- .
The score of such a triplet is defined as . The score of the entire strip is the sum of the scores of all triplets that satisfy the conditions. This score can be very large; you only need to output the remainder when the total score is divided by .
Input Format
The first line contains two positive integers and separated by a space. is the number of cells on the strip, and is the number of colors.
The second line contains positive integers separated by spaces; the -th number is the number written on cell .
The third line contains positive integers separated by spaces; the -th number is the color of cell .
Output Format
A single integer, the remainder of the total score modulo .
6 2
5 5 3 2 2 2
2 2 1 1 2 1
82
15 4
5 10 8 2 2 2 9 9 7 7 5 6 4 2 4
2 2 3 3 4 3 3 2 4 4 4 4 1 1 1
1388
Hint
Sample 1 Explanation.
The strip is as shown in the figure in the problem description.
All valid triplets are: , .
Therefore, the total score is $(1 + 5) \times (5 + 2) + (4 + 6) \times (2 + 2) = 42 + 40 = 82$.
Constraints:
- For testdata groups to , , .
- For testdata groups to , , .
- For testdata groups to , , , and no color appears more than times.
- For all testdata groups, , , , .
Translated by ChatGPT 5
京公网安备 11011102002149号