Problem:


Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000



Sample input:
4
2 10 8 40


Sample output:
2

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N = int(lines.pop(0))
    A = list(map(int, lines.pop(0).split(' ')))
    A.sort()
    min_a = A.pop(0)
    while(len(A) > 0):
        x = A.pop(0)
        y = x % min_a
        if y != 0:
            A.append(y)
        if y != 0 and min_a > y:
            A.append(min_a)
            min_a = y
    print(min_a)



#
# main
#
lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Accepted

Solution code:
N = int(input()) 
l = list(map(int,input().split()))
mini = max(l) +1
mini_idx = N+1
while mini != sum(l):
    for i, point in enumerate(l):
        if point < mini and point != 0:
            mini = point
            mini_idx = i

    for i, point in enumerate(l):
        if mini_idx != i:
            l[i] = l[i] % mini
print(mini)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[a[0]]
def sks(a,b):
    if a == 0 or b == 0:
        return(max(a,b))
    else:
        c = min(a,b)
        d = max(a,b)
        return sks(c,d%c)
for i in range(n-1):
    b.append(sks(b[i],a[i+1]))
print(b[n-1])

Submission status: Accepted

Solution code:
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

n = int(input())
enemy = list(set(map(int,input().split())))
if len(enemy)==1:
    print(enemy[0])
else:
    enemy.sort()
    min_n = 10**10
    for i in range(len(enemy)-1):
        min_n = min(min_n,gcd(enemy[i],enemy[i+1]))
    print(min_n)

Submission status: Accepted

Solution code:
N = int(input())
life = list(map(int, input().split()))
while len(life) >1:
    life.sort()
    life_array = [life[0]] + [i%life[0] for i in life if i%life[0] !=0]
    life = life_array
    #for i in life_array:
        #if i not in life:
            #life.append(i)
 
print(life[0])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from fractions import gcd
from functools import reduce

N = int(input())
nums = sorted(list(map(int, input().split())), reverse=True)

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

def find_gcd(ls):
    x = reduce(gcd, ls)
    return x

def main():
    print(find_gcd(nums))
    return 0

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = sorted([int(i) for i in input().split()])

monster = a[0]

while True:
    last = a.pop()
    attacked = last % monster
    if attacked == 0:
        pass
    elif attacked < monster:
        monster = attacked
        a.insert(0, attacked)

    if len(a) == 1:
        print(a[0])
        break

Submission status: Accepted

Solution code:
N = int(input())
Mons = list(map(int, input().split()))
Mons = list(set(Mons))
Mons.sort()

minHP = Mons[0]

if minHP == 1 or len(Mons) == 1:
    print(minHP)
    exit()
    
minSA = minHP

for a,b in zip(Mons[:],Mons[1:]):
    sa = b % a
    if sa and sa < minSA:
        minSA = sa

ans = minSA

while 1:
    hoge = [val % ans for val in Mons if val % ans]

    if hoge:
        if ans == min(hoge):
            break
        else:
            ans = min(hoge)
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
while len(a) >= 2 :
    k = a[1] % a[0]
    if k == 0 :
        a.pop(1)
    else :
        a[1] = k
        a[0], a[1] = a[1], a[0]
print(a[0])

Submission status: Accepted

Solution code:
n = input()

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


ans = 0
while True:
    # print(A)
    if len(A)==1:
        break
    A.sort()

    c = 0
    for i in range(1, len(A)):
        # print(i)
        res = A[i]% A[0]
        if res == 0:
            c+=1
        A[i]=res
    for i in range(c):
        A.remove(0)

print(A[0])

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int,input().split()))
def GCD(a,b): #Greatest Common Diverser
	x = max(a,b)
	y = min(a,b)
	if x % y == 0:
		return y
	else:
		while x % y != 0:
			z = x % y
			x = y
			y = z
		else:
			return y


ans = l[0]
for i in range(N):
	ans=GCD(ans,l[i])
print(ans)

Submission status: Accepted

Solution code:
j=input()
mst_str=input().split()
mst=list(map(int, mst_str))
n=len(mst)

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

