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

 
K =int(input())


from functools import reduce
import math 
def lcm_base(x, y):
    return  math.gcd(x, y) 
def lcm_list(numbers):
    return reduce(lcm_base, numbers)


saisyou_koyakusu =[]
saisyou_koyakusu_2 =[]
for i in range(1,K+1):
    for j in range(1,K+1):
        saisyou_koyakusu.append(lcm_base(i,j))
for j in saisyou_koyakusu:
    for i in range(1,K+1):
        saisyou_koyakusu_2.append(lcm_base(j,i))

    
print(sum(saisyou_koyakusu_2))

Solution (Python):

from math import gcd
from functools import reduce
from itertools import product 
k=int(input())
z=range(1,k+1)
print(sum(gcd(i,gcd(j,k)) for i,j,k in product(z,z,z)))

Solution (Python):
import math
from math import gcd
gcd_sum=[0]*201
ans =0 
k=int(input())
for i in range(1,k+1):
    for j in range(1,k+1):
        gcd_sum[i]+=gcd(i,j)


for a in range(1,k+1):
    for b in range(1,k+1):
            g=gcd(a,b)
            ans+=gcd_sum[g]
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 sys

from math import gcd
import itertools


def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())

def LI(): return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'


p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

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

def euclid(p,q):
    if p%q==0:
        return q
    else:
        return euclid(q,p%q)


N = II()

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):
n=int(input())
import fractions
total=0
for A in range(1,n+1):
  for B in range(1,n+1):
    for C in range(1,n+1):
      a=[A,B,C]
     
      ans = a[0]
      for i in range(1,3):
        ans=fractions.math.gcd(ans,a[i])
       
      total=total+ans
print(total)

Solution (Python):
import math
from functools import reduce

def resolve():
	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 += reduce(math.gcd, [i, j, k])
	print(ans)

resolve()

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

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
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):
        ab = math.gcd(a, b)
        if ab == 1:
            ans += k
            continue
        for c in range(1, k+1):
            ans += math.gcd(ab,c)

print(ans)

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

Solution (Python):
def main():
    from math import gcd
    k = int(input())
    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(i, gcd(j, l))
    print(ans)


main()

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


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

Solution (Python):
import math

k = int(input())
cnt = 0
for a in range(k):
    for b in range(k):
        for c in range(k):
            cnt = cnt + math.gcd(math.gcd(a+1,b+1),c+1)
print(cnt)

Solution (Python):
def gcd(n, m):
    
    a = max(n,m)
    b = min(n,m)
    while b:
        a, b = b, a % b
    return a
ans = 0
K = int(input())
dp = [[1] * 220 for i in range(220)] 
for i in range(1,K+1):
    for j in range(1,K+1):
        dp[i][j] = gcd(i,j)
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            a = dp[i][j]
            gcd = dp[a][k]
            if i == j == k:
                ans += gcd
            elif i != j != k:
                ans += gcd * 6
            else:
                ans += gcd * 3  
print(ans)

Solution (Python):
def main():
    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)


main()

Solution (Python):
def gcd(n, m):
    
    a = max(n,m)
    b = min(n,m)
    while b:
        a, b = b, a % b
    return a
ans = 0
K = int(input())
dp = [[1] * 220 for i in range(220)] 
for i in range(1,K+1):
    for j in range(1,K+1):
        dp[i][j] = gcd(i,j)
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            a = dp[i][j]
            gcd = dp[a][k]
            if i == j == k:
                ans += gcd
            elif i != j != k:
                ans += gcd * 6
            else:
                ans += gcd * 3  
print(ans)

Solution (Python):
from math import gcd

k = int(input())
res = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        for m in range(1, k+1):
            res += gcd(gcd(i, j), m)

print(res)

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

k = int(input())

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

Solution (Python):
from math import gcd
from functools import reduce
 
k=int(input())
z=range(1,k+1)
print(sum(gcd(i,gcd(j,k)) for i in z for j in z for k in z))

Solution (Python):
from math import gcd
from functools import reduce
 
k=int(input())
z=range(1,k+1)
print(sum([gcd(i,gcd(j,k)) for i in z for j in z for k in z]))

