Problem:
 Score : 300 points    Problem Statement Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)} .  Here \gcd(a,b,c) denotes the greatest common divisor of a , b , and c .      Constraints  1 \leq K \leq 200  K is an integer.         Input Input is given from Standard Input in the following format:  K       Output Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)} .        Sample Input 1 2      Sample Output 1 9  \gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)  +\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)  =1+1+1+1+1+1+1+2=9  Thus, the answer is 9 .       Sample Input 2 200      Sample Output 2 10813692

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

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

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

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:
                sum += i
            elif i==j or j==l or l==i:
                sum += gcd(i,j,l)*3
            else:
                sum += gcd(i,j,l)*6

print(sum)

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

def ca(a,b):
    if a == 1:
        return 1
    while True:
        if a == 1:
            return 1
        if a == 0:
            return b
        t = a
        a = b %a
        b = t
    return b

def cal(a,b,c):
    mo = ca(a,b)
    ans = ca(mo,c)
    return ans


ans = 0
for a in range(1,K-1):
    ans += a
    ans += 6*cal(a,a,K)
    for b in range(a+1,K):
        ans += 6*cal(a,b,b)
        for c in range(b+1,K+1):
            ans += 6*cal(a,b,c)
    
ans += K + K-1 +6
if K == 1:
    print(1)
elif K == 2:
    print('9')
else:

    print(ans)

Solution (Python):
import sys
import numpy as np

N=int(input())
count = 0

for i in range(1,N+1):
    i_ins=[]
    for x in range(1,i+1):
        if i//x != 0 and i%x == 0:
            i_ins.append(x)
    for j in range(1,N+1):
        i_j_ins=[]
        for y in i_ins:
            if j//y != 0 and j%y == 0:
                i_j_ins.append(y)
        if i_j_ins[-1]==1:
            
            count = count + i_j_ins[-1]*N
        else:
            for k in range(1,N+1):
                i_j_k_ins=[]
                for z in reversed(i_j_ins):
                    if k//z != 0 and k%z == 0:
                        i_j_k_ins.append(z)
                        break
            
                count = count + i_j_k_ins[-1]

print(count)

Solution (Python):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
n =int(input())
n += 1
sum = 0
for i in range(1, n):
  for j in range(1, n):
    for m in range(1, n):
      sum += gcd(i, j, m)

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

print(ans)

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

K = int(input())

ans = 0
for i in range(1,K+1):
    for j in range(i, K+1):
        temp = gcd(i, j)
        for k in range(j, K+1):
            if i == j and j == k:
                ans += gcd(temp, k)
            elif i == j or j == k:
                ans += gcd(temp, k) * 3
            else:
                ans += gcd(temp, k) * 6

print(ans)

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


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

ans = 0

for abc in itertools.combinations_with_replacement([i+1 for i in range(K)], 3):
  a,b,c = abc
  gcd_ab = gcd(a, b)
  gcd_abc = gcd(gcd_ab, c)

  
  if a == b == c:
    ans += gcd_abc
  elif a==b or b==c or a==c:
    ans += gcd_abc*3
  else:
    ans += gcd_abc * 6

print(ans)

Solution (Python):
import math

def main():

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

    
    count = [0]*201
    
    for i in r:
        for j in r:
            count[math.gcd(i,j)] += 1
    
    final = 0
    
    for i in range(1,201):
        if count[i] > 0:
            for j in r:
                final += count[i]*math.gcd(i,j)
    
    print(final)


main()

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):
            tmp_gcd = math.gcd(i, j)
            tmp_gcd = math.gcd(tmp_gcd, k)
            if i == j == k:
                ans += tmp_gcd
            elif i == j or i == k or j == k:
                ans += tmp_gcd * 3
            else:
                ans += tmp_gcd * 6
print(ans)

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

def gcd(numbers):
    return reduce(lambda x,y: math.gcd(x,y),numbers)

def comb(K):
    return itertools.combinations_with_replacement(range(1,K+1),3)

def main():
    k = int(input())
    if k == 1:
        print(1)
        return
    elif k == 2:
        print(9)
        return

    final = 0
    comb_list = list(comb(k))

    for i in comb_list:
        if len(set(i)) == 1:
            final += 1*gcd(i)
        elif len(set(i)) == 2:
            final += 3*gcd(i)
        else:
            final += math.factorial(3)*gcd(i)

    
    print(final)
main()

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

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

Solution (Python):
import math


k = int(input())
answer = 0
for w_a in range(1, k + 1):
    for w_b in range(1, k + 1):
        w = math.gcd(w_a, w_b)
        for w_c in range(1, k + 1):
            answer += math.gcd(w, w_c)

print(answer)

Solution (Python):
from functools import lru_cache