if n==1:
    print(mst[0])
else:
    HP = gcd(mst[0], mst[1])
    if n==2:print(HP)
    else:
        for i in range(2,n):
            HP=gcd(HP,mst[i])
        print(HP)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

A.sort()

num = 0

if N == A.count(A[0]):
    num = 1
else:
    l = A
    x = []

    while True:
        x = []
        x.append(l[0])
        for i in range(1, len(l)):
            if 0 < l[i] % l[0]:
                x.append(l[i] % l[0])

        if len(x) == 1:
            break

        x.sort()

        l = x

print(A[0] if num == 1 else x[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = map(int, input())
A = list(map(int, input().split()))

while len(A) >= 2:
    idx = A.index(min(A))
    x = A.pop(idx)
    B = list()
    for i in range(len(A)):
        if A[i] % x != 0:
            B.append(A[i] % x)
    B.append(x)
    A = B[:]

print(A[0])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n=int(input())
a = list(map(int, input().split()))
a.sort()
r=a.pop(0)
while len(a)>0:
  x = a.pop(0)
  if x%r!=0:
    a.append(r)
    a.append(x%r)
    a.sort()
    r=a.pop(0)
    
    

print(r)

Submission status: Accepted

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


n=int(input())
l=list(map(int,input().split()))
g=l[0]
for i in range(1,n):
      g=gcd(g,l[i])
print(g)

Submission status: Accepted

Solution code:
import sys

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

a.sort()
answer = a[0]
for i in range(1, n):
    if a[i] % a[0] == 0: continue

    x, y = a[0], a[i] # x < y
    while x != 0:
        x, y = y % x, x
    answer = min(answer, y)

print(answer)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
N = li()
A = list(li())

def gcd(A, B):

    # normal gcd logic
    while B not in (1, 0):
        A, B = (A, B) if A > B else (B, A)
        _, mod = divmod(A, B)
        A, B = B, mod
    if B == 0:
        return A
    elif B == 1:
        return 1

A = list(filter(lambda x:x>0, A))
A.sort()
if len(A) == 0:
    print(0)
else:
    res = A[0]
    for a in A:
        res = gcd(a, res)
        if res == 1:
            break

    print(res)

Submission status: Accepted

Solution code:
import fileinput


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


def gcdl(l):
    r = gcd(l[0], l[1])
    for i in range(2, len(l)):
        r = gcd(r, l[i])
    return r


def algorithm(n, a):
    return gcdl(a)


def pass_inputs(input_lines):
    n = input_lines[0][0]
    return algorithm(n, input_lines[1])


def convert_int(s):
    try:
        r = int(s)
    except ValueError:
        r = s
    return r


def run():
    out = pass_inputs([[convert_int(v) for v in l.strip().split(" ")] for l in fileinput.input()])
    print(out)


if __name__ == "__main__":
    run()

Submission status: Accepted

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

def sakujo(a_list):
    for i in range(len(a_list)):
        if a_list[i]!=0:
            end=i
            break
    return a_list[i:]

while True:
    a_s.sort()
    a_s=sakujo(a_s)
    if len(a_s)==1:
        break
    for i in range(1,len(a_s)):
        a_s[i]=a_s[i]%a_s[0]

print(sum(a_s))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
# a.sort()
# now = a[0]
# for i in range(1, n):
#     if (a[i] % now != 0):
#         now = a[i] % now
# print(now)
import fractions
from functools import reduce
print(reduce(fractions.gcd,a))

Submission status: Accepted

Solution code:
from functools import reduce

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

n = int(input())
enemies = list(map(int,input().split()))
print(reduce(gcd, enemies))

Submission status: Accepted

Solution code:
N = int(input())
A=[int(i) for i in input().split()]

# print(N,A)
# print(A)

while(True):
    A.sort()
    min_A=A[0]
    c=0
    for i in range(len(A)):
        tmp=A[i]%min_A
        if(tmp==0):
            c+=1
            A[i]=min_A
        else:
            A[i]=tmp
    if(c==len(A)):
        break

A.sort()
print(A[0])

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int,input().split()))
i = 0
while True:
    A.sort()
    for a in range(len(A)-1):
        if not(A[a] == 0) and A[a+1] % A[a] == 0:
                A[a+1] = A[a+1] % A[a]
        else:
            A[a+1] = A[a+1] - A[a]

    A = [i for i in A if  i > 0]
    if len(A) == 1:
        break

