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

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop

from math import gcd

INF = float('inf')
NIL = - 1

N = int(input())
ans = 0
for i in range(1, N + 1):
    if i == 1:
        ans += N ** 2
        continue
    for j in range(1, N + 1):
        if j == 1:
            ans += N
            continue
        tmp = gcd(i, j)
        if tmp == 1:
            ans += N 
            continue
        for k in range(1, N + 1):
            if k == 1:
                ans += 1
                continue
            ans += gcd(tmp, k)
print(ans)

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

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

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop

from math import gcd

INF = float('inf')
NIL = - 1

N = int(input())
ans = 0
for i in range(1, N + 1):
    if i == 1:
        ans += N ** 2
        continue
    for j in range(1, N + 1):
        if j == 1:
            ans += N
            continue
        tmp = gcd(i, j)
        for k in range(1, N + 1):
            ans += gcd(tmp, k)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop

from math import gcd
    
INF = float('inf')
NIL = - 1

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):
[print(sum([g(g(i,j),k)for g in[__import__('math').gcd]for n in[int(input())] for r in[range(1,n+1)]for i in r for j in r for k in r]))]

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

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

Solution (Python):

import sys
from collections import Counter
from itertools import permutations, combinations
from math import gcd
from math import ceil, floor
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    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(tmp, c)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd

k = int(input())

r = range(1, k + 1)
ans = sum([gcd(gcd(a, b), c) for a in r for b in r for c in r])
print(ans)

Solution (Python):
from math import gcd

k = int(input())

r = range(1, k + 1)
ans = sum([gcd(gcd(a, b), c) for a in r for b in r for c in r])
print(ans)

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

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

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

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

ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        t = gcd(a, b)
        if t == 1:
            ans += k
            continue
        for c in range(1, k+1):
            ans += gcd(t, c)
print(ans)

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

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

ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        t = gcd(a, b)
        if t == 1:
            ans += k
            continue
        for c in range(1, k+1):
            ans += gcd(t, c)
print(ans)

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

from fractions import gcd


d = {}

def f(x, y, z):
    
    if d.get((x,y)):
        s = d[(x,y)]
    else:
        s = gcd(x, y)
        d[(x,y)] = s

    if d.get((s,z)):
        t = d[(s,z)]
    else:
        t = gcd(s,z)
        d[(s,z)] = t

    return t

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

print(ans)

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

"""
memo = [[-1 for i in range(k+1)] for j in range(k+1)]
def gcd(a, b):
    if b == 0:
        return a
    else:
        if memo[a][b] == -1:
            memo[a][b] = gcd(b, a % b)
        return memo[a][b]
"""

def my_gcd(a, b):
    if a < b: a, b = b, a
    c = a % b
    if c == 0: return b
    while c!=0:
        c = a % b
        a = b
        b = c
    return a

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

Solution (Python):
import math

K = int(input())
l = 0
ans = 0

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

Solution (Python):
from math import gcd


def solve():

    K = int(input())

    rangeK = range(1, K+1)
    ans = 0

    for a in rangeK:
        for b in rangeK:
            for c in rangeK:
                ans += gcd(gcd(a, b), c)

    print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):
from fractions import gcd

memo = {}
ans = 0

k = int(input())

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

for a in range(1, k+1):
    for b in range(a, k+1):
        g = my_gcd[(a, b)]
        for c in range(b, k+1):
            g2 = my_gcd[(g, c)]

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

print(ans)

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

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

Solution (Python):
import math
K=int(input())
print(sum(math.gcd(math.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)))

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

Solution (Python):
import math
K=int(input())
print(sum(math.gcd(math.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))))

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

K=int(read())
l=[[0]*(K+1) for _ in range(K+1)]
for i in range(1,K+1):
    for j in range(i,K+1):
        x=gcd(i,j)
        l[i][j],l[j][i]=x,x

