Last time Solve 3 Medium for Boot camp for Beginners.
#53 ABC118-C
** Thoughts ** I thought that what I was doing was similar to the mutual division method, and when I looked at the sample case, I found that it was correct, so I implemented it as it was. Calculate the greatest common divisor for all elements.
import fractions #Not math
n = int(input())
a = list(map(int,input().split()))
ans = fractions.gcd(a[0],a[1])
for i in range(2,n):
ans = fractions.gcd(ans,a[i])
print(ans)
** Thoughts ** Calculate all cases. It's a waste to sum every time, so calculate well
n = int(input())
a = list(map(int,input().split()))
d = sum(a)
ans = float('inf')
for i in range(n-1):
if i == 0:
x = a[0]
y = d - x
else:
x += a[i]
y -= a[i]
ans = min(abs(x-y),ans)
print(ans)
** Thoughts ** Typical least common multiple problem
import fractions
n = int(input())
t = [int(input()) for _ in range(n)]
ans = t[0]
for i in range(1,n):
ans = ans * t[i] // fractions.gcd(ans, t[i])
print(ans)
I can't concentrate on the problem because my life rhythm is broken, so I'll fix it. see you.
Recommended Posts