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
from functools import reduce

K = int(input())
result = 0

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

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

print(result)

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


def gcd(a, b):
    if b == 0:
        return a
    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
K = int(input())
ans = 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):
        ans += math.gcd(tmp,k)
print(ans)

Solution (Python):
import math

k = int(input())

k += 1
ans = 0

ab = []

for a in range(1, k):
    for b in range(1, k):
       ab.append(math.gcd(a, b))

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

print(ans)

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

Solution (Python):
import math
 
k=int(input())
cnt=0
tmp=[]
for a in range(k):
  a=a+1
  for b in range(k):
    b=b+1
    tmp.append(math.gcd(a,b))
for i in tmp:
  if i==1:
    cnt=cnt+k
  else:
    for j in range(k):
      j=j+1
      cnt=cnt+math.gcd(i,j)
print(cnt)

Solution (Python):
import math

k = int(input())
two = []
total = 0
for x in range(1, k + 1):
	for y in range(1, k + 1):
		two.append(math.gcd(x, y))

for z in range(1, k + 1):
	for i in two:
		total += math.gcd(z, i)
print(total)

Solution (Python):
from math import gcd

K = int(input())
ans = 0
for i in range(1, K + 1):
    ans += i
for i in range(1, K + 1):
    for j in range(i + 1, K + 1):
        ans += gcd(i, j) * 6

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

print(ans)

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

def Main(N):
    ans = 0
    memo2 = [[0 for _ in range(N+1)] for _ in range(N+1)]
    for i in range(1, N+1):
        for j in range(1, N+1):
            if memo2[i][j] == 0:
                memo2[i][j] = gcd(i,j)
            for k in range(1, N+1):
                M = memo2[i][j]
                if memo2[M][k] == 0:
                    memo2[M][k] = gcd(M, k)
                ans += memo2[M][k]
    return ans
    

def main():
    N = int(input())
    print(Main(N))

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

print(ans)

Solution (Python):
from math import gcd

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

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

import math
from functools import reduce

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

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


sum = 0
for i in range(1,K+1):
  for j in range(i+1,K+1):
    for k in range(j+1,K+1):
      sum = sum + 6*gcd(i,j,k)
      
for i in range(1,K+1):
  for j in range(i+1,K+1):
     sum = sum + 6*math.gcd(i,j)
      
sum = sum + K*(K+1)//2

print(sum)

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(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
 
K = int(input())
total = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            total += reduce(math.gcd, [a,b,c])
 
print(total)

Solution (Python):
import math
from functools import reduce

k = int(input())
l = []
s = 0


for a in range(1,k+1):
    for b in range(1,k+1):
        l.append(math.gcd(a, b))

for c in range(1,k+1):
  for i in range(len(l)):
    s += math.gcd(l[i],c)
print(s)

Solution (Python):
import math

K = int(input())
X = [0]*(K+1)

for a in range(1, K+1):
    for b in range(1, K+1):
        g = math.gcd(a, b)
        X[g] += 1


Y = [0]*(K+1)
for x in range(1, K+1):
    for c in range(1, K+1):
        g = math.gcd(x, c)
        Y[g] += X[x]

print(sum(i*Y[i] for i in range(1, K+1)))

Solution (Python):
import fractions

K = int(input())

gcds = []

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

result = 0

for a in range(K):
  for b in range(K):
    gcd_ab = gcds[a][b]
    for c in range(K):
      result += (gcds[gcd_ab-1][c])

print(result)

Solution (Python):





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

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
 
 
def gcd(a, b, c):
  d = math.gcd(a, b)
  return math.gcd(c, d)
 
K = int(input())
sum = 0
for i in range(1, K+1):
  sum += gcd(i, i, i)
  
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:
        sum += 0
      elif i == j or j == k or i == k:
        sum += gcd(i , j , k )*3
      elif i != j and j != k :
        sum += gcd(i, j, k)*6

print(sum)

Solution (Python):
import sys
from math import gcd

K = int(sys.stdin.readline().rstrip())
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 += a
            elif a == b:
                ans += gcd(a, c) * 3
            elif a == c:
                ans += gcd(a, b) * 3
            elif b == c:
                ans += gcd(a, b) * 3
            else:
                ans += gcd(gcd(a, b), c) * 6

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)
count=0
N=int(input())
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 and k==i:
				count+=(gcd_list([i,j,k]))
			elif i==j or j==k or i==k:
				count+=(gcd_list([i,j,k]))*3
			else:
				count+=(gcd_list([i,j,k]))*6

