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

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

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


main()

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

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

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

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


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


ans = 0
for p in range(1, k + 1):
    for q in range(1, k + 1):
        for r in range(1, k + 1):
            ans += gcd(gcd(p, q), r)
print(ans)

Solution (Python):

max_codiv = []
 

n = int(input())
 
import math

for i in range(1, n+1):
	for j in range(1, n+1):
		i_j = math.gcd(i, j)
		for k in range(1, n+1):
			i_j_k = math.gcd(i_j, k)
			max_codiv.append(i_j_k)
print(sum(max_codiv))

Solution (Python):
mod = 10**9 + 7
import math 
from bisect import bisect_left 
from collections import Counter
from itertools import combinations
 
def ain():
    return map(int, input().split())
def fain():
    return map(float, input().split())
 
def mc2(s):
    return (s*(s-1))//2 + s
    


    


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

Solution (Python):
import sys
import itertools
import math


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    k = [i for i in range(1, int(readline()) + 1)]
    t = 0
    for v in itertools.product(k, repeat=3):
        t += math.gcd(math.gcd(v[0], v[1]), v[2])
    print(t)


if __name__ == '__main__':
    solve()

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

def func_gcd(a, b):
    if b > a:
        a, b = b, a
    
    while b > 0:
        r = a % b
        a = b
        b = r

    return a

ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for l in range(1, K + 1):
            temp = func_gcd(j, l)
            temp2= func_gcd(i, temp)
            ans += temp2

print(ans)

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

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

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

Solution (Python):
from math import gcd

K = int(input())
ans=0
array = [0] * 201

for X in range(1,K+1):
    for c in range(1,K+1):
        array[X] += gcd(X,c)

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

print(ans)

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

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

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

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

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

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()
from math import gcd
from itertools import product
from functools import reduce
def resolve():
    n = int(input())
    res = 0
    for i, j, k in product(range(1, n + 1), repeat = 3):
        res += reduce(gcd, (i, j, k))
    print(res)
resolve()

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

print(ans)

Solution (Python):
import math
from functools import reduce

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

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

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

Solution (Python):
from math import gcd

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

Solution (Python):
from math import gcd

k = int(input())

res = 0
mul = (0, 1, 3, 6)
for a in range(1, k+1):
    for b in range(a, k+1):
        for c in range(b, k+1):
            res += gcd(gcd(a, b), c) * mul[len({a, b, c})]

print(res)

Solution (Python):

k=int(input())
 
from math import gcd
s6=0
for i in range(2,k-1):
    for j in range(i+1,k):
        for jj in range(j+1,k+1):
            s6+=gcd(gcd(i,j),jj)
 
s3=0
for i in range(2,k):
    for j in range(i+1,k+1):
        s3+=gcd(i,j)
 
s1=int((k+1)*k/2)
 
 
print(s1+s3*6+s6*6+(k-1)*6+(k-2)*(k-1)*3)

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

Solution (Python):
import math
from itertools import combinations_with_replacement

K = int(input())

ans = 0


for i, j, k in combinations_with_replacement(list(range(1,K+1)), 3):
    lstset = {i, j, k}
    if len(lstset)==3: ans += math.gcd(math.gcd(i,j), k) * 6
    elif len(lstset)==2: ans += math.gcd(math.gcd(i,j), k) * 3
    else: ans +=  math.gcd(math.gcd(i,j), k)
    
print(ans)

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

def resolve():
  K = int(input())
  gcd = math.gcd
  result = sum([ gcd(x[0], x[1]) for x in product([ gcd(x[0], x[1]) for x in product(range(1, K+1), range(1, K+1)) ], range(1, K+1)) ])
  print(result)

if __name__ == "__main__":
    resolve()

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

print(ans)

Solution (Python):
import math


K=int(input())

ans=0

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

print(ans)

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

Answer = 0

def getGCD(a,b,c):
    if a == b == c:
        d = 1
    elif a == b or b == c:
        d = 3
    else:
        d = 6
        
    surplus = a % c
    while surplus != 0:
        a,c = c,surplus
        surplus = a % c
    surplus = b % c
    while surplus != 0:
        b,c = c,surplus
        surplus = b % c
    return c * d