ans=0
for a in range(1,K+1):
    for b in range(1,K+1):
        x=l[a][b]
        for c in range(1,K+1):
            ans+=l[x][c]

print(ans)

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

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

K=int(read_())
ans=0
for a in range(1,K+1):
    for b in range(1,K+1):
        x=math.gcd(a,b)
        for c in range(1,K+1):
            ans+=math.gcd(x,c)
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


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

Solution (Python):
from math import gcd


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

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

print(ans)

Solution (Python):
def ABC162C(a,b):
    while b != 0:
        a %= b
        if a == 0:break
        b %= a
    return max(a,b)

k = int(input())
p=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for m in range(1,k+1):
            p+=ABC162C(i,ABC162C(j,m))
print(p)

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

print(ans)

Solution (Python):
k = int(input())
import math
num = 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)
            num += b
print(num)

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

print(res)

Solution (Python):
import math

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

Solution (Python):
import math

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

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

Solution (Python):


from functools import reduce

k = int(input())

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

def gcds(*nums):
	return reduce(gcd, nums)

res = 0
for a in range(1, k + 1):
	for b in range(1, k + 1):
		for c in range(1, k + 1):
			res += gcds(a, b, c)

print(res)

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

ans = []

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

ans = [
        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(ans))

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

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

def combs(k):
    res = []
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                res.append(gcd(a, b, c))
    print(sum(res))

combs(int(input()))

Solution (Python):
import math

n = int(input())

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

Solution (Python):
def lcm(a,b):
    if a < b:
        a,b = b,a
    while a % b != 0:
        a,b = b,a%b
    return b

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

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

Solution (Python):
from math import gcd

k = int(input())

total = 0
for i in range(1,k+1):
    for j in range(i+1,k+1):
        tmp = gcd(i,j)
        total += tmp * 6
        for m in range(j+1,k+1):
            total += gcd(tmp,m) * 6
total += k*(k+1)//2

print(total)

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)
ans = nums.sum()
print(ans)

Solution (Python):
import math
N = int(input())
GCD = [[math.gcd(i, j) for i in range(1, N+1)] for j in range(1, N+1)]

all_gcd = [GCD[GCD[i][j]-1][m] for i in range(N) for j in range(N) for m in range(N)]
print(sum(all_gcd))

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

def computeGCD(x, y):
    while (y):
        x, y = y, x % y

    return x

result = list()
sum = 0
for a in range(1, K+1):
    intermediate_result = list()
    for b in range(1, K+1):
        intermediate_result.append(computeGCD(a, b))
    result.append(intermediate_result)

for a in range(1, K+1):
    for b in range(1, K+1):
        intermediate = result[a-1][b-1]
        for c in range(1, K+1):
            sum += result[intermediate-1][c-1]

print(sum)

Solution (Python):
import math
N = int(input())
all_gcd = [math.gcd(math.gcd(i, j), m) for i in range(1, N+1) for j in range(1, N+1) for m in range(1, N+1)]
print(sum(all_gcd))

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

def computeGCD(x, y):
    while (y):
        x, y = y, x % y

    return x

sum = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        intermediate = computeGCD(a, b)
        for c in range(1, K+1):
            sum += computeGCD(c, intermediate)

print(sum)

Solution (Python):
import math
N = int(input())
all_gcd = []
for i in range(1, N+1):
  for j in range(1, N+1):
    x = math.gcd(i, j) if i != j else i
    for m in range(1, N+1):
      y = math.gcd(x, m) if x != m else x
      all_gcd.append(y)

print(sum(all_gcd))

Solution (Python):
import math
from functools import reduce 
import itertools
k = int(input())
l = [i for i in range(1,k+1)]
ans = 0
def gcd(*numbers):
    return reduce(math.gcd, numbers)
for x in itertools.product(l,repeat=3):
    ans += gcd(x[0],x[1],x[2])
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(a, gcd(b, c))
print(ans)

Solution (Python):
from math import gcd

K = int(input())

def gcd_calc(x:int, y:int):
  while(y != 0):
    z = x % y;
    x = y;
    y = z;
  return x

output = 0

for a in range(1, K+1):
  for b in range(1, K+1):
    for c in range(1, K+1):
      output += gcd_calc(a, gcd_calc(b,c))

print(output)

Solution (Python):
import math
from numba import jit
from numba import prange

K = int(input())


@jit(parallel=True)
def apt(N):
    cnt = 0
    for i in prange(1, K+1):
        for j in prange(1, K + 1):
            for k in prange(1, K + 1):
                cnt += math.gcd(k, math.gcd(i, j))
    return cnt


print(apt(K))

Solution (Python):
import math

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


print(cnt)

Solution (Python):
import math
from numba import jit

K = int(input())


@jit
def apt(N):
    cnt = 0
    for i in range(1, K+1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                cnt += math.gcd(k, math.gcd(i, j))
    return cnt


print(apt(K))

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

result = 0
for k in range(1, K + 1):
  for l in range(1, K + 1):
    for m in range(1, K + 1):
      gcd_res = gcd(gcd(k, l), m)
      result += gcd_res

print(result)

Solution (Python):
def euclid(a, b):
  division = a if (a > b) else b
  divider = b if (a > b) else a
  reminder = 0
  while divider != 0:
    reminder = division % divider
    division = divider
    divider = reminder
  
  return division

K = int(input())

result = 0
for k in range(1, K + 1):
  for l in range(1, K + 1):
    for m in range(1, K + 1):
      gcd = euclid(euclid(k, l), m)
      result += gcd

print(result)

Solution (Python):
import sys 






def GCD(p,q):
	if (p%q==0):
		return q
	return GCD(q,p%q)
s = 0
k = int(sys.stdin.readline())
for i in range(1,k+1):
	for j in range(1,k+1):
		for m in range(1,k+1):
			s += GCD(GCD(i,j),m)
sys.stdout.write(str(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):
def getN():
    return int(input())





def gcd(p, q):
    while q != 0:
        r = p % q
        p = q
        q = r
    return p

def main():
    
    K = getN()

    result = 0

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

    return

if __name__ == "__main__":
    main()

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

Solution (Python):

from math import gcd

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


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):
            
            gcd_ijk = gcd(gcd_ij, k)
            
            ans += gcd_ijk

print(ans)

Solution (Python):
ans = [1, 9, 30, 76, 141, 267, 400, 624, 885, 1249, 1590, 2208, 2689, 3411, 4248, 5248, 6081, 7485, 8530, 10248, 11889, 13687, 15228, 17988, 20053, 22569, 25242, 28588, 31053, 35463, 38284, 42540, 46581, 50893, 55362, 61824, 65857, 71247, 76884, 84388, 89349, 97881, 103342, 111528, 120141, 128047, 134580, 146316, 154177, 164817, 174438, 185836, 194157, 207927, 218812, 233268, 245277, 257857, 268182, 288216, 299257, 313635, 330204, 347836, 362973, 383709, 397042, 416448, 434025, 456967, 471948, 499740, 515581, 536073, 559758, 583960, 604833, 633651, 652216, 683712, 709065, 734233, 754734, 793188, 818917, 846603, 874512, 909496, 933081, 977145, 1006126, 1041504, 1073385, 1106467, 1138536, 1187112, 1215145, 1255101, 1295142, 1342852, 1373253, 1422195, 1453816, 1502376, 1553361, 1595437, 1629570, 1691292, 1726717, 1782111, 1827492, 1887772, 1925853, 1986837, 2033674, 2089776, 2145333, 2197483, 2246640, 2332104, 2379085, 2434833, 2490534, 2554600, 2609625, 2693919, 2742052, 2813988, 2875245, 2952085, 3003306, 3096024, 3157249, 3224511, 3306240, 3388576, 3444609, 3533637, 3591322, 3693924, 3767085, 3842623, 3912324, 4027884, 4102093, 4181949, 4270422, 4361548, 4427853, 4548003, 4616104, 4718640, 4812789, 4918561, 5003286, 5131848, 5205481, 5299011, 5392008, 5521384, 5610705, 5739009, 5818390, 5930196, 6052893, 6156139, 6239472, 6402720, 6493681, 6623853, 6741078, 6864016, 6953457, 7094451, 7215016, 7359936, 7475145, 7593865, 7689630, 7886244, 7984165, 8130747, 8253888, 8403448, 8523897, 8684853, 8802826, 8949612, 9105537, 9267595, 9376656, 9574704, 9686065, 9827097, 9997134, 10174780, 10290813, 10493367, 10611772, 10813692]
n = int(input())

print(ans[n - 1])

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

Solution (Python):

from math import gcd


def main():
    K = int(input())
    gcd_three = lambda x, y, z: gcd(gcd(x, y), z)
    ans = sum(
        gcd_three(i, j, k)
        for i in range(1, K + 1)
        for j in range(1, K + 1)
        for k in range(1, K + 1)
    )
    print(ans)


if __name__ == "__main__":
    main()

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

ans = 0
def gcd(x,y):
    while y:
        x, y = y, x % y
    return x

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

print(ans)

Solution (Python):

from math import gcd


def main():
    K = int(input())
    gcd_three = lambda x, y, z: gcd(gcd(x, y), z)
    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_three(i, j, k)
    print(ans)


if __name__ == "__main__":
    main()

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

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)

Solution (Python):

import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

import math

n = int(input())
ans = 0

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

print(ans)

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

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

print(sum)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
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):
		a = gcd(i, j)
		for k in range(1, K+1):
			ans += gcd(a, k)
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

K = INT()

@lru_cache(maxsize=None)
def memo_gcd(a, b):
	return gcd(a, b)

ans = 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):
			ans += memo_gcd(a, k)
print(ans)

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

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

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

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

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

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

A=[0]*K

for i in range(K,0,-1):
    b=pow(K//i,3)
    A[i-1]=b

for i in range(K,0,-1):
    for j in range(K//i-1):
        c=i*(j+2)
        A[i-1]-=A[c-1]

s=0
for i in range(K):
    s+=A[i]*(i+1)

print(s)

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):
from math import gcd
k = int(input())
うんち = 0
for でびさま in range(1,k+1):
    for でびでび in range(1,k+1):
        for でびる in range(1,k+1):
            うんち += gcd(でびさま,gcd(でびでび,でびる))
print(うんち)

Solution (Python):
from math import gcd
k = int(input())
うんち = 0
for デビ in range(1,k+1):
    for デビデ in range(1,k+1):
        for ビル in range(1,k+1):
            うんち += gcd(デビ,gcd(デビデ,ビル))
print(うんち)

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

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

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

print(total)

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

Solution (Python):


import sys
from math import gcd

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

K = INT()

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)

Solution (Python):

count=0
import math
n=int(input())
a=1
b=1
c=1
while c<=n:
    while b<=n:
        while a<=n:
            d=math.gcd(a,b)
            e=math.gcd(d,c)
            count+=e
            a+=1
        a=1
        b+=1
    b=1
    c+=1
print(count)

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

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

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

print(ans)

Solution (Python):
from __future__ import print_function

import sys
from math import gcd
from functools import reduce

input = sys.stdin.readline


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


def main():
    k = int(input().strip())
    ans=0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for k in range(1,k+1):
                ans += find_gcd([i,j,k])
    print(ans)


if __name__ == '__main__':
    main()

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)


K = int(input())

result = 0
for i in range(1, K + 1):
    for p in range(1, K + 1):
        for q in range(1, K + 1):
            result += gcd(i, p, q)
            
print(result)

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