print(count)

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

a=[]
b=0

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

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

print(b)

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

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

print(cnt)

Solution (Python):
import math
N = int(input())
def gcdtriple(a,b,c):
    return math.gcd(a, math.gcd(b,c))
ans = 0
for a in range(1, N+1):
    for b in range(1, a+1):
        for c in range(1, b+1):
            if a == b and b == c:
                ans += gcdtriple(a,b,c)
            elif a == b or b == c or c == a:
                ans += gcdtriple(a,b,c) * 3
            else:
                ans += gcdtriple(a,b,c) * 6
print(ans)

Solution (Python):
import math
K = int(input())
p = int(K*(K+1)/2)
for a in range(K):
    for b in range(a+1, K):
        g = math.gcd(a+1, b+1)
        p += 6*g
        for c in range(b+1, K):
                p += 6*math.gcd(g, c+1)
print(p)

Solution (Python):
import fractions as fr
k = int(input())
ans = (k+1)*k//2
tttt = [[0]*(k+1) for _ in range(k+1)]
for i in range(1, k+1):
  for j in range(1, k+1):
    if i == j:
      continue
    else:
      if tttt[i][j] != 0:
        g = tttt[i][j]
        ans += 3*g
      else:
        g = fr.gcd(i,j)
        tttt[i][j] = g
        tttt[j][i] = g
        ans += 3*g
for i in range(1, k-1):
  for j in range(i+1, k):
    for l in range(j+1, k+1):
      temp = tttt[i][j]
      temp2 = tttt[temp][l]
      ans+= 6*temp2
print(ans)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
import math
from functools import reduce
import numpy as np

def II(): return int(sys.stdin.readline())
def MI(): return map(int, 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)]

k = int(input())
def gcd(*numbers):
    return reduce(math.gcd, numbers)

count = 0
a = []
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):
            count += math.gcd(a,l)
print(count)

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

for x in range(1, k+1):
    t += x

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

for a in range(1, k+1):
    for b in range(a+1, k+1):
        for c in range(b+1, k+1):
            d = math.gcd(a, b)
            t += 6*math.gcd(d, c)
print(t)

Solution (Python):
import math

n = int(input()) 
total = 0
for a in range(1, n + 1):
    for b in range(1, n + 1):
        g = math.gcd(a ,b)
        if g == 1:
            total += n
        else:
            for c in range(1, n + 1):
                total += math.gcd(g, c)

print(total)

Solution (Python):
import math

def mgcd(a, b, c):
    g = math.gcd(a, b)
    g = math.gcd(g, c)
    return g
    
memo={}
cnt = 0

K = int(input())

for a in range(1, K+1):
    for b in range(1, K+1):
        p = [a, b]
        
        key = str(p)
        if memo.get(key):
            
            continue
        else:
            g = math.gcd(a, b)
            memo[key] = g
            


for c in range(1, K+1):
    for v in memo.values():
        
        cnt += math.gcd(v, c)
        
print(cnt)

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)


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):
            if i==1 or j==1 or k ==1:
                ans += 1
            else:
                ans += gcd(i,j,k)

print(ans)

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

print(c)

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

K = int(input())
sum = 0
for a in range(1,K-1):
    for b in range(a+1,K):
        for c in range(b+1,K+1):
            sum += gcd(gcd(a,b),c) * 6

for a in range(1,K+1):
    for b in range(1,K+1):
        sum += gcd(gcd(a,b),b) * 3

for i in range(1,K+1):
    sum -=2*i
    
print(sum)

Solution (Python):

import math

K = int(input())

gcd = []

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

print(sum(gcd))

Solution (Python):
import math
from functools import reduce

k = int(input())

ans = 0

ab = [math.gcd(a,b) for a in range(1, k+1) for b in range(1, k+1)]

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

print(ans)

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

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

k=int(input())

l=[]
for i in range(1,k+1):
    l.append(i)

c_list = list(itertools.combinations_with_replacement(l,3))

cnt=0
for l in c_list:
    a = l[0]
    b = l[1]
    c = l[2]
    if a == b == c:
        d = 1
    elif a == b or b == c or c == a:
        d = 3
    else:
        d = 6
    cnt += gcd(a,b,c)*d
print(cnt)

Solution (Python):
import math

