문제 확인하기
import java.io.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Result {
public static int getTotalX(List<Integer> a, List<Integer> b) {
int cnt = 0;
int current = a.get(a.size() - 1);
int flag;
while (current <= b.get(0)) {
flag = 0;
for (int i : a) {
if (current % i != 0) {
flag = 1;
break;
}
}
if (flag == 0) {
for (int i : b) {
if (i % current != 0) {
flag = 1;
break;
}
}
}
if (flag == 0) {
cnt++;
}
current++;
}
return cnt;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(firstMultipleInput[0]);
int m = Integer.parseInt(firstMultipleInput[1]);
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
List<Integer> brr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
int total = Result.getTotalX(arr, brr);
bufferedWriter.write(String.valueOf(total));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}