Problem:


Least Common Multiple

Find the least common multiple (LCM) of given n integers.

Input

n
a1 a2 ... an

n is given in the first line. Then, n integers are given in the second line.

Output

Print the least common multiple of the given integers in a line.

Constraints

2 ≤ n ≤ 10

1 ≤ ai ≤ 1000

Product of given integers ai(i = 1, 2, ... n) does not exceed 231-1

Sample Input 1

3
3 4 6

Sample Output 1

12

Sample Input 2

4
1 2 3 5

Sample Output 2

30



Sample input:
3
3 4 6


Sample output:
12

Submission status: Accepted

Solution code:
import math
input()
b = 1
for a in [int(x) for x in input().split()]:
    b = a * b // math.gcd(a, b)
print(b)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Elementary Number Theory - Least Common Multiple
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_C&lang=jp

"""
import sys
from math import gcd
from functools import reduce

def solve(A):
    return reduce(lambda x, y: x * y // gcd(x, y), A)


def main(args):
    _ = input()
    A = [int(a) for a in input().split()]
    ans = solve(A)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
n=int(input())
L=input().split( )
a=[int(L[i]) for i in range(n)]
for i in range(n-1):
    x=a[i]
    y=a[i+1]
    while x%y!=0:
        w=y
        y=x%y
        x=w
    a[i+1]=a[i+1]*a[i]//y


print(a[n-1])

Submission status: Accepted

Solution code:
from fractions import gcd
n=int(input())
A=list(map(int,input().split()))
ans=A[0]
for i in range(n-1):
    i=A[i+1]
    g=gcd(ans,i)
    ans*=(i//g)
print(ans)

Submission status: Accepted

Solution code:
def euc(x, y):
	if y == 0: return x
	return euc(y, x%y)

N = int(input())
A = list(map(int, input().split()))
now = A[0]
for i in range(1, N):
	now = (now*A[i])//euc(now, A[i])
print(now)

Submission status: Accepted

Solution code:
def g(a, b):
    while b:
        a, b = b, a%b
    return a
n = int(input())
a = list(map(int, input().split()))
ans = a[0]*a[1]/g(a[0],a[1])
for i in a[2:]:
    ans = ans*i/g(ans,i)
print(int(ans))

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): #intのlist
	return list(map(int,input().split()))
def m(): #複数文字
	return map(int,input().split())
def onem(): #Nとかの取得
	return int(input())
def s(x): #圧縮
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): #listをスペースごとに分ける
	return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
	return max(map(max,x))

import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

n = onem()

l = l()

print(lcm_list(l))

Submission status: Accepted

Solution code:
def calc_gcd(x, y):
    
    if x < y:
        x, y = y, x

    while y > 0:
        x, y = y, x % y

    return x



n = int(input())
A = list(map(int, input().split()))


a_last = None

for a in A:
    if a_last == None:
        a_last = a
        continue

    gcd = calc_gcd(a, a_last)
    
    a_last = a * a_last / gcd


print(int(a_last))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

import math

def LCM(i, j):
    return i * j // math.gcd(i, j)

ans = 1
for i in a:
    ans = LCM(ans, i)
print(ans)

Submission status: Accepted

Solution code:
import functools 

def gcd(a, b):
    if a > 0 and b == 0:
        return a
    if a < b:
        a, b = b, a
    r = a % b
    return gcd(b, r)

def lcm(a, b):
    return  a * b // gcd(a, b)

line = input()
n = int(line)
line = input()
a = list(map(int, line.split()))
print(functools.reduce(lambda x,y: lcm(x, y), a))

Submission status: Accepted

Solution code:
import math
def lcm(a,b):
    return a*b//math.gcd(a,b)
n=int(input())
a=list(map(int,input().split()))
result=a[0]
for i in range(1,n):
    result=lcm(result,a[i])
print(result)

Submission status: Accepted

Solution code:
def gcd(a,b):
  if b==0:
    return a
  else:
    return gcd(b,a%b)

n=int(input())
arr=list(map(int,input().split()))
for i in range(n-2,-1,-1):
  arr[i]=(arr[i]*arr[i+1])//gcd(arr[i],arr[i+1])
print(arr[0])

Submission status: Accepted

Solution code:
import math

n = int(input())
num = list(map(int, input().split()))

for i in range(n - 1):
    num[i + 1] = int(num[i] * num[i + 1] / math.gcd(num[i], num[i + 1]))

print(int(num[n - 1]))

Submission status: Accepted

Solution code:
import math
n = int(input())
lst = list(map(int, input().split()))


def lcm(a, b):
    return a * b // math.gcd(a, b)
ans = 1
for _ in lst:
    ans = lcm(_, ans)

print(ans)

Submission status: Accepted

Solution code:
def lcm_base(x, y):
    return (x*y) // math.gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


import math
from functools import reduce

n = int(input())
alist = list(map(int, input().split()))

print(lcm_list(alist))

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
    
n = int(input())
a = list(map(int, input().split()))

lcm = 1
for i in a:
    lcm = lcm*i//gcd(lcm,i)

print(lcm)

Submission status: Accepted

Solution code:
import sys,math
from functools import reduce
input = sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))

def lcm_base(x,y):
    return (x*y)//math.gcd(x,y)
def lcm(*N):
    return reduce(lcm_base,N,1)

print(lcm(*l))

Submission status: Accepted

Solution code:
import math
from typing import List
from functools import reduce

def least_common_multiple(x: int, y: int) -> int:
    return x * y // math.gcd(x, y)


def least_common_multiple_of_numbers(numbers: List[int]) -> int:
    if len(numbers) == 1:
        return numbers[0]

    # while len(numbers) != 1:
    #     new_nums = []
    #     for i in range(len(numbers) // 2):
    #         new_nums.append(least_common_multiple(numbers[2*i], numbers[2*i+1]))
    #     if len(numbers) % 2 != 0:
    #         new_nums.append(numbers[-1])
    #     numbers = new_nums
    # return numbers

    return reduce(least_common_multiple, numbers)


def main():
    m = int(input())
    numbers = list(map(int, input().split()))
    gcm = least_common_multiple_of_numbers(numbers)
    print(gcm)
    return


main()

Submission status: Accepted

Solution code:
from functools import reduce

def gcd(a,b): #最小公約数
    if ((a%b) == 0 ):
        return b
    else:    
        return (gcd(b,a%b))

def lcm(a,b):
    return a * b // gcd(a,b)

n = int(input())
l = list(map(int,input().split()))

print (reduce(lcm,l))

Submission status: Accepted

Solution code:
import sys

# Set max recursion limit
sys.setrecursionlimit(1000000)


def li_input():
    return [int(_) for _ in input().split()]

def gcd(a, b):
    if a % b == 0:
        return b
    else:
        return gcd(b, a % b)

def lcm(a, b):
    return a * b // gcd(a, b)

def lcm_list(L):
    _lcm = lcm(L[0], L[1])

    for i in range(2, len(L)):
        _lcm = lcm(_lcm, L[i])

    return _lcm

def main():
    N = int(input())
    L = li_input()

    print(lcm_list(L))


main()

Submission status: Accepted

Solution code:
import math

l = int(input())
a = list(map(int, input().split()))
dict = {}
for n in a:
    ans = []
    i = 2
    k = math.sqrt(n)
    dic = {}
    while i <= k:
        if n%i == 0:
            n = n//i
            ans.append(i)
            i -= 1
            if n == 1:
                break
        i += 1
    if len(ans) == 0 or n != 1:
        ans.append(n)
    for i in ans:
        if i in dic:
            dic[i] += 1
        else:
            dic[i] = 1
    for i in dic.keys():
        if i in dict:
            dict[i] = max(dict[i], dic[i])
        else:
            dict[i] = dic[i]
sum = 1
for x,y in dict.items():
    sum *= x**y
print(sum)

Submission status: Accepted

Solution code:
n = int(input())
A = tuple(map(int, input().split()))
def gcd(a, b):
    if a < b:
        a, b = b, a
    while b != 0:
        a, b = b, a % b
    return a

b = A[0]
for a in A:
    b = a * b // gcd(a, b)
print(b)

Submission status: Accepted

Solution code:
def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return int(a / gcd(a, b)) * b

def lcml(l):
	if len(l) < 1:
		return 1
	elif len(l) == 1:
		return l[0]
	return lcm(l[0], lcml(l[1:]))

n = int(input())
print(lcml([int(x) for x in input().split()]))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))

def gcd(x,y):
    if max(x, y) % min(x, y) == 0:
        return min(x,y)
    else:
        return gcd ( max(x,y) % min (x,y) , min(x,y))

def lcm(x,y):
    return x * y // gcd(x,y)

ans = A[0]
for i in range(n-1):
    ans = lcm(ans, A[i+1])

print(ans)

Submission status: Accepted

Solution code:
import functools
import math
n=int(input())
a=list(map(int,input().split(" ")))
print(functools.reduce(lambda x, y: x*y//math.gcd(x,y), a))

Submission status: Runtime Error

Solution code:
import math
def sieve(n):
    p = [1] * (n + 1)
    p[0] = p[1] = 0
    
    for i in range(math.ceil((n + 1) ** 0.5)):
        if p[i]:
            for j in range(2 * i, len(p), i):
                p[j] = 0
    return p

def prime_factor(n):
    p = sieve(int(n ** 0.5))
    factor = []
    for pi in compress(count(0), p):
        while n % pi == 0:
            n //= pi
            factor.append(pi)

    if n != 1:
        factor.append(n)
    return factor

from collections import Counter
from sys import stdin
readline = stdin.readline

n = int(readline())
c =[Counter(prime_factor(ai)) for ai in map(int, readline().split())]
s = set()
for ci in c:
    s.update(ci.keys())

ret = 1
for i in [i ** max(ci[i] for ci in c) for i in s]:
    ret *= i
print(ret)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
4
1 2 3 5

output:
30
"""

