Problem:
 Score : 300 points    Problem Statement Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)} .  Here \gcd(a,b,c) denotes the greatest common divisor of a , b , and c .      Constraints  1 \leq K \leq 200  K is an integer.         Input Input is given from Standard Input in the following format:  K       Output Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)} .        Sample Input 1 2      Sample Output 1 9  \gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)  +\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)  =1+1+1+1+1+1+1+2=9  Thus, the answer is 9 .       Sample Input 2 200      Sample Output 2 10813692

Solution (Python):
import math

K = int(input())
ans = 0
l = []
for i in range(K+1):
    l.append([0]*(K+1))

for i in range(1, K+1):
    for j in range(i, K+1):
        for k in range(j, K+1):
            num = math.gcd(math.gcd(i, j), k)
            if i == j:
                if j == k:
                    ans += num
                else:
                    ans += 3*num
            else:
                if j == k:
                    ans += 3*num
                else:
                    ans += 6*num

print(ans)

Solution (Python):
def euclid(a, b):
    global l
    if b == 0:
        return a
    else:
        ch = l[a][b]
        if ch == 0:
            return euclid(b, a%b)
        else:
            return ch

def gcd(a, b, c):
    global l
    d = l[c][b]
    if d == 0:
        d = euclid(c, b)
        l[c][b] = d

    e = l[max(d, a)][min(d, a)]
    if e == 0:
        e = euclid(max(d, a), min(d, a))
        l[max(d, a)][min(d, a)] = e

    return e

K = int(input())
ans = 0
l = []
for i in range(K+1):
    l.append([0]*(K+1))

for i in range(1, K+1):
    for j in range(i, K+1):
        for k in range(j, K+1):
            num = gcd(i, j, k)
            if i == j:
                if j == k:
                    ans += num
                else:
                    ans += 3*num
            else:
                if j == k:
                    ans += 3*num
                else:
                    ans += 6*num

print(ans)

Solution (Python):
import math

k = int(input())

ab = []
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        ab.append(math.gcd(a, b))
for i in ab:
    for c in range(1,k+1):
        ans += math.gcd(i, c)

print(ans)

Solution (Python):
import math

k = int(input())

ab = []
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        if a % b == 0:
            ab.append(b)
        elif b % a == 0:
            ab.append(a)
        else:
            ab.append(math.gcd(a, b))
for i in ab:
    for c in range(1,k+1):
        if i % c == 0:
            ans += c
        elif c % i == 0:
            ans += i
        else:
            ans += math.gcd(i, c)

print(ans)

Solution (Python):
from math import gcd
 
n = int(input())
ans = 0

for a in range(1,n+1):
    for b in range(1,n+1):
        for c in range(1,n+1):
            ans += gcd(gcd(a,b),c)
print(ans)

Solution (Python):
k = int(input())

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

def gcf3(a,b,c):
    f_ab = gcf(a,b)
    return gcf(f_ab,c)

ans = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        for c in range(b,k+1):
            if a == b == c:
                ans += gcf3(a,b,c)
            elif a != b and b != c and a != c:
                ans += gcf3(a,b,c)*6
            else:
                ans += gcf3(a,b,c)*3
print(ans)

Solution (Python):
k = int(input())

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

def gcf3(a,b,c):
    f_ab = gcf(a,b)
    return gcf(f_ab,c)

ans = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        for c in range(b,k+1):
            if a == b == c:
                ans += gcf3(a,b,c)
            elif a != b and b != c and a != c:
                ans += gcf3(a,b,c)*6
            else:
                ans += gcf3(a,b,c)*3
print(ans)

Solution (Python):
def gcd(a, b, c):
  x = gcd2(a, b)
  y = gcd2(b, c)
  return gcd2(x, y)

def gcd2(x, y):
    if y == 0:
        return x
    else:
        return gcd2(y,x%y)

def main():
  K = int(input())
  ans = 0
  for p in range(1, K+1):
    for s in range(p, K+1):
      for k in range(s, K+1):
        if p == s == k:
          ans += p
        elif p == s or s == k or p == k:
          ans += 3 * gcd(p, s, k)
        else:
          ans += 6 * gcd(p, s, k)
  print(ans)

if __name__=='__main__':
    main()

Solution (Python):
import math
rst = 0
K = int(input())
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(tmp, k)
print(rst)

