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 itertools
import math


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


def main() -> None:
    k = int(input())

    one_to_k = range(1, k + 1)
    s1 = sum(one_to_k)
    s2 = sum(math.gcd(a, b) for a, b in itertools.combinations(one_to_k, 2))
    s3 = sum(gcd(a, b, c) for a, b, c in itertools.combinations(one_to_k, 3))
    print(s1 + 6 * s2 + 6 * s3)


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

print(ans)

Solution (Python):
from sys import stdin
from math import gcd


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    K = int(_in[0])  
    
    cnt = 0
    for a in range(1,K+1):
        cnt += a
        for b in range(a+1,K+1):
            ab = gcd(a,b)
            cnt += 6*ab
            for c in range(b+1,K+1):
                cnt += 6*gcd(ab, c)
    
    print(cnt)

Solution (Python):
from math import *

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):
def main():
    s = 0
    n = int(input())
    gd1s = []
    combs = {}
    '''
    for i in range(1, n+1):
        for j in range(1, n+1):
            try:
                gd1 = combs[(i, j)]
                combs[(j, i)] = gd1
                gd1s.append(gd1)
            except:
                gd1 = gcd(i, j)
                gd1s.append(gd1)
                combs[(i, j)] = gd1

    for k in range(1, n+1):
        for g in gd1s:
            s += combs[(g,k)]
    print(s)
    '''
    for i in range(1, n+1):
        for j in range(1, n+1):
            gcd_tmp = gcd(i, j)
            for k in range(1, n+1):
                s += gcd(gcd_tmp, k)
    print(s)

                

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

if __name__ == "__main__":
    main()

Solution (Python):

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

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd

def gcdx(x,y,z):
    return gcd(gcd(x,y),z)
n = int(input())
a = 0
longp = []

for i in range(1,n+1):
    for j in range(1,n+1):
        for p in range(1,n+1):
            a += gcdx(i,j,p)
           
print(a)

Solution (Python):
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a
  
def gcd3(a,b,c):
    return gcd(gcd(a,b),c)
    
k= int(input())    
total = 0
for i in range(1,k+1):
    for ii in range(1,k+1):
        for iii in range(1,k+1):
            total += gcd3(i,ii,iii)
 
print(total)

Solution (Python):
import math

k = int(input())

ans = 0

for i in range(k):
  for j in range(i,k):
    for h in range(j, k):
      if i == j == h:
        ans += math.gcd(math.gcd(i+1, j+1), h+1)
      elif (i != j) and (j != h) and (i != h):
        ans += math.gcd(math.gcd(i+1, j+1), h+1) * 6
      else:
      	ans += math.gcd(math.gcd(i+1, j+1), h+1) * 3
      
print(ans)

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

def main():
	mp = {}
	k = int(input())	
	s = 0
	for i in range(1, k + 1):
		for j in range(1, k + 1):
			d = gcd(j, i)
			if d not in mp:
				mp[d] = 0 
			mp[d] += 1 
	for key in mp:
		for j in range(1, k+1):
			s += gcd(key, j) * mp[key]
	print(s)		
	return 0

main()

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

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

print(ans)

Solution (Python):
import math

N = int(input())
s = 0

i = 1
j = 1
k = 1

while i <= N:
	j = i + 1
	while j <= N:
		k = j + 1
		l = math.gcd(i,j)
		while k <= N:
			s = s + math.gcd(l,k)*6
			k = k + 1
		s = s + math.gcd(i,j)*6
		j = j + 1
	s = s + math.gcd(i,i)
	i = i + 1

print(s)

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

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

ans = 0
if n == 1:
    ans = 1
else:
    for i in range(n):
        i = i + 1
        for j in range(n):
            j = j + 1
            for k in range(n):
                k = k + 1
                ij = gcd(i,j)
                ans = ans + gcd(ij,k)

print(ans)

Solution (Python):
import math


N = int(input())

s = 0

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

print(s)

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

print(res)

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




import math 

def gcds(l):
    res=0
    for val in l:
        res=math.gcd(res,val)
    return res
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for m in range(1,k+1):
            ans+=gcds([i,j,m])
print(ans)

Solution (Python):
from math import gcd

k = int(input())

gcd_sum = [0] * 201
res = 0

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

for a in range(1, k + 1):
	for b in range(1, k + 1):
		g = gcd(a, b)
		res += gcd_sum[g]

print(res)

Solution (Python):




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

def gcd3(lista):
    ans=lista[0]
    for i in range(1,3):
        ans=gcd2(ans,lista[i])
    return ans

K=int(input())
sums=0
for i in range(K):
    for j in range(K):
        for k in range(K):
            sums+=gcd3([1+i,1+j,1+k])
print(sums)

Solution (Python):
import math
 
n=int(input())
 
sum=0
for i in range(1,n+1):
  for j in range(1,n+1):
      x=math.gcd(i,j)
      for h in range(1,n+1):
        p=math.gcd(x,h)
        sum=sum+p
print(sum)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

k = int(input())

ans = 0

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

print(ans)

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

Solution (Python):


import math
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):
            cal=math.gcd(math.gcd(a,b),c) 
            
            if a!=b and b!=c:
                count+=cal*6
            elif a==b==c:
                count+=cal
            else:
                count+=cal*3
            

print(count)

Solution (Python):
import math

n=int(input())

sum=0
for i in range(1,n+1):
  for j in range(1,n+1):
      x=math.gcd(i,j)
      for h in range(1,n+1):
        p=math.gcd(x,h)
        sum=sum+p
print(sum)

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

print(ans)

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

Solution (Python):
from math import gcd

k = int(input())

ans = 0

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

print(ans)

Solution (Python):
import math
g = math.gcd
r = range(1, int(input())+1)
print(sum(g(g(a, b), c) for a in r for b in r for c 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):
        gcd_ab = gcd(a, b)
        for c in range(1,K+1):
            ans += gcd(gcd_ab, c)
print(ans)

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

print(t)

Solution (Python):
from numpy import*;print(sum((f:=gcd.outer)(f(r:=range(1,int(input())+1),r),r)))

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

print(t)

Solution (Python):
import math

def gcd(x, y, z):
    ex = math.gcd(x, y)
    return math.gcd(ex, z)

def comb(x):
    num = len(set(x))
    if num == 1:
        return 1
    elif num == 2:
        return 3
    else:
        return 6

k = int(input())
ans = 0
for i in range(1, k + 1):
    for j in range(i, k + 1):
        for l in range(j, k + 1):
            g = gcd(i, j, l)
            ans += g * comb([i, j, l])
print(ans)

Solution (Python):
from numpy import*;f=gcd.outer;print(sum(f(f(r:=range(1,int(input())+1),r),r)))

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):
        a = gcd(x, y)
        for z in range(1, k + 1):
            ans += gcd(a, z)
print(ans)

Solution (Python):
import math

k = int(input())

res = 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):
            res += math.gcd(x, l)

print(res)

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

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

Solution (Python):
from math import gcd

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

Solution (Python):

K = int(input())


import math

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

print(answer)

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

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

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

Solution (Python):
import math
import fractions

k = input()

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

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

print(result)

Solution (Python):
import math
gcd1 = 0
gcd3 = 0
gcd6 = 0
N = int(input())
for a in range(1, N + 1):
  for b in range(1, N + 1):
    for c in range(1, N + 1):
      if a < b < c:
        gcd6 += math.gcd(a,math.gcd(b, c))
      elif a == b < c or a < b == c:
        gcd3 += math.gcd(a,math.gcd(b, c))
      elif a == b == c:
        gcd1 += math.gcd(a,math.gcd(b, c))
print(6 * gcd6 + 3 * gcd3 + gcd1)

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

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

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

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

ans = 0

for a in range(1, K+1):
  for b in range(1, K+1):
    r = gcp(a, b)
    for c in range(1, K+1):
      ans += gcp(r, c)
      
print(ans)

Solution (Python):
import math

K = int(input())
li = []
ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        li.append(math.gcd(i,j))
for g in li:
    for k in range(1,K+1):
        ans += math.gcd(g,k)
print(ans)

Solution (Python):
from math import gcd

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

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

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

Solution (Python):
import math

K = int(input())

ans = 0
'''
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
           ab = math.gcd(i,j)
           abc = math.gcd(ab,l)
           print(i,j,l)
           ans += abc
'''
           
for i in range(1,K+1):
    for j in range(i,K+1):
        for l in range(j,K+1):
           ab = math.gcd(i,j)
           abc = math.gcd(ab,l)
           
           if i == j and j == l:
               abc = abc
               
           elif i == j or j == l or i == l:
               abc = 3*abc
               
           elif i != j and i != l and j != l:
               abc = 6*abc
               
           
           ans += abc
           


print(ans)

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

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

from math import gcd
 
res=0
 
for n in range(1,k+1):
    for i in range(1,k+1):
        for j in range(1,k+1):

            res+=gcd(gcd(n,i),j)

print(res)

Solution (Python):
from math import gcd

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

print(ans)

Solution (Python):
from math import gcd

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

print(ans)

Solution (Python):
import math
from functools import reduce

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

K = int(input())
output = 0
for i1 in range(1,K+1):
    for i2 in range(1,K+1):
        for i3 in range(1,K+1):
            output += gcd(i1,i2,i3)
print(output)

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

gcd = []
for i in range(210):
    gcd.append([-1] * 210)
for i in range(205):
    for j in range(205):
        gcd[i][j] = GCD(i, j)

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

Solution (Python):
import math

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

Solution (Python):
import math
k = int(input())
a = []
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):
            y = math.gcd(l, x)
            a.append(y)
print(sum(a))

Solution (Python):

import sys
input = sys.stdin.readline
import math

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

Solution (Python):
import math
k = int(input())
a = []
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):
            y = math.gcd(l, x)
            a.append(y)
print(sum(a))

Solution (Python):
from math import gcd
k = int(input())
gcd_sum=[0]*201
ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        gcd_sum[i] += gcd(i,j)
for i in range(1,k+1):
    for j in range(1,k+1):
        g = gcd(i,j)
        ans += gcd_sum[g]
print(ans)

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

def gcd(n, m):
    if m == 0:
        return n
    return gcd(m, n % m)

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

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

def gcd(a, b):
    if a < 1 or b < 1 :
        if a > 0:
            return a
        return b
    remainder = 0
    if a % b == 0 :
        return b
    remainder = a % b
    a = b
    b = remainder
    return gcd(a, b)

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):
from itertools import product
from math import gcd

k = int(input())

result = 0
for a, b, c in product(range(1, k+1), repeat=3):
    result += gcd(a, gcd(b, c))

print(result)

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

gcd = [[0]*K for i in range(K)]
L = [0]*K

for a in range(1,K+1):
    for b in range(1,K+1):
        A = fractions.gcd(a,b)
        gcd[a-1][b-1] = A
        L[A-1] +=1

sum=0
for c in range(1,K+1):
    for i in range(1,K+1):
        B = gcd[c-1][i-1]
        sum +=  B * L[i-1]

print(sum)

Solution (Python):
k = int(input())
from math import gcd
ans = 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):
      ans += gcd(gcd_ab, c)
print(ans)

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(1000000)

def Euclid_algorithm(a:int, b:int)->int:
    if a < b:
        a, b = b, a
    else:
        pass
    if b == 0:
        return a
    else:
        return Euclid_algorithm(b, a % b)

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

Solution (Python):
def main():
  from math import gcd
  k = int(input())
  
  total = 0
  for a in range(1,k+1):
    for b in range(1,k+1):
      temp = gcd(a, b)
      for c in range(1,k+1):
        total+= gcd(temp, c)
  print(total)
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):
        tmp = gcd(i, j)
        for k in range(1, K+1):
            ans += gcd(tmp, k)
print(ans)

Solution (Python):
import math

K=int(input())
sum=0


for i in range(1,K+1):
    for j in range(1,K+1):
        a=math.gcd(i,j)
        for k in range(1,K+1):
            b=math.gcd(a, k)
            sum = sum + b

print(sum)

Solution (Python):
import math

K=int(input())
sum=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):
            b=math.gcd(a, k)
            sum = sum + b

print(sum)

Solution (Python):
import math
from functools import reduce

k = int(input())

answer = (1+k)*k/2

for i in range(1, k):
  for j in range(i+1, k+1):
    answer += 6*math.gcd(i, j)
    
for x in range(1, k-1):
  for y in range(x+1, k):
    for z in range(y+1, k+1):
      answer += 6*reduce(math.gcd, [x, y, z])
      
print(int(answer))

Solution (Python):
from math import gcd

K=int(input())

total=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):
      total +=gcd(ab,c)

print(total)

Solution (Python):

from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math
from math import gcd


N = int(input())
ans = 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):
            ans += gcd(b, k)

print(ans)

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

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

G = []
i,j = 1,1
while i < 201:
    j = 1
    g0 = []
    while j < 201:
        g0.append(gcd(i,j))
        j += 1
    g0 = tuple(g0)
    G.append(g0)
    i += 1
n = int(input())
i = 1
j = 1
k = 1
ans = 0
while i < n+1:
    j = 1
    while j < n+1:
        k = 1
        p = G[i-1][j-1]
        while k < n+1:
            ans += G[k-1][p-1]
            k += 1
        j += 1
    i += 1
print(ans)

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

Solution (Python):
k = int(input())
def gcd2(a,b):
    if b ==0:
        return a
    else:
        return gcd2(b, a%b)
    
def gcd3(a,b,c):
    d = gcd2(a,b)
    return gcd2(d,c)

total = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for h in range(1,k+1):
            total += gcd3(i,j,h)
print(total)

Solution (Python):
import math

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

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

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

G = []
i,j = 1,1
while i < 201:
    j = 1
    g0 = []
    while j < 201:
        g0.append(gcd(i,j))
        j += 1
    g0 = tuple(g0)
    G.append(g0)
    i += 1
n = int(input())
i = 1
j = 1
k = 1
ans = 0
while i < n+1:
    j = 1
    while j < n+1:
        k = 1
        p = G[i-1][j-1]
        while k < n+1:
            ans += G[k-1][p-1]
            k += 1
        j += 1
    i += 1
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
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
 
def main():
    k = int(input())
    ans = 0
 
    for i in range(k):
        for j in range(k):
            for ii in range(k):
                ans += gcd(i+1,gcd(j+1,ii+1))
 
    print(ans)
 
if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd

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

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd

k = int(input())
ans = 0

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

print(ans)

Solution (Python):


import sys
import pprint
from math import gcd


sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	k = int(sys.stdin.readline())
	s = 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):
				s += gcd(x, c)
	print(s)
	


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

Solution (Python):
import fractions


k = int(input())

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

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 time
def gcd(a, b):
    return a if b == 0 else gcd(b, a%b)

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

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

Solution (Python):
K = int(input())
mod = int(1e9) + 7
X = [0 for _ in range(K)]
def doubling(n, m):
  y = 1
  base = n
  while m != 0:
    if m % 2 == 1:
      y *= base
      y %= mod
    base *= base
    base %= mod
    m //= 2
  return y
SS = 0
for i in range(K, 0, -1):
  d = K // i
  S = doubling(d, 3)
  if d > 1:
    for j in range(2, d+1):
      S -= X[j*i-1]
      S %= mod
  X[i-1] = S
  SS += i * S
  SS %= mod
print(SS)

Solution (Python):


import sys
import pprint
from math import gcd


sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	k = int(sys.stdin.readline())
	s = 0
	for a in range(1, k+1):
		for b in range(1, k+1):
			for c in range(1, k+1):
				s += gcd(gcd(a, b), c)
	print(s)
	


main()

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

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

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

Solution (Python):
def mapint_inp():
    return map(int, input().split())
 
def intinp():
    return int(input())
 
import math
import itertools
  
K = intinp()
 
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):
k=int(input())
sum=0

def gcd(x,y):
  while y!=0:
    x,y=y,x%y
  return x
    
for i in range(1,k+1):
  for j in range(1,k+1):
    for z in range(1,k+1):
      sum+=gcd(i,gcd(j,z))
      
print(sum)

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):
k = int(input())
import math
import fractions
ans = 0
e = 0
d = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        e = math.gcd(a,b)
        
        for c in range(b,k+1):
            d = math.gcd(e,c)
            
            if a != b and b!= c and a!= c:
                ans+= d*6
            elif (a==b and a!=c) or (a==c and a!= b) or (b == c and a!= b):
                ans+= d*3
            else:
                ans+= d
print(ans)

Solution (Python):
k = int(input())
import math
import fractions
ans = 0
e = 0
d = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        
        e = fractions.gcd(a,b)
        for c in range(b,k+1):
            
            d = fractions.gcd(e,c)
            if a != b and b!= c and a!= c:
                ans+= d*6
            elif (a==b and a!=c) or (a==c and a!= b) or (b == c and a!= b):
                ans+= d*3
            else:
                ans+= d
print(ans)

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

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

print(ans)