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

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007




def main():
	N = inputInt()

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



	print(ans)

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

	while a > 0:
		a, b = b % a, a
	return b






class Graph(object):
    """
    隣接リストによる有向グラフ
    """

    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra(object):
    """
    ダイクストラ法（二分ヒープ）による最短経路探索
    計算量: O((E+V)logV)
    """

    def __init__(self, graph, start):
        self.g = graph.graph

        
        
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0

        
        self.prev = defaultdict(lambda: None)

        
        self.Q = []
        heappush(self.Q, (self.dist[start], start))

        while self.Q:
            
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self, goal):
        """
        startノードからgoalノードまでの最短距離
        """
        return self.dist[goal]

    def shortest_path(self, goal):
        """
        startノードからgoalノードまでの最短経路
        """
        path = []
        node = goal
        while node is not None:
            path.append(node)
            node = self.prev[node]
        return path[::-1]

def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))



def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

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

print(ans)

Solution (Python):
import math
from functools import reduce

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 k in range(1,K+1):
            L = [i,j,k]
            cnt += gcd(*L)
print(cnt)

Solution (Python):
import sys
from math import gcd
def input(): return sys.stdin.readline().rstrip()

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

if __name__ == '__main__':
    main()

Solution (Python):










from math import gcd



def main():
    k = int(input())
    total = 0

    for a in range(1,k+1):
        for b in range(1,k+1):
            for c in range(1,k+1):
                total += gcd(gcd(a,b),c)
    print(total)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
import math
from functools import reduce

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

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

N = int(input())

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

print(ans)

Solution (Python):



K = int(input())


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


def main():
    ans = 0
    d = {}
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            l = (i, j) if i > j else (j, i)
            if l in d:
                x = d[l]
            else:
                x = gcd(l[0], l[1])
                d[l] = x
            for k in range(1, K + 1):
                l = (x, k) if x > k else (k, x)
                if l in d:
                    y = d[l]
                else:
                    y = gcd(l[0], l[1])
                    d[l] = y
                ans += y
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
k = int(input())
ans = 0
import math
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 sqrt,ceil,gcd
        
        
        






arr=[[0 for i in range(201)] for j in range(201)]
for i in range(1,201):
    for j in range(1,201):
        arr[i][j]=gcd(i,j)



a=int(input())
b=a
c=a
ans=0
for i in range(1,a+1):
    for j in range(1,b+1):
        for k in range(1,c+1):
            x=arr[i][j]
            y=arr[x][k]
            ans+=y
print(ans)

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

n=int(input())
ans=0
for i in range(1,n+1):
	for j in range(i,n+1):
		for k in range(j,n+1):
			if i!=j!=k:
				ans+=6*gcd(gcd(i,j),k)
			elif i==j==k:
				ans+=i
			else:
				ans+=3*gcd(gcd(i,j),k)
print(ans)

Solution (Python):
import sys
import math
sys.setrecursionlimit(1000000)

cache = {}
def solve(K):
    s = 0
    for a in range(1, K+1):
        cache[a] = 0
        for b in range(1, K+1):
            cache[a] += math.gcd(a, b)

    for a in range(1, K+1):
        for b in range(1, K+1):
            n = math.gcd(a, b)
            s += cache[n]
    return s

if __name__ == "__main__":
    N = int(input())
    print(solve(N))

Solution (Python):
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def fb(i):
    if i % 15 == 0 or i % 3 == 0 or i % 5 == 0:
        return True
    else:
        return False


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    ret = 0
    for n in range(1, N+1):
        for m in range(1, N + 1):
            for l in range(1, N + 1):
                ret += math.gcd(math.gcd(n,m),l)
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):

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

Solution (Python):
import sys
input = sys.stdin.readline
def gcd(a,b):
	if(a==0 or b==0):
		return a+b
	return gcd(b,a%b)
yo = input()
if(yo[-1]=='\n'):
	yo=yo[:-1]
k=int(yo)
ans=0
for i in range(1,k+1):
	for j in range(1,k+1):
		hcf = gcd(i,j)
		for m in range(1,k+1):
			ans+=(gcd(hcf,m))
print(ans)

Solution (Python):
import math

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

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

memo = dict()
def gcd(x,y):
    if (x,y) in memo:
        return memo[(x,y)]
    if y==0: return x
    memo[(x,y)]=gcd(y,x%y)
    return memo[(x,y)]

ans = 0
gs = dict()
for a in range(1,K+1):
    for b in range(1,K+1):
        g = gcd(a,b)
        gs[g] = gs.get(g,0)+1
for g,n in gs.items():
    for c in range(1,K+1):
        ans += gcd(g,c)*n
print(ans)

Solution (Python):
import math

k = int(input())

ret = list()
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):
            ret.append(math.gcd(ab, c))

print(sum(ret))

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

Solution (Python):

import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


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


def GCDs(*a):
    
    if len(a) == 0:
        return -1  

    if len(a) == 1:
        return a[0]

    res = a[0]
    for i in range(1, len(a)):
        res = GCD(res, a[i])
    return res


def solve(K: int):
    tot = 0
    for i in range(1, K+1):
        for j in range(i, K+1):
            b = GCD(i, j)
            for k in range(j, K+1):
                c = GCD(b, k)
                if i == j == k:
                    tot += c
                elif i == j or j == k or i == k:
                    tot += 3*c
                else:
                    tot += 6*c
    print(tot)
    return


def main():

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


if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd




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

    sm =0
    for i in range(1,N+1):
        for j in range(1,N+1):
            tmp = gcd(i,j)
            for k in range(1,N+1):
                sm += gcd(tmp,k)
    print(sm)



if __name__ == "__main__":
    resolve()

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

N = int(input())

ans = 0

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

print(ans)

Solution (Python):
def gcd(x,y):
    if x<y:
        x,y=y,x
    if y==0:
        return x
    else:
        return gcd(y,x%y)
    
K=int(input())
num=0
for i in range(1,K-1):
    for j in range(i+1,K):
        for k in range(j+1,K+1):
            num+=6*gcd(i,gcd(j,k))
for i in range(1,K):
    for j in range(i+1,K+1):
        num+=6*gcd(i, j)
for i in range(1,K+1):
    num+=i
print(num)

Solution (Python):
import sys
from math import gcd

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


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


if __name__ == '__main__':
    solve()

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

N = int(input())

s = 0
for i in range(1, N+1):
  for j in range(1, N+1):
    for k in range(1, N+1):
    	g = math.gcd(i, j)
    	g = math.gcd(g, k)
    	s += g
print(s)

Solution (Python):

def gcd(a, b):
    if a < b :
        a, b = b, a
    while True:
        a, b = b, a % b
        if b == 0:
            return a
        
k =  int(input())
s = 0
dic = {}
for i in range(1, k+1):
    for j in range(i + 1, k+1):
        a = gcd(i, j)
        for l in range(1, k+1):
            s += gcd(a, l)
            
s = s* 2
for i in range(1, k+1):
    j = i
    a = gcd(i, j)
    for l in range(1, k+1):
        s += gcd(a, l)
print(s)

Solution (Python):
class LCM():
    
    def __init__(self,a,b):
        if a < b:a,b = b,a
        self.a = a
        self.b = b

    
    def gcd(self):
        a,b = self.a,self.b
        def nono(a,b):
            if a < b:a,b = b,a
            if a % b == 0:return b
            else:return nono(b,a % b)
        return nono(a,b)

    
    def lcm(self):
        a,b = self.a,self.b
        def nono(a,b):
            if a < b:a,b = b,a
            if a % b == 0:return b
            else:return nono(b,a % b)
        num = nono(a,b)
        return a*b//num

n = int(input())
ans = 0
dp = [[[-1 for i in range(201)] for j in range(201)] for k in range(201)]
for i in range(1,n+1):
    for j in range(1,1+n):
        for k in range(1,1+n):
            ls = [i,j,k]
            ls.sort()
            if dp[ls[0]][ls[1]][ls[2]] != -1:
                ans += dp[ls[0]][ls[1]][ls[2]]
                continue
            c = LCM(i,j)
            l = LCM(c.gcd(),k)
            a = l.gcd()
            ans += a
            dp[ls[0]][ls[1]][ls[2]] = a

print(ans)

Solution (Python):
import math
import sys


input = sys.stdin.readline


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


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

print(ans)

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

Solution (Python):
import math

K = int(input())

ans = 0

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

print(ans)

Solution (Python):

from math import gcd

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

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

k = int(input())
a = [[0] * k for _ in range(k)]
ans = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        a[i - 1][j - 1] = gcd(i, j)
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for l in range(1, k + 1):
            ans += gcd(a[i - 1][j - 1], l)
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):
            val = gcd(i,j)
            ans += gcd(val, k)
print(ans)

Solution (Python):
import sys,math

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	k = II()
	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==k):
					ans += math.gcd(math.gcd(i,j),k)
				elif(i!=j and j!=k and k!=i):
					ans += math.gcd(math.gcd(i,j),k)*6
				else:
					ans += math.gcd(math.gcd(i,j),k)*3
	print(ans)



if __name__ == '__main__':
	main()

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

ans = 0

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

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

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

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


print(ans)

Solution (Python):
from math  import gcd
import sys
sys.setrecursionlimit(10**5)

K = int(input())
ans = 0
a = 1
while a <= K:
  b = 1
  while b <= K:
    c = 1
    while c <= K:
      ans += gcd(a, gcd(b, c))
      c += 1
    b += 1
  a += 1
print(ans)

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

print(ans)

Solution (Python):

from math import gcd


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


if __name__ == '__main__':
    main()

Solution (Python):
def c_sum_of_gcd_of_tuples_easy():
    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)
    return ans

print(c_sum_of_gcd_of_tuples_easy())

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'


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



main()

Solution (Python):
def gcd(a,b):
    if a<b:
        a,b = b,a
    elif b==0:
        return a
    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):
            mm = gcd(i,j)
            ans += gcd(mm,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):
        x = gcd(i, j)
        for k in range(1, k + 1):
            ans += gcd(x, k)

print(ans)

Solution (Python):
from math import gcd


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


if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import Counter
from pprint import pprint
from math import gcd
from functools import reduce

def myinput():
    return map(int,input().split())

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

k = int(input())

def gcd3(*numbers):
    return reduce(gcd, numbers)

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

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

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

MOD = 1000000007

def smax(a,b):
    if a>b:
        return a
    else:
        return b

def smin(a,b):
    if a<b:
        return a
    else:
        return b

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

from collections import defaultdict

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

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

    ans = 0
    for key in dd:
        for k in range(1,K+1):
            ans += dd[key]*gcd(key,k)
    print(ans)


if __name__ == '__main__':
    main()

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

if __name__ == '__main__':
    solve()

Solution (Python):

import sys

def solve(K: int):
    from math import gcd
    from itertools import product
    from functools import reduce
    def f():
        for abc in product(range(1, K+1), repeat=3):
            yield reduce(gcd, abc)
    print(sum(f()))



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

if __name__ == '__main__':
    main()

Solution (Python):
import math

K = int(input())

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

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


if __name__ == '__main__':
    main()

Solution (Python):
def gcd(a,b):
    while b:
        a,b=b,a%b
    return a
from functools import reduce
def gcd_list(numbers):
   return reduce(gcd, numbers)
k=int(input())
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for x in range(1,k+1):
            ans+=gcd_list([i,j,x])
print(ans)

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


K = int(input())
tot = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        x = gcd(a, b)
        for c in range(1, K+1):
            y = gcd(x, c)
            tot += y
print(tot)

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

Solution (Python):
import math
from functools import reduce

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

k = int(input())
count = 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:
                count += gcd(a,b,c)
            elif a == b:
                count += gcd(a,b,c)*3
            elif b == c:
                count += gcd(a,b,c)*3
            else:
                count += gcd(a,b,c)*6
        
print(count)

Solution (Python):
from math import gcd
k = int(input())
gcdl = [[0 for i in range(k + 1)]for i in range(k+1)]
for i in range(1, k + 1):
    for j in range(1, k + 1):
        gcdl[i][j] = gcd(i, j)
s = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for l in range(1, k + 1):
            s += gcdl[i][gcdl[j][l]]

print(s)

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

Solution (Python):
from math import gcd


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

n = inp()
s = 0
for i in range(1,n + 1):
    for j in range(1,n + 1):
        b = gcd(i,j)
        for k in range(1, n + 1):
            s += gcd(b,k)
print(s)

Solution (Python):
from math import gcd

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

Solution (Python):
import sys
input = sys.stdin.buffer.readline

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

K = int(input())
cnt = [0] * (K ** 2 + 1)

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


answer = 0
for ab in range(1, K ** 2 + 1):
    if cnt[ab]:
        for c in range(1, K + 1):
            answer += cnt[ab] * gcd(ab, c)
print(answer)

Solution (Python):
import collections

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

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

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

    

ans = 0

k = int(input())
for ai in range(1,k+1):
    for bi in range(ai,k+1):
        for ci in range(bi,k+1):
            count = len(set({ai,bi,ci}))
            if count == 1:
                comb = 1
            elif count ==2:
                comb =  3
            else:
                comb = 6
            ans += gcd(gcd(ai,bi),ci) * comb
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)