Solution (Python):
from math import gcd
n = int(input())
ans = 0
for i in range(1,n + 1):
    for j in range(1, n + 1):
        f = gcd(i, j)
        for k in range(1, n + 1):
            ans += gcd(f, k)
print(ans)

Solution (Python):
def gcds(*numbers):
    from math import gcd
    from functools import reduce
    return reduce(gcd, numbers)

def resolve():
    K = int(input())
    ans = 0
    import itertools
    for i, j, k in itertools.combinations_with_replacement(range(1, K+1), 3):
        l = len(set([i, j, k]))
        mu = 1
        if l == 3:
            mu = 6
        elif l == 2:
            mu = 3
        ans += gcds(i, j, k) * mu
    print(ans)

if '__main__' == __name__:
    resolve()

Solution (Python):
import math





def main():
    K = int(input())
    ans = 0
    
    for i in range(1,K+1,1):
        for j in range(1,K+1,1):
            for k in range(1,K+1,1):
                ans += math.gcd(i,math.gcd(j,k))

    return ans

print(main())

Solution (Python):
import math
from functools import reduce

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

def main():
    K = int(input())
    ans = 0
    
    for i in range(1,K+1,1):
        ans += gcd(i,i,i)
    
    tmp = 0
    for i in range(1,K+1,1):
        for j in range(1,K+1,1):
            if j != i:
                tmp += gcd(i,i,j)
    ans += tmp*3
    
    tmp2 = 0
    for i in range(1,K+1,1):
        for j in range(i+1,K+1,1):
            for h in range(j+1,K+1,1):
                tmp2 += gcd(i,j,h)

    ans += tmp2*6

    return ans

print(main())

Solution (Python):
import math
from functools import reduce

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

ans=0
k=int(input())
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      ans+=gcd(i,j,l)
print(ans)

Solution (Python):
from math import gcd
K=int(input())
ans=0
for i in range(1,K+1):
  for j in range(1,K+1):
    a=gcd(i,j)
    if a==1:
      ans+=K
    else:
      for k in range(1,K+1):
        ans+=gcd(a,k)
print(ans)

Solution (Python):
import math
from functools import reduce

K = int(input())

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

ans = 0

for i in range(1, K+1):
  for j in range(1, K+1):
    for k in range(1, K+1):
      ans += gcd(i, j, k)

print(ans)

Solution (Python):
import math
from functools import reduce

def main():
    K = int(input())
    ans = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            for c in range(1, K + 1):
                ans += reduce(math.gcd, [a, b, c])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math


def abc162c_sum_of_gcd_of_tuples():
    k = int(input())
    total = 0
    memo = [[-1] * k for _ in range(k)]
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            if memo[a - 1][b - 1] == -1:
                memo[a - 1][b - 1] = math.gcd(a, b)
            g = memo[a - 1][b - 1]
            for c in range(1, k + 1):
                if memo[g - 1][c - 1] == -1:
                    memo[g - 1][c - 1] = math.gcd(g, c)
                total += memo[g - 1][c - 1]
    print(total)

abc162c_sum_of_gcd_of_tuples()

Solution (Python):
K=int(input())
import math
l=[]
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            x=math.gcd(i,math.gcd(j,k))
            l.append(x)
print(sum(l))

Solution (Python):
import math
K = int(input()) + 1
s = 0
for i in range(1,K):
    for j in range(1,K):
        ij = math.gcd(i, j)
        for k in range(1,K):
            s += math.gcd(ij, k)
print(s)

Solution (Python):
import math
from functools import reduce

k = int(input())

def find_gcd(*numbers):
    return reduce(math.gcd, numbers)

ans = 0
for a in range(1, k+1, 1):
    for b in range(1, k+1, 1):
        for c in range(1, k+1, 1):
            gcd = find_gcd(a, b, c)
            ans += gcd

print(ans)

Solution (Python):
import math
K = int(input())
ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        x = math.gcd(a, b)
        for c in range(1, K + 1):
            ans += math.gcd(x, c)
print(ans)

Solution (Python):
def mdc(a, b):
    if b == 0: return a
    return mdc(b, a%b)

def main():
    resp = 0
    k = int(input())
    for i in range(1, k + 1):
        for j in range(1, k + 1):
            for w in range(1, k + 1):
                resp += mdc(mdc(i, j), w)

    print(resp)
                
    
