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 functools
from math import gcd
k = int(input())
cnt = 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 and j == l:
                cnt += i
            elif i == j and j != l:
                li = [i,l]
                cnt += functools.reduce(gcd,li)*3
            elif i != j and j == l:
                li = [i,j]
                cnt += functools.reduce(gcd,li)*3
            else:
                li = [i,j,l]
                cnt += functools.reduce(gcd,li)*6
print(cnt)

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

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

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

Solution (Python):
from fractions import gcd

K = int(input())

ans = 0
L = [0]*(K+1)
for a in range(1,K+1):
    for b in range(1,K+1):
        L[gcd(a,b)] += 1


for a in range(1,K+1):
    if L[a] == 0:
        continue
    for c in range(1,K+1):
        ans += L[a]*gcd(a,c)

print(ans)

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

k = int(input())

import functools
from fractions import gcd

cache = dict()

ab = []
for a in range(1, k + 1):
    for b in range(1, k + 1):
        i1, i2 = min(a, b), max(a, b)
        if (i1, i2) in cache:
            ab.append(cache[(i1, i2)])
            continue
        v = gcd(i1, i2)
        cache[(i1, i2)] = v
        ab.append(v)

ans = 0
for d in ab:
    for c in range(1, k + 1):
        i1, i2 = min(d, c), max(d, c)
        if (i1, i2) in cache:
            ans += cache[(i1, i2)]
            continue
        v = gcd(i1, i2)
        cache[(i1, i2)] = v
        ans += v

print(ans)

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

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

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


print(ans)

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

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

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

def Gcd(x, y): 
    if x < y:
        x, y = y, x
    a = x
    b = y
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b

sum = 0
for a in range(1, K + 1):
    sum += a

for a in range(1, K + 1):
    for b in range(1, a):
        sum += Gcd(a, b) * 6

for a in range(1, K + 1):
    for b in range(1, a):
        for c in range(1, b):
            sum += Gcd(Gcd(a, b), c) * 6
print(sum)

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

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


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

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

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

print(goal)

Solution (Python):
def gcd(m, n):
    while n:
        m, n = n, m % n
    return m

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):
            _ans = gcd(gcd(a, b), c)
            if a == b and b == c:
                ans += _ans
            elif a == b or b == c:
                ans += 3 * _ans
            else:
                ans += 6 * _ans
print(ans)

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

K = int(input())
s = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(b, K+1):
            if b == c:
                s += gcd(a, b, c)
            else:
                s += 2*gcd(a, b, c)
print(s)

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

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

k = int(input())
l = list(range(1, k+1))                                                
s = 0

for i in itertools.product(l, repeat=3):
  s += gcd(*i)
print(s)

Solution (Python):
import math
from functools import reduce

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

k = int(input())
ans = 0
a = list(range(1,k+1))
b = list(range(1,k+1))
c = list(range(1,k+1))

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

            if i==j and j==k and k==i:
                sum += i

            else:
                ans = i
                ans = math.gcd(ans,j)
                ans = math.gcd(ans,k)

                if i!=j and j!=k and k!=i:
                    sum += 6*ans
                else:
                    sum += 3*ans
print(sum)

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

main()

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 i in range(1, N+1):
    for j in range(i, N+1):
        for k in range(j, N+1):
            n = len(set([i, j, k]))
            if n == 3:
                ans += gcd(i, j, k) * 6
            elif n == 2:
                ans += gcd(i, j, k) * 3
            else:
                ans += gcd(i, j, k)
print(ans)

Solution (Python):
def euclid(a, b):
    while b:
        a, b = b, a%b
    return a
  
K = int(input())

count=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:
        kake = 1
      elif i == j or j == k or i == k:
        kake = 3
      else:
        kake = 6
      count += euclid(euclid(i, j), k) * kake

print(count)

Solution (Python):
import itertools
import math

K = int(input())


def _gcd(a, b, c):
    return math.gcd(c, math.gcd(a, b))


print(sum(map(lambda x: _gcd(*x), itertools.product(range(1, K + 1), repeat=3))))

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

print(ans)

Solution (Python):
from fractions import gcd

K = int(input())

ans = 0
L = [0]*(K+1)
for a in range(1,K+1):
    for b in range(1,K+1):
        L[gcd(a,b)] += 1


