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
n =int(input())
c=0
for i in range(1,n+1):
    for j in range(1,n+1):
        d= math.gcd(i,j)
        for k in range(1,n+1):
            c+=math.gcd(d,k)
print(c)

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

Solution (Python):
import math
from functools import reduce

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

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

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

def gcd2(a,b): 
  while True:
    mod = a % b
    if mod == 0:
      return b
    a = b
    b = mod

memo = {}
def gcd(a,b,c):
  return gcd2(gcd2(a,b),c)







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

      total += g

print(total)

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

ans=0
for i in range(k):
  for j in range(k):
    q=math.gcd(i+1,j+1)
    for l in range(k):
      ans+=math.gcd(q,l+1)
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):
    g=math.gcd(a,b)
    for c in range(1,K+1):
      ans+=math.gcd(g,c)
print(ans)

Solution (Python):
import math
K =  int(input())
ans = []
bc = [math.gcd(b, c) for b in range(1, K+1) for c in range(1, K+1)]
for a in range(1, K+1):
    for i in bc:
        x = math.gcd(a, i)
        ans.append(x)
print(sum(ans))

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from math import gcd

K = int(input())

one = 0
two = 0
three = 0
for i in range(1,K-1):
    for j in range(i+1,K):
        for k in range(j+1,K+1):
            d = gcd(i,j)
            d = gcd(d,k)
            three += d

for i in range(1,K):
    for j in range(i+1,K+1):
        d = gcd(i,j)
        two += d
for i in range(1,K+1):
    one += i

ans = one + two * 6 + three * 6
print(ans)

Solution (Python):
import math

K = int(input())

g = []
for i in range(1, K+1):
    for j in range(1, K+1):
        g.append(math.gcd(i, j))

result = 0
for i in range(1, K+1):
    for t in g:
        result += math.gcd(i, t)

print(result)

Solution (Python):
import math
from functools import reduce


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


total = 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):
            total += gcd(i, j, k)

print(total)

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

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):
    for k in range(1,n+1):
      ans += gcd(gcd(i,j),k)
print(ans)

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):
    for k in range(1,n+1):
      ans += gcd(gcd(i,j),k)
print(ans)

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

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):
    g = gcd(i, j)
    for k in range(1, k + 1):
      ans += gcd(g, k)
print(ans)

Solution (Python):
import itertools as itt
import math

k = int(input())

ans = 0
for i in itt.combinations_with_replacement(range(1, k+1), 3):
    if i[0] == i[1] and i[0] != i[2]:
        ans += 3 * math.gcd(math.gcd(i[0], i[1]), i[2])
    elif i[1] == i[2] and i[1] != i[0]:
        ans += 3 * math.gcd(math.gcd(i[0], i[1]), i[2])
    elif i[0] == i[1] and i[0] == i[2]:
        ans += math.gcd(math.gcd(i[0], i[1]), i[2])
    else:
        ans += 6 * math.gcd(math.gcd(i[0], i[1]), i[2])

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):
        for k in range(1,k+1):
            ans += gcd(gcd(i,j),k)

print(ans)

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):
        for k in range(1, n + 1):
            ans += gcd(gcd(i, j), k)

print(ans)

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

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):
			temp=a
			temp=gcd(temp, b)
			temp=gcd(temp, c)
			ans+=temp
print(ans)

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):
    x = gcd(i,j)
    for k in range(1,n+1):
        ans += gcd(x,k)
print(ans)

Solution (Python):
import math
k = int(input())
sum = 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):
            sum += math.gcd(x, c)

print(sum)

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

for a in range(1,k+1):
    for b in range(1,k+1): 
        for c in range(1,k+1):
            sum += gcd(gcd(a, b), c)

print(sum)

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

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

print(sum)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

from math import *

K=k()
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(k,gcd(i,j))
print(ans)

Solution (Python):
def N():
    return int(input())
def L():
    return list(map(int,input()))
def NL(n):
    return [list(map(int,input())) for _ in range(n)]
import math
k = N() + 1
sum = 0
for i in range(1,k):
    for j in range(1,k):
        for l in range(1,k):
            
            sum += math.gcd(math.gcd(i,j),l)
print(sum)

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

Solution (Python):
from math import *

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+=gcd(k,gcd(i,j))
print(ans)

Solution (Python):
import math

N=int(input())
ans=0
for i in range(1,N+1):
    for j in range(i,N+1):
        for k in range(j,N+1):
            if i==j and j==k:
                ans+=i
            elif i<j and j<k:
                ans+=6*math.gcd(i,math.gcd(j,k))
            else:
                ans+=3*math.gcd(i,math.gcd(j,k))
print(ans)

Solution (Python):
from math import gcd
K=int(input())
g=0
for a in range(1,K-1):
  for b in range(a+1,K):
    for c in range(b+1,K+1):
      g+=gcd(gcd(a,b),c)*6
for  d in range(1,K):
  for e in range(d+1,K+1):
    g+=gcd(d,e)*6
g+=K*(K+1)//2
print(g)

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

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

Solution (Python):
import math
def resolve():
    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)
resolve()

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

for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):

            sum_gcd += math.gcd(math.gcd(a,b) , c)

print(sum_gcd)

Solution (Python):
from math import gcd

k = int(input())

ans = 0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            ans += gcd(gcd(x,y),z)

print(ans)

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

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):
K = int(input())
import math
from functools import reduce

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


temp = []

for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            temp.append(gcd(i,j,k))
print(sum(temp))

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

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

result = 0

for a in range(1, K+1):
    result += a
    
    b = a + 1
    while b <= K:
        result += 6 * gcd(a, b)
        
        l = gcd(a, b)
        c = b + 1
        while c <= K:
            result += 6 * gcd(l, c)
            
            c+= 1
        b += 1
print(result)

Solution (Python):
import math


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 += math.gcd(a, math.gcd(b, c))

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd

k = int(input())


ans = 0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            ans += gcd(gcd(x,y),z)

print(ans)

Solution (Python):
k = int(input())
ans = 0
def gcd(a,b):
    if a % b == 0:
        return b
    c = a % b
    return gcd(b,c)

for l in range(1,k+1):
    for m in range(l,k+1):
        for n in range(m,k+1):
            tmp1 = gcd(l,n)
            tmp2= gcd(tmp1,m)
            if (l==m==n):
                ans+=tmp2
            elif(l==m or m==n):
                ans+= 3*tmp2
            else:
                ans += 6*tmp2


print(ans)

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


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):
            d = gcd(a, b)
            if len({a, b, c}) == 1:
                ans += gcd(c, d)
            elif len({a, b, c}) == 2:
                ans += 3 * gcd(c, d)
            else:
                ans += 6 * gcd(c, d)
print(ans)

Solution (Python):
def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b,a%b)
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):
      d = gcd(a, b)
      if len({a,b,c}) == 1:
        ans += gcd(c, d)
      elif len({a,b,c}) == 2:
        ans += 3*gcd(c, d)
      else:
        ans += 6*gcd(c, d)
print(ans)

Solution (Python):
import math
y=int(input())
cnt=0
for i in range (1,y+1):
    for k in range(1,y+1):
        for j in range(1,y+1):
            a = [i,j,k]
            ans = a[0]
            for x in range(len(a)):
                ans = math.gcd(ans, a[x])
            cnt+=ans
print(cnt)

Solution (Python):
from math import gcd
K = int(input())
ans = 0
for x in range(1, K+1):
    for y in range(1, K+1):
        for z in range(1, K+1):
            ans += gcd(gcd(x,y),z)
print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict,deque

input = sys.stdin.readline
def inar():
    return [int(el) for el in input().split()]
def find(a,b,c):
    gc=math.gcd(a,b)
    return math.gcd(gc,c)
def main():
    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+=find(i,j,k)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math

K = int(input())

sum = 0
for i in range(1,K+1):
  for j in range(1,K+1):
    temp = math.gcd(i,j)
    for k in range(1,K+1):
      sum+=math.gcd(temp,k)
print (sum)

Solution (Python):
import math

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

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

Solution (Python):
N = int(input())
kotae = []
for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N+1):
            s = i
            t = j
            u = k
            x = 1
            while x != 0:
                v = t%s
                if v != 0:
                    t = s
                    s = v
                else:
                    t = s
                    x = u%t
                    if x != 0:
                        u = t
                        t = x
                    else:
                        kotae.append(t)
print(sum(kotae))

