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 sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)





K = inputI()

l = [[gcd(i,j) for i in range(1,K+1)] for j in range(1,K+1)]

dic = [0 for i in range(1,K+2)]

for item in l:
  for i in item:
    dic[i] += 1
    


res = 0
for a in range(1,K+1):
  for d in range(1,K+1):
    res += dic[a] * l[a-1][d-1]
      
print(res)

Solution (Python):
import math
import itertools
k = int(input())
a = [i for i in range(1,k+1)]

two = list(itertools.product(a,a))

ans = 0
for x,y in two:
    gcd1 = math.gcd(x,y)
    for i in range(1,k+1):
        gcd2 = math.gcd(gcd1,i)
        ans += gcd2
        
print(ans)

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

k = int(input())
total = 0

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


for i in range(1,k+1,1):
    for j in range(i,k+1,1):
        for l in range(j,k+1,1):
            GCD = gcd(i, j, l)
            l = [i,j,l]
            cnt = 0
            for num in l:
                cnt = max(cnt, l.count(num))
            if cnt == 1:
                sum = 6
            elif cnt == 2:
                sum = 3
            elif cnt == 3:
                sum = 1

            total += GCD*sum

print(total)

Solution (Python):
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): 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 LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)

MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

input = sys.stdin.readline


K = I()
def gcd(a,b):
    while b!=0:
        c = a%b
        a=b
        b=c
    return a
ans = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            ans += gcd(gcd(i+1,j+1),k+1)

print(ans)

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

K = input()
K = int(K)
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,gcd(b,c))

print(ans)

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

K = int(input())

gcd_sum = 0
target_range = range(1, K + 1)
for comb in itertools.combinations_with_replacement(target_range, 3):
  a, b, c = comb[0], comb[1], comb[2]
  gcd = reduce(math.gcd, comb)
  
  if a == b == c:
    gcd_sum += gcd
  elif a != b != c:
    gcd_sum += gcd * 6
  else:
    gcd_sum += gcd * 3


print(gcd_sum)

Solution (Python):
import collections
import math

k = int(input())
cnts = collections.defaultdict(int)
for i in range(1, k+1):
    for j in range(1, k+1):
        cnts[math.gcd(i,j)] += 1
ans = 0
for i in range(1, k+1):
    for key in cnts.keys():
        ans += math.gcd(i, key)*cnts[key]
print(ans)

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

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

Solution (Python):
import math

K = int(input())

gcd2 = [[0 for i in range(K)] for i in range(K)]

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

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

print(sum)

Solution (Python):
import math

K = int(input())

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

print(amount)

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

Solution (Python):
import math
  
k=int(input())
r=0
ls=[]

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

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

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

Solution (Python):
from math import gcd

K = int(input())

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

Solution (Python):
import numpy as np
def C():
    n = int(input())
    dp_gcd2d = [[0 for i in range(n+1)] for i in range(n+1)]
    dp = [[[0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)]
    def gcd(a, b):
        if b == 0:
            dp_gcd2d[a][b] = a
            return a
        elif dp_gcd2d[a][b] != 0:
            return dp_gcd2d[a][b]
        else:
            dp_gcd2d[a][b] = dp_gcd2d[b][a%b]
            return gcd(b, a%b)
    for i in range(1, n+1):
        for j in range(1, n+1):
            dp_gcd2d[i][j] = gcd(i,j)
    for i in range(1, n+1):
        for j in range(i, n+1):
            for k in range(j, n+1):
                dp[i][j][k] = dp_gcd2d[dp_gcd2d[i][j]][k]
                dp[j][i][k] = dp[i][j][k]
                dp[i][k][j] = dp[i][j][k]
                dp[k][i][j] = dp[i][j][k]
                dp[k][j][i] = dp[i][j][k]
                dp[j][k][i] = dp[i][j][k]
    print(np.sum(np.array(dp)))
    

C()

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

memo =[[0 for i in range(K+1)] for j in range(K+1)]
for i in range(K+1):
    memo[1][i] = 1
    memo[i][1] = 1

cnt =0

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


print(cnt)

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

res = 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):
            res += gcd(t, c)

print(res)

Solution (Python):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
'''
l = []
for a in range(1,int(input())+1):
    for b in range(1, int(input())+1):
        for c in range(1,int(input())+1):
            l.append(gcd(a,b,c))
'''
N = input() 
N = int(N)+1
l = (gcd(a,b,c) for a in range(1,N) for b in range(1,N) for c in range(1,N))
print(sum(l))

Solution (Python):
import sys
from fractions import gcd

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

K = int(read())
ans = 0

gcd_list = [[0]*210 for i in range(210)]
num_ct = [0]*210

for a in range(K):
    for b in range(K):
        tmp = gcd(a+1,b+1)
        gcd_list[a+1][b+1] = tmp
        num_ct[tmp] += 1

for c in range(K):
    for tmp in range(K):
        ans += gcd_list[c+1][tmp+1] * num_ct[tmp+1]

print(ans)

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

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

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

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

k = int(input())
res = [0] * (k + 1)

