Infinite Game
Time limit5sMemory limit128 MB
Given sets A and B of positive steps taken alternately right and left, decide whether every integer can be reached.
- Level
Medium6 of 10
- Topics
- Number theory, Dynamic programming, Math, Game theory
- Solved
- No attempts yet
Problem
Sanggeun stands at integer on the number line. He moves using two finite sets of positive integers, and .
Each move follows these rules:
- On every odd-numbered move he chooses a number from set and steps that many units to the right.
- On every even-numbered move he chooses a number from set and steps that many units to the left.
So the first move uses , the second uses , the third uses again, and so on, alternating between the two sets. On each move he may freely choose any number from the corresponding set.
Write a program that decides whether Sanggeun can reach every integer , both positive and negative.
Input
The first line contains the number of test cases ().
Each test case begins with a line containing two integers and separated by a space (), where is the size of set and is the size of set . The next lines each contain one element of , and the following lines each contain one element of . Every element is an integer between and , inclusive.
Output
For each test case, print YES on its own line if Sanggeun can reach every integer, and NO otherwise.