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
from math import gcd
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

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

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
import math  
from itertools import product  

x = int(input())  

s = 0  
for a, b, c in product(range(1, x+1), repeat=3):  
  s += math.gcd(math.gcd(a, b), c)  
print(s)

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):
      if i == j == k:
        c += i
      elif i == j:
        c += math.gcd(i,k)
      elif i == k:
        c += math.gcd(i,j)
      elif j == k:
        c += math.gcd(i,k)
      else:
        c += math.gcd(math.gcd(i, j), k)
print(c)

Solution (Python):
import math

k = int(input())
sum = 0

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

Solution (Python):
import math

K = int(input())
print(sum([math.gcd(math.gcd(i, j), k) for i in range(1, K+1) for j in range(1, K+1) for k in range(1, K+1)]))

Solution (Python):
import math
from collections import Counter

K = int(input())

memo = {frozenset((i, j)): math.gcd(i, j) 
        for i in range(1, K+1) for j in range(i, K+1)}

counter = Counter([memo[frozenset((i, j))] 
                   for i in range(1, K+1) for j in range(1, K+1)])

total = 0
for i in range(1, K+1):
  for value, count in counter.items():
    total += memo[frozenset((i, value))] * count
    
print(total)

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

Solution (Python):
import math
from functools import reduce


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


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_list([i, j, k])
print(s)

Solution (Python):
import math

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

Solution (Python):
k = int(input())
sum = 0
def gcd(p,q):
  if p%q == 0:
    return q
  return gcd(q,p%q)
  
for i in range(1,k+1):
   for j in range(1,k+1):
      for k in range(1,k+1):
        sum += gcd(gcd(i,j),k)

print(sum)

Solution (Python):
import math

K = int(input())

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

Solution (Python):
import math
from functools import reduce


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


k = int(input())
s = 0


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


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



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

print(s)

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

main()

Solution (Python):

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

n = int(input())

li = [[0 for i in range(n+1)] for j in range(n+1)]
for i in range(n+1):
  for j in range(0, i+1):
    li[i][j] = gcd(i, j)

ans = 0
for i in range(1, n+1):
  for j in range(1, i+1):
    for k in range(1, j+1):
      if i == j and j == k:
        ans += li[i][li[j][k]]
      elif i == j or j == k:
        ans += 3*li[i][li[j][k]]
      else:
        ans += 6*li[i][li[j][k]]
print(ans)

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


K = int(input())
count = 0


count += K * (K+1) / 2


for a in range(1, K):
  for b in range(a+1, K+1):
    count += gcd(b, a) * 6


for a in range(1, K-1):
  for b in range(a+1, K):
    ab = gcd(b, a)
    
    for c in range(b+1, K+1):
      count += gcd(c, ab)  * 6

print(int(count))

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 functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
"""
"""
def gcd(a, b):
    if b == 0: return a
    return gcd(b, a % b)