for i in range(1,N):
    for j in range(i,N):
        for k in range(j,N):
            Answer += getGCD(k,j,i)
            
print(Answer)

Solution (Python):
import math


K=int(input())

ans=0

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

print(ans)

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

Answer = 0

def getGCD(a,b,c):
    if a == b and b == c:
        d = 1
    elif a == b or b == c or c == a:
        d = 3
    else:
        d = 6
        
    surplus = a % c
    while surplus != 0:
        a = int(c)
        c = int(surplus)
        surplus = a % c
    surplus = b % c
    while surplus != 0:
        b = int(c)
        c = int(surplus)
        surplus = b % c
    return c * d


for i in range(1,N):
    for j in range(i,N):
        for k in range(j,N):
            Answer += getGCD(k,j,i)
            
print(Answer)

Solution (Python):


import math

K = int(input())
ans = 0

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

print(ans)

Solution (Python):
k = int(input())
cnt = [0] * (k + 1)

for i in range(k, 0, -1):
    cnt[i] = (k // i) ** 3
    for j in range(i * 2, k + 1, i):
        cnt[i] -= cnt[j]

print(sum((i* cnt[i] for i in range(1, k + 1))))

Solution (Python):
k = int(input())
n = 3
cnt = [0] * (k + 1)

for i in range(k, 0, -1):
    cnt[i] = (k // i) ** n
    for j in range(i * 2, k + 1, i):
        cnt[i] -= cnt[j]

print(sum((i* cnt[i] for i in range(1, k + 1))))

Solution (Python):
def gcd(p, q):
    if p % q == 0:
        return q
    else:
        return gcd(q, p % q)
k = int(input())
ans = 0
for m in range(1, k+1):
    for n in range(1, k+1):
        for l in range(1, k+1):
            ans += gcd(gcd(m, n), l)
print(ans)

Solution (Python):


K=int(input())



def pront(*n):
    if debug==1:
        print(n)
        
debug=1


import math as m
Z=[ [-1 for i in range(K+1)] for j in range(K+1)]
def C_1(i,j):
    if Z[i][j]!=-1:
        return Z[i][j]
    else:
        x=m.gcd(i,j)
        Z[i][j]=x
        Z[j][i]=x
        return Z[i][j]

x=0
for i in range(1,K+1):
    for j in range(1,K+1):
        y=C_1(i,j)
        if y==1:
            x+=K
        else:
            for k in range(1,K+1):
                x+=C_1(y,k)
print(x)

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

Solution (Python):
k = int(input())
n = 3
cnt = [0] * (k + 1)

for i in range(k, 0, -1):
    cnt[i] = (k // i) ** n
    multiple = i * 2
    while multiple <= k:
        cnt[i] -= cnt[multiple]
        multiple += i

print(sum((i* cnt[i] for i in range(1, k + 1))))

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

def resolve():
  K = int(input())
  gcd = math.gcd
  step1 = [ gcd(x[0], x[1]) for x in product(range(1, K+1), range(1, K+1)) ]
  result = sum([ gcd(x[0], x[1]) for x in product(step1, range(1, K+1)) ])
  print(result)

if __name__ == "__main__":
    resolve()

Solution (Python):
import math


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

    print(ans)

main()

Solution (Python):
import math
s = input()
k = int(s)
n = 3
ans={}
for i in reversed(range(1, k+1)):
    ans[i]=math.floor(k/i)**n
    l=2
    j=i*l
    while j<=k:
        ans[i]=ans[i]-ans[j]
        l +=1
        j = i*l
res=0
for i in ans:
    res+=i*ans[i]
print(res)

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

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

print(total)

Solution (Python):
import math


def main():
    k = int(input())
    ans = 0
    gcd = math.gcd
    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)

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):

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

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

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

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

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

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

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

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

Solution (Python):
import math
k=int(input())
a=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      if i<=j<=l:
        if i==j==l:
          a=a+i
        elif i==j and j!=l:
          a=a+3*math.gcd(i,l)
        elif i!=j and j==l:
          a=a+3*math.gcd(i,l)
        elif i!=j and j!=l:
          a=a+6*math.gcd(i,math.gcd(j,l))
      else:
        a=a+0
print(a)

Solution (Python):
import math

n = int(input())
ans = 0
a = {}
for i in range(1,n+1):
    for j in range(1,n+1):
        tmp = math.gcd(i,j)
        a[tmp] = a.get(tmp, 0)
        a[tmp] += 1

for key, val in a.items():
    for j in range(1, n+1):
        ans += math.gcd(key,j) * val

print(ans)

Solution (Python):
import math

def main():
    n = int(input())
    ans = 0
    a = {}
    for i in range(1,n+1):
        for j in range(1,n+1):
            tmp = math.gcd(i,j)
            a[tmp] = a.get(tmp, 0)
            a[tmp] += 1


    for key, val in a.items():
        for j in range(1, n+1):
            ans += math.gcd(key,j) * val

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):



import sys
input = sys.stdin.readline
from math import gcd


def main():
    K=int(input())
    ret = 0
    gc=[[-1]*K for _ in range(K)]
    for a in range(K):
        for b in range(K):
            if gc[a][b]==-1:
                gc[a][b]=gcd(a+1,b+1)
                gc[b][a]=gc[a][b]
            x=gc[a][b]
            for c in range(K):
                if gc[x-1][c]==-1:
                    gc[x-1][c]=gcd(x,c+1)
                    gc[c][x-1]=gc[x-1][c]
                ret += gc[x-1][c]
    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd

K = int(input())

ans = 0














L = [0] * 201

for g in range(1, K+1):
    for c in range(1, K+1):
        L[g] += gcd(g, c)


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

print(ans)

Solution (Python):
import math

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

    print(ans)

if __name__ == "__main__":
    main()

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

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

Solution (Python):
import math
import itertools
GCD_over_150=[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())
if N<150:
	GCD=[]
	GCD1=[]
	for i in range (1,N+1):
		GCD.append(i)
	for i in itertools.product(GCD,repeat=3):
		GCD1.append(math.gcd(math.gcd(i[0],i[1]),i[2]))
	print(sum(GCD1))    
else:
    print(GCD_over_150[N-150])

Solution (Python):
K=int(input())
N=3
pr=10**9+7
a=0
l=[None]*(K+1)
for i in range(K,0,-1):
    t=pow(K//i,N,pr)
    c=i*2
    while c<=K:
        t-=l[c]
        c+=i
    l[i]=t
    a=(a+t*i)%pr
print(a)

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

Solution (Python):
K=int(input())
 
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a
 
ans=0
 
for a in range(1,K+1):
    for b in range(1,K+1):
        hoge=gcd(a,b)
        for c in range(1,K+1):
            g=gcd(hoge,c)
            ans += g
 
print(ans)

Solution (Python):



import sys
input = sys.stdin.readline
from math import gcd

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

if __name__ == '__main__':
    main()

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

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

    print(ans)

Solution (Python):
import math

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

    print(ans)

if __name__ == "__main__":
    main()

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

ans = 0
h = {}
for i in range(1, K+1):
  for j in range(1, K+1):
    x = gcd(i, j)
    h[x] = h.get(x, 0)
    h[x] += 1

keys = list(h.keys())
n = len(keys)
for i in range(n):
  tmp = 0
  key = keys[i]
  for j in range(1, K+1):
    tmp += gcd(key, j)
  ans += tmp * h[key]

print(ans)

Solution (Python):
N = int(input())
L = [[1]* 201 for _ in range(201)] 

def gcd (a,b):
  if a < b:
    a, b = b, a
  A, B = a, b
  if L[a][b] != 1:
    return L[a][b]
  while b:
    a,b = b, a % b
  L[A][B] = a
  return a

ans = 0
for x in range(1,N+1):
  for y in range(x,N+1):
    for z in range(y,N+1):
      t = gcd(x,y)
      u = gcd(t,z)
      if x==y==z:
        ans += u
      elif x!=y and y!= z:
        ans += u * 6
      else:
        ans += u * 3
      
print (ans)

Solution (Python):
N = int(input())
L = [[1]* 201 for _ in range(201)] 

def gcd (a,b):
  if a < b:
    a, b = b, a
  A, B = a, b
  if L[a][b] != 1:
    return L[a][b]
  while b:
    a,b = b, a % b
  L[A][B] = a
  return a

ans = 0
for x in range(1,N+1):
  for y in range(x,N+1):
    for z in range(y,N+1):
      t = gcd(x,y)
      u = gcd(t,z)
      if x==y==z:
        ans += u
      elif x!=y and y!= z:
        ans += u * 6
      else:
        ans += u * 3
      
print (ans)

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

Solution (Python):
N = int(input())
L = [[1]* 201 for _ in range(201)] 

def gcd (a,b):
  if a < b:
    a, b = b, a
  A, B = a, b
  if L[a][b] != 1:
    return L[a][b]
  while b:
    a,b = b, a % b
  L[A][B] = a
  return a

ans = 0
for x in range(1,N+1):
  for y in range(1,N+1):
    for z in range(1,N+1):
      t = gcd(x,y)
      u = gcd(t,z)
      ans += u
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):
import math
K = int(input())
total = 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):
            total+=math.gcd(tmp,c)
print(total)

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


from math import gcd





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

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
from math import gcd

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

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):


import numpy as np
from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit("i4(i4, i4)", cache=True)
def gcd(x, y):
    if x < y:
        x, y = y, x  
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

@njit("i4(i4)", cache=True)
def getresult(k):
    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)
    return res

if __name__ == "__main__":
    main()

Solution (Python):


import numpy as np
from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit("i4(i4, i4)", cache=True)
def gcd(x, y):
    if x < y:
        x, y = y, x  
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

@njit("i8(i4)", cache=True)
def getresult(k):
    res = np.array([0], dtype=np.int64)
    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)
    return res[0]

if __name__ == "__main__":
    main()

Solution (Python):


from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit
def getresult(k):
    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)
    return res

@njit("i4(i4, i4)", cache=True)
def gcd(x, y):
    if x < y:
        x, y = y, x  
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

if __name__ == "__main__":
    main()

Solution (Python):


from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit
def getresult(k):
    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)
    return res

@njit("i4(i4, i4)")
def gcd(x, y):
    if x < y:
        x, y = y, x  
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

if __name__ == "__main__":
    main()

Solution (Python):
from math import gcd

def gcd3(x, y, z):
    return gcd(gcd(x, y), z)

K = int(input())

if __name__ == "__main__":
    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):
K = int(input())
ans = 0
import math

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 numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit
def getresult(k):
    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)
    return res

@njit
def gcd(x, y):
    if x < y:
        x, y = y, x  
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

if __name__ == "__main__":
    main()

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

Solution (Python):
import sys

input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop




def gcd(a, b):
    """
    Greatest Common Divisor
    Euclidean Algortihm
    """
    if a < b:
        gcd(b, a)
    while b != 0:
        a, b = b, a % b
    return a


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
from time import time


def main():
    K = int(input())
    answer = 0

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

if __name__ == '__main__':
    start = time()
    main()

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, floor, sqrt

INF = float('inf')


def is_prime(n):
    if n < 2:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False

    m = floor(sqrt(n)) + 1
    for p in range(3, m, 2):
        if n % p == 0:
            return False
    return True


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)
        tmp_prime = is_prime(tmp)
        if tmp == 1:
            ans += N
            continue

        for k in range(1, N + 1):
            if k == 1:
                ans += 1
                continue
            if tmp_prime:
                if k % tmp == 0:
                    ans += tmp
                    continue
                else:
                    ans += 1
                    continue
            else:
                ans += gcd(tmp, k)

print(ans)

Solution (Python):
from math import gcd
k = int(input())
ar = [0] * 41000
for i in range(1, 201):
    for j in range(1, 201): ar[i*200+j] = gcd(i, j)

ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        d = ar[a*200+b]
        if d == 1:
            ans += k
            continue
        for c in range(1, k+1): ans += ar[d*200+c]
print(ans)

Solution (Python):

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

Solution (Python):
g=__import__('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)
        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)