ans = 0

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

for n in range(1, k + 1):
    for r in range(1, k + 1):
        ans += gcd(r, n) * res[r]

print(ans)

Solution (Python):
import math

k=int(input())

ans=0

for l in range(1,k+1):
  for m in range(1,k+1):
    tmp=math.gcd(l,m)
    for n in range(1,k+1):
      gcd=math.gcd(tmp,n)
      ans+=gcd
      
print(ans)

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

Solution (Python):
import math

n = int(input())

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

Solution (Python):
from functools import lru_cache
@lru_cache()
def gcd(x,y):
    r = x%y
    if r == 0:
        return y
    return gcd(y,r)

K = int(input())
Sum = 0

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

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

print(S)

Solution (Python):
import math

k=int(input())

sum=0
for l in range(k):
    for m in range(k):
        tmp=math.gcd(l+1,m+1)
        for n in range(k):
            gcd=math.gcd(tmp,n+1)
            sum+=gcd

print(sum)

Solution (Python):
import numpy as np

k = int(input()) + 1
x = np.arange(1, k)
s = np.gcd.outer(np.gcd.outer(x, x), x)
print(np.sum(s))

Solution (Python):
import math
from functools import reduce


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

n=int(input())
l1=range(1,n+1)
l2=range(1,n+1)
l3=range(1,n+1)

count=0

for i in l1:
    for j in l2:
        te=math.gcd(i,j)
        for k in l3:
            count+=math.gcd(te,k)

print(count)

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


sum_of_gcd_tuples()

Solution (Python):
import math

K = int(input())

A = 0


for i in range(K):
    A += i+1
    

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


for i in range(K):
    for j in range(K):
        for k in range(K):
            if i > j and j > k:
                A += 6*math.gcd(math.gcd(i+1, j+1), k+1)

print(A)

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

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

print(gcdsum)

Solution (Python):
import math as mt
import itertools as iters

K = int(input())
A = 0
for a,b,c in iters.product(range(1,K+1),repeat=3):
    A += mt.gcd(a,mt.gcd(b,c))
print(A)

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

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

Solution (Python):
import math

k = int(input())
ans = 0

for i in range(1, k+1):
    for j in range(i, k+1):
        g_2 = math.gcd(i, j)
        for k in range(j, k+1):
            g_3 = math.gcd(g_2, k)
            if i==j and j==k:
                ans += g_3
            elif i==j or j==k or k==i:
                ans += g_3 * 3
            else:
                ans += g_3 * 6

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

Solution (Python):
import math

k = int(input())
rng = range(1, k+1)

gcd_ls = [math.gcd(i, j) for i in rng for j in rng]
ans = [math.gcd(i, l) for i in rng for l in gcd_ls]

print(sum(ans))

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

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


print(ans)

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


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


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

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


print(ans)

Solution (Python):
print([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][int(input())])

Solution (Python):
import math

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

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

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

Solution (Python):
from math import gcd

K = int(input())
out = 0
Count = [0]*K
gcds = [[0]*K for i in range(K)]

for i in range (1,K+1):
    for j in range(1,+K+1):
        value = gcd(i,j)
        gcds[i-1][j-1]=value
        Count[value-1]+=1

for i in range(1,K+1):
    for j in range(1,K+1):
        out +=gcds[i-1][j-1]*Count[j-1] 
        
print(out)

Solution (Python):
from math import gcd


k = int(input())

ans = 0

gcd_sum = [0] * 201

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):
        q = gcd(a, b)
        ans += gcd_sum[q]

print(ans)

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

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 += reduce(gcd,[i,j,k])

print(ans)

Solution (Python):
from math import gcd

def resolve():
    k = int(input())
    l = [None] * k ** 2
    for i in range(k):
        for j in range(k):
            l[i * k + j] = gcd(i + 1, j + 1)
    ans = 0
    for i in l:
        for j in range(k):
            ans += gcd(i, j + 1)
    print(ans)


if __name__ == '__main__':
    resolve()

Solution (Python):

import math

n = int(input())
count = 0

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

Solution (Python):
K=int(input())
import math
def f(x):
  if x==1:
    return 1
  elif x==2:
    return 9
  else:
    m=0
    for j in range(1,x):
      m+=math.gcd(j,x)*6
    for i in range(1,x-1):
      for k in range(i+1,x):
        m+=math.gcd(x,math.gcd(i,k))*6
    return m+x+f(x-1)
print(f(K))

Solution (Python):
import sys
from math import gcd

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    k = int(rl())
    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(c, d)
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from math import gcd

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    k = int(rl())
    ans = 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)
                ans += gcd(c, d)
    print(ans)


if __name__ == '__main__':
    solve()

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

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

