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:
n = int(input())
a = [ int(elem) for elem in input().split() ]
a.sort()

def calc(a):
    new_a = [ elem % a[0] if elem % a[0] != 0 else elem for elem in a ]
    new_a.sort()
    if new_a[0] == 1:
        return new_a[0]
    elif new_a[0] == a[0]:
        return a[0]
    else:
        return calc(new_a)

print(calc(a))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    while True:
        if A[0] == 1:
            print(1)
            return
        for i in range(len(A)):
            if i == 0:
                continue
            else:
                if A[i] % A[0] != 0:
                    A[i] = A[i] % A[0]
                    A.sort()
                    break
            if i == len(A) - 1:
                print(A[0])
                return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

while num > 1:
    a = min(A)
    j = 0
    flag = True
    for n in range(num):
        if A[n] == a and flag:
            A[j] = a
            j += 1
            flag = False
        else:
            b = A[n] % a
            if b > 0:
                A[j] = b
                j += 1
    num = j

print(A[0])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(" ")))
A = [a for a in A if a >= 1]

def gcd(x, y):
    while y != 0:
        x, y = y, x % y
    return x

temp = A[0]
for i in range(N-1):
    temp = gcd(temp, A[i+1])
    if temp == 1:
        break
print(temp)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = a[0]
for i in range(1,n):
    while a[i] % ans:
        a[i] = a[i] %ans
        if a[i] < ans:
            tmp = a[i]
            a[i] = ans
            ans = tmp
print(ans)

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()))
num = A[0]
for i in range(N):
    num = gcd(num, A[i])
print(num)

Submission status: Accepted

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

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


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

Submission status: Accepted

Solution code:
def f(a, m):
  if a % m == 0:
    return m
  else:
    return a % m

def solve():
  N = int(input())
  A = list(map(int, input().split()))
  while True:
    m = min(A)
    A = list(map(lambda a: f(a, m), A))
    if all([a == m for a in A]): return m

print(solve())

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()))
lsize = len(a)
bef = a[0]
for i in range(1, lsize):
  bef = gcd(bef,a[i])
hp = bef
print(hp)

Submission status: Accepted

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

m = []
flag = 0
while flag ==0:
    l.sort()
    p = len(l)
    for i in range(1,p):
        a = l[i]%l[0]
        if a != 0:
            l[i] = a
        else:
            l[i] = l[0]

    kkk = set(l)
    if len(kkk) <= 1 or min(l) == 1:
        flag = 1

if len(l) == 0:
    print(l[0])
else:
    l.sort()
    print(l[0])

Submission status: Accepted

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

while True:
    m = min(As)
    idx = As.index(m)
    As = [a if i == idx else a % m for i, a in enumerate(As)]
    As = [a for a in As if a > 0]
    if len(As) == 1:
        break
print(As[0])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = list(set(a))
while (10000000):
    a.sort()
    div_flag = True
    for i in range(1, len(a)):
        if a[i]%a[0] != 0:
            div_flag = False
    if div_flag is True:
        print(a[0])
        exit()
    for i in range(1, len(a)):
        a[i] = a[i]%a[0]
    while 0 in a :
        a.remove(0)

Submission status: Accepted

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

def gozyo():
    global a
    for i in range(1,len(a)):
        a[i]%=a[0]
    a.sort()
    while a[0]==0:
        a.pop(0)

while len(a)>1:
    gozyo()
print(a[0])

Submission status: Accepted

Solution code:
a_len = int(input())
a_list = list(map(int, input().split(' ')))


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


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

Submission status: Accepted

Solution code:
# coding=utf-8


def all_equal(num_list):
    return all([num_list[0] == num for num in num_list])


def self_injure(num_list):
    if all_equal(num_list):
        return num_list[0]
    if 1 in num_list:
        return 1
    hp_min = min(num_list)
    num_list2 = [hp_min if num % hp_min == 0 else num % hp_min for num in num_list]
    return self_injure(num_list2)


if __name__ == '__main__':
    N = int(input())
    monsters_hp = list(map(int, input().split()))
    hps = monsters_hp[:]

    print(self_injure(hps))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
monster_num = int(input())
monster_hp_list = list(map(int, input().split(' ')))

while True:
    min_val = min(monster_hp_list)
    monster_hp_list = [i % min_val for i in monster_hp_list if i % min_val != 0]
    monster_hp_list.append(min_val)
    if len(monster_hp_list) == 1:
        break