Solution (Python):
k = int(input())
def gcd1 (a, b):
    while True:
        if (a < b):
            a, b = b, a
        c = a%b
        if (c == 0):
            return (b)
        else:
            a = b
            b = c

def gcd2 (a, b, c):
    tmp = gcd1(a, b)
    ans = gcd1(tmp, c)
    return (ans)

count = 0
for i in range(k):
    for j in range(i, k):
        for l in range(j, k):
            tmp = gcd2(i + 1, j + 1, l + 1)
            if (i == j == l):
                count = count + tmp
            elif (i == j or j == l):
                count = count + tmp*3
            else:
                count = count + tmp*6
print(count)

Solution (Python):
k = int(input())
def gcd1 (a, b):
    while True:
        if (a < b):
            a, b = b, a
        c = a%b
        if (c == 0):
            return (b)
        else:
            a = b
            b = c

count = 0
for i in range(k):
    for j in range(k):
        tmp = gcd1(i + 1, j + 1)
        if (tmp == 1):
            count = count + k
        else:
            for l in range(k):
                tmp2 = gcd1(tmp, l + 1)
                count = count + tmp2

print(count)

Solution (Python):
import math

k = int(input())
ans = 0

for i in range(1,k+1):
    for j in range(1,k+1):
        a = math.gcd(i,j)
        for l in range(1,k+1):
            ans += math.gcd(a,l)

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):
    for c in range(1,k+1):
      ans += gcd(gcd(a,b),c)
print(ans)

Solution (Python):
import math
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):
            s = math.gcd(a, b)
            t = math.gcd(s, c)
            if a == c:
                ans += t
            elif (a == b or b == c) and a != c:
                ans += 3*t
            else:
                ans += 6*t
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):
    tmp=math.gcd(a,b)
    for c in range(1,k+1):
      ans+=math.gcd(tmp,c)

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):
        tmp = math.gcd(a, b)
        for c in range(1, K+1):
            ans += math.gcd(tmp, c)

print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
    k = int(input())

    k2 = np.arange(1, k+1)
    k2gcd = np.gcd.outer(k2, np.gcd.outer(k2, k2))
    print(k2gcd.sum())

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from math import ceil as C, floor as F, sqrt, gcd as G
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data) 

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()

ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
           ans += G(i, G(j, k))

print(ans)

Solution (Python):
import sys
from math import ceil as C, floor as F, sqrt, gcd as G
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data) 

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()

ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
           ans += G(i, G(j, k))

print(ans)

Solution (Python):
n=int(input())
def gcd(a,b):
    if b==0: return a
    else:
        return gcd(b,a%b)
def lcm(a,b):
    return a*b//gcd(a,b)


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

Solution (Python):
n = int(input())
memo = [[0] * (n+1) for x in range(n+1)]

def gcd(x,y):
  if y == 0:
    return x
  if not memo[x][y] == 0:
    return memo[x][y]
  memo[x][y] = gcd(y,x % y)
  return gcd(y,x % y)

res = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    q = gcd(i,j)
    if q == 1:
      res += n
    else:
      for k in range(1,n+1):
        p = gcd(q,k)
        res += p
print(res)

Solution (Python):
import math

K = int(input())
sum = 0

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):
      sum += math.gcd(tmp, k)
      
print(sum)

Solution (Python):
import math
K = int(input())
rst = 0
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):
import functools
import math
def gcd_2(*vals):
    return functools.reduce(math.gcd, vals)
K = int(input())
ANS = 0
L = list(range(1, K+1))
M = list(range(1, K+1))
N = list(range(1, K+1))



for i in range(K):
    for j in range(K):
        for k in range(K):
            ANS = ANS + gcd_2(L[i], M[j], N[k])

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):
        for c in range(1,K+1):
            result+=gcd(gcd(a,b),c)
print(result)

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


if __name__ == "__main__":
    main()

Solution (Python):
import math
K = int(input())
rst = 0
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
def main():
    ans = 0
    k = int(input())
    for i in range(k):
        for j in range(k):
            for l in range(k):
                ans += gcd(i+1,gcd(j+1,l+1))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math
answer = 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):
      answer += math.gcd(math.gcd(a, b), c)
print(answer)

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


total = 0

for x in range(1,k+1):
    for y in range(x,k+1):
        for z in range(y,k+1):
            if x == y == z:
                total += x
            elif x == y or y == z:
                total += 3 * math.gcd(math.gcd(x,y),z)
            else:
                total += 6 * math.gcd(math.gcd(x,y),z)