import sys
import math
from functools import reduce


def gcd(x, y):
    if x < y:
        x, y = y, x

    while y > 0:
        r = x % y
        x = y
        y = r

    return x


def lcm(a, b):
    return a * b // math.gcd(a, b)


def solve(_n_list):
    return reduce(lcm, _n_list)


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    cnt = int(_input[0])
    n_list = tuple(map(int, _input[1].split()))
    print(solve(n_list))

Submission status: Runtime Error

Solution code:
import sys
from math import gcd
from functools import reduce

def solve():
    n = int(input())
    a = [int(i) for i in input().split()]

    ans = reduce(lcm, a, 1)

    print(ans)

def lcm(a, b):
    return a * b // gcd(a, b)

if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
import numpy as np
import math
n = int(input())
a = np.array([int(n) for n in input().split()])
divisors = []
for i in range(2, math.ceil(math.sqrt(a.max()))+1):
    while True:
        cond = (a%i==0)
        if cond.sum() > 1:
            a[cond] //= i
            divisors.append(i)
        else:
            break
print(int(np.concatenate((a,divisors)).cumprod()[-1]))

Submission status: Runtime Error

Solution code:
import numpy

Submission status: Runtime Error

Solution code:
import math

def lcm(a, b):
    return int(a * b / int(math.gcd(a, b)))

def solve():
    n = input()
    nums = list(map(int, input().split()))
    c_m = lcm(nums[0], nums[1])
    for n in nums[2:]:
        c_m = lcm(c_m, n)
    print(c_m)

solve()

Submission status: Runtime Error

Solution code:
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

a, b = map(int, raw_input().split())
g = gcd(a,b)
l = a*b/g
print l

Submission status: Runtime Error

Solution code:
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

n = int(raw_input())
an = map(int, raw_input())
l = a[0]

for i in range(1,len(a)-1):
    b = a[i]
    g = gcd(l,b)
    l = a*b/g
print l