print(monster_hp_list[0])

Submission status: Accepted

Solution code:
from fractions import gcd

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

sortedA = sorted(A)
answer = A[0]

for x in range(0,N):
        answer = gcd(answer,A[x])
        if answer == 1:
            break

print(answer)

Submission status: Accepted

Solution code:
n=int(input())

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

m = 0
while True:
  m = max(A)
  for i in range(n):
    if A[i] != 0:
      m = min(m,A[i])
  c = set(A) - set([0])
  if len(c) == 1:
    break
  
  for i in range(n):
    if A[i] == m:
      continue
    if A[i] == A[i]%m:
      continue
    else:
      A[i] = A[i]%m
print(m)

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()))

a.sort()
#b = [a[i+1]-a[i] for i in range(n-1)]
c = a[0]
for i in range(1, n):
    c = gcd(c, a[i])

print(c)

Submission status: Accepted

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

Submission status: Accepted

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

As = sorted(A)
As = [a%As[0] if a%As[0] != 0 else As[0] for a in As]
As = sorted(As)

def judge(tmp_one, tmp_two):
    flg = 1

    while flg:
        if tmp_two%tmp_one == 0:
            tmp_two = tmp_one
        else:
            tmp_two = tmp_two%tmp_one

        if tmp_one%tmp_two == 0:
            tmp_one = tmp_two
        else:
            tmp_one = tmp_one%tmp_two
        if tmp_one == tmp_two:
            flg = 0

    return tmp_one

tmp_one = As[0]
for i in range(1, N):
    if i == N - 1:
        tmp_two = As[i]
    elif As[i] != As[0]:
        tmp_two = As[i]
        break

tmp = judge(tmp_one, tmp_two)
print(tmp)

Submission status: Accepted

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

A.sort()

a = A[0]
b = 0
if a % 2 == 0:
    for i in A[1:]:
        if i % 2 == 1:
            b = i
            
if b < a:
    b = A[1]

b -= int(b/a) * a

if b == 0:
    print(a)
    exit()

res = 0
while b > 0 and a > 0:
    na = max(a,b)
    nb = min(a,b)
    a = na - nb
    b = nb
    res = b
    #print(a,b)

print(res)

Submission status: Accepted

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

while len(A) > 1:
    n = A.pop(0)
    A = list(map(lambda x: x % n, A))
    A.sort()
    A.append(n)

    while 0 in A:
        A.pop(0)
print(A[0])

Submission status: Accepted

Solution code:
#!venv/bin/python

import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq

#N,M = [int(x) for x in input().split()]

N = int(input())
A = [-1 * int(x) for x in input().split()]

alives = N
A = sorted(A)

heap = heapq.heapify(A)
heap = A
num = 0
while alives > 1:
    biggest = -1 * heapq.heappop(heap)
    second = -1 * heapq.heappop(heap)
    times = int(math.floor(biggest / second))
    new_hp = biggest - second * times
    if new_hp <= 0:
        alives -= 1
    heapq.heappush(heap, second * -1)
    heapq.heappush(heap,new_hp * -1)
    num += 1

for a in A:
    if a < 0:
        print (a * -1)
        break

Submission status: Accepted

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

A.sort()

m = A[0]

while True:
    flag = True
    new = list()
    for a in A:
        tmp = a % m
        if tmp != 0 and tmp < m:
            new.append(tmp)
            flag = False

    if flag:
        break
    else:
        m = min(new)

print(m)

Submission status: Accepted

Solution code:
def main():
    n=int(input())  #数値入力
    a=list(map(int, input().split()))  #リスト入力（数値)
    num = a[0]

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

# 最大公約数
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

main()

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
data = np.array([int(i) for i in input().split()])
while True:
  datamin = data.min()
  data = data%datamin
  
  if data.sum()<=1:
    if data.sum()==0:
      print(datamin)
      exit()
    else:
      print(data[data>0].min())
      exit()
  data = np.append(datamin,data)
  data = data[data>0]

Submission status: Accepted

Solution code:
from heapq import heapify, heappush, heappop
n = int(input())
A = list(map(int, input().split()))
heapify(A)
while len(A) != 1:
    t = heappop(A)
    A = [a % t for a in A if a % t != 0]
    if A is None:
        A = [t]
        break
    heappush(A, t)
print(A[0])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

