You are viewing a single comment's thread. Return to all comments →
import java.io.*; import java.util.stream.Stream; public class BetweenTwoSets { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) { int[] firstLine = getArray(br.readLine()); int[] firstArray = getArray(br.readLine()); int[] secondArray = getArray(br.readLine()); bw.write(String.valueOf(countNumbersBetweenSets(firstLine, firstArray, secondArray))); bw.flush(); } catch (IOException e) { throw new RuntimeException(e); } } private static int[] getArray(String line) throws IOException { return Stream.of(line.split(" ")).mapToInt(Integer::parseInt).toArray(); } private static int countNumbersBetweenSets(int[] size, int[] firstArray, int[] secondArray) { int lcmOfFirst = calculateLcmOfArray(firstArray, size[0]); int gcdOfSecond = calculateGcdOfArray(secondArray, size[1]); int count = 0; int currentLcm = lcmOfFirst; while (currentLcm <= gcdOfSecond) { if (gcdOfSecond % currentLcm == 0) { count++; } currentLcm += lcmOfFirst; } return count; } private static int calculateLcmOfArray(int[] array, int size) { int result = array[0]; for (int i = 1; i < size; i++) { result = lcm(result, array[i]); } return result; } private static int calculateGcdOfArray(int[] array, int size) { int result = array[0]; for (int i = 1; i < size; i++) { result = gcd(result, array[i]); } return result; } private static int lcm(int a, int b) { if (a == 0 || b == 0) return 0; return Math.abs(a * b) / gcd(a, b); } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Between Two Sets
You are viewing a single comment's thread. Return to all comments →