Captivating process
시간 제한4초메모리 제한1024 MB
1..N에서 정의된 두 함수 f와 g가 매분 두 수를 각각 f, g로 옮길 때, 각 질의 (x, y)에 대해 두 수가 언젠가 같아지는지 판정한다.
문제
Yulia wrote the number on the blackboard, and Zakhar wrote . The kids are bored and have come up with an extremely captivating activity. Once every minute, they erase their numbers simultaneously and write new numbers instead. Yulia writes new numbers according to the following rule: if her number equaled , it is substituted by . Zakhar does the same, but the rule is different: if his number equaled , it is substituted by .
They will stop when their numbers match. This coould happen right away (if ), or later, or maybe never. Your task is to determine for different values of and , if the kids will ever end writing out numbers.
입력
The first line contains two integers: and ().
The second line contains numbers separated by spaces: .
The third line contains numbers in the same format: .
In the th of the following lines there are the initial numbers and .
It is guaranteed that the numbers , , , are all integers and fall within the range of through .
출력
Print lines: in the th line, print YES, if the process that started from the numbers and , ends, and NO otherwise.