-
Notifications
You must be signed in to change notification settings - Fork 0
/
Between Two Sets
57 lines (41 loc) · 1.17 KB
/
Between Two Sets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/python3
import math
import os
import random
import re
import sys
def gcd(x, y):
# Function to find the greatest common divisor using Euclidean algorithm
while(y):
x, y = y, x % y
return x
def lcm(x, y):
# Function to find the least common multiple
return x * y // gcd(x, y)
def getTotalX(a, b):
# Find the LCM of all elements in array A
lcm_a = a[0]
for num in a[1:]:
lcm_a = lcm(lcm_a, num)
# Find the GCD of all elements in array B
gcd_b = b[0]
for num in b[1:]:
gcd_b = gcd(gcd_b, num)
# Count the multiples of LCM_a that evenly divide GCD_b
count = 0
multiple = lcm_a
while multiple <= gcd_b:
if gcd_b % multiple == 0:
count += 1
multiple += lcm_a
return count
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])
arr = list(map(int, input().rstrip().split()))
brr = list(map(int, input().rstrip().split()))
total = getTotalX(arr, brr)
fptr.write(str(total) + '\n')
fptr.close()