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

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

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

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

Solution (Python):

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

K = int(input())
ans = 0
for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            if a != b and b != c and a != c:
                ans += 6*gcd(gcd(a,b),c)
            elif a == b or b == c or a == c:
                if a == b and b == c:
                    ans += gcd(gcd(a,b),c)
                else:
                    ans += 3*gcd(gcd(a,b),c)
print(ans)

Solution (Python):
import math
from functools import reduce
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    k=INT()
    ans=0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for h in range(1,k+1):
                ans+=math.gcd(math.gcd(i,j),h)
                
    print(ans)
if __name__ == '__main__':
    do()

Solution (Python):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
k = int(input())
sum=0
for i in range(1,k+1):
  for j in range(i+1,k+1):
    for l in range(j+1,k+1):
      sum+=6*gcd(i,j,l)
for i in range(1,k+1):
  for j in range (1,k+1):
    sum+=3*gcd(i,j)
sum-=k*(k+1)
print(sum)

Solution (Python):
import math
from functools import reduce

n = int(input())
ans = 0

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

for x in range(n):
    for k in range(n):
        for l in range(n):
            ans += gcd(x+1,k+1,l+1)

print(ans)

Solution (Python):
import collections

def U1(a,b):
    if b > a:
        b, a = a, b
    while a%b != 0:
        r = a%b
        a = b
        b = r
    return b

K = int(input())
c = 0
arr = []

for i in range(1,K+1):
    for j in range(1,K+1):
        arr.append(U1(i,j))

arr=collections.Counter(arr)

for key,value in arr.items():
    for k in range(1,K+1):
        c += U1(k,key)*value

print(c)

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

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

res += k*(k+1)//2

for i in range(1,k+1):
  for j in range(1,k+1):
    if i==j:
      continue
    res += math.gcd(i,j)*3

for i in range(1,k+1):
  for j in range(i+1,k+1):
    for m in range(j+1,k+1):
      if len(list(set([i,j,k]))) == 3:
        res += math.gcd(math.gcd(i,j),m)*6
print(res)

Solution (Python):
import math

math.gcd(2,10)
N =int(input())
total = 0
a=1
b=1
c=1
while a <=N:
	b=1
	while b <=N:
		c=1
		while c <=N:
			if math.gcd(a,b)==1:
				total += N
				break
			total += math.gcd(a,math.gcd(b,c))
			c+=1
		b+=1 
	a+=1
print(total)

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

Solution (Python):
import math
import collections
k=int(input())
ans=0
a=[]
for i in range(1,k+1):
    for j in range(1,k+1):
        a.append(math.gcd(i,j))
a=collections.Counter(a)
for key,value in a.items():
    for i in range(1,k+1):
        ans+=math.gcd(i,key)*value
print(ans)

Solution (Python):
import numpy as np
K = int(input())
l = [0] *(K+1)
res = 0
for i in range(1,K+1):
    for j in range(1, K+1):
        gcd_ab = np.gcd(i,j)
        l[gcd_ab] +=1

for i in range(1, K+1):
    for j in range(1,K+1):
        gcd_c = np.gcd(i,j)
        res += l[j]* gcd_c


print(res)

Solution (Python):
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a

k = int(input())
temp = [[xgcd(i, j) for i in range(1, k+1)] for j in range(1, k+1)]
ans = 0
for a in range(k):
    for b in range(k):
        t = temp[a][b]
        for c in range(k):
            ans += temp[t-1][c]
print(ans)

Solution (Python):
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a

k = int(input())
temp = [[xgcd(i, j) for i in range(1, k+1)] for j in range(1, k+1)]
ans = 0
for a in range(k):
    for b in range(k):
        t = temp[a][b]
        ans += sum([temp[t-1][c] for c in range(k)])
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)
        if g == 1:
            ans += K
            continue      
        for c in range(1, K + 1):
            ans += math.gcd(c, g)

print(ans)

Solution (Python):
import math

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

Solution (Python):
import math
from functools import reduce

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

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

                ans+=math.gcd(math.gcd(i+1,j+1),k+1)
                
print(ans)

Solution (Python):

import sys
import math

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


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

if __name__ == '__main__':
    main()

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

Solution (Python):
"""
def gcd(a, b ):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b
"""
import math as f


K = int(input())
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 += f.gcd(f.gcd(i, j), k)
            elif i == j or j == k or k == i:
                result += 3 * f.gcd(f.gcd(i, j), k)
            else:
                result += 6 * f.gcd(f.gcd(i, j), k)

print(result)

Solution (Python):
import math

K = int(input())
check = {}
ans = 0

for i in range(201):
    check[i] = 0

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

for c in range(1, K+1):
    for k in check.keys():
        num = math.gcd(c, k) * check[k]
        ans += num

print(ans)

Solution (Python):
import math
k = int(input())
ans = 0
ab = {}

for a in range(1,k+1):
  for b in range(1,k+1):
    tmp = math.gcd(a, b)
    if tmp not in ab:
      ab[tmp] = 1
    else:
      ab[tmp] += 1

for c in range(1,k+1):
  for d in ab.keys():
    ans += math.gcd(c, d) * ab[d]

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 sys import stdin
import numpy as np
def main():
    
    readline=stdin.readline
    k=int(readline())

    a=np.arange(1,k+1,dtype=np.int64)
    b=np.gcd.outer(np.gcd.outer(a,a),a)
    print(b.sum())

if __name__=="__main__":
    main()

Solution (Python):
import itertools
import math
from functools import reduce
def gcd(numbers):
    return reduce(math.gcd, numbers)
N = int(input())
l = [i for i in range(1,N+1)]
h = itertools.combinations_with_replacement(l, 3)

result=0
for v in h:
    ans=gcd(v)
    i,j,k=zip(v)
    if (i==j)&(i==k):
        result += ans
    elif ((i==j)&(i!=k))|((i==k)&(i!=j))|((j==k)&(j!=i)):
        result += (ans*3)
    else:
        result += (ans*6)
    
print(result)

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

print(res)

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

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

def gcd(a, b, c):
    mini = a
    if a > b:
        mini = b
    if b > c:
        mini = c
    if (a % mini == 0) & (b % mini == 0) & (c % mini == 0):
        return mini
    if mini % 2 == 0:
        h = mini / 2
        if (a % h == 0) & (b % h == 0) & (c % h == 0):
            return h
    for i in range((mini + 1)//3 + 1, 1, -1):
        if (a % i == 0) & (b % i == 0) & (c % i == 0):
            return i
    return 1

s = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for l in range(1, k + 1):
            if i <= j <= l:
                if i < j < l:
                    a = gcd(i, j, l) * 6
                    s += int(a)
                elif ((i == j) & (j != l)) | ((i != j) & (j == l)):
                    a = gcd(i, j, l) * 3
                    s += int(a)
                else:
                    a = gcd(i, j, l) 
                    s += int(a)
            else:
                continue

print(s)

Solution (Python):
import math

K = int(input())
ans = 0
tmp = {}

for i in range(201):
    tmp[i] = 0

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

for k in range(1, K+1):
    for key in tmp.keys():
        ans += math.gcd(key, k) * tmp[key]
print(ans)

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

Solution (Python):
import math
from functools import reduce

K = int(input())
total = 0


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


for i in range(1, K+1):
    for j in range(1, i+1):
        for k in range(1, j+1):
            s = [i, j, k]
            if i == j == k:
                total += gcd_list(*s)
            elif i == j and j != k:
                total += gcd_list(*s) * 3
            elif i != j and j == k:
                total += gcd_list(*s) * 3
            else:
                total += gcd_list(*s) * 6

print(total)

Solution (Python):
import sys
import math

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

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



main()

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

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

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

Solution (Python):


import math

N = int(input())
result = 0

for i in range(1, N+1):
    for j in range(1, N+1):
        tmp = math.gcd(i, j)
        for k in range(1, N+1):
            result += math.gcd(tmp, k)

print(result)

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

Solution (Python):

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

K = ir()
A = np.arange(1, K+1, dtype=np.int32)
B = np.gcd.outer(np.gcd.outer(A, A), A)
answer = B.sum()
print(answer)

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 sys
from math import gcd

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

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

print(answer)

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

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

Solution (Python):
import math

K = int(input())

ans = K * (K + 1) / 2

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

print(int(ans))

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

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

def gcd(l):
    ans = l[0]
    for i in l:  ans = gcd_(ans, i)
    return ans

ans = 0

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

print(int(ans))

Solution (Python):
from math import gcd
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())
a = k*(k+1)//2
for i in range(k):
  for j in range(i):
    b = math.gcd(i+1,j+1)
    a += b*6
    for k in range(j):
      a += math.gcd(b,k+1)*6
print(a)

Solution (Python):
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
a=0
N=int(input())
for i in range(1,N+1):
  for j in range(i,N+1):
    for k in range(j,N+1):
      if i==j==k:
        a+=i
      elif i==j or j==k or i==k:
        c=euclid(i, j)
        c=euclid(c, k)
        a+=3*c
      else:
        c=euclid(i, j)
        c=euclid(c, k)
        a+=6*c
print(a)

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

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

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

print(tot_sum)

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

Solution (Python):
import math
k = int(input())
s = 0
b = []
for i in range(1,k+1):
    for j in range(i,k+1):
        for l in range(j,k+1):
            p = len(set([i,j,l]))
            if p ==1:
                t = math.gcd(i,j)
                q = math.gcd(t,l)
                s += q
                b.append(q)
            elif p ==2:
                t = math.gcd(i,j)
                q = math.gcd(t,l)
                s += q*3
                b.append(q)
            else:
                t = math.gcd(i,j)
                q = math.gcd(t,l)
                s += q*6
                b.append(q)
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(gcd(a,b),c)

print(ans)

Solution (Python):

import copy
import random
import bisect 
import fractions 

import sys
import collections

from math import gcd
mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

K = int(input())
""" answer 1
ans = 0
tmp = {}
for i in range(201):
    tmp[i] = 0

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

for k in range(1, K +1):
    for key in tmp.keys():
        ans += math.gcd(key, k) * tmp[key]
print(ans)

"""


"""answer 2　ぎりぎり
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)

"""

ans = 0


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


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


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

Solution (Python):

import copy
import random
import bisect 
import fractions 

import sys
import collections

from math import gcd
mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

K = int(input())
""" answer 1
ans = 0
tmp = {}
for i in range(201):
    tmp[i] = 0

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

for k in range(1, K +1):
    for key in tmp.keys():
        ans += math.gcd(key, k) * tmp[key]
print(ans)

"""

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 copy
import random
import bisect 
import fractions 
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

K = int(input())


ans = 0

tmp = {}
for i in range(201):
    tmp[i] = 0

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

for k in range(1, K +1):
    for key in tmp.keys():
        ans += math.gcd(key, k) * tmp[key]
print(ans)

"""
K = 3

1 1 1  1
1 1 2  1
1 1 3  1
1 2 1  1
1 2 2  1
1 2 3  1
2 1 1  1
2 1 2  1
2 1 3  1
3 1 1  1
3 1 2  1
3 1 3  1
3 2 1  1
3 2 2  1
3 2 3  1
3 3 1  1
3 3 2  1
3 3 3  3

"""

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

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

Solution (Python):
import itertools
import math

k = int(input())
b = []
for i in range(1,k+1):
    b.append(i)
a = list(itertools.combinations_with_replacement(b, 3))
c = 0
for i in a:
    if (i[0]==i[1] or i[1]==i[2]):
        if (i[0]==i[1] and i[1]==i[2]):
         c += math.gcd(math.gcd(i[0],i[1]),i[2])
        else:
            c += 3* math.gcd(math.gcd(i[0],i[1]),i[2])
    else:
        c += 6*math.gcd(math.gcd(i[0],i[1]),i[2])

print(c)

Solution (Python):
import math
k = int(input())
ans = 0
def gcd(a, b, c):
    p = math.gcd(a, b)
    return math.gcd(p, 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 and j == l:
                ans += i
            elif i==j or j==l or l==i:
                ans += 3*gcd(i, j, l)
            else:
                ans += 6 * gcd(i, j, l)
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):
        s=math.gcd(a,b)
        for c in range(1,k+1):
            t=math.gcd(s,c)
            ans+=t
print(ans)

Solution (Python):
import math

K = int(input())

ans = 0
memo = {}
for a in range(1, K+1):
    for b in range(1, K+1):
        gcd_ab = math.gcd(a, b)
        try:
            memo[gcd_ab] += 1
        except:
            memo[gcd_ab] = 1


for c in range(1, K+1):
    for key in memo.keys():
        ans += math.gcd(c, key) * memo[key]

print(ans)

Solution (Python):
import math

def calc(N):
    gcd = [[0 for _ in range(N + 1)] for _ in range(N + 1)]

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

    ans = 0
    for i in range(1,N + 1):
        for j in range(1,N + 1):
            for k in range(1,N + 1):
                g = gcd[i][j]
                ans += gcd[g][k]
    return ans


N = int(input())

print(calc(N))

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

k = int(read())
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 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):
import math
import itertools

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