main()

Solution (Python):
from math import gcd
n = int(input())
ans = 0
for i in range(1,n + 1):
    for j in range(1, n + 1):
        f = gcd(i, j)
        for k in range(1, n + 1):
            ans += gcd(f, k)
print(ans)

Solution (Python):
k=int(input())
n=3
mod=10**9+7
lst=[0]*(k+1)
ans=0

for i in range(k,0,-1):
  lst[i]+=pow(k//i,n,mod)
  if k//i==1:
    continue
  else:
    for j in range(2,k//i+1):
      lst[i]-=lst[i*j]
      
for i in range(1,k+1):
  ans+=(i*lst[i])%mod
  ans%=mod
  
print(ans)

Solution (Python):
import math
 
w = 0
k = int(input())
for i in range(1, k+1):
    for j in range(1, k+1):
        m = math.gcd(i, j)
        for l in range(1, k+1):
            w += math.gcd(m, l)
print(w)

Solution (Python):
import math
 
w = 0
k = int(input())
for i in range(1, k+1):
    for j in range(1, k+1):
        m = math.gcd(i, j)
        for l in range(1, k+1):
            w += math.gcd(m, l)
print(w)

Solution (Python):
import math
 
w = 0
k = int(input())
for i in range(1, k+1):
    for j in range(1, k+1):
        m = math.gcd(i, j)
        for l in range(1, k+1):
            w += math.gcd(m, l)
print(w)

Solution (Python):
import math
 
w = 0
k = int(input())
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            m = math.gcd(i, j)
            w += math.gcd(m, l)
print(w)

Solution (Python):
import math
import itertools
def gcd(x,y,z):
    xy = math.gcd(x,y)
    xyz = math.gcd(xy,z)
    return xyz
k1 = int(input())
k = list(range(1,k1+1))
l = []
for i in itertools.combinations_with_replacement(k,3):
    l.append(i)
num = 0
for h in l:
    t = gcd(h[0],h[1],h[2])
    if h[0] == h[1] == h[2]:
        num += t
    elif h[0] == h[1] or h[1] == h[2] or h[2] == h[0]:
        num += 3*t
    else:
        num += 6*t
print(num)

Solution (Python):
import math
from functools import reduce

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

K = int(input())
ans = 0

for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            if i == j and j == k:
                ans += gcd(i,j,k)
            elif i != j and j != k and k!= i:
                ans += gcd(i,j,k)*6
            else:
                ans += gcd(i,j,k)*3
print(ans)

Solution (Python):
def main():
  import math
  N = int(input())
  gcd={}
  ans=0
  for i in range(1,N+1):
    for j in range(1, N+1):
      key=i*N+j
      gcd[key]=math.gcd(i,j)
  
  for k, i in gcd.items():
    for j in range(1, N+1):
      key=j*N+i
      ans+=gcd[key]
        
  print(ans)

if __name__=='__main__':
  main()

Solution (Python):
def main():
  import math
  N = int(input())
  ans=0
  for i in range(1,N+1):
    for j in range(1, N+1):
      for k in range(1, N+1):
          t=math.gcd(i,j)
          t=math.gcd(t,k)
          ans+=t
        
  print(ans)

if __name__=='__main__':
  main()

Solution (Python):
k=int(input())

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

ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            ans+=gcd(gcd(i,j),l)
print(ans)

Solution (Python):
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
ans = 0
k = int(input())
for a in range(1,k + 1):
    for b in range(1,k + 1):
        for c in range(1,k + 1):
            ans += gcd(a,b,c)
print(ans)

Solution (Python):
import math

n = int(input())
ans = [math.gcd(i+1, math.gcd(j+1, k+1)) for i in range(n) for j in range(n) for k in range(n)]
print(sum(ans))

Solution (Python):
import math
K=int(input())
ans=0
for h in range(1,K+1):
    for i in range(1,K+1):
        gcd=math.gcd(h,i)
        if gcd==1:
            ans+=K
        else:
            for j in range(1,K+1):
                ans+=math.gcd(gcd,j)
print(ans)

Solution (Python):
from math import *
s=0
n=int(input())
for i in range(1,n+1):
    for j in range(1,n+1):
        for k in range(1,n+1):
            g=gcd(i,j)
            g=gcd(g,k)
            s+=g
print(s)

Solution (Python):
import math
from numba import jit

k=int(input())

@jit
def f():
  ans=0
  for a in range(1,k+1):
    for b in range(1,k+1):
      for c in range(1,k+1):
        g=math.gcd(math.gcd(a,b),c)
        ans+=g
  return ans

print(f())

Solution (Python):
import fractions
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

class Counter:
    def __init__(self):
        self.dict = {}

    def add(self,x):
        if x in self.dict: self.dict[x] += 1
        else: self.dict[x] = 1

    def decrement(self,x):
        self.dict[x] -= 1
        if self.dict[x] <= 0:
            del self.dict[x]

    def get_dict(self):
        return self.dict


INF=10**20
def main():
    K=ii()

    ans = 0
    
    counter = Counter()

    for a in range(1,K+1):
        for b in range(1,K+1):
                x = fractions.gcd(a,b)
                counter.add(x)

    count = counter.get_dict()
    for c in range(1,K+1):
        for key in count.keys():
            val = count[key]
            x = fractions.gcd(key,c)
            ans += val * x



    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):





import math
N=int(input())
count=0
for i in range(1,N+1):
    for j in range(1,N+1):
        s=math.gcd(i,j)
        for k in range(1,N+1):
            count+=math.gcd(s,k)
print(count)

Solution (Python):
import math
k=int(input())
ans=0
gcd1=[0]*200
for i in range(1,k+1):
    for j in range(1,k+1):
        gcd1[math.gcd(i,j)-1]+=1


for i in range(1,k+1):
    for j in range(1,k+1):
        if gcd1[j-1]>=1:
            ans+=math.gcd(j,i)*gcd1[j-1]

print(ans)

Solution (Python):
k=int(input())
from math import gcd
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for s in range(1,k+1):
            ans+=gcd(gcd(i,j),s)
print(ans)

Solution (Python):

import math
import numpy as np
import itertools
k = int(input())
ans = 0

if k == 1:
    ans = 1
else:
    for i in range(1,k+1):
        for j in range(1,k+1):
            a = math.gcd(i,j)
            for k in range(1,k+1):
                b = math.gcd(a,k)
                ans += b    
print(ans)

Solution (Python):
import math

def f_gcd(a, b):
    return math.gcd(a, b)

k = int(input())

ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            ans += f_gcd(math.gcd(a, b), c)

print(ans)

Solution (Python):
from math import gcd
from itertools import combinations_with_replacement

k = int(input())
lst = [i for i in range(1,k+1)]
itr = combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += gcd(a,b) * 3
  else:
    ans += gcd(gcd(i[0],i[1]), i[2]) * 6

print(ans)

Solution (Python):
from math import gcd
from itertools import combinations_with_replacement

k = int(input())
lst = [i for i in range(1,k+1)]
itr = combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += gcd(a,b) * 3
  else:
    ans += gcd(gcd(i[0],i[1]), i[2]) * 6

print(ans)

Solution (Python):
import math
import itertools

k = int(input())
lst = [i for i in range(1,k+1)]
itr = itertools.combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += math.gcd(a,b) * 3
  else:
    ans += math.gcd(math.gcd(i[0],i[1]), i[2]) * 6

print(ans)

Solution (Python):
from math import gcd

k = int(input())
ans = 0
for l in range(1,k+1):
  for m in range(1,k+1):
    for n in range(1,k+1):
      ans += gcd(gcd(l,m),n)

print(ans)

Solution (Python):









import math
ans = 0
K = int(input())
for i in range(1,K+1):
    for j in range(1,K+1):
        tmp = math.gcd(i,j)
        for k in range(1,K+1):
            ans += math.gcd(k,tmp)
print(ans)

Solution (Python):
import math

k = int(input())
ans = 0
for l in range(1,k+1):
  for m in range(1,k+1):
    for n in range(1,k+1):
      ans += math.gcd(math.gcd(l,m),n)

print(ans)

Solution (Python):
import math
import itertools
def gcd(lst):
  return math.gcd(math.gcd(lst[0],lst[1]), lst[2])

k = int(input())
lst = [i for i in range(1,k+1)]
itr = itertools.combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += math.gcd(a,b) * 3
  else:
    ans += math.gcd(math.gcd(i[0],i[1]), i[2]) * 6

print(ans)

Solution (Python):
import math
import itertools
def gcd(lst):
  return math.gcd(math.gcd(lst[0],lst[1]), lst[2])

k = int(input())
lst = [i for i in range(1,k+1)]
itr = itertools.combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += math.gcd(a,b) * 3
  else:
    ans += math.gcd(math.gcd(i[0],i[1]), i[2]) * 6

print(ans)

Solution (Python):
import math
import itertools
def gcd(lst):
  return math.gcd(math.gcd(lst[0],lst[1]), lst[2])

k = int(input())
lst = [i for i in range(1,k+1)]
itr = itertools.combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += math.gcd(a,b) * 3
  else:
    ans += math.gcd(math.gcd(i[0],i[1]), i[2]) * 6

print(ans)

Solution (Python):
from math import gcd

K = int(input())

result = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        t = gcd(a, b)
        for c in range(1, K + 1):
            result += gcd(t, c)
print(result)

Solution (Python):
from math import gcd
n = int(input())
ans = 0
for i in range(1,n + 1):
    for j in range(1, n + 1):
        f = gcd(i, j)
        for k in range(1, n + 1):
            ans += gcd(f, k)
print(ans)

Solution (Python):
import math as np
k = int(input())
D = {}
for a in range(1, k + 1):
    for b in range(1, k + 1):
        try:
            D[np.gcd(a, b)] += 1
        except:
            D[np.gcd(a, b)] = 1
ans = 0
for c in range(1, k + 1):
    for key, cnt in D.items():
        ans += cnt * np.gcd(c, key)
print(ans)

Solution (Python):
import numpy as np
k = int(input())
D = {}
for a in range(1, k + 1):
    for b in range(1, k + 1):
        try:
            D[np.gcd(a, b)] += 1
        except:
            D[np.gcd(a, b)] = 1
ans = 0
for c in range(1, k + 1):
    for key, cnt in D.items():
        ans += cnt * np.gcd(c, key)
print(ans)

Solution (Python):
from math import gcd
k=int(input())
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        ans_=gcd(i,j)
        for l in range(1,k+1):
            ans+=gcd(ans_,l)
print(ans)

Solution (Python):
import math


k = int(input())

sum_gcd = 0

for a in range(1, k + 1):
    for b in range(1, k + 1):
        gcd_ab = math.gcd(a, b)

        for c in range(1, k + 1):
            sum_gcd += math.gcd(gcd_ab, c)

print(sum_gcd)

Solution (Python):
from math import gcd

K = int(input())

ans = 0
r = range(1, K+1)
for i in r:
    for j in r:
        for k in r:
            ans += gcd(gcd(i, j), k)

print(ans)

Solution (Python):
import math

k = int(input())
ans = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        num = math.gcd(i, j)
        for k in range(1, k+1):
            ans += math.gcd(num, k)
print(ans)

Solution (Python):
import math
from functools import reduce

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

K = int(input())
ans = 0

for a in range(1,K+1):
  for b in range(1,K+1):
    for c in range(1,K+1):
      ans += gcd(a,b,c)
      

print(ans)

Solution (Python):
def main():
    from math import gcd
    K = int(input())
    ans = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            g = gcd(a, b)
            for c in range(1, K+1):
                ans += gcd(g, c)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math

K = int(input())

ans = 0
for a in range(1,K+1):
  for b in range(1,K+1):
    for c in range(1,K+1):
      ans += math.gcd(math.gcd(a,b),c)

print(ans)

Solution (Python):

import itertools
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
    
def main():
    k = int(input())
    array = list(range(1, k+1))
    ans = sum(array)
    for i in range(2, 4):
        for x in itertools.combinations(array, i):
            x = list(x)
            tmp = gcd(*x)
            ans += tmp * 6
    print(ans)
    
main()

Solution (Python):
import math
from functools import reduce
def gcd(*numbers):
  return reduce(math.gcd, numbers)

K = int(input())
ans = 0

for a in range(1, K+1):
  for b in range(1, K+1):
    for c in range(1, K+1):
      ans += gcd(a, b, c)
print(ans)

Solution (Python):
import math
from functools import reduce

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

k = int(input())
ans = 0
for a in range(1, k+1):
  for b in range(a, k+1):
    for c in range(b, k+1):
      t = len({a,b,c})
      if t == 3:
        ans += gcd(a,b,c) * 6
      elif t == 2:
        ans += gcd(a,b,c) * 3
      else:
        ans += gcd(a,b,c)
        
print(ans)

Solution (Python):
import math

K = int(input())
wa = []

for i in range(1,K+1):
    for j in range(1,K+1):
        double = math.gcd(i,j)
        for k in range(1,K+1):
            triple = math.gcd(double,k)
            wa.append(triple)

print(sum(wa))

Solution (Python):
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors


k = ini()
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        d = gcd(a,b)
        for c in range(1,k+1):
            ans += gcd(d,c)
print(ans)

Solution (Python):
import math
k = int(input())
k += 1

ans = 0
for a in range(1, k):
    for b in range(1, k):
        for c in range(1, k):
            ans += math.gcd(a, math.gcd(b, c))

print(ans)

Solution (Python):
import math

K = int(input())
s = 0
for i in range(K):
  for j in range(K):
    x = math.gcd(i + 1, j + 1)
    for k in range(K):
      s += math.gcd(x, k + 1)

print(s)

Solution (Python):
import numpy as np
K = int(input())
k = np.arange(1,K+1, dtype=np.int64)
print(np.gcd.outer(np.gcd.outer(k,k),k).sum())

Solution (Python):
import math
from functools import reduce
K = int(input())

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

ans = 0
for a in range(1,K+1):
  if (a==1):
    ans += K*K
    continue
  for b in range(1,K+1):
    t = gcd(a,b)
    if(t==1):
      ans += K
      continue
    for c in range(1,K+1):
      ans += gcd(t,c)
print(ans)

Solution (Python):
import math
from functools import reduce
K = int(input())

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

ans = 0
for a in range(1,K+1):
  if (a==1):
    ans += K*K
    continue
  for b in range(1,K+1):
    t = gcd(a,b)
    if(t==1):
      ans += K
      continue
    for c in range(1,K+1):
      ans += gcd(t,c)
print(ans)

Solution (Python):
import math
a = int(input())
lis =[]
ans = 0
for x in range(1,a+1):
    for y in range(1, a+1):
        lis.append(math.gcd(x,y))
for z in range(1, a+1):
    for l in lis:
        ans += math.gcd(z,l)
print(ans)

Solution (Python):
import math
k = int(input())
su = 0
lis = []
for x in range(1, k+1):
  for y in range(1, k+1):
    a = math.gcd(x, y)
    lis.append(a)

for z in range(1, k+1):
  for p in lis:
    ans = math.gcd(p, z)
    su += ans
  
print(su)

Solution (Python):
import math
k = int(input())
dec = {}

for a in range(1, k+1):
    for b in range(1, k+1):
        if math.gcd(a, b) not in dec:
            dec[math.gcd(a,b)] = 1
        else:
            dec[math.gcd(a,b)] += 1
ans = 0
for c in range(1, k+1):
    for d in dec.keys():
        ans += math.gcd(d, c) * dec[d]
print(ans)

Solution (Python):
import math
k = int(input())
ans = 0

for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            ans += math.gcd(math.gcd(a, b), c)
print(ans)

Solution (Python):
def solve():
    import math
    K = int(input())
    ans = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            for k in range(1, K+1):
                ans += math.gcd(math.gcd(i,j), k)
    print(ans)

if __name__ == "__main__":
    solve()

Solution (Python):

def main():
    from itertools import product
    import math

    K = int(input())

    ans = 0
    for a, b, c in product(range(1, K + 1), repeat=3):
        ans += math.gcd(math.gcd(a, b), c)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math 
k=int(input())
ans=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for k in range(1,k+1):
      ans+=(math.gcd(math.gcd(i,j),k))
print(ans)

Solution (Python):
import numpy as np
K = int(input())
k = np.arange(1,K+1)
print(np.sum(np.gcd.outer(np.gcd.outer(k,k),k)))

Solution (Python):
K = int(input())

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

dec = {}
for a in range(1,K+1):
    for b in range(1, K+1):
        if gcd(a,b) not in dec:
            dec[gcd(a,b)] = 1
        else:
            dec[gcd(a,b)] += 1
ans = 0
for c in range(1, K+1):
    for key in dec.keys():
        ans += gcd(c, key)*dec[key]

print(ans)

Solution (Python):
from math import gcd

k = int(input())
ans = 0

for a in range(1,k+1):
    for b in range(1,k+1):
        n = gcd(a,b)
        for c in range(1,k+1):
            ans += gcd(n,c)
print(ans)

Solution (Python):
import math
k = int(input())
ans = 0
for q in range(1,k+1):
    for r in range(1,k+1):
        x = math.gcd(q,r)
        for s in range(1,k+1):
            ans += math.gcd(x,s)

print(ans)

Solution (Python):
import math

N=int(input())
sum=0
for i in range(1,N+1):
  for j in range(1,N+1):
    x=math.gcd(i,j)
    for k in range(1,N+1):
      y=math.gcd(x,k)
      sum = sum+y
print(sum)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
from functools import reduce
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

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

K = I()
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            ans += gcd(i, j, k)
print(ans)

Solution (Python):
K=int(input())
ans=0
 
import math
 
for i in range(1,K+1):
  for j in range(1,K+1):
    x = math.gcd(i,j)
    for k in range(1,K+1):
      ans += math.gcd(x,k)

print(ans)

Solution (Python):
import math

sum = 0
K = int(input())

for a in range(1, K+1):
	for b in range(1, K+1):
		x = math.gcd(a, b)
		for c in range(1, K+1):
			sum += math.gcd(x, c)

print(sum)

Solution (Python):
k=int(input())
import math
def ggcd(a,b,c):
    return math.gcd(math.gcd(a,b),math.gcd(a,c))
a=0
for i in range(1,1+k):
    for j in range(1,1+k):
        for l in range(1,1+k):
            a+=ggcd(i,j,l)
print(a)

Solution (Python):
import array

K = int(input())
s = 0

cache = [1] * K


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


for i in range(1, K+1):
    cache[i-1] = [1] * K
    for j in range(i, K+1):
        cache[i-1][j-1] = array.array('i', [1] * K)
        for k in range(j, K+1):
            cache[i-1][j-1][k-1] = gcd(k, gcd(j, i))

for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            a, b, c = sorted([i, j, k])
            s += cache[a-1][b-1][c-1]

print(s)

Solution (Python):
K = int(input())
s = 0

cache = {}


def gcd(a, b):
    key = (a, b)
    r = cache.get(key, None)
    if r:
        return r
    while b != 0:
        a, b = b, a % b
    cache[key] = a
    return a


for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            a, b, c = tuple(sorted([i, j, k]))
            s += gcd(a, gcd(b, c))

print(s)

Solution (Python):
k = int(input())
from math import gcd

ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for k in range(1,k+1):
            ans += gcd(gcd(i,j), k)
print(ans)

Solution (Python):
import math
k=int(input())
ans=0

for a in range(1,k+1):
    for b in range(1,k+1):
        x=math.gcd(a,b)
        for c in range(1,k+1):
            ans += math.gcd(x,c)

print(ans)

Solution (Python):
def gcd(p, q):
    if (p%q == 0):
        return q
    return gcd(q, p%q)
K = int(input())
result = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            result += gcd(gcd(i, j), k)
print(result)

Solution (Python):
import math
K = int(input())
result = 0

for i in range(1, K+1):
    result += i

d = {}
for i in range(1, K+1):
    for j in range(i+1, K+1):
        d[(i, j)] = math.gcd(i, j)
        result += 6 * d[(i, j)]

for i in range(1, K+1):
    for j in range(i+1, K+1):
        for k in range(j+1, K+1):
            tmp = 0
            if i < j:
                tmp = d[(i, j)]
            else:
                tmp = d[(j, i)]
            if tmp < k:
                result += 6 * d[(tmp, k)]
            else:
                result += 6 * d[(k, tmp)]
print(result)

Solution (Python):
def gcd(a, b):
    if a < b:
        a, b = b, a
    while b:
        a, b = b, a % b
    return a

k = int(input())
sum = 0
for a in range(k):
    for b in range(k):
        gcd_ab = gcd(a + 1, b + 1)
        for c in range(k):
            sum += gcd(gcd_ab, c + 1)
print(sum)

Solution (Python):
import math
k = int(input())
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        res = math.gcd(a,b)
        for c in range(1,k+1):
            ans += math.gcd(res, c)

print(ans)

Solution (Python):
import math
K=int(input())
ans=0
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      ans+=math.gcd(math.gcd(i,j),k)
print(ans)