def main():
    N = input()
    N = int(N)
    N = N+1
    
    sum = 0

    for a in range(1, N):
        for b in range(1, N):
            for c in range(1, N):

                if a == b or b == c or a == c:
                    tmp = math.gcd(a,b)
                    gcd = math.gcd(tmp,c)
                    sum = sum + gcd

                elif a < b and b < c:
                    tmp = math.gcd(a,b)
                    gcd = math.gcd(tmp,c)
                    sum = sum + 6*gcd

                else:
                    continue

    print(sum)




if __name__ == "__main__":
    main()

Solution (Python):
import itertools
import math

k = int(input())

l = list(itertools.combinations_with_replacement(list(range(1, k+1)), 3))
ans = 0

for i in l:
    a, b, c = i
    if a == b == c:
        ans += math.gcd(math.gcd(a, b), c)
    elif (a == b) or (b == c):
        ans += 3 * (math.gcd(math.gcd(a, b), c))
    else:
        ans += 6 * (math.gcd(math.gcd(a, b), c))

print(ans)

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

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

ans=0
re=[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]

if K < 102:
    for a in range(1,K+1):
        for b in range(1,K+1):
            hoge=gcd(a,b)
            for c in range(1,K+1):
                g=gcd(hoge,c)
                ans += g
else:
    ans=re[K-101]



print(ans)

Solution (Python):
import math

k = int(input())
ans = 0
m = 0
for a in range(1,k+1):
    for b in range(a+1,k+1):
        for c in range(b+1, k+1):
            m = math.gcd(a,b)
            ans += 6*math.gcd(m,c)
for a in range(1,k+1):
    for c in range(a+1,k+1):
        ans += 3*math.gcd(a,c)
for c in range(1,k+1):
    for a in range(c+1,k+1):
        ans += 3*math.gcd(a,c)
for a in range(1,k+1):
    ans += a
print(ans)

Solution (Python):
import math
n = int(input())
m = 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 and k != i:  
        m += 6*math.gcd(i,math.gcd(j,k))
      elif i == j and j == k:  
        m += math.gcd(i,math.gcd(j,k))
      else:  
        m += 3*math.gcd(i,math.gcd(j,k))

print(m)

Solution (Python):
from fractions import gcd
import math
K = (int)(input())
sum = 0        
for a in range(3,K+1):
    for b in range(2,a):
        for c in range(1,b):
            sum += 6*math.gcd(math.gcd(a,b),c)
for a in range(1,K+1):
    sum += a
for a in range(2,K+1):
    for b in range(1,a):
        sum += 6*math.gcd(a,b)
print(sum)

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

sum1 = 0
sum2 = 0
sum3 = 0

for a in range(1,N+1):
  for b in range(a+1, N+1):
    for c in range(b+1, N+1):
      sum1 += gcd(a,b,c)
      
for a in range(1,N+1):
  for b in range(a+1, N+1):
    sum2 += gcd(a,b)
    
sum3 = N*(N+1)/2

print(int(sum1*6+sum2*6+sum3))

Solution (Python):
from math import gcd

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

Solution (Python):
import itertools

k = int(input())

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

answer = 0
nums = list(range(1, k+1))
for balls in itertools.combinations_with_replacement(nums, 3):
    i, j, l = balls[0], balls[1], balls[2]
    ans = gcd(i, j)
    ans = gcd(ans, l)
    if len(set(balls)) == 1:
        answer += ans
    elif len(set(balls)) == 2:
        answer += ans*3
    elif len(set(balls)) == 3:
        answer += ans*6
            
print(answer)

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

def main():
    n=int(input())
    d=[[[None]*n for i in range(n)]for j in range(n)]
    ans=0

    for i in range(n):
        for j in range(i,n):
            ij=m.gcd(i+1,j+1)
            for k in range(j,n):
                d[i][j][k]=m.gcd(ij,k+1)
    

    for i in range(n):
        for j in range(n):
            for k in range(n):
                
                if i<=j and j<=k:
                    ans+=d[i][j][k]
                elif i<=k and k<=j:
                    ans+=d[i][k][j]
                elif j<=i and i<=k:
                    ans+=d[j][i][k]
                elif j<=k and k<=i:
                    ans+=d[j][k][i]
                elif k<=i and i<=j:
                    ans+=d[k][i][j]
                elif k<=j and j<=i:
                    ans+=d[k][j][i]
    print(ans)

main()

Solution (Python):
import math

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

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

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
    
ans = 0
for a in range(K):
    a += 1
    if a == 1:
        ans += K*K
        continue
    for b in range(K):
        b += 1
        if b == 1:
            ans += K
            continue
        tmp = gcd(a, b)
        if tmp == 1:
            ans += K
            continue
        for c in range(K):
            c += 1
            if c == 1:
                ans += 1
                continue
            dada = gcd(tmp, c)
            ans += dada
            

print(ans)

Solution (Python):
from math import gcd

k = int(input())

result = 0
for a in range(1, k + 1):
    if a == 1:
        result += k ** 2
        continue
    for b in range(1, k + 1):
        if b == 1:
            result += k
            continue
        for c in range(1, k + 1):
            result += gcd(gcd(a, b), c)

print(result)

Solution (Python):
import math

K = int(input())
sum = 0

for i in range(1, K+1):
    for j in range(1, K+1):
        for l in range(1, K+1):
            sum += math.gcd(i, math.gcd(j, l))

print(sum)

Solution (Python):
import math
from functools import reduce
answer=0
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
K=int(input())
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            
            answer+=gcd(i,j,k)
                
                
print(answer)

Solution (Python):
import math
ans=0
k=int(input())
gcdlist=[[math.inf]*(k+1) for i in range(k+1)]
for i in range(1,k+1):
    for j in range(1,k+1):
        gcdlist[i][j]=math.gcd(i,j)
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans+=gcdlist[gcdlist[a][b]][c]
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)

K = int(input())

S = 0

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

            S = S + gcd(a,b,c)

print(S)

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

k = int(input())
ans = 0

for i in range(1, k+1):
    for j in range(i, k+1):
        for l in range(j, k+1):
            dup = len(set([i, j, l]))
            if dup == 1:
                num = 1
            elif dup == 2:
                num = len(list(permutations([i, j, l]))) / 2
            else:
                num = len(list(permutations([i, j, l])))
            ans += gcd(i, gcd(j, l)) * num

print(int(ans))

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

import itertools
import math
from functools import reduce

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

lis = list(range(1,K+1))
S = [gcd_list(team) for team in itertools.product(lis, repeat = 3)]

print(sum(S))

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


ans = 0
n = int(input())
for k in range(1, n+1):
    for b in range(k + 1, n + 1):
        for c in range(b + 1, n + 1):
            gcd1 = dcg(k, b)
            gcd2 = dcg(gcd1, c)
            ans += gcd2

ans *= 6

tmp = 0

for k in range(1, n+1):
    for b in range(k+1, n+1):
        gcd = dcg(k,b)
        tmp += gcd
        
tmp *= 6

tmp2 = sum(range(1,n+1))

print(ans+tmp+tmp2)

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

k = int(input())
res = 0

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

Solution (Python):
import math
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 += math.gcd(i, math.gcd(j, k))
            elif (i == j) or (j == k):
                ans += 3 * math.gcd(i, math.gcd(j, k))
            else:
                ans += 6 * math.gcd(i, math.gcd(j, k))
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 sys
import math
import bisect

d = dict()

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

def build_table():
    global d
    for i in range(1, 201):
        for j in range(1, 201):
            d[(i, j)] = gcd(i, j)

def query_gcd_3(a, b, c):
    g = d[(a, b)]
    return d[(g, c)]

def main():
    build_table()
    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 += query_gcd_3(i, j, k)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
from functools import lru_cache

gcd = [[0] * 201 for _ in range(201)]

for i in range(201):
    for j in range(201):
        gcd[i][j] = math.gcd(i, j)

K = int(input())

total = 0

for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            total += gcd[gcd[a][b]][c]

print(total)

Solution (Python):
import sys

K=int(sys.stdin.readline().rstrip())
 
def gcd(a,b):

    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b

cnt1 = 0
cnt2 = 0
cnt3 = 0
for n1 in range(0,K):
    cnt1 += n1+1
    for n2 in range(n1+1, K):
        temp = gcd(n1+1, n2+1)
        cnt2 += temp*6
        for n3 in range(n2+1, K):
            cnt3 += gcd(temp, n3+1)*6
        

ans = cnt1 + cnt2 + cnt3
print(ans)

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

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
from functools import reduce

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

K = int(input())
S=0
for i in range(1,K+1):
	for h in range(i,K+1):
		for g in range(h,K+1):

			if i==h and h==g:
				S+=gcd(g,h,i)
			elif i==h or h==g or g==i:
				S+=3*gcd(g,h,i)
			else:
				S+=6*gcd(g,h,i)
			
print(S)

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

for i in range(1, K + 1):
  for j in range(1, K + 1):
    gcd1 = math.gcd(i, j)
    for k in range(1, K + 1):
      gcd2 = math.gcd(k, gcd1)
      sum += gcd2