@lru_cache(maxsize=100000)
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


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


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

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
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    K = Scanner.int()
    ans = 0
    for i in range(1, K + 1):
        for j in range(1, K+1):
            for k in range(1, K + 1):
                g = Math.gcd(i, j)
                g = Math.gcd(g, k)
                ans += g

    print(ans)


def main():
    
    solve()


if __name__ == "__main__":
    main()

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

Solution (Python):
import itertools
k=int(input())
def solve(a,b):
	if a<b:
		if b%a==0:
			return a
		else:
			return solve(a,b%a)
	if a%b==0:
		return b
	else:
		return solve(b,a%b)
arr=[i+1 for i in range(k)]
l=list(itertools.combinations_with_replacement(arr,3))

s=0
dic={}

for i in l:
	a,b,c=i
	if a==b and a==c:
		s+=solve(c,solve(a,b))
	elif a==b or b==c or a==c:
		s+=3*(solve(c,solve(a,b)))
	else:
		s+=6*(solve(c,solve(a,b)))
print(s)

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

print(ans)

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

for i in range(1,201):
    gcdtemp=[0]
    for j in range(1,201):
        gcdtemp.append(gcd(i,j))
    gcdcal.append(gcdtemp)

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

Solution (Python):
from itertools import combinations as c
from math import gcd

n = int(input())+1

def f(n):
  res = 0
  for x, y, z in c(range(1, n), 3):
    res += gcd(x, gcd(y, z))
  res *= 6
  res += sum(range(1,n))
  for x, y in c(range(1, n), 2):
    res += 6*gcd(x, y)
  return res
print(f(n))

Solution (Python):
import math
from functools import reduce

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

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

print(x)

Solution (Python):
import math
from functools import reduce

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

x=0
k=int(input())
r = range(1, k + 1)
x = sum([gcd(a, b, c) for a in r for b in r for c in r])
print(x)

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)

Solution (Python):
from math import gcd

N = int(input())

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

Solution (Python):
import math
from collections import defaultdict
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:input()

"""========main code==============="""

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

Solution (Python):
import sys
from math import gcd

sys.setrecursionlimit(10 ** 6)


def input(): return sys.stdin.readline().rstrip()


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

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

print(sum)

Solution (Python):
from math import gcd
k = int(input())
t = 0
for i1 in range(1, k+1):
    for i2 in range(1, k+1):
        for i3 in range(1, k+1):
            t+=gcd(gcd(i1, i2), i3)
print(t)

Solution (Python):
from math import gcd
k = int(input())
ans = 0
t = 0
for i1 in range(1, k+1):
    for i2 in range(1, k+1):
        for i3 in range(1, k+1):
            t += gcd(gcd(i1, i2), i3)
print(t)

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 fractions import gcd


def memo(fnc):

    cache = {}

    def inner(*args):

        if args in cache:
            return cache[args]
        cache[args] = fnc(*args)
        return cache[args]

    return inner


@memo
def my_gcd(m, n):

    return gcd(m, n)


k = int(input())
ans = 0
for i in range(1, k+1):
    for j in range(i, k+1):
        g1 = my_gcd(i, j)
        for l in range(j, k+1):
            if i == j == l:
                w = 1
            elif i == j or j == l:
                w = 3
            else:
                w = 6
            ans += w * my_gcd(g1, l)


print(ans)

Solution (Python):
import numpy as np

K = int(input())
 
x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
print(nums.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 l in range(1, k+1):
            ans += gcd(gcd(i, j), l)

print(ans)

Solution (Python):
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
s=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):
            s=s+gcd(i,j,k)
print(s)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
 
K = int(read())
 
x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
print(nums.sum())

Solution (Python):
from functools import reduce
from math import gcd
 
 
k = int(input())
B=list()
D=list()
 
for i in range(1,k+1):
  for j in range(1,k+1):
        a=[i,j]
        b = reduce(gcd,a)
        B.append(b)
 
for i in B:
    for k in range(1,k+1):
        c = [i,k]
        d = reduce(gcd,c)
        D.append(d)
print(sum(D))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
 
K = int(read())
 
x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
print(nums.sum())

Solution (Python):
k = int(input())
import math
ans = 0
for i in range(1,k+1):
         for j in range(i,k+1):
                  for l in range(j,k+1):
                           tmp = math.gcd(i,j)
                           tmp = math.gcd(tmp,l)
                           if i == j == l:
                                    ans += tmp
                           elif i == j or j == l or l == i:
                                    ans += 3 * tmp
                           else:
                                    ans += 6 * tmp
print(ans)