print(A[0])

Submission status: Accepted

Solution code:
input()
a = list(map(int, input().split()))
m = min(a)
while (len(a) > 1):
  m = min(a)
  b = [m]
  for i in a:
    if i % m != 0:
      b.append(i % m)
  a = b
print(m)

Submission status: Accepted

Solution code:
N=int(raw_input())
M=map(int,raw_input().split())

while min(M) != sum(M):
    M.sort()
    m =  M[0]
#    print M, m, sum(M)
    for i, a in enumerate(M):
        if i == 0:
            continue
        else:
            M[i] = a % m
            
#        print M, min(M), sum(M), "end"

#    print M, min(M), sum(M), "e"
    M = [i for i in M if i != 0]
#    print M, min(M), sum(M), "N"

print sum(M)

Submission status: Accepted

Solution code:
def calc(a):
    m = min(a)
    b = [i % m for i in a if i % m != 0]
    if b:
        return calc(b+[m])
    else:
        return min(a)
      
_ = int(input())
print(calc(list(map(int, input().split()))))

Submission status: Accepted

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

tmp=A[0]
for a in A[1:]:
  if a%tmp!=0:
    tmp=a%tmp
    
while 1:
  A_tmp=[a%tmp for a in A if a%tmp!=0]
  if sum(A_tmp)!=0:
    tmp=min(A_tmp)
  else:
    break
print(tmp)

Submission status: Accepted

Solution code:
input()
*s, = map(int, input().split())
base = min(s)

while True:
  s = [i%base for i in s]
  s.append(base)
  s = list(set(s))
  s.remove(0)

  if base > min(s):
    base = min(s)
    continue
  else:
    break

print(base)

Submission status: Accepted

Solution code:
input()

living_monsters = list(map(int, input().split()))

while len(living_monsters) > 1:
    living_monsters.sort()
    weakest_monster = living_monsters.pop(0)
    attacked_monsters = []
    for monster in living_monsters:
        attacked_monster = monster % weakest_monster
        if attacked_monster != 0:
            attacked_monsters.append(attacked_monster)
    living_monsters = [weakest_monster] + attacked_monsters

print(living_monsters[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 16 20:52:40 2019

@author: Owner
"""


import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
    
all(nstr.count(c) for c in '753')

ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
#A, B, totAB = zip(*ABT)


(x >> i) & 1

"""
N = int(input())

A = [0]*N
A[:] = map(int, input().split())

A.sort()
while len(A) != 1:
    for n in range(len(A)-1):
        A[n+1] = A[n+1] % A[0]
    A = [e for e in A if e != 0]
    A.sort()
print(A[0])

Submission status: Accepted

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

while True:
    index_list = [i for i, v in enumerate(monster) if v >0]
    mini = monster[index_list[0]]
    for i in range(1, len(index_list)):
        if mini > monster[index_list[i]]:
            mini =  monster[index_list[i]]

    miniindex_list = [i for i, v in enumerate(monster) if v ==mini]
    min_index = miniindex_list[0]

    for i in range(len(monster)):
        if not i == min_index:
            monster[i] = monster[i] % monster[min_index]
            
    non_zeroindex_list = [i for i, v in enumerate(monster) if v >0] 
    non_zeroindex = non_zeroindex_list[0]
    if len(non_zeroindex_list) == 1:
        break
print(monster[non_zeroindex])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

def io_generator():
	while True:
		yield raw_input()

#+++++++++++++++++++

def gojo(i,j):
	a=max(i,j)
	b=min(i,j)
	while b > 0:
		t=b
		c=a/b
		b=a%b
		a=t
	return a
		

def main(io):
	a=int(io.next())
	l= list(map(int, io.next().split()))
	
	a=l[0]
	for i in l[1:]:
		a=gojo(a,i)
	return a


#++++++++++++++++++++

if __name__ == "__main__":
	io=io_generator()
	print main(io)

Submission status: Accepted

Solution code:
from fractions import gcd
from functools import reduce

N = int(input())

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

print(int(reduce(gcd,A)))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000)
input = sys.stdin.readline

def chkprint(a):
	a_name = ''
	for k, v in globals().items():
		if id(v) == id(a):
			a_name = k
			break
	print(a_name+' = '+str(a), file=sys.stderr)

def mt(f):
	import time
	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		print(e - s, 'sec', file=sys.stderr)
		return ret
	return wrap

@mt
def slv(N, A):
	flag = True
	while len(A) > 1:
		m = min(A)
		A = [a % m for a in A if a % m != 0]
		A.append(m)
		print(A, file=sys.stderr)
	return m


N = int(input())
A = [int(i) for i in input().split()]
'''
N = 5
A = [13, 5, 18, 100000000]
'''
print(slv(N, A))

Submission status: Accepted

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

while len(list_HP)!=1:
	new_list = []
	smallest = min(list_HP)

	for i in range(len(list_HP)):
		temp = int(list_HP[i]/smallest)
		list_HP[i] = list_HP[i] - (smallest*temp)
		if list_HP[i] != 0:
				new_list.append(list_HP[i])
	list_HP = new_list
	list_HP.append(smallest)

print(list_HP[0])

Submission status: Accepted

Solution code:
N = int(input())
A= []

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

#ユークリッド互除法の関数の定義
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)


if N==1:
    print(A[0])
else:
    element = gcd(A[0], A[1])
    if N==2:
        print(element)
    else:
        for i in range(2,N):
            element=gcd(element,A[i])
        print(element)

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	A = list(map(int, input().split()))
	while(len(A) != 1):
		A.sort()
		mini = A[0]
		i = 1
		while(True):
			if A[i] % mini == 0:
				A.pop(i)
			else:
				A[i] = A[i] % mini
				i += 1
			if i == len(A):
				break
		A = list(set(A))
	print(A[0])

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
def attack(H):
    #最小の要素を頭に出す
    H.sort()
    H_min = H[0]
    for i in range(1,len(H)):
        H[i]  = H[i] % H_min
    #0を削除
    new_H = [i for i in H if i != 0]
    return new_H

N = int(input())
HP_str = input().split()
HP = [0 for i in range(N)]
for j in range(N):
    HP[j] = int(HP_str[j])

while(True):
    HP = attack(HP)
    if len(HP) == 1:
        break

HP.sort()
HP.reverse()
print(HP[0])

Submission status: Accepted

Solution code:
#coding:utf-8

N = int(input())

A = [int(an) for an in input().split()]

def royale(An):

    tmp = An
    while True:
        n_tmp = []
        # 残り1体の時は終了
        if len(tmp) == 1:
            return tmp[0]

        # 一番体力が小さいモンスターを決定する
        min_hp = min(tmp)
        n_tmp.append(min_hp)
        min_num = tmp.index(min_hp)

        # 体力が1のモンスターがいるなら1で確定
        if min_hp == 1:
            return 1

        # 他を攻撃する
        for i in range(len(tmp)):
            # 自分以外
            if i != min_num:
                tmp[i] = tmp[i] % min_hp
                if tmp[i] != 0:
                    n_tmp.append(tmp[i])
        tmp = n_tmp

print(royale(A))

Submission status: Accepted

Solution code:
N = int(input())
monster = list(map(int, input().split()))

monster.sort(reverse = True)

while sum(monster[1:N]) != 0:
    monster.sort(reverse = True)
    for i in range(N-1):
        if monster[i+1] != 0:
            monster[i] = monster[i] % monster[i+1]

print(monster[0])

Submission status: Accepted

Solution code:
num = int(input())

monsters_hp = [int(i) for i in input().split()]

