#C12023. Transformable Sequence Operations

    ID: 41405 Type: Default 1000ms 256MiB

Transformable Sequence Operations

Transformable Sequence Operations

You are given two sequences of positive integers: an initial sequence and a target sequence. You are allowed to perform the following operation on the initial sequence:

  • Choose any two adjacent elements a and b and replace them with two numbers: gcd(a, b) and lcm(a, b) respectively, where gcd denotes the greatest common divisor and lcm denotes the least common multiple.

Your task is to determine if it is possible to transform the initial sequence into the target sequence by applying a sequence of such operations (possibly zero). Note that if the lengths of the two sequences are different, the transformation is automatically impossible.

The mathematical formulas used in the operations are given by:

  • \( gcd(a,b) \): the greatest common divisor of \( a \) and \( b \).
  • \( lcm(a,b) = \frac{a \times b}{gcd(a,b)} \): the least common multiple of \( a \) and \( b \).

inputFormat

The input is read from standard input (stdin) and has the following format:

 n
 a1 a2 ... an
 m
 b1 b2 ... bm

Here:

  • n is the number of elements in the initial sequence.
  • a1, a2, ..., an are the elements of the initial sequence.
  • m is the number of elements in the target sequence.
  • b1, b2, ..., bm are the elements of the target sequence.

Note: The sequences are space-separated and provided in a single input stream.

outputFormat

Output a single line to the standard output (stdout) containing either YES if the transformation from the initial sequence to the target sequence is possible using the allowed operations, or NO if it is not.

## sample
5
12 15 10 20 25
5
3 60 10 20 25
YES