def main():
  input = sys.stdin.readline
  N = int(input().strip())
  A = list(map(int, input().split()))
  
  a = min(A)
  B = [b % a for b in A if b % a != 0]
  if (len(B) == 0) or (min(B) == 0):
    print(a)
  else:
    b = min(B)
    while a % b != 0:
      c = b
      b = a % b
      a = c
    print(b)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n = int(input())

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

while True:
    a.sort()
    flag = True
    j = 0
    while a[j] == 0:
        j += 1
    for i in range(j+1,n):
        a[i] %= a[j]
        if a[i] != 0:
            flag = False
    if flag:
        break 

print(a[j])

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
a = A[0]
for i in range(N - 1):
#    a = A[i]
    b = A[i + 1]
    plus = a % b
    while plus != 0:
        a = b
        b = plus
        plus = a % b
    a = b
print(a)

Submission status: Accepted

Solution code:
input()
a = [int(v) for v in input().split()]
ans = None
while True:
        mi = float('inf')
        for v in a:
                if v == 0:
                        continue
                mi = min(mi, v)
        if mi == float('inf'):
                break
        if ans is None or mi < ans:
                ans = mi
        zero_cnt = 0
        for i in range(len(a)):
                if a[i] % mi == 0:
                        zero_cnt += 1
                        continue
                a[i] %= mi
        if zero_cnt == len(a):
                break
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

np_lst = np.array(lst)
min_val = np.min(np_lst)

ans = -1
while(True):
    tmp = set(np_lst%min_val)
    tmp = tmp - set([0])
    if min_val == 1:
        ans = 1
        break
    elif len(tmp) == 0:
        ans = min_val
        break
    else:
        if min(tmp) < min_val:
            min_val = min(tmp)
        else:
            ans = min_val
            break

print(ans)

Submission status: Accepted

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

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

# min(A)より小さな値が作れるか検証していく感じか？
A.sort()
l = len(A)

tmp1 = list(A)
tmp2 = []

while len(tmp1) > 1:
    m = tmp1[0]
    tmp2.append(m)
    for d in tmp1:
        a = d % m
        if 0 < a < d:
            tmp2.append(a)
    tmp2.sort()
    tmp1 = tmp2
    tmp2 = []

print(tmp1[0])

Submission status: Accepted

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



def f(l, s):
    return s if l % s == 0 else f(s, l % s)
    
z = f(max(L), min(L))

for i in L:
    z=f(i,z)
ans=10000000000
for i in range(len(L)):
    if L[i]%z==0:
        pass
    elif L[i]%z<ans:
        ans=L[i]%z

if ans==10000000000:
    print(z)
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

def input_numbers():
    temp = list(map(int, input().split()))
    return temp

def GCD(a, b):
    if(a < b):
        temp = a
        a = b
        b = temp
    a = a % b
    if(a == 0):
        return b
    else:
        return GCD(a, b)

n = int(input())
a = input_numbers()

g = a[0]
for i in range(1, n):
    g = GCD(g, a[i])

print(g)

Submission status: Accepted

Solution code:
import fractions

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

x = fractions.gcd(A[0],A[1])

for i in range(2,N):
    x = fractions.gcd(A[i],x)

print(x)

Submission status: Accepted

Solution code:
from functools import reduce

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

n = int(input())
lst = list(map(int, input().split()))
 
def gcd_list(numbers):
    return reduce(gcd, numbers)
 
print(gcd_list(lst))

Submission status: Accepted

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

flag = 0
i = 1
while flag == 0:
    if A[i] % A[0] != 0:
        A[i] = A[i] % A[0]
        A[i],A[0] = A[0],A[i]
        i = 1
    else:
        i += 1
        if i == N:
            flag = 1
            print(A[0])
            exit()

Submission status: Accepted

Solution code:
from functools import reduce
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
  


def lcm_list(*numbers):
    return reduce(gcd, numbers)

n=int(input())
inp=input().split(" ")
mons=[int(i) for i in inp]
print(lcm_list(*mons))

Submission status: Accepted

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

while True:
    if len(a) == 1:
        break
    a.sort()
    for i in range(len(a)-1, 0, -1):
        a[i] %= a[0]
        if a[i] == 0:
            del a[i]
print(a[0])

Submission status: Accepted

Solution code:
import fractions

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

A.sort()

res = A[0]
for aa in A:
    res = fractions.gcd(res, aa)

print(res)