print(total)

Solution (Python):
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")


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 += math.gcd(math.gcd(i,j),k)

print(ans)

Solution (Python):
import math
K = int(input())
rst = 0
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):


import math

k = int(input())
s = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        gcdab = math.gcd(a,b)
        if  gcdab== 1:
            s += k
        else:
            for c in range(1,k+1):
                s += math.gcd(gcdab,c)

print(s)

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):
        tmp = gcd(i, j)
        if tmp == 1:
            ans += tmp*K
        else:
            for k in range(1, K+1):
                ans += gcd(tmp, k)
            

print(ans)

Solution (Python):
K = int(input())
def gcd(a, b):
    if a < b:
        return gcd(b, a)
    elif a % b == 0:
        return b
    else:
        r = a % b
        return gcd(b, r)

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

print(ans)

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

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

ans = 0
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):
            ans += math.gcd(a, k)
print(ans)

Solution (Python):
from math import gcd


def main():
    K = int(input())
    ans = 0

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


if __name__ == '__main__':
    main()

Solution (Python):
import math
K = int(input())
rst = 0
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):
import math

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

print(ans)

Solution (Python):
import math
K = int(input())
rst = 0
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

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(gcd(a,b),c)

print(ans)

Solution (Python):
import math
from functools import reduce

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

n = int(input())
sum = 0

for i in range(1, n + 1):
    for j in range(1, n + 1):
        for k in range(1, n + 1):
            sum += gcd(i, j, k)

print(sum)

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):
    for c in range(1, k+1):
      result += gcd(gcd(a, b), c)

print(result)

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

Solution (Python):
import math
K = int(input())
rst = 0
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):
import math

k = int(input())
total = 0
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):
            total += math.gcd(tmp,k)
print(total)

Solution (Python):
from math import gcd

def main():
    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 += gcd(gcd(i, j), k)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd
from functools import reduce

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

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 += gcd_list([i,j,k])
      
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):
k=int(input())
import math
s=0
result=0
for a in range(1,k+1,1):
    for b in range(a,k+1,1):
        for c in range(b,k+1,1):
            gcd=math.gcd(math.gcd(a,b),math.gcd(b,c))
            if a==b==c:
                pass
            elif (a==b and b!=c) or (b==c and b!=a) or (a==c and a!=c):
                gcd=3*gcd
            else:
                gcd=6*gcd
            
            result+=gcd
print(result)

Solution (Python):


from math import gcd as g


k = int(input())
ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        temp = g(a, b)
        for c in range(1, k+1):
            ans += g(temp, c)


print(ans)

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(k, tmp)
print(rst)

Solution (Python):
from math import gcd as g
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 += g(g(a, b), c)
print(ans)

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(k, tmp)
print(rst)

Solution (Python):
import math
K=int(input())
sum =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):
                        sum +=math.gcd(x,c)

                        
                        

print(sum)

Solution (Python):

import sys
import math
from functools import reduce
def input():
    return sys.stdin.readline()[:-1]
def gcd(*numbers):
    return reduce(math.gcd, numbers)

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 += gcd(a, b, c)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
input = sys.stdin.readline

def main():
    k = int(input())
    ans = 0

    for a in range( 1 , k+1 ):
        for b in range( a , k+1 ):
            x = math.gcd( a , b )
            for c in range( b , k+1 ):
                y = math.gcd( x , c )
                if a == b == c:
                    ans += y
                elif a==b or b==c or c==a:
                    ans += 3 * y
                else:
                    ans += 6 * y

    print(ans)



main()

Solution (Python):
from math import gcd 

k=int(input())

cnt=0

for i in range(1,k+1):
    for j in range(1,k+1):
        a=gcd(i,j)
        for k in range(1,k+1):
            cnt+=gcd(a,k)


print(cnt)

Solution (Python):
import math

n = int(input())

s = 0
for i in range(1,n+1):
    for j in range(1,n+1):
        for k in range(1,n+1):
            s += math.gcd(math.gcd(i,j),k)

print(s)

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

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

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

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

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

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

Solution (Python):
from math import gcd


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 += gcd(gcd(a, b), c)

    print(ans)


if __name__ == "__main__":
    main()