Solution (Python):
k = int(input())
import math
ans = 0
for i in range(1,k+1):
         for j in range(i,k+1):
                  for l in range(j,k+1):
                           tmp = math.gcd(i,j)
                           tmp = math.gcd(tmp,l)
                           if i == j == l:
                                    ans += tmp
                           elif i == j or j == l or l == i:
                                    ans += 3 * tmp
                           else:
                                    ans += 6 * tmp
print(ans)

Solution (Python):
def gcd(a, b):
    while b != 0 :
        t = a % b
        a = b
        b = t
    return a
        
sum = 0
N=int(input(""))
for a in range(1, N + 1):
    for b in range(1, N + 1):
        x = gcd(a, b)
        if x == 1:
            sum = sum + N
        elif x == 2:
            sum = sum + (N + 1) // 2 + N // 2 * 2
        elif x == 3:
            sum = sum + N // 3 * 3 + (N - N // 3)
        elif x == 5:
            sum = sum + N // 5 * 5 + (N - N // 5)
        else:
            for c in range(1, N + 1):
                sum = sum + gcd(x, c)
print(sum)

Solution (Python):
def gcd(a, b):
    while b != 0 :
        t = a % b
        a = b
        b = t
    return a
        
sum = 0
N=int(input(""))
for a in range(1, N + 1):
    for b in range(1, N + 1):
        x = gcd(a, b)
        if x == 1:
            sum = sum + N
        elif x == 2:
            sum = sum + (N + 1) // 2 + N // 2 * 2
        elif x == 3:
            sum = sum + N // 3 * 3 + (N - N // 3)
        else:
            for c in range(1, N + 1):
                sum = sum + gcd(x, c)
print(sum)

Solution (Python):
def gcd(a, b):
    while b != 0 :
        t = a % b
        a = b
        b = t
    return a
        
sum = 0
N=int(input(""))
for a in range(1, N + 1):
    for b in range(1, N + 1):
        x = gcd(a, b)
        if x == 1:
            sum = sum + N
        elif x == 2:
            sum = sum + (N + 1) // 2 + N // 2 * 2
        else:
            for c in range(1, N + 1):
                sum = sum + gcd(x, c)
print(sum)

Solution (Python):
def gcd(a, b):
    while b != 0 :
        t = a % b
        a = b
        b = t
    return a
        
sum = 0
N=int(input(""))
for a in range(1, N + 1):
    for b in range(1, N + 1):
        x = gcd(a, b)
        if x == 1:
            sum = sum + N
        else:
            for c in range(1, N + 1):
                sum = sum + gcd(x, c)
print(sum)

Solution (Python):
from numba import jit

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

@jit
def main2(K):
    ans = 0
    for i in range(1,K+1):
        for j in range(1,K+1):
            for k in range(1,K+1):
                ans += gcd(gcd(i,j),k)
    return ans    


K = int(input())
print(main2(K))

Solution (Python):
from numba import jit

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

@jit
def main2(K):
    ans = 0
    for i in range(1,K+1):
        for j in range(1,K+1):
            tmp = gcd(i,j)
            for k in range(1,K+1):
                ans += gcd(tmp,k)
    return ans    


K = int(input())
print(main2(K))

Solution (Python):
from math import gcd

"""
def gcd(x, y, z):
  while y:
    x, y = y, x % y
  while z:
    x, z = z, x % z
  return x
"""

N = int(input())
wa = 0
for i in range (1,N+1):
  for j in range (1,N+1):
    for h in range (1,N+1):
      wa += gcd(gcd(i,j),h) 
print(wa)

Solution (Python):
import math

def nyu():
    K = int(input())
    return K


def check(K ):
    sum =0

    for k1 in range(1,K+1):
        for k2 in range(1,K+1):
            for k3 in range(1,K+1):
                sum +=  (math.gcd(k1,math.gcd(k2,k3)))
    print(sum)
     
            
K = nyu()
check(K)

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

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

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

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

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

def gcd3(a, b , c):
  return gcd(gcd(a, b), c)

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 += i
      elif i == j:
        ans += gcd(i, k) * 3
      elif j == k:
        ans += gcd(i, j) * 3
      else:
        ans += gcd3(i, j, k) * 6
print(ans)

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

Solution (Python):
import math
K=int(input())
N=0
k=K*K
d1=[0]*int(k)
i=0
j=0
for b in range(K):
    for c in range(K):
        d1[i]=math.gcd((c+1),(b+1))
        i+=1
for a in range(K):
    for x in d1:
        N+=math.gcd(x,(a+1))
print(N)

Solution (Python):
import math
K=int(input())
N=0
k=K*K
d1=[0]*int(k)
i=0
j=0
for b in range(K):
    for c in range(K):
        d1[i]=math.gcd((c+1),(b+1))
        i+=1
