Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.function.*;
- import java.util.regex.*;
- import java.util.stream.*;
- import static java.util.stream.Collectors.joining;
- import static java.util.stream.Collectors.toList;
- class Result {
- static int gcd(int x, int y){
- if(y==0){
- return x;
- }
- return gcd(y,x%y);
- }
- static int lcm(int x, int y){
- return (x*y)/(gcd(x,y));
- }
- public static int getTotalX(List<Integer> a, List<Integer> b) {
- int lcmVal=a.get(0);
- int gcdVal=b.get(0);
- for(int i=0; i<a.size(); i++){
- lcmVal = lcm(lcmVal, a.get(i));
- }
- for(int i=0; i<b.size(); i++){
- gcdVal = gcd(gcdVal, b.get(i));
- }
- List<Integer> lst = new ArrayList<>();
- for(int j=1; j<((int)Math.sqrt(gcdVal))+1;j++){
- if(gcdVal%j==0 && (gcdVal/j)*j==gcdVal){
- if(j!=gcdVal/j){
- lst.add(j);
- lst.add(gcdVal/j);
- }else{
- lst.add(j);
- }
- }
- }
- int count=0;
- for(Integer k:lst){
- // System.out.println(k);
- if(k>=lcmVal && k%lcmVal==0){
- // System.out.println(k);
- count+=1;
- }
- }
- return count;
- }
- }
- 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(toList());
- List<Integer> brr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
- .map(Integer::parseInt)
- .collect(toList());
- int total = Result.getTotalX(arr, brr);
- bufferedWriter.write(String.valueOf(total));
- bufferedWriter.newLine();
- bufferedReader.close();
- bufferedWriter.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement