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):
def main():
    import math
    import sys

    input = sys.stdin.readline

    n = int(input())
    c_1 = 0
    c_2 = 0
    c_3 = 0

    for i in range(n):
        for j in range(i,n):
            for k in range(j,n):
                if i == j and j == k:
                    c_1  += i+1
                elif i != j and j != k and k != i:
                    c_2 += math.gcd(math.gcd(i+1,j+1),k+1)
                else:
                    c_3 += math.gcd(i+1,k+1)

    print(c_1 + c_2 * 6 + c_3 * 3)

if __name__ == '__main__':
    main()

Solution (Python):
import math
p = int(input())
counta = 0

def mygcd(a_,b_,c_):
    return math.gcd(math.gcd(a_, b_),c_)

for i in range(1,p+1):
    for j in range(1,p+1):
        for k in range(1,p+1):
            counta += mygcd(i,j,k)
print(counta)

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

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

    tmp = []
    for a in range(1, k+1):
        for b in range(1, k+1):
            tmp.append(gcd(a,b))

    ans = 0
    for c in range(1,k+1):
        for i in tmp:
            ans += gcd(i, c)
    
    print(ans)
    
resolve()

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

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

Solution (Python):
import math

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

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


  
    
        
          
            
        
          

def is_prime(n):
    if n % 2 == 0 and n > 2:
        return False
    return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))

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

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

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

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

Solution (Python):
import math

n = int(input())
cnt=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):
            cnt +=  math.gcd(k,t)

print(cnt)

Solution (Python):
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import math

k = ni()
ans = 0

for i in range(1,k+1):
    for j in range(1,k+1):
        a = math.gcd(i,j)
        for p in range(1,k+1):
            ans += math.gcd(a,p)

print(ans)

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

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

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

Solution (Python):
from math import gcd

K = int(input())

ans = 0

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

print(ans)

Solution (Python):
import math

a=0

K=int(input())
for i in range(1,K+1):
    for j in range(i,K+1):
        for r in range(j,K+1):
            if i==j and i==r:
                a+=math.gcd(i,math.gcd(j,r))
            elif i==j or j==r:
                a+=math.gcd(i,math.gcd(j,r))*3
            else:
                a+=math.gcd(i,math.gcd(j,r))*6
print(a)

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

print(ans)

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

Solution (Python):
import math

n=int(input())
t=0
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):
from math import gcd
from functools import reduce





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

Solution (Python):
from math import gcd
K = int(input())
sum = 0
for a in range(K):
    for b in range(K):
        g = gcd(a + 1, b + 1)
        for c in range(K):
            sum += gcd(g, c + 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):
        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):
    for x in range(1, k + 1):
        for z in range(1, k + 1):
            ans += gcd(z, gcd(i, x))
print(ans)

Solution (Python):

import math
K=int(input())
ans=0
A=0

for n in range(1,K+1):
    for m in range(1,K+1):
        x=math.gcd(n,m)
        for l in range(1,K+1):
            A=math.gcd(x,l)    
            ans+=A
            
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 g in range(1,k+1):
      ans += gcd(gcd(i,j), g)

print(ans)

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

def main():
    K = int(readline())

    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)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

K = int(sys.stdin.readline().rstrip())

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

cache = [[gcd(a, b) for b in range(K + 1)] for a in range(K + 1)]
  
sum = 0

for a in range(K):
	for b in range(K):
		ab = cache[a + 1][b + 1]
		for c in range(K):
			sum += cache[ab][c + 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):
        if gcd(i,j) == 1: ans += K
        else:
            for k in range(1,K + 1):
                ans += gcd(gcd(i,j),k)
print(ans)

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

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

ans = 0

for a in range(1, K + 1):
    for b in range(a, K + 1):
        ab = math.gcd(a, b)
        for c in range(b, K + 1):
            abc = math.gcd(ab, c)
            if a == b == c:
                m = 1
            elif a == b or b == c or c == a:
                m = 3
            else:
                m = 6
            ans += m * abc

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

Solution (Python):
from math import gcd

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

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

ans = 0
for i in range(1, k + 1):
    for x in range(1, k + 1):
        gcdix = gcd(i, x)
        for z in range(1, k + 1):
            ans += gcd(z, gcdix)
print(ans)

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 math

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

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

Solution (Python):
import math
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(i,k+1):
    for m in range(j,k+1):
      tem=[]
      tem = [i,j,m]
      
      if  len(set(tem)) == 1 :
        ans += i
      elif len(set(tem)) == 2:
        ans += gcd(i,j,m) * 3
      elif len(set(tem)) == 3:
        ans += gcd(i,j,m) * 6

print(ans)

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


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


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

print(result)

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

print(ans)

Solution (Python):


from math import gcd


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

        for k in range(1, t + 1):
            ans += gcd(r, k)

print(ans)

Solution (Python):
import math

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

Solution (Python):
import math

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

Solution (Python):
import numpy as np
K=int(input())
s3=0
s2=0
s1=0
s0=0
for a in range(1,K-1):
    for b in range(a+1,K):
        d=np.gcd(a,b)
        for c in range(b+1,K+1):
            s0+=np.gcd(c,d)
for a in range(1,K):
    for c in range(a+1,K+1):
        s1+=np.gcd(a,c)
for a in range(1,K):
    for b in range(a+1,K+1):
        s3+=np.gcd(a,b)
for k in range(K+1):
    s2+=k
print(s0*6+s1*3+s3*3+s2)

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

Solution (Python):
import math

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

if __name__ == '__main__':
    main()

Solution (Python):
import math

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

Solution (Python):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(input())
total = K*K
for i in range(2,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      total += gcd(i,j,k)
print(total)

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

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

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

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

Solution (Python):
from math import gcd

K=int(input())
sum=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):      
      sum += gcd(tmp,k)
      
print(sum)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import gcd, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
K = INT()
 
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            ans += gcd(gcd(i, j), k)
print(ans)

Solution (Python):
import math









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

Solution (Python):
import math

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


print(S)

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

Solution (Python):
import math
import itertools

K = int(input())
s = 0
l = range(K)

for i in itertools.combinations_with_replacement(l, 3):
    if i[0] == i[1] == i[2]:
        s += math.gcd(math.gcd(i[0]+1, i[1]+1), i[2]+1)
    elif i[0] != i[1] != i[2]:
        s += (math.gcd(math.gcd(i[0]+1, i[1]+1), i[2]+1)*6)
    else:
        s += (math.gcd(math.gcd(i[0]+1, i[1]+1), i[2]+1)*3)
print(s)

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

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

limit_num = int(input())
result_list = []
for num in itertools.product(range(1, limit_num + 1), repeat=3):
	result_list.append(gcd(num))

print(sum(result_list))

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

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

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

import math

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


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import gcd, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
K = INT()
 
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            ans += gcd(gcd(i, j), k)
print(ans)

Solution (Python):
import math
from functools import reduce

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

K = int(input())
box = []
for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            if a==b and b==c:
                box.append(gcd(a,b,c))
            elif a!=b and b!=c and c!=a:
                box.append(gcd(a,b,c)*6)
            else:
                box.append(gcd(a,b,c)*3)
print(sum(box))

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

Solution (Python):
from functools import reduce
import math
import sys
 
K = int(input())
 
def gcd(*args):
  return reduce(math.gcd, args)
 
if K==1:
  print("1")
  sys.exit()
elif K==2:
  print("9")
  sys.exit()
 
ans = K * (K+1)//2
for i in range(1,K):
  for j in range(i+1,K+1):
    ans += 6*math.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(i,j,k)
      
print(ans)

Solution (Python):
import math

K = int(input())
ans = 0
for a in range(1,K-1):
    for b in range(a+1,K):
        for c in range(b+1,K+1):
            r = math.gcd(math.gcd(a,b),c)
            r *= 6
            ans += r
for a in range(1,K):
    for b in range(a+1,K+1):
        r = math.gcd(a,b)
        r *= 6
        ans += r
for a in range(1,K+1):
    ans += a
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):
        ij = gcd(i,j)
        for l in range(1, k+1):
            ans += gcd(ij, l)

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

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

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):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import gcd, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
K = INT()
 
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            ans += gcd(gcd(i, j), k)
print(ans)

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

import fractions

ans = 0

import itertools

gcd_dic = {}

for i in itertools.combinations(range(1, K + 1), 2):
    ans_m = fractions.gcd(i[0], i[1])
    ans += ans_m * 6
    gcd_dic[(i[0], i[1])] = ans_m
    gcd_dic[(i[1], i[0])] = ans_m

for i in itertools.combinations(range(1, K+1), 3):
    ans_m = i[0]
    for l in i[1:]:
        if (ans_m, l) in gcd_dic:
            ans_m = gcd_dic[(ans_m, l)]
        else:
            ans_m = fractions.gcd(ans_m, l)
    ans += ans_m*6

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

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

print(ans)

Solution (Python):
import math
from functools import reduce
import itertools
import numpy as np

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

print(ans)

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

Solution (Python):
from math import gcd

K=int(input())
sum=0

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

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

import math
ans = 0
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):
MOD = 10 ** 9 + 7
INF = 10 ** 12
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  itertools import permutations
from math import gcd

def main():
    k = int(input())
    ans = 0
    for i in range(1,k + 1):
        for j in range(1,k + 1):
            for l in range(1,k + 1):
                ans += gcd(gcd(i,j),l)
    print(ans)
if __name__ =='__main__':
    main()

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

Solution (Python):
from math import gcd
key = int(input())
sin = 0
for i in range(1,key+1):
    for j in range(1,key+1):
        temp = gcd(i,j)
        for m in range(1,key+1):
            sin += gcd(m,temp)
print(sin)

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(ij, l)

print(ans)

Solution (Python):
import numpy as np
import sys
import math



k = int(input())


def gcd(a,b):
    if a > b:
        a , b = b , a

    
    r = b % a

    if r == 0:
        return a
    else:
        return gcd(a,r)
    

sum = 0



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

print(sum)

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

Solution (Python):
from math import gcd
from collections import Counter
def printYes():
    print("YES")
def printNo():
    print("NO")

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

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

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

Solution (Python):
import math

K = int(input())
sumGCD = K * K * K
for a in range(2, K + 1, 1):
    for b in range(2, K + 1, 1):
        for c in range(2, K + 1, 1):
            arr = [a, b, c]
            gcdNum = math.gcd(math.gcd(a, b), c)
            
            
            
            sumGCD += gcdNum - 1
print(sumGCD)

Solution (Python):
def gcd(a,b,c):
    
    seq  = [a,b,c]
    seq.sort()
    while b > 0:
        r = c%b
        c = b
        b = r
    
    seq_2 = [a,c]
    seq_2.sort()
    while a > 0:
        r = c%a
        c = a
        a = r
    return c

from functools import reduce

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):
            t = len(set([i,j,k]))
            
            if t == 1:
                ans += gcd(i,j,k)
                
            elif t == 2:
                ans += gcd(i,j,k)*3
            
            elif t == 3:
                ans += gcd(i,j,k)*6
    

print(ans)

Solution (Python):
import math

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

if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd 
import sys
sys.setrecursionlimit(10000000)


dxdy=((1,0),(0,1))

def pin(type=int):
    return map(type,input().rstrip().split())


def resolve():  
    N=int(input())
    r=range(1,N+1)
    ans=0
    for a in r:
        for b in r:
            for c in r:
                ans+= gcd(gcd(a,b),c)
    print(ans)
resolve()

Solution (Python):
N = int(input())
N = N+1
sum = 0

from math import gcd

for i in range(1, N):
    for j in range(1, N):
        for k in range(1, N):
            sum += gcd(gcd(i, j), k)

print( sum )

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


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

print(ans)

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

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

Solution (Python):
from math import gcd 
import sys
sys.setrecursionlimit(10000000)


dxdy=((1,0),(0,1))

def pin(type=int):
    return map(type,input().rstrip().split())


def resolve():  
    N=int(input())
    r=range(1,N+1)
    ans=0
    for a in r:
        for b in r:
            for c in r:
                ans+= gcd(gcd(a,b),c)
    print(ans)
resolve()

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

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

Solution (Python):
import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
import sys
import bisect


sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))

K = ii()

ans = 0


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

print(ans)

Solution (Python):
import math
ans = 0
n = int(input())
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):
from math import gcd
k=int(input())
ans=0
for p in range(1,k+1):
  for q in range(1,k+1):
    for r in range(1,k+1):
      ans+=gcd(p,gcd(q,r))
print(ans)

Solution (Python):
import math
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
count=0
N=int(input())
for i in range(1,N+1):
	for j in range(1,N+1):
		for k in range(1,N+1):
			count+=(gcd_list([i,j,k]))
print(count)