for a in range(K):
    for x in d1:
        N+=math.gcd(x,(a+1))
print(N)

Solution (Python):
from scipy import gcd
from collections import Counter

def sum_of_gcd(N):
    temp = [gcd(i, k) for i in range(1, N+1) for k in range(1, N+1)]
    counts = Counter(temp)

    return sum([gcd(c, i)*counts[c] for i in range(1, N+1) for c in counts.keys()])

print(sum_of_gcd(int(input())))

Solution (Python):
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial
def gcd(a,b):
	a,b=max(a,b),min(a,b)
	while a%b!=0:
		a,b=b,a%b
	return b
n=int(input())
su=0
for i in range(1,n+1):
	for j in range(1,n+1):
		a=gcd(i,j)
		for k in range(1,n+1):
			su+=gcd(a,k)
print(su)

Solution (Python):
def gcd(a,b):
    a,b=max(a,b),min(a,b)
    while b>0:
        a,b=b,a%b
    return(a)

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

Solution (Python):
k = int(input())
import math
ans = 0
for i in range(1,k+1):
         for j in range(i,k+1):
                  for l in range(j,k+1):
                           tmp = math.gcd(i,j)
                           tmp = math.gcd(tmp,l)
                           if i == j == l:
                                    ans += tmp
                           elif i == j or j == l or l == i:
                                    ans += 3 * tmp
                           else:
                                    ans += 6 * tmp
print(ans)

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

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

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

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

s=0

def check(a, b):
  judge=False
  while(judge==False):
    a,b=b,a%b
    if(b==0):
      judge = True
      return a
    elif(b==1):
      return 1
    
for i in range(1, n+1):
  for j in range(1, n+1):
    for k in range(1, n+1):
      s+=check(k,check(i,check(j,k)))


print(s)

Solution (Python):
import math

K = int(input())
result = 0

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

print(result)

Solution (Python):
import math
sum = 0
k = int(input())+1
for i in range(1, k):
    for j in range(1, k):
        temp = math.gcd(i, j)
        for x in range(1, k):
            sum += math.gcd(temp, x)
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)
exit()

Solution (Python):
import math

def gcd3(a, b, c):
    temp = math.gcd(a, b)
    return math.gcd(temp, c)

n = int(input())
s = sum([gcd3(a, b, c) for a in range(1, n + 1) for b in range(1, n + 1) for c in range(1, n + 1)])
print(s)

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

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, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
from queue import PriorityQueue
from fractions import gcd
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def gcd(*numbers):
    return functools.reduce(math.gcd, numbers)
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort(reverse=True)
    return divisors

K = _I()

ans = 0
d = {}
vals = []
for i in range(1, K+1):
    for j in range(1, K+1):
        nums = [i,j]
        nums.sort()
        key = str(i) + '-' + str(j)
        if key in d:
            vals.append(d[key])
        else:
            val = gcd(i,j)
            d[key] = val
            vals.append(val)


for val in vals:
    for k in range(1, K+1):
        
        nums = [i,j]
        nums.sort()
        key = str(val) + '-' + str(k)
        if key in d:
            ans += d[key]
        else:
            val = gcd(val, k)
            ans += val
            d[key] = val
print(ans)
exit()

Solution (Python):
n = int(input())
 
def gcd(a,b):
  ma = max(a,b)
  mb = min(a,b)
  jy = ma % mb
  if jy == 0 :
    return mb
  else:
    return gcd(mb,jy)
sum = 0
for i in range(1,n+1):
  sum += i

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

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

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

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

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

print(ans)

Solution (Python):
import math
import collections

k = int(input())

gcd_sum_dict = collections.defaultdict(int)
for i in range(1, k+1):
    gcd_sum_dict[i] = 0
    for j in range(1, k+1):
        gcd_sum_dict[i] += math.gcd(i, j)

ans = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        ans += gcd_sum_dict[math.gcd(i, j)]

print(ans)

Solution (Python):
K=int(input())
def gcd(a,b):
    res=0
    while b!=0:
        res=a%b
        a=b
        b=res
    return a

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

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

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

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

def gcd(a,b):
  ma = max(a,b)
  mb = min(a,b)
  jy = ma % mb
  if jy == 0 :
    return mb
  else:
    return gcd(mb,jy)
sum = 0
for i in range(n):
  for j in range(n):
    for k in range(n):
      sum += gcd(i+1,gcd(j+1,k+1))

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

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

Solution (Python):
from math import gcd
from sys import stdin
K = int(stdin.readline().rstrip())
ans = 0
for i in range(1, K+1):
  for j in range(1, K+1):
    for k in range(1, K+1):
      ans += gcd(gcd(i, j), k)

print(ans)

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