Submission status: Accepted

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

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

a=max(tmp)
for i in tmp:
    a=gcd(a,i)

print(a)

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools


# ひとつ入力
n = int(input())

# リストに入れる
a = [int(i) for i in input().split()] 

def gcd(p,q):
    if p < q:
        r = p
        p = q
        q = r
    if q == 0:
        return p
    r = p % q
    return gcd(q,r)
        

for i in range(1,n):
    a[0] = gcd(a[0],a[i])
print(a[0])

Submission status: Accepted

Solution code:
import fractions
N = int(input())
A = list(map(int, input().split()))
GCD_i=A[0]
for i in range(len(A)):
  GCD_i = fractions.gcd(GCD_i,A[i])
print(GCD_i)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
# import bisect
import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def d(self, p):
        re = 0
        for i in range(len(p) - 1):
            re += ((p[i][0] - p[i + 1][0]) ** 2 + (p[i][1] - p[i + 1][1]) ** 2) ** 0.5
        return re

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)  # 再帰定義


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n = ip.I()
    a = ip.IL()
    res = max(a)

    for i in r(n):
        res = min(res, id.gcd(a[0], a[i]))

    print(res)


main()

Submission status: Accepted

Solution code:
from functools import reduce
n = int(input())
a = list(map(int, input().split()))
def gcd(a, b):
    """Return greatest common divisor using Euclid's Algorithm."""
    while b:      
        a, b = b, a % b
    return a
def gcmm(*args):
  return reduce(gcd, args)
print(gcmm(*a))

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
while True:
    ma = min(a)
    a = [x % ma for x in a]
    a = [ma if x == 0 else x for x in a]
    if max(a) == min(a):
        print(min(a))
        exit()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
while True:
    m = max(a)
    for i in range(n):
        if a[i] > 0 and a[i] < m:
            m = a[i]
    ind = a.index(m)
    count = 0
    for i in range(n):
        if i != ind:
            a[i] = a[i]%m
        if a[i] == 0:
            count += 1
    if count == n-1:
        print(a[ind])
        break

Submission status: Accepted

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

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

ans = hp[0]
for i in range(len(hp) - 1):
    ans = gcd(ans, hp[i+1])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ai = list(map(int, input().split(' ')))
while len(ai) > 1:
    ai = list(sorted(list(ai)))
    ai_head = ai[0]
    ai = list(filter(lambda x: x != 0, map(lambda x: x%ai_head, list(ai[1:]))))
    ai.append(ai_head)
print(ai[0])

Submission status: Accepted

Solution code:
N = int(input())
HP = [int(i) for i in input().split()]
m = min(HP)
while(HP.count(m) < N):
    HP = [i % m if i % m != 0 else m for i in HP]
    m = min(HP)

print(m)

Submission status: Accepted

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

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

for i in range(N-1):
  A[i+1] = gcd(A[i], A[i+1])
  
print(A[N-1])

Submission status: Accepted

Solution code:
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(a):
  for i in range(n - 1):
    a[i + 1] = gcd(a[i], a[i + 1])
  return a[n - 1]
  
print(gcd_list(a))

Submission status: Accepted

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

from functools import reduce
def gcds(*numbers):
    print(reduce(gcd, numbers))
    
gcds(*list(map(int, input().split(" "))))

Submission status: Accepted

Solution code:
#coding:utf-8

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

min_A = min(A)

while True:
    for i in range(N):
        if A[i] == min_A:
            continue
        A[i] = A[i] % min_A
    min_A = min([i for i in A if i!= 0])
        
    if max(A)==1:
        print(min_A)
        break

    if max(A) == min_A:
        print(min_A)
        break

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 = [int(x) for x in input().split()]

for i in range(N):
    if i == 0:
        tmp = A[i]
    if i == N:
        break
    else:
        tmp = gcd(tmp, A[i])

print(tmp)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
L = []
minA = min(A)
for i, a in enumerate(A):
  if a % minA != 0:
    L.append(a % minA)
if len(L) == 0:
  print(minA)
  exit()
L.append(minA)
L = list(set(L))
while len(L) > 1:
  L.sort()
  for i, l in enumerate(L):
    if i == 0:
      continue
    L[i] = l - L[0]
  L = list(set(L))
print(L[0])

Submission status: Accepted

