You are given the numbers , , , , and two sequences: and of natural numbers. The sequence is formed by traversing the elements of from to , starting from position 0.
Each number in is replaced with the closest natural power of an element from . If a number is equidistant from two powers, the smaller value is chosen. For example, if , the powers of the elements in sequence are: .
Task
- If , find the sum of the values in the sequence after the replacements.
- If , when a number is less than the chosen power, it will be replaced with the negative of the chosen power (For example, instead of 5, it would be -5). Then calculate by traversing the elements of in order () and applying the formula
S=((S*k+v[i])%n+n)%n
. Initially, =0.
Input data
The first line of the input file abmnk.in
contains . The second line contains the numbers , , and , in this order, separated by spaces. The third line contains the sequence of natural numbers greater than or equal to 2, separated by spaces. The fourth line contains the sequence of natural numbers separated by spaces.
Output data
The first line of the output file abmnk.out
will contain a single integer, the answer to requirement .
Constraints and clarifications
- It is guaranteed that gcd .
- The numbers in the input file are less than or equal to
Subtasks
# | Points | Constraints |
---|---|---|
1 | 11 | |
2 | 18 | |
3 | 12 | |
4 | 7 | |
5 | 52 |
Example 1
abmnk.in
1
5 15 4
2 32 10000 17 3
5 16 289 57 32 27 70 1 10000000 100000000 4 90 81 1024 531441
abmnk.out
108921736
Explanation
We traverse from 4 to 4 and obtain .
Example 2
abmnk.in
2
5 15 4
2 32 10000 17 3
5 16 289 57 32 27 70 1 10000000 100000000 4 90 81 1024 531441
abmnk.out
8
Explanation
In the end, .