for a in range(1,K+1):
    if L[a] == 0:
        continue
    for c in range(1,K+1):
        ans += L[a]*gcd(a,c)

print(ans)

Solution (Python):
import math

k = int(input())
gcd_list = []
for i in range(1, k+1):
    for j in range(1, k+1):
        gcd_list.append(math.gcd(i, j))

sum_num = 0
for i in gcd_list:
    for j in range(1, k+1):
        sum_num += math.gcd(i, j)
print(sum_num)

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

print(ans)

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

cache = [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, 
10962213]

print(cache[K-1])

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):
s=[0,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(s[int(input())])

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

K = int(input())
ans=0
lst = [0]*201
for i in range(1,K+1):
  for j in range(1,K+1):
    lst[gcd(i,j)]+=1
for i in range(1,K+1):
  for j in range(1,K+1):
    ans += lst[i]*gcd(i,j)
print(ans)

Solution (Python):
import math

N=int(input())


ans=0

i=1
ans=0
while i<=N:
    j=1
    while j<=N:
        k=1
        while k<=N:
            ans+=math.gcd(math.gcd(i,j),k)
            k+=1
        j+=1
    i+=1
print(ans)

Solution (Python):
import math
import random
import sys
import heapq
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, UserString
from functools import lru_cache, cmp_to_key, reduce
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations, product)

sys.setrecursionlimit(10**7)
MAX_INT = 2**62-1

input = sys.stdin.buffer.readline

@lru_cache(None)
def gcd(a, b):
    if not b:
        return a
    return gcd(b, a % b)

if __name__ == "__main__":
    n = int(input())
    ans = (n + 1) * n // 2

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

Solution (Python):

from math import gcd

k=int(input())

kij=dict()

for i in range(1,k+1):
    for j in range(1,k+1):
        gij=gcd(i,j)
        if gij in kij:
            kij[gij]=kij[gij]+1
        else:
            kij[gij]=1

isum=0
for kk in kij.items():
    for m in range(1,k+1):
        isum=isum+gcd(m,kk[0])*kk[1]

print(isum)

Solution (Python):
import math
from functools import reduce

K = int(input())

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

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

Solution (Python):
import math

k = int(input())

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

print(cnt)

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

import math
result=0
for i in range(1,n+1):
    for ii in range(1,n+1):
        for iii in range(1,n+1):
            temp=math.gcd(i,ii)
            result+=math.gcd(temp,iii)
print(result)

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

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

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

Solution (Python):
import math
from functools import reduce

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

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

Solution (Python):
import itertools
import math

def main():

    K = int(input())
    K_list = list(range(1, K+1))
    sum = 0

    combi=  list(itertools.combinations_with_replacement(K_list, 3))

    for tup_ in combi:
        
        dup = len(set(tup_))
        if dup == 3:
            coef = 6
        elif dup == 2:
            coef = 3
        else:
            coef = 1
        i, j, k = tup_
        sum += coef * math.gcd(math.gcd(i,j), k)

    print(sum)
if __name__ == "__main__":
    main()

Solution (Python):
from math import gcd
k = int(input())
counter = 0
dic_gcd = {}

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

Solution (Python):
import math
def gcdf(a, b):
    if b == 0:
        return a
    return gcdf(b, a % b)
memo = {}
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):
            
            if (i, j) not in memo.keys():
                memo[(i, j)] = gcdf(i, j)
                gcd = memo[i, j]
            else:
                gcd = memo[(i, j)]
            if (gcd, l) not in memo.keys():
                memo[(gcd, l)] = gcdf(gcd, l)
                ans += memo[(gcd, l)]
            else:
                ans += memo[(gcd, l)]
print(ans)

Solution (Python):
result = 0

def gcdAB(A, B):
    if(B == 0):
        return A
    return gcdAB(B, A%B)

def gcdABC(A, B, C):
    ABgcd = gcdAB(A, B)
    return gcdAB(ABgcd, C)


K = int(input())
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            result += gcdABC(a,b,c)
            
print(result)

Solution (Python):
K = int(input())
import math
i = 0
l = list(range(K+1))[1:]

for a in l:
    for b in l:
        for c in l:
            i += math.gcd(math.gcd(a,b), c)