attacker = 0

def attack(attacker, monsters_hp):
    for i in range(len(monsters_hp)):
        if i == attacker:
            continue
        else:
            monster_hp = monsters_hp[i]
            if monsters_hp[attacker] == 0:
                continue
            hp_mod = monster_hp % monsters_hp[attacker]
            monsters_hp[i] = hp_mod
    return monsters_hp

def calc_zero(monsters_hp):
    count = 0
    for monster_hp in monsters_hp:
        if monster_hp == 0:
            count += 1
    return count

def change_attacker(monsters_hp):
    minimum = calc_minimum(monsters_hp)
    for i in range(len(monsters_hp)):
        if monsters_hp[i] == minimum:
            return i
def calc_minimum(monsters_hp):
    minimum = monsters_hp[0]
    for i in range(len(monsters_hp)):
        if monsters_hp[i] == 0:
            continue
        elif minimum >= monsters_hp[i]:
            minimum = monsters_hp[i]
    if minimum == 0:
        minimum += 1
    return minimum

while True:
    attack(attacker, monsters_hp)
    attacker = change_attacker(monsters_hp)
    count = calc_zero(monsters_hp)
    if count == (len(monsters_hp) - 1):
        break

print(max(monsters_hp))

Submission status: Accepted

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

def gcd_list(numbers):
    return reduce(gcd, numbers)

ans = gcd_list(arr)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
monsters = list(map(int,input().split()))
monsters = sorted(monsters)

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

if N == 1:
    print(monsters[0])
else:
    ans = gcd(monsters[0], monsters[1])
    if N == 2:
        print(ans)
    else:
        for i in range(2, N):
            ans = gcd(ans,monsters[i])
        print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(int(i) for i in input().split())

A.sort()


if((A[1] - A[0]) == 0):
    min_HP = A[0]
    print(min_HP)
else:
    min_HP = A[1] - A[0]
    while((A[1] - A[0]) > 0):
        A.insert(0, A[1] - A[0])
        A.sort()
        if((A[1] - A[0]) == 0):
            break
        min_HP = A[1] - A[0]

    # そのループでまだmin_HPが更新されていない
    while(True):
        min_flag = False
        for a in A:
            if ((a % min_HP) < min_HP and (a % min_HP) > 0):
                min_HP = a % min_HP
                min_flag = True
        if( min_HP > 0 and min_flag ):
            continue
        else:
            break
    print(min_HP)

Submission status: Accepted

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

N = int(input())
A = sorted(list(map(int, input().split())))

ans = A[0]
for a in A:
    ans = gcd(a, ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

#モンスターiの体力:mon[i]
mon = list(map(int, input().split()))


zero = 0
while mon.count(0) != (n-1):
    mon.sort()
    min = mon[zero]
    tei = zero + 1
    for i in range(tei,n):
        mon[i] = mon[i] % min
        if mon[i] == 0:
            zero += 1
mon.sort()
print(mon[zero])

Submission status: Accepted

Solution code:
input();a=set(map(int,input().split()))
while(len(a)>1):
 m=min(a)
 a={e%m for e in a-{m}if e%m>0}|(a&{m})
print(*a)

Submission status: Accepted

Solution code:
import fractions
N = int(input())
A = list(map(int, input().split()))
import math
A.sort()
ans = A[0]

for i in A:
    ans = fractions.gcd(ans, i)

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
n= int(input())
a= list(map(int,input().split()))

def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)
if n==1:
    print(a[0])
else:
    element = gcd(a[0], a[1])
    if n==2:print(element)
    else:
        for i in range(2,n):
            element=gcd(element,a[i])
        print(element)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
import collections

a=sorted(a)
mi=a[0]
g=0
k=0
t=0
gset=set()
loopnum=len(a)-1
i=1
while i<=loopnum:
    if(0<a[i]%mi<mi):
        a.append(mi)
        mi=a[i]%mi
        loopnum+=1
    i+=1
        
if(t==N-1):
    print(mi)
else:
    print(mi)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = min(A)