"""
import math

k = int(input())


cnt=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):
            cnt += math.gcd(tmp,c)

print(cnt)

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

Solution (Python):
from  math import gcd

 
k = int(input())
sum_c = 0
 
for a in range(1, k + 1):
    for b in range(1, k + 1):
        for c in range(1, k + 1):
            sum_c += gcd(gcd(a, b), c)
            
print(sum_c)

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

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):
from math import gcd
 
K = int(input())
 
result = 0
for a in range(1, K + 1):
  for b in range(1, K + 1):
    for c in range(1, K + 1):
      result += gcd(c, gcd(a, b))
      
print(result)

Solution (Python):


import sys
import math


















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    k = ii()
    ans = 0
    for a in range(1, k+1):
        for b in range(1, k+1):
            for c in range(1, k+1):
                ans += math.gcd(math.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):
    ans += a
    for b in range(a + 1, K + 1):
        tmp = gcd(a, b)
        ans += tmp * 6
        for c in range(b + 1, K + 1):
            ans += gcd(tmp, c) * 6
print(ans)

Solution (Python):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

import fractions

dicts={}

K=one_int()

def reg(a,b):
    key = str(a)+","+str(b)
    if key not in dicts:
        dicts[key] = fractions.gcd(a,b)
        dicts[str(b)+","+str(a)] = dicts[key]
    temp = dicts[key]
    return temp

sums = 0
count=0
for a in range(1,K+1):
    for b in range(1,K+1):
        temp1 = reg(a,b)
        for c in range(1,K+1):
            temp = reg(temp1,c)
            sums += temp

print(sums)

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

k = int(input())

@jit
def main(k):
    point = 0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for k in range(1,k+1):
                point += gcd(gcd(i,j),k)
    return point

print(main(k))

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

Solution (Python):
import math

K = int(input())

sum = 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):
            sum += math.gcd(tmp,c)
            

print(sum)

Solution (Python):
def gcd(x, y):
    if x < y:
        x, y = y, x
    while y != 0:
        org_x = x
        x = y
        y = org_x % y
    return x

k = int(input())

sum = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        gcd_ab = gcd(a, b)
        for c in range(1, k+1):
            sum += gcd(gcd_ab, c)

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

Solution (Python):
k = int(input())
arr =  [0]*(k+1)
count = [0]*(k+1)
sum = 0
n = 3
for i in range(k,1,-1):
    x = k//i
    p = int(pow(x,n))
    arr[i]=i*p 
    count[i]=p
    for j in range(x-1):
        arr[i]-=i*count[i*(j+2)]
        count[i]-=count[i*(j+2)]
csum = 0
for i in range(2,k+1):
    sum+=arr[i]
    csum+=count[i]
sum+=(int(pow(k,n)) - csum)
print(int(sum%(1e9+7)))

Solution (Python):
from math import gcd

k = int(input())

ir = range(1, k+1)
jr = range(1, k+1)
lr = range(1, k+1)

count = 0
for i in ir:
  for j in jr:
    for l in lr:
      count += gcd(i, gcd(j, l))

print(count)

Solution (Python):

from math import gcd
k = int(input())
ans = 0
for i in range(1,k+1):
	for p in range(1,k+1):
		for q in range(1,k+1):
			ans+=gcd(q,gcd(i,p))

print(ans)

Solution (Python):
import math

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

print(total)

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

print(ans)

Solution (Python):
from math import gcd

def gcd3(a,b,c):
    return(gcd(gcd(a,b),c))

k = int(input())

print(sum([gcd3(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 collections import deque
import math


k = int(input())


def equal_num(dq):
    equal_sum = -1
    dq_len = len(dq)

    for _ in range(dq_len):
        if dq[0] != dq[1]:
            equal_sum += 1
        dq.rotate()

    equal_sum *= 3

    if equal_sum <= 0:
        equal_sum = 1

    
    return equal_sum


gcd_sum = 0
for h in range(1, k + 1):
    for i in range(h, k + 1):
        for j in range(i, k + 1):
            gcd_tmp = math.gcd(j, math.gcd(h, i))
            gcd_sum += gcd_tmp * equal_num(deque([h, i, j]))

print(gcd_sum)

Solution (Python):
import math
K = int(input())
total = 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:
        times = 1
      elif i == j or j == k or k == i:
        times = 3
      else:
        times = 6
      total += math.gcd(math.gcd(i,j), k) * times
print(total)

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

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

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

Solution (Python):
import sys
from math import gcd
input = sys.stdin.readline
def main():
    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)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
from math import gcd
input = sys.stdin.readline
def main():
    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)
if __name__ == '__main__':
    main()

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

print(s)

Solution (Python):
import math

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

n = int(input())
x = n*(n+1)//2
y = sum([math.gcd(i, j) for i in range(1, n+1) for j in range(i+1, n+1)])*6
z = sum([gcd(i, j, k) for i in range(1, n+1) for j in range(i+1, n+1) for k in range(j+1, n+1)])*6
print(x+y+z)

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N = I()
 
ans = 0
for a in range(1, N+1):
    for b in range(1, N+1):
        for c in range(1, N+1):
            ans += math.gcd(math.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())
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())
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):
def gcd_2(a,b):
    if a<=b:
        x_min=a
        x_max=b
    else:
        x_min=b
        x_max=a
        
    while True:

        if x_max % x_min ==0:
            return x_min
        
        x_temp = x_max % x_min
        
        x_max = x_min
        x_min = x_temp
        
def gcd(a,b,c):
    d=gcd_2(a,b)
    ans=gcd_2(d,c)
    return ans
  
N=int(input())
sum_gcd=0
for a_1 in range(1,N+1):
    for a_2 in range(1,N+1):
        for a_3 in range(1,N+1):
            sum_gcd+=gcd(a_1,a_2,a_3)
print(sum_gcd)

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

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

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

sum = 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):
      sum += math.gcd(x, c)
      
print(sum)

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

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

if __name__== "__main__":
    main()

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

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

if __name__== "__main__":
    main()

Solution (Python):
K = int(input())
g = [[0 for i in range(201)] for j in range(201)]


def gcd(x, y):
    if x*y == 0:
        return x+y
    if x == y:
        return x
    elif x > y:
        return gcd(y, x)
    else:
        return gcd(y%x, x)
    
for i in range(201):
    for j in range(201):
        g[i][j] = gcd(i, j)
    
s = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            s += g[a][g[b][c]]
print(s)

Solution (Python):
import math

n=int(input())
 
def diffs(n):
  total=0
  for k in range(1,n):
    for l in range(k+1,n):
      total+=math.gcd(n,math.gcd(k,l))*6
  for k in range(1,n):
    total+=math.gcd(n,k)*6
  total += n
  return total

sum=0
for i in range(1,n+1):
  sum += diffs(i)

print(sum)

Solution (Python):
K = int(input())
def gcd(a, b):
    while b != 0:
        r = a%b
        a = b
        b = r
    return a
gcd_ab = [gcd(a,b) for a in range(1, K+1) for b in range(1, K+1)]
gcd_abc = [gcd(a_b,c) for a_b in gcd_ab for c in range(1, K+1)] 
print(sum(gcd_abc))

Solution (Python):
import math
from functools import reduce
a = int(input())
result = 0
for x in range(1, a+1):
    for y in range(1, a+1):
        for z in range(1, a+1):
            result += reduce(math.gcd, (x, y, z))
            
print(result)

Solution (Python):
import math

N = int(input())
sum = 0

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

print(sum)

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


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


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):
import math
import numpy as np
import numba
import math

n = int(input())

@numba.jit
def main(n):
  lis = np.zeros((n, n, n))
  for i in range(n):
    for j in range(n):
      for k in range(n):
        lis[i][j][k] = math.gcd(math.gcd(i+1, j+1), k+1)
  return int(np.sum(lis))

print(main(n))

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

Solution (Python):
import math
from functools import reduce

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

N = int(input())
num_list = [[i+1 for i in range(N)],[i+1 for i in range(N)],[i+1 for i in range(N)]]
sum = 0
for a in num_list[0]:
    for b in num_list[1]:
        for c in num_list[2]:
            sum = sum + gcd(a,b,c)

print(sum)

Solution (Python):
import numpy as np


def gcd(a, b):
    while b > 0 :
        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):
        g = gcd(a, b)
        for c in range(1,K+1):
            ans += gcd(c, g)

print(ans)

Solution (Python):
import io


def gcd(a, b):
    while b > 0 :
        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(c, gcd(a, b))

print(ans)

Solution (Python):

n = int(input())
ans = 0


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


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

Solution (Python):

n = int(input())
ans = 0


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


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

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

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

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

print(ret)

Solution (Python):
import math

k = int(input())
result = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        p = math.gcd(a, b)
        for c in range(1, k + 1):
            q = math.gcd(p, c)
            result += q
print(result)

Solution (Python):

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

def permutation(n):
    if n == 1:
        return 1
    elif n == 2:
        return 3
    else:
        return 6

def solve():
    n = int(input())
    s = 0
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            for k in range(j, n + 1):
                diff = 1
                if j != k:
                    diff += 1
                if i != j:
                    diff += 1
                s += gcd(i, gcd(j, k)) * permutation(diff)

    print(s)
    return s

solve()

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

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

Solution (Python):
import sys
import numpy as np

sys.setrecursionlimit(10 ** 9)

K = int(input())

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

ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        tmp = gcd(i,j)
        if tmp == 1:
            ans += 1*K
        else:
            for k in range(1,K+1):
                ans += gcd(gcd(i,j), k)

print(ans)

Solution (Python):
import sys
import numpy as np

sys.setrecursionlimit(10 ** 9)

K = int(input())

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

ans = 0
for i in range(K):
    for j in range(K):
        tmp = gcd(i+1,j+1)
        if tmp == 1:
            ans += 1*K
        else:
            for k in range(K):
                ans += gcd(gcd(i+1,j+1), k+1)

print(ans)

Solution (Python):
from math import gcd
import sys


sys.setrecursionlimit(10 ** 9)

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


sys.setrecursionlimit(10 ** 9)

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

n = int(input())

res = 0
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 sys


sys.setrecursionlimit(10 ** 9)

K = int(input())

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

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

print(ans)

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

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

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

print(ans)

Solution (Python):
import math
K = int(input())
N = 0
 
for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            g = math.gcd(math.gcd(a,b),c)
            if(a != b or a != c):
                if(a != b and a != c and b != c):
                    g *= 6
                else:
                    g *= 3
            N += g
 
print(N)

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

Solution (Python):
import math
K = int(input())
N = 0
 
for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            g = math.gcd(a,b)
            g = math.gcd(g,c)
            if(a != b or a != c):
                if(a != b and a != c and b != c):
                    g *= 6
                else:
                    g *= 3
            N += g
 
print(N)

Solution (Python):
import math
K = int(input())
N = 0
 
for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            g = math.gcd(a,b)
            g = math.gcd(g,c)
            if(a != b or a != c):
                if(a != b and a != c and b != c):
                    g *= 6
                else:
                    g *= 3
            N += g
 
print(N)

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

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

print(N)

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

for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            g = math.gcd(a,b)
            g = math.gcd(g,c)
            if(a != 1 or b != 1 or c != 1 ):
                if(a != b or a != c):
                    if(a != K or b != K or c != K ):
                        if(a != b and a != c and b != c):
                            g *= 6
                        else:
                            g *= 3
            N += g

print(N)

Solution (Python):
from math import gcd

K = int(input())
valSum = 0

for A in range(1,K+1):
    for B in range(1,K+1):
        for C in range(1,K+1):
            valSum += gcd(gcd(A,B),C)

print(valSum)

Solution (Python):
from math import gcd

K = int(input())
valSum = 0

for A in range(1,K+1):
    for B in range(1,K+1):
        for C in range(1,K+1):
            valSum += gcd(gcd(A,B),C)

print(valSum)

Solution (Python):
i=int(input())
import math
list=[]
box=[]
for a in range(1,i+1):
    for b in range(1,i+1):
        x=math.gcd(a,b)
        for c in range(1,i+1):
            list.append(math.gcd(x,c))
print(sum(list))

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

for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            g = math.gcd(a,b)
            g = math.gcd(g,c)
            if(a != 1 or b != 1 or c != 1 ):
                if(a != b or a != c):
                    if(a != K or b != K or c != K ):
                        if(a != b and a != c and b != c):
                            g *= 6
                        else:
                            g *= 3
            N += g

print(N)

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());a=0
x=[]
for i in range(k):
    d=0
    for j in range(k):
        d+=gcd(i+1,j+1)
    x.append(d)
for i in range(k):
  for j in range(k):
      a+=x[gcd(i+1,j+1)-1]
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):
            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(i, K+1):
        for k in range(j, K+1):
            if i==j and i==k: g = i
            elif (i==j and i!=k):
                g = math.gcd(i,math.gcd(j,k))*3
            elif (i==k and i!=j):
                g = math.gcd(i,math.gcd(j,k))*3
            elif (j==k and i!=j):
                g = math.gcd(i,math.gcd(j,k))*3
            else : g = math.gcd(i,math.gcd(j,k))*6
            ans += g
print(ans)

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

for a in range(1,K+1):
  for b in range(1,K+1):
    for c in range(1,K+1):
      sum += gcd(gcd(a,b),c);

print(sum)

Solution (Python):
import math
from functools import reduce

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

k = int(input())
sum = 0

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

print(sum)

Solution (Python):
from math import gcd

def gcd_3(a, b, c):
    g = gcd(a, b)
    return gcd(g, c)

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

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)