print(i)

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

Solution (Python):
import math
from functools import reduce
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):
            tmp = math.gcd(i,j)
            tmp2 = math.gcd(j,k)
            tmp3 = math.gcd(tmp,tmp2)
            sum += tmp3
print(sum)

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

k = int(input())

ans_1 = 0

for a in range(1, k + 1):
    ans_1 += a


ans_2 = 0
for a in range(1, k):
    for b in range(a + 1, k + 1):
        ans_2 += gcd(a, b)
ans_2 *= 6


ans_3 = 0
for a in range(1, k - 1):
    for b in range(a + 1, k):
        for c in range(b + 1, k + 1):
            d = gcd(a, b)
            ans_3 += gcd(c, d)
ans_3 *= 6

ans = ans_1 + ans_2 + ans_3
print(ans)

Solution (Python):
import math
K = int(input())
N = list()
ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        N.append(math.gcd(a, b))

for n in N:
    for c in range(1, K+1):
        ans += math.gcd(n, c)

print(ans)

Solution (Python):
import math
N=int(input())
R=0

def gcd_3(a,b,c):
  return math.gcd(math.gcd(a,b), c)

for a in range(1,N+1):
  for b in range(1,N+1):
    for c in range(1,N+1):
      R+=gcd_3(a,b,c)

print(R)

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

Solution (Python):

import sys
import math

def solve(K: int):
    answer = 0
    GCDLIST = [[1]*(K+1) for _ in range(K+1)]

    for i in range(1,K+1):
        for j in range(1,K+1):
            g = math.gcd(i,j)
            GCDLIST[i][j] = g
            GCDLIST[j][i] = g
    
    for i in range(1,K+1):
        for j in range(1,K+1): 
            for k in range(1,K+1):
                answer += GCDLIST[GCDLIST[i][j]][k]
    
    print(answer)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    solve(K)

if __name__ == '__main__':
    main()

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

GCD = [[1 for x in range(K+1)] for y in range(K+1)]

for x in range(1, K+1):
  for y in range(1, K+1):
    GCD[x][y] = math.gcd(x, y)



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

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

Solution (Python):
import math
from functools import reduce

k = int(input())
ans = 0

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

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

print(ans + k * (k + 1) // 2)

Solution (Python):
import math
import functools

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 += functools.reduce(math.gcd, (i, j, l))
print(sum)

Solution (Python):
import math

K=int(input())

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

print(ans)

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

Solution (Python):
import math

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


if __name__ == '__main__':
    main()

Solution (Python):
import math

k = int(input())
sum = 0

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

print(sum)

Solution (Python):
import math

K = int(input())

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

ans = 0
for a in range(K):
    for b in range(K):
        for c in range(K):
            if gcd[a][b][c] == 0:
                res = math.gcd(math.gcd(a+1,b+1),c+1)
                gcd[a][b][c] = res
                gcd[b][c][a] = res
                gcd[c][a][b] = res
                gcd[b][a][c] = res
                gcd[c][b][a] = res
                gcd[a][c][b] = res
                ans += res
            else:
                ans += gcd[a][b][c]

print(ans)

Solution (Python):
import math
K = int(input())
result = 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):
            result += math.gcd(tmp,k)
print(result)

Solution (Python):
import math
from functools import reduce

def gcd_get(*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 l in range(1, K+1):
            ans += gcd_get(i, j, l)
print(ans)

Solution (Python):
from math import *
g=[[0]*202 for _ in range(202)]
for i in range(1,201):
    for j in range(1,201):
        g[i][j]=gcd(i,j)
n=int(input())
a=0
for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            a+=g[g[i][j]][k]
print(a)

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

print(ans)

Solution (Python):
k=int(input())
import math
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):
N = int(input())




import math
from functools import reduce



def gcd(*numbers):
    return reduce(math.gcd, numbers)
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(a,b,c)
print(ans)

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

k = ii()


dp = [[0]*201 for _ in range(201)]
def gcd(a,b):
    if a < b:
        a,b = b,a
    if dp[a][b] != 0:
        return dp[a][b]
    while b!=0:
        a,b=b,a%b
    dp[a][b] = a
    return a