while True:
    for i in range(len(A)):
        if A[i] % ans != 0:
            A[i] %= ans
    if min(A) < ans:
        ans = min(A)
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
import fractions

n = int(input())
l = [int(i) for i in input().split()]
exgcd = reduce(fractions.gcd, l)

print(exgcd)

Submission status: Accepted

Solution code:
from fractions import gcd
from functools import reduce

def gcd_list(numbers):
    return reduce(gcd,numbers)

N = int(input())
A = list(map(int,input().split()))
print(gcd_list(A))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def main():
    n = int(input())

    HP = [int(e) for e in input().split(" ")]

    while True:
        # 一番 HP の少ないモンスターが他に攻撃すれば良い
        min_idx = HP.index(min([e for e in HP if e > 0]))

        died = 0
        for i in range(n):
            if i == min_idx:
                continue
            if HP[i] <= 0:
                died += 1
                continue
            HP[i] %= HP[min_idx]
        if died == len(HP)-1:
            break

    for m in HP:
        if m > 0:
            print(m)

main()

Submission status: Accepted

Solution code:
n = int(input())
a_li = list(set(int(x) for x in input().split()))

while 1:
    res = a_li[-1]
    a_li.sort(reverse = True)
    cou = 0
    for i in a_li[:-1]:
        a_li[cou] = i % a_li[-1]
        cou += 1
    a_li = list(set(a_li))
    try:
        a_li.remove(0)
    except:
        pass
    if len(a_li) == 0:
        print(res)
        break
    if len(a_li) == 1:
        print(a_li[0])
        break

Submission status: Accepted

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

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

ans  = gcd(A[0],A[1])
for a in range(2,N):
    ans = gcd(ans,A[a])
print(ans)

Submission status: Accepted

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

from functools import reduce
def gcd(a,b):
    if a<b:a,b=b,a
    if a%b==0:return b
    return gcd(b,a%b)

gcd=reduce(gcd,a)
print(gcd)

Submission status: Accepted

Solution code:
import fractions
from functools import reduce

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

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

print(gcd(*A))

Submission status: Accepted

Solution code:
from functools import reduce


def gcd(a, b):
    if a < b:
        a, b = b, a
    while b != 0:
        a, b = b, a % b
    return a


N = int(input())
A = map(int, input().split())
print(reduce(gcd, A))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort()
min=A[0]
A[-1]%=min
if A[-1]!=0:
    min=A[-1]
for i in range(N-1):
    A[-i-2]%=min
    if A[-i-2]!=0:
        min=A[-i-2]
while A.count(0)!=N:
    for i in range(N):
        A[-i-1]%=min
        if A[-i-1]!=0:
            min=A[-i-1]
print(min)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

pm = min(A)
m = pm
while True:
    na = [a % m for a in A if a % m != 0]
    if len(na) == 0:
        break
    pm, m = m, min(na)
    if pm == m:
        break

print(m)

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

n = int(input())
a = [int(i) for i in input().split(" ")]

for i in reversed(range(1, n)):
    a[i - 1] = gcd(a[i], a[i - 1])

print(a[0])

Submission status: Accepted

Solution code:
import numpy as np
N = input()
N = int(N)
A = input()
A = A.split()
for i in range(len(A)):
    A[i] = int(A[i])
A = np.array(A)
while True:
    min = np.min(A)
    A = A%min
    B = np.where(A==0)
    C = np.delete(A,B)
    C = np.unique(C)
    A = np.append(C,[min])
    if len(A) == 1:
        break
print(A[0])

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
hp = list(map(int, input().split()))
hp = np.array(hp)
m = min(hp)
while 1:
  temp = hp%m
  temp = temp[temp!=0]
  if len(temp):
    M = min(temp)
    if M < m:
      m = M
    else:
      break
  else:
    break
print(m)

Submission status: Accepted

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

N = int(input())

a = [int(x) for x in input().split()]
ans = a[0]
for i in a:
    ans = lcm(ans,i)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import math

