King's Decree
Maged the lion is the king of the jungle, and rules his subjects with an iron fist. However, he does not like talking to his subjects, so he instead gives decrees to a scribe, who transcribes and publishes the decrees. Recently, Brooke has been hired as scribe, and she figures that this process is insecure. Brooke wants to subtly modify the decrees so she can exert her own influence over jungle policy, without making her tampering obvious.
Each decree is a sequence of integers, each between
and
inclusive.
Brooke maintains a collection of decrees. Initially, this collection contains only the decree given by Maged. She can perform the following operations as many times as she likes:
- She may add any two decrees in her collection, producing a new decree whose values are their element-wise sum modulo
.
- She may cyclically shift any decree in her collection, wrapping around, producing a new decree.
- She may duplicate any decree in her collection.
Brooke wants to know whether she can produce a specific target decree through these operations. Can you let her know if it is possible?
Input
The first line contains a single integer (
), the length of the decrees.
The next line contains space-separated integers between
and
, the initial decree given to Brooke.
The next line contains space-separated integers between
and
, the final decree that Brooke wants to create through her tampering.
Output
Output Yes if it is possible to create the target decree through these operations, otherwise output No.
Example
Input 1
5
1 0 0 0 0
2 3 0 22 1
Output 1
Yes
Here the initial decree is 1 0 0 0 0. By cyclically shifting it, Brooke can create a decree with a single in any position and
everywhere else. By duplicating and adding these shifted decrees, she can choose each position independently, so any target decree of length
can be created.
Input 2
4
1 0 1 0
2 3 2 3
Output 2
Yes
Input 3
4
1 0 1 0
2 3 2 4
Output 3
No
In this case, the initial decree and all of its cyclic shifts have positions and
equal, and positions
and
equal. Adding such decrees preserves this property. The target has different values in positions
and
, so it cannot be created.
Comments