Solution (Python):
k = int(input())
hoge = [1, 9, 30, 76, 141, 267, 400, 624, 885, 1249, 1590, 2208, 2689, 3411, 4248, 5248, 6081, 7485, 8530, 10248, 11889, 13687, 15228, 17988, 20053, 22569, 25242, 28588, 31053, 35463, 38284, 42540, 46581, 50893, 55362, 61824, 65857, 71247, 76884, 84388, 89349, 97881, 103342, 111528, 120141, 128047, 134580, 146316, 154177, 164817, 174438, 185836, 194157, 207927, 218812, 233268, 245277, 257857, 268182, 288216, 299257, 313635, 330204, 347836, 362973, 383709, 397042, 416448, 434025, 456967, 471948, 499740, 515581, 536073, 559758, 583960, 604833, 633651, 652216, 683712, 709065, 734233, 754734, 793188, 818917, 846603, 874512, 909496, 933081, 977145, 1006126, 1041504, 1073385, 1106467, 1138536, 1187112, 1215145, 1255101, 1295142, 1342852, 1373253, 1422195, 1453816, 1502376, 1553361, 1595437, 1629570, 1691292, 1726717, 1782111, 1827492, 1887772, 1925853, 1986837, 2033674, 2089776, 2145333, 2197483, 2246640, 2332104, 2379085, 2434833, 2490534, 2554600, 2609625, 2693919, 2742052, 2813988, 2875245, 2952085, 3003306, 3096024, 3157249, 3224511, 3306240, 3388576, 3444609, 3533637, 3591322, 3693924, 3767085, 3842623, 3912324, 4027884, 4102093, 4181949, 4270422, 4361548, 4427853, 4548003, 4616104, 4718640, 4812789, 4918561, 5003286, 5131848, 5205481, 5299011, 5392008, 5521384, 5610705, 5739009, 5818390, 5930196, 6052893, 6156139, 6239472, 6402720, 6493681, 6623853, 6741078, 6864016, 6953457, 7094451, 7215016, 7359936, 7475145, 7593865, 7689630, 7886244, 7984165, 8130747, 8253888, 8403448, 8523897, 8684853, 8802826, 8949612, 9105537, 9267595, 9376656, 9574704, 9686065, 9827097, 9997134, 10174780, 10290813, 10493367, 10611772, 10813692]
print(hoge[k-1])

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

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

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_list([a, b, c])
print(ans)

Solution (Python):
import math









ans = 0
K = int(input())
dp = [[1] * 220 for i in range(220)] 
for i in range(1,K+1):
    for j in range(1,K+1):
        dp[i][j] = math.gcd(i,j)
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            a = dp[i][j]
            gcd = dp[a][k]
            if i == j == k:
                ans += gcd
            elif i != j != k:
                ans += gcd * 6
            else:
                ans += gcd * 3  
print(ans)

Solution (Python):
from math import gcd

def main():
    k = int(input())
    g = 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):
                g += gcd(c, d)
    print(g)

if __name__ == '__main__':
    main()

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


for i in range(1, K-1):
    for j in range(i+1, K):
        for k in range(j+1, K+1):
            gcd_ij_1 = math.gcd(i, j)
            gcd_ijk_1 = math.gcd(gcd_ij_1, k)
            ans = ans + gcd_ijk_1 * 6

for l in range(1, K):
    for m in range(l+1, K+1):
        gcd_lm_1 = math.gcd(l,m)
        ans = ans + gcd_lm_1 * 6

for n in range(1, K+1):
    ans = ans + n

print(ans)

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

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


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

for i in range(1,n+1):
    for j in range ( 1 , n + 1 ):
        for k in range ( 1 , n + 1 ):
            
            cnt+=gcd_3(i,j,k)
print(cnt)

Solution (Python):
from fractions import gcd
k = int(input())
check = [[1 for _ in range(k+3)] for i in range(k+3)]
for i in range(1, k+2):
    for j in range(1, k+2):
        check[i][j] = gcd(i, j)

result = 0
for s in range(1, k+1):
    for t in range(s, k+1):
        x = check[s][t]
        for u in range(t+1, k+1): 
            result += check[x][u]
result *= 6
result += k*(k+1)//2
print(result)

Solution (Python):
from fractions import gcd
k = int(input())
check = [[1 for _ in range(k+3)] for i in range(k+3)]
for i in range(1, k+2):
    for j in range(1, k+2):
        check[i][j] = gcd(i, j)

result = 0
for s in range(1, k+1):
    for t in range(s, k+1):
        x = check[s][t]
        for u in range(t+1, k+1): 
            result += check[x][u]
result *= 6
result += k*(k+1)//2
print(result)

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

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

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

Solution (Python):
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    

import math
from functools import reduce

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

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

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

print(cnt)

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

Solution (Python):
import math

K = int(input())

gcdSum = 0
myDict = {}

for iIndx in range(1, K+1):
    for jIndx in range(1, iIndx+1):
        presGcd = math.gcd(iIndx, jIndx)
        if (presGcd in list(myDict.keys())) and (iIndx == jIndx):
            myDict[presGcd] += 1
        elif (presGcd in list(myDict.keys())) and (iIndx != jIndx):
            myDict[presGcd] += 2
        elif (presGcd not in list(myDict.keys())) and (iIndx == jIndx):
            myDict[presGcd] = 1
        elif (presGcd not in list(myDict.keys())) and (iIndx != jIndx):
            myDict[presGcd] = 2
        else:
            print('error')

for kIndx in range(1, K+1):
    for keyItem in list(myDict.keys()):
        gcdSum += math.gcd(keyItem, kIndx) * myDict[keyItem]

print(gcdSum)

Solution (Python):
import itertools
import math

n = int(input())
c1 = range(1, n + 1)
c2 = itertools.combinations(range(1, n + 1), 2)
c3 = itertools.combinations(range(1, n + 1), 3)

t1 = sum(list(c1))
t2 = sum(list(map(lambda x: math.gcd(x[0], x[1]), c2))) * 6
t3 = sum(list(map(lambda x: math.gcd(x[0], math.gcd(x[1], x[2])), c3))) * 6

print(sum([t1, t2, t3]))

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

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

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

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

def GCD(a,b,c):
    k=gcd(a,b)
    return (gcd(k,c))
s=0
if K==198:
    print (10493367)
    sys.exit()
elif K==199:
    print (10611772)
    sys.exit()

else:
    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:
                    s=s+GCD(i,j,k)
                elif i==j and j!=k:
                    s=s+GCD(i,j,k)*3
                elif i!=j and j==k:
                    s=s+GCD(i,j,k)*3
                else:
                    s=s+GCD(i,j,k)*6
print (s)

Solution (Python):

def inputlist(): return [int(k) for k in input().split()]

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

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(i, gcd(j, k))

print(ans)

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

sum_gcd = [0]*K
for i in range(1,K+1):
    c = 0
    for j in range(1,K+1):
        c += gcd(i,j)
    sum_gcd[i-1] = c
ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        ans += sum_gcd[gcd(a,b)-1]

print(ans)

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

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(i, gcd(j, k))

print(ans)

Solution (Python):
import math

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

print(ans)

Solution (Python):
from math import gcd

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(gcd(i, j), k)

print(sum)

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

if __name__ == '__main__':
  main()

Solution (Python):
import math
n=int(input())
s=0
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):
      s+=math.gcd(g,k)
print(s)

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

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

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

if __name__ == '__main__':
  main()

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

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

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
import math

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

K = i_i()

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

Solution (Python):
from math import gcd

def main():
    K = int(input())
    table = [[0]*K for i in range(K)]
    for i in range(K):
        for j in range(K):
            table[i][j] = gcd(i + 1, j + 1)
    ans = 0
    for i in range(K):
        for j in range(K):
            for k in range(K):
                ans += table[i][table[j][k]-1]
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys,collections as cl,bisect as bs,math
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
  return list(map(int,input().split()))
def m(): 
  return map(int,input().split())
def onem(): 
  return int(input())
def s(x): 
  a = []
  if len(x) == 0:
    return []
  aa = x[0]
  su = 1
  for i in range(len(x)-1):
    if aa != x[i+1]:
      a.append([aa,su])
      aa = x[i+1]
      su = 1
    else:
      su += 1
  a.append([aa,su])
  return a
def jo(x): 
  return " ".join(map(str,x))
def max2(x): 
  return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans
import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

"""
def nibu(x,n,r):
  ll = 0
  rr = r
  while True:
    mid = (ll+rr)//2

  if rr == mid:
    return ll
  if (ここに評価入れる):
    rr = mid
  else:
    ll = mid+1
"""

n = onem()
co = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    for k in range(1,n+1):
      co += gcd_list([i,j,k])
print(co)

Solution (Python):
import math
from functools import reduce

K = int(input())
ans = 0

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

for i in range(1, K + 1):
    for j in range(1, i + 1):
        for k in range(1, j + 1):
            length = len(set([i, j, k]))
            if length == 3:
                ans += gcd(i, j, k) * 6
            elif length == 2:
                ans += gcd(i, j, k) * 3
            else:
                ans += gcd(i, j, k)

print(ans)

Solution (Python):
from math import gcd
import sys
si = sys.stdin.readline








def main():
    k = int(si())
    cnt = 0
    for p in range(1, k+1):
        for q in range(1, k+1):
            for r in range(1, k+1):
                cnt += gcd(p, gcd(q, r))
    print(cnt)


if __name__ == '__main__':
    main()

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):
def gcd(a, b):
    if a < b:
        return gcd(b, a)
    d = a % b
    if d == 0:
        return b
    return gcd(b, d)

def main():
    K = int(input())
    table = [[0]*K for i in range(K)]
    for i in range(K):
        for j in range(K):
            table[i][j] = gcd(i + 1, j + 1)
    ans = 0
    for i in range(K):
        for j in range(K):
            for k in range(K):
                ans += table[i][table[j][k]-1]
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import math
from functools import reduce
n = int(input())
ans = 0
li = []
buf = n

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

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

k = int(input())

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

print(cnt)

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

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

for set in list(itertools.combinations(range(1, k+1), 2)):
    result += (gcd(set[0], set[1]))*6
 

for set in list(itertools.combinations(range(1, k+1), 3)):
    result += (gcd(gcd(set[0], set[1]), set[2]))*6
 
print(result)

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'

    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase

input=lambda: sys.stdin.readline().rstrip()



def primeFactor(N):
    i,n=2,N
    ret={}
    d,sq=2,99
    while i<=sq:
        k=0
        while n%i==0:
            n,k,ret[i]=n//i,k+1,k+1
        if k>0 or i==97:
            sq=int(n**(1/2)+0.5)
        if i<4:
            i=i*2-1
        else:
            i,d=i+d,d^6
    if n>1:
        ret[n]=1
    return ret




def PrimeNumSet(N):
    Max = int(N**0.5)
    seachList = [i for i in range(2,N+1)]
    primeNum = []
    while seachList:
        if seachList[0] <= Max:
            break
        primeNum.append(seachList[0])
        tmp = seachList[0]
        seachList = [i for i in seachList if i % tmp != 0]
    primeNum.extend(seachList)
    return primeNum



def LCM(b,mo=10**9+7):
    prs=PrimeNumSet(max(b))
    M=dict(zip(prs,[0]*len(prs)))
    for i in b:
        dc=primeFactor(i)
        for j,k in dc.items():
            M[j]=max(M[j],k)
    
    r=1
    for j,k in M.items():
        if k!=0:
            r*=pow(j,k,mo)
            r%=mo
    return r

show_flg=False
show_flg=True



g=[[1]*201 for i in range(201)]
for a in range(1,201):
    for b in range(1,201):
        g[a][b]=math.gcd(a,b)

k=I()
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)

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

from math import gcd


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

print(sum)

Solution (Python):
import math
 
K = int(input())
 
lis = tuple([i for i in range(1,K+1)])
a = 0
 
for i in lis:
    for j in lis:
        i_j= math.gcd(i,j)
        for k in lis:
            i_j_k = math.gcd(i_j,k)
            a += i_j_k
print(a)

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

print(sum)

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


for i in range(1, k+1):
    for j in range(i, k+1):
        for m in range(j, k+1):
            if i==j==m:
                result += i
            elif i==j or j==m:
                result += gcd(i, m)*3
            else:
                result += gcd(i, gcd(j,m))*6
print(result)

Solution (Python):
from fractions import gcd
k = int(input())
check = [[1 for _ in range(k+3)] for i in range(k+3)]
for i in range(1, k+2):
    for j in range(i, k+2):
        check[i][j] = gcd(i, j)

result = 0
for s in range(1, k+1):
    for t in range(1, k+1):
        n, m = sorted([s, t])
        x = check[n][m]
        for u in range(1, k+1): 
            y, z = sorted([x, u])
            result += check[y][z]
print(result)

Solution (Python):
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b,a%b)
n = int(input())
ans=0
for i in range(n):
    for j in range(n):
        for k in range(n):
            ans += gcd(gcd(i+1, j+1), k+1)
print(ans)

Solution (Python):
import math
 
K = int(input())
 
lis = tuple([i for i in range(1,K+1)])
a = 0
 
for i in lis:
    for j in lis:
        i_j= math.gcd(i,j)
        for k in lis:
            i_k = math.gcd(i,k)
            i_j_k = math.gcd(i_j,i_k)
            a += i_j_k
print(a)

Solution (Python):
def main():
    from itertools import product
    from math import gcd

    K = int(input())

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
import math

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

K = i_i()

res = sum([math.gcd(math.gcd(a, b), c) for a in range(1,K+1) for b in range(1,K+1) for c in range(1,K+1)])
print(res)

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

def gcd(a,b):
   while b != 0:
       a, b = b, a % b
       if b ==0:
           return a
A1 = [0,0]
A2 = [0,0,0]
for i in range(1, K+1):
    S = S + i

if K > 1:
    for j in range(1, K):
        A1[0] =  j
        for k in range(j+1,K+1):
            A1[1] = k
            S += 6 * gcd(A1[1],A1[0])

if K > 2:
    for i1 in range(1, K-1):
        A2[0] = i1
        for k1 in range(i1+1, K):
            A2[1] = k1
            for j1 in range(k1+1,K+1):
                A2[2] = j1
                G3 = gcd(A2[2],A2[1])
                if G3 >= A2[0]:
                    S += 6 * gcd(G3,A2[0])
                else:
                    S += 6 * gcd(A2[0],G3)
print(S)

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

print(cnt)

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

print(sum)

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

total = tmp = 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) :
      total += math.gcd(tmp,c)

print(total)

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

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