Solution code:
import sys

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def dfs(A):
    m = min(A[A != 0])  # sortしてるので必ず最小値
    # sub_A = [a % m for a in A[1:]]
    sub_A = A[1:] % m  # 最小値で余り
    ans = np.insert(sub_A[sub_A != 0], 0, m)  # 非ゼロ抽出
    if len(ans) != 1:
        ans = dfs(np.sort(ans))
    return ans


def solve():
    N = ii()
    A = np.array(sorted(il()))
    return dfs(A).item()


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

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

while True:
    if len(l)==1:
        print(l[0])
        break
    else:
        ml = min(l)
        l = [i % ml for i in l]
        l.append(ml)
        l = filter(lambda x: x > 0, l)
        l = list(set(l))
        if len(l)==1:
            print(l[0])
            break
        ml = min(l)
        l = [i - ml for i in l]
        l.append(ml)
        l = filter(lambda x: x > 0, l)
        l = list(set(l))

Submission status: Accepted

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


def gcd (a,b):
    if (b == 0):
        return a
    else:
        return gcd(b, a % b)
res = baa[0]
for c in baa[1::]:
    res = gcd(res , c)
print(res)

Submission status: Accepted

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

for i in range(N-1):
    while True:
        A[i+1] %= A[i]
        A[i+1], A[i] = A[i], A[i+1]
        if A[i] == 1:
            print(1)
            exit(0)
        elif A[i] == 0:
            break

print(A[-1])

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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/2/16
Solved on 2019/2/16
@author: shinjisu
"""


# ABC 118
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


def dmp(x):
    global debug
    if debug:
        print(x)
    return x


def gcd(x, y):  # 最大公約数
    m = max(x, y)
    n = min(x, y)
    while m % n != 0:
        w = m % n
        m = n
        n = w
    return n


def probC():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hp = A[N-1]
    for i in range(N-1):
        hp = gcd(hp, A[i])
        dmp(hp)
    return hp


debug = False  # True False
print(probC())


def probD():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    return 123


def probA():
    A, B = getIntList()
    dmp((A, B))
    if B % A == 0:
        return A+B
    else:
        return B-A


def probB():
    N, M = getIntList()
    A = getIntMat(N)
    dmp((N, M))
    dmp(A)
    food = set([])
    food = set(A[0][1:])
    dmp(food)
    for i in range(N):
        food2 = set(A[i][1:])
        food = food & food2
        dmp(food)
    return len(food)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random

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

def fight(a):
    a.sort()
    survive = [a[0]]
    for i in range(1,len(a)):
        if a[i]%a[0]!=0:
            survive.append(a[i]%a[0])
    return survive

while len(a)>1:
    a = fight(a)

print(a[0])

Submission status: Accepted

Solution code:
#coding:utf-8

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

while len(a)>1:
  for i in range(1,len(a)):
    a[i] %= a[0]
  a.sort()
  while True:
    if a[0] == 0:
      a.remove(0)
    else:
      break;

print(a[0])

Submission status: Accepted

Solution code:
def servey(a):
  for i in range(len(a)):
    global ans
    tmp=a[i]%ans
    if tmp!=0 and tmp<ans:
      ans=tmp
      return False
  return True

n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=min(a)
while True:
  if servey(a):
    break
print(ans)

Submission status: Accepted

Solution code:
def find_gcd(x, y):   
    while(y): 
        x, y = y, x % y 
    return x 
          
t = int(input())       
l = [int(i) for i in input().split()] 
  
num1 = l[0] 
num2 = l[1] 
gcd = find_gcd(num1, num2) 
  
for i in range(2, len(l)): 
    gcd = find_gcd(gcd, l[i]) 
      
print(gcd)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def check(a,n):
    if a == [0 for i in range(n)]:
        return False
    for i in range(n-1):
        if a[i] != 0:
            return True
    return False
n = int(input())
a = input().split()  
for i in range(n):
    a[i] = int(a[i])
a.sort()
while(check(a,n)):
    j = 0
    while(a[j] == 0):
        j += 1
    for i in range(j+1,len(a)):
        a[i] = a[i] % a[j]
    a.sort()
j = 0
while(a[j] == 0):
    j += 1
print(a[j])

Submission status: Accepted

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

b = min(a)
li = [x%b for x in a]
c = max(li)

if c == 0:
    ans = b
else:
    while c != 0:
        d = c # 一時記憶
        c = b%c
        if c == 0:
            ans = d
        else:
            b = d # 戻す
print(ans)