def gcds(a,b,c):
    d = gcd(a,b)
    e = gcd(d,c)
    return e
ans = 0
for i in range(1,k+1):
    for l in range(1,k+1):
        for t in range(1,k+1):
            ans += gcds(i,l,t)
print(ans)

Solution (Python):
from math import gcd
k=int(input())
l=[]
for a in range(1,k):
    for b in range(a+1,k+1):
        l.append(gcd(a,b))
s=2*sum(l)+(k*(k+1))//2
for m in l:
    for c in range(1,k+1):
        s=s+2*gcd(c,m)
print(s)

Solution (Python):
from functools import reduce
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):
            a = [i,j,k]
            b = reduce(gcd,a)
            cnt = cnt+b
print(cnt)

Solution (Python):




import sys
from io import StringIO
import unittest
import math
from functools import reduce

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

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

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



    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
from math import gcd
k=int(input())
a=[0]*k
b=[0]*(k*k)
c=[0]*(k*k*k)
for i in range(k):
  a[i]=i+1
for i in range(k):
  for j in range(k):
    b[k*i+j]=gcd(a[i],j+1)
for i in range(k):
  for j in range(k):
    for l in range(k):
      c[k*k*i+k*j+l]=gcd(b[k*i+j],l+1)
print(sum(c))

Solution (Python):
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
import sys
sys.setrecursionlimit(10 ** 7)


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



def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  


def grid(n): return [s_list() for _ in range(n)]


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


def main():
    K = si()
    ans = 0
    q = [0]*201
    for i in range(1, K+1):
        for j in range(1, K+1):
            q[gcd(i, j)] += 1

    for k in range(1, K+1):
        for i in range(1, K+1):
            ans += gcd(i, k)*q[i]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
import math

def solve(K: int):
    sum = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            for c in range(1, K + 1):
                sum += math.gcd(math.gcd(a,b), c)

    print(sum)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    solve(K)

if __name__ == '__main__':
    main()

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)


K = int(input())





permutaions = list(itertools.combinations_with_replacement(
    list(range(1, K + 1)), 3))
count = 0
for permutaion in permutaions:
    
    if permutaion[0] == permutaion[1] and permutaion[1] == permutaion[2]:
        count += gcd(permutaion[0], permutaion[1], permutaion[2])
    elif permutaion[0] != permutaion[1] and permutaion[1] != permutaion[2] and permutaion[0] != permutaion[2]:
        count += (6 * gcd(permutaion[0], permutaion[1], permutaion[2]))
    else:
        count += (3 * gcd(permutaion[0], permutaion[1], permutaion[2]))
print(count)

Solution (Python):





import fractions
dp = [[0] * 201 for i in range(201)]
def gcd_fgf(a, b):
    if dp[a][b] == 0:
        if dp[b][a] == 0:
            dp[a][b] = fractions.gcd(a, b)
            return dp[a][b]
        else:
            return dp[b][a]
    else:
        return dp[a][b]


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):
            wkgcd = gcd_fgf(a, b)
            ans = ans + gcd_fgf(wkgcd, c)


print(ans)

Solution (Python):
import math

a=input()
a=int(a)
sum=0

def gcda(x,y,z):
    temp=math.gcd(x,y)
    return math.gcd(temp,z)

for i in range(1,a+1):
    for j in range(1,a+1):
        for k in range(1,a+1):
            sum+=gcda(i,j,k)

print(sum)

Solution (Python):
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):
            tmp = math.gcd(i,j)
            ans += math.gcd(tmp,k)
            
print(ans)

Solution (Python):
import math
from functools import reduce

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

k = int(input())
ans = 0
a = [i + 1 for i in range(k)]
b = [i + 1 for i in range(k)]
c = [i + 1 for i in range(k)]

for x in a:
    for y in b:
        for z in c:
            ans += gcd(x, y, z)

print(ans)

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

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

Solution (Python):
def gcd(a,b):
  a,b=max(a,b),min(a,b)
  while b!=0:
    a,b=b,a%b
  return a
ct=0
k=int(input())
for i in range(1,k+1):
  for j in range(1,k+1):
    for k in range(1,k+1):
      ct+=gcd(gcd(i,j),k)
print(ct)

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(math.gcd(i,j),l);

print(ans);