K = int(input())
abc = itertools.combinations_with_replacement(list(range(1,K+1)),3)

ans = 0
for a,b,c in abc:
    p = len(set([a,b,c]))
    if p==1:
        ans += gcd(a,b,c)
    elif p==2:
        ans += gcd(a,b,c) * 3
    else:
        ans += gcd(a,b,c) * 6
print(ans)

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

Solution (Python):
from math import gcd

n = int(input())

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

print(sum)

Solution (Python):
def gcd(a,b):
    if dp[a][b]==-1:
        if b==0:
            return a
        else:
            ans=gcd(b,a%b)
            dp[a][b]=ans
            return ans
    else:
        return dp[a][b]

K=int(input())

dp=[[-1 for i in range(K+1)] for j in range(K+1)]

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

print(total)

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

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

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

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
from collections import defaultdict

k = int(input())

d = defaultdict(int)
for a in range(1, k+1):
	for b in range(1, k+1):
		d[math.gcd(a,b)] += 1
n = 0
for c in range(1, k+1):
	for g in d:
		n += math.gcd(c, g) * d[g]
			
print(n)

Solution (Python):
from math import gcd

n = int(input())

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

print(sum)

Solution (Python):
import math
import collections

K = int(input())


cnt = collections.defaultdict(int)
for i in range(1, K + 1):
    for j in range(1, K + 1):
        key = math.gcd(i, j)
        
        cnt[key] = cnt[key] + 1

total = 0
for k in range(1, K + 1):
    for key in cnt.keys():
        
        total = total + math.gcd(k, key) * cnt[key]

print(total)

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

print(S)

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

K = int(input())

print(sum(gcd(gcd(a, b), c) for a, b, c in product(range(1, K + 1), repeat=3)))

Solution (Python):





import sys
import math


sys.setrecursionlimit(10 ** 8)





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

def msum(a, b, c):
    if (a <= b) and (a <= c): 
        return math.gcd(a, math.gcd(b,c))
        
        
        
            
    elif (b <= a) and (b <= c):
        return(math.gcd(b, math.gcd(a,c)))
    elif (c <= a) and (c <= b):
        return math.gcd(c, math.gcd(a,b))
    else:
        print("Grande érreur dans ta tête!!!")

def çözmek(num):
    csum = 0
    for i in range(1, num+1):
        for j in range(1, num+1):
            for l in range(1, num+1):
                
                
                csum += math.gcd(i, math.gcd(j,l))

    return csum


if __name__ == "__main__":
    K = int(input())
    print(çözmek(K))

Solution (Python):
import math


def main():
    k = int(input())
    k1 = k + 1

    sum = 0
    for i in range(1, k1):
        for j in range(1, k1):
            for m in range(1, k1):
                sum += math.gcd(math.gcd(i, j), m)
    print(sum)

main()

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

Solution (Python):
import math


def main():
    k = int(input())
    k1 = k + 1

    sum = 0
    for i in range(1, k1):
        for j in range(1, k1):
            for m in range(1, k1):
                sum += math.gcd(math.gcd(i, j), m)
    print(sum)

main()

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

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

Solution (Python):
s=0
k=int(input())
from math import gcd
from itertools import product
L=product([i for i in range(1,k+1)],repeat=2)
S=[gcd(a,b) for (a,b) in L]
from collections import Counter
S_counter=Counter(S)
for key,value in S_counter.items():
    for i in range(1,k+1):
        s+=gcd(key,i)*value
print(s)

Solution (Python):
from math import gcd
 
k = int(input())
ans = 0
for x in range(1, k + 1):
    for y in range(1, k + 1):
        for z in range(1, k + 1):
            ans += gcd(gcd(x, y), z)
print(ans)

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

print(ans)

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

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):
k = int(input())
from math import gcd

samu = 0

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


print(samu)

Solution (Python):
import math

count = 0

K = int(input())
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):
            count += math.gcd(temp, c)

print(count)

Solution (Python):
from math import gcd 

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

print(mysum)

Solution (Python):
import math

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

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

Solution (Python):
import math

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

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

Solution (Python):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
K = int(input())
count = 0
for i in range(1,K-1):
  for j in range(i+1,K):
    for k in range(j+1,K+1):
      count += 6*(gcd(i,j,k))
      
for i in range(1,K):
  for j in range(i+1,K+1):
    count += 6*(gcd(i,j))

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

Solution (Python):
from math import gcd


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


if __name__ == '__main__':
    main()