n = int(input())
A = list(map(int, input().split()))
A = np.asarray(A)
A = np.sort(A)
while(A.size != 1):
    minimum = A[0]
    m = A.size
    B = np.asarray([minimum]*m)
    A = A%B
    A[0] = minimum
    A = A[A > 0]
    A = np.sort(A)
print(minimum)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

while np.count_nonzero(A) != 0:
    m = min(A)
    idx = list(A).index(m)
    A = A % m
    A[idx] = m
    A = A[np.nonzero(A)]
    if np.count_nonzero(A) == 1:
        break

print(A[0])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
sort = sorted(A)
# amari = []
# while len(amari) != 1:
#   amari.clear()
#   if 0 in sort: sort.remove(0)
#   for item in sort:
#     if item%sort[0] not in amari:
#       amari.append(item%sort[0])
#   last_sort0 = sort[0]
#   sort = sorted(amari[:])
#   sort.append(last_sort0)

# print(last_sort0-amari[0])

while len(sort) != 1:
  for i in range(1,len(sort)):
    sort[i] = sort[i]%sort[0]
  sort = [j for j in sorted(sort) if j != 0]

print(sort[0])

Submission status: Accepted

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

minutes_mod = min(l)

# for i in l:
#     if i < minutes_mod:
#         new_l = [j % i for j in l]
#     else:
#         new_l = [j % minutes_mod for j in l]
#     new_l_set = list(set(new_l))
#     if 0 in new_l_set:
#         new_l_set.remove(0)
#     if len(new_l_set) != 0:
#         minutes_mod_list.append(min(new_l_set))
#         minutes_mod = min(new_l_set)

new_l = list(set([i % minutes_mod for i in l]))
new_l.append(minutes_mod)
if 0 in new_l:
    new_l.remove(0)

another_list = list(set(new_l))
minutes = min(another_list)

while 1 :
    first_minutes = minutes
    another_list = list(set([i % minutes for i in another_list]))
    another_list.append(minutes)
    if 0 in another_list:
        another_list.remove(0)

    minutes = min(another_list)

    if first_minutes == minutes:
        break
print(minutes)

Submission status: Accepted

Solution code:
from functools import reduce

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

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def gcd_list(numbers):
    return reduce(gcd, numbers)
m = gcd_list(A)
flag = 0
for i in range(N):
    if A[i] % m == 0:
        flag += 1
    else:
        pass
if flag == N:
    print(m)
else:
    print(1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
gcd = lambda x, y: gcd(y, x % y) if x % y else y
ans = A[0]
for i in range(1, N):
    ans = gcd(ans, A[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
memo = {}
def f(a,b):
    if b==0:
        return a
    else:
        return f(b,a%b)
a = sorted(a)
ans = float('inf')
for i in range(len(a)-1):
    ans = min(ans,f(a[i],a[i+1]))
print(ans)

Submission status: Accepted

Solution code:
def gcd(m,n):
    x = max(m,n)
    y = min(m,n)
    if x%y==0:
        return y
    else:
        while x%y!=0:
            z = x%y
            x = y
            y = z
        else:
            return z
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = a[0]
for i in range(1,n):
     ans = min(ans,gcd(ans,a[i]))
print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
m=min(a)
while len(a)>1:
    m=min(a)
    f=0
    for i in range(len(a)):
        if a[i]!=m or f:
            a[i]%=m
        else:
            f=1
    *a,=filter(lambda x:x,a)
print(m)

Submission status: Accepted

Solution code:
import numpy as np
 
N=int(input())    
listA = list(map(int,input().split()))
 
minA = min(listA)
 
while True:
   minA = min(listA)
   minApos = np.argmin(listA)
   listA = [x % minA for x in listA]
   listA[minApos] = minA
   listA= [x for x in listA if x >0]
   if len(set(listA))==1:
       print(minA)
       break

Submission status: Accepted

Solution code:
def gcd(a, b):
  if b == 0:
    return a
  else:
    return(gcd(b, a % b))
  
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
  if i == 0:
    ans = A[i]
  else:
    ans = gcd(ans, A[i])
print(ans)