#P9589. 「PFLOI R1」PFL 除法

「PFLOI R1」PFL 除法

Background

Is it necessary to connect the backgrounds of all contest problems together.

Just like that, the door to a new world opened to them...

"Meow!" A cute calico cat greeted them.

"Did you just arrive here?"

"Mm."

"I'll show you around. After all, I'm so cute!"

"..." The calico cat suddenly stopped, looked over the sequence in its hands, and then smiled again:

"But you have to answer my question first."

Problem Description

The calico cat has a sequence AA of length nn and another sequence BB of length mm. You may perform the following operation any number of times:

  • Choose two integers ii and jj such that 1in1 \le i \le n, 1jm1 \le j \le m, and BjAiB_j \mid A_i, then change AiA_i to AiBj\frac{A_i}{B_j}.

Note: Each element in AA and BB can be chosen and operated on multiple times.

In the end, make all elements in AA equal. Please output the minimum number of operations. If there is no solution, output -1.

Input Format

The first line contains two positive integers nn and mm.

The second line contains nn positive integers representing sequence AA.

The third line contains mm positive integers representing sequence BB.

Output Format

Output one integer representing the minimum number of operations. If there is no solution, output -1.

4 5
16 24 28 36
11 4 7 3 2
6
2 3
11 13
13 1 11
2
2 2
2 3
4 5
-1

Hint

This problem uses bundled tests.

Subtask ID Special Property Score
11 All elements in AA are equal. 55
22 n=2n = 2 1515
33 n,m103n, m \le 10^3 2020
44 n,m104n, m \le 10^4
55 None. 4040

For all data, 1n,m5×1051 \le n, m \le 5 \times 10^5, 1Ai,Bi5×1051 \le A_i, B_i \le 5 \times 10^5.

Translated by ChatGPT 5