def gcd(a, b):
    if a > b:
        a, b = b, a
    if b % a == 0:
        return a
    else:
        return gcd(b-(b//a)*a, a)

res = 0
for i in range(1, k+1):
    res += i
for i in range(1, k+1):
    for j in range(i+1, k+1):
        tmp = gcd(i, j)
        res += 6*tmp
for i in range(1, k+1):
    for j in range(i+1, k+1):
        for h in range(j+1, k+1):
            tmp = gcd(i, gcd(j, h))
            res += 6*tmp
print(res)

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

Solution (Python):
import math

num = int(input())
sum = 0

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

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

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

print(ans)

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

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

print(ans)

Solution (Python):
"""
    Author : thekushalghosh
    Team   : CodeDiggers
"""
import sys,math,cmath,time
start_time = time.time()

def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().split()))



def solve():
    n = inp()
    c = 0
    for i in range(1,n + 1):
        for j in range(1,n + 1):
            for k in range(1,n + 1):
                c = c + math.gcd(math.gcd(i,j),k)
    print(c)


ONLINE_JUDGE = __debug__
if not ONLINE_JUDGE:
    sys.stdin = open('input.txt','r')
    sys.stdout = open('output.txt','w')
else:
    input = sys.stdin.readline
t = 1
for tt in range(t):
    solve()
if not ONLINE_JUDGE:
    print("Time Elapsed:",time.time() - start_time,"seconds")
sys.stdout.close()

Solution (Python):
import math

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

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

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

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):
def main():
	s = int(input())
	import math
	from functools import reduce
	import itertools
	def gcd(*numbers):
		return reduce(math.gcd, numbers)

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

if __name__ == '__main__':
	main()

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

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

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

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

Solution (Python):


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

Solution (Python):


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

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

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

print(main())

Solution (Python):
import math

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

print(ans)

Solution (Python):
import math

K = int(input())
ans = 0
for 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):
def gcd(p,q):
    while(q!=0):
        r=p%q
        p=q
        q=r
    return p
    
n=int(input())
sum=0
for a in range(1,n+1):
    for b in range(1,n+1):
        for c in range(1,n+1):
            sum+=gcd(a,gcd(b,c))
print(sum)

Solution (Python):
def gcd(a,b):
    while b != 0:
        a,b = b, a%b
    return a
def gcd3(a,b,c):
    return gcd(a,gcd(b,c))

ans = 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):
            ans += gcd3(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 a in range(1,K+1):
    for b in range(1,a+1):
        for c in range(1,b+1):
            if a == b == c:
                S += a
            if a == b != c or a != b == c:
                S += 3*math.gcd(a,c)
            if a != b != c:
                S += 6*gcd(a, b, c)
print(S)

Solution (Python):
import math
n=int(input())
ans=0
d={}
for a in range(1,n+1):
    for b in range(1,n+1):
        p=math.gcd(a,b)
        if d.get(p)!=None:
            d[p]+=1
        else:
            d[p]=1
for c in range(1,n+1):
    for e in range(1,n+1):
        p=math.gcd(c,e)
        ans+=d[e]*p
print(ans)

Solution (Python):
import numpy as np
n=int(input())
k=0
def f(a,b):
  if a < b:
    a,b=b,a
  while True:
    if a%b==0:
      return b
    else:
      a,b=b,a%b
for a in range(1,n+1):
  for b in range(1,a+1):
    for c in range(1,b+1):
      m=f(f(a,b),c)
      if a==b and b==c:
        k+=m
      elif a==b or b==c:
        k+=3*m
      else:
        k+=6*m
print(k)

Solution (Python):
import math

k = int( input() )
out_sum = 0


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


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


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

print( out_sum )

Solution (Python):
class Gcd:

    def __init__(self, K):
        self.table = [[None] * K for i in range(K)]
        return

    def gcd(self, x, y):
        if self.table[x - 1][y - 1] is not None:
            return self.table[x - 1][y - 1]

        if x == y:
            self.table[x - 1][y - 1] = x
            return x
        elif x > y:
            z = x % y
            if z == 0:
                self.table[x - 1][y - 1] = y
                return y
            else:
                z = self.gcd(z, y)
                self.table[x - 1][y - 1] = z
                return z
        else:
            return self.gcd(y, x)

def quest_c():
    K = int(input())

    gcd = Gcd(K)
    sum = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            d = gcd.gcd(a, b)
            if d == 1:
                sum += K
            else:
                for c in range(1, K + 1):
                    sum += gcd.gcd(d, c)
    print(sum)

if __name__ == '__main__':
    quest_c()

Solution (Python):
import math
from functools import reduce
def main():
    K = int(input())
    s = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            for k in range(1, K+1):
                s += math.gcd(math.gcd(i, j), k)
    print(s)
if __name__ == "__main__":
    main()

Solution (Python):

K = int(input())
bcd=[[int(-1) for x in range(K+1)]for j in range(K+1)]


def retBcd(a,b):
    if bcd[a][b] != -1 :
        return bcd[a][b]
    if b == 0:
        bcd[a][b] = a
        bcd[b][a] = a
        return a
    else:
        ret = retBcd(b,a%b)
        bcd[a][b] = ret
        bcd[b][a] = ret
        return ret

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

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

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

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

Solution (Python):
import math

k = int(input())

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

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

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

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

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

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

Solution (Python):
from math import gcd

K = int(input())

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

print(ans)