print(sum)

Solution (Python):
import math
k = int(input())
ans = []
for a in range(1,k+1) :
    for b in range(1, k+1):
        for c in range(1, k+1):
            d = math.gcd(a,b)
            ans.append(math.gcd(c, d))

print(sum(ans))

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


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



k = int(input().replace("\n",""))
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
import time

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

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

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

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

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

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

    ans = 0
    c = list(itertools.combinations_with_replacement(range(1,k+1), 3))
    for elem in c:
        n_gcd = gcd_list(list(elem))
        length = len(set(elem))
        if length == 1:
            k = 1
        elif length == 2:
            k = 3
        elif length == 3:
            k = 6
        ans += k * n_gcd 
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
from fractions import gcd
import math
import functools
t = []
ans = 0
for i in range(1,n+1):
    for j in range(i,n+1):
        for k in range(j,n+1):
            
            t = [i,j,k]
            if i==j==k:
                gcd = functools.reduce(math.gcd, t)
                ans += gcd
            elif i!=k and j!=k and i!=j:
                gcd = functools.reduce(math.gcd, t)
                ans += gcd*6
            else:

                gcd = functools.reduce(math.gcd, t)
             
                ans += gcd*3
            

print(ans)

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

g2 = [[0]*(K+1) for _ in range(K+1)]
def gcd(a, b):
    if g2[a][b] > 0:
        return g2[a][b]
    tmpa = a
    tmpb = b
    while b > 0:
        a,b = b, a%b
    g2[tmpa][tmpb] = a
    g2[tmpb][tmpa] = a
    return a

def gcd_3(a,b,c):
    g = gcd(a,b)
    g = gcd(g,c)
    return g

sm = 0
for a in range(1, K+1):
    sm += a
    for b in range(a+1, K+1):
        for c in range(b+1, K+1):
            sm += gcd_3(a,b,c) * 6
    for b in range(1, K+1):
        if a != b:
            sm += gcd(a,b) * 3

print(sm)

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

import math
ans=0

all=0
for i in range(1,N+1):
    all+=i

for i in range(1,N+1):
    for j in range(1,N+1):
        ans+=3*math.gcd(i,j)
        
ans=ans-2*all

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

Solution (Python):
import math



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

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

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

li = [[0]*(k+1) for i in range(k+1)]
for i in range(k+1):
    for j in range(k+1):
        li[i][j] = fractions.gcd(i,j)

for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            x = li[i][j]
            y = li[x][l]
            ans += y
print(ans)

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

K = int(input())

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

sum = 0

for a in range(1, K+1):
    for b in range(a, K+1):
        for c in range(b, K+1):
            G = gcd(a,b,c)
            if a == b and b == c:
                sum += G
            elif a == b or b == c:
                sum += G*3
            else:
                sum += G*6

print(sum)

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

K = int(input())
count = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            d = gcd(a,b)
            count += gcd(c, d)
print(count)

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

K=int(input())
count=0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            count+=gcd(a,b,c)
print(count)

Solution (Python):
import fractions

K = int(input())
ans = 0

for a in range(1,K+1):
    for b in range(a,K+1):
        t = fractions.gcd(a,b)
        for c in range(b,K+1):
            if(t!=1):
                tmp = fractions.gcd(t,c)
            else:
                tmp = 1
            if(a==b and b==c):
                ans += tmp
            elif(a!=b and b!=c and c!=a):
                ans += 6*tmp
            else:
                ans += 3*tmp
print(int(ans))

Solution (Python):
import math
n = int(input())
ans = 0
t = [math.gcd(i, j) for i in range(1, n+1) for j in range(1, n+1)]

for i in t:
    for j in range(1, n+1):
        ans += math.gcd(i, j)

print(ans)

Solution (Python):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(input())
t = 0
t1 = 0
t2 = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        for l in range(j,K+1):
            if i == j == l:
                t = t + i
            elif i == j or j ==l or l ==i:
                t = t + 3 * gcd(i, j,l)
            else:
                t = t + 6 * gcd(i,j,l)
             
           
           
print(t)

Solution (Python):
import math

def solve(n):
    n = n+1
    ans = 0
    for i in range(1,n):
        for j in range(1,n):
            tmp = math.gcd(i, j)
            for k in range(1,n):
                ans += math.gcd(tmp, k)
    return ans

def main():
    k = int(input())
    print(solve(k))

if __name__ == '__main__':
    main()

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