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

def f(*numbers):
    return reduce(math.gcd, numbers)
n=int(input())



def sigma2(frm, to):
    r = 0;
    for i in range(frm, to+1):
        for j in range(frm, to+1):
            for k in range(frm, to+1):
                r+=f(i,j,k)
    return r

print(sigma2(1,n))

Solution (Python):
def gcd(a,b):
    while b:
        a,b=b,a%b
        
    return a
    
ans=0
    
K=int(input())
for i in range(1,K+1):
    for j in range(1,K+1):
        gcd_=gcd(i,j)
        
    
        for k in range(1,K+1):
            ans+=gcd(k,gcd_)
            
print(ans)

Solution (Python):
import math
k = int(input())
box = []
for i in range(1,k+1):
  for j in range(1,k+1):
    gap = math.gcd(i,j)
    if gap == 1:
      box.append(1*k)
    else:
      for l in range(1,k+1):
        gap2 = math.gcd(gap,l)
        box.append(gap2)        
print(sum(box))

Solution (Python):
class GCD:
    def __init__(self):
        self.numlist = dict()
    def culc(self, K):
        ret = 0
        
        for i in range(1,K):
            for j in range(i + 1,K+1):
                ret += self.gcd_(i,j) * 6

        for i in range(1,K-1):
            for j in range(i+1, K):
                for k in range(j+1,K+1):
                    ret += self.gcd(i,j,k)*6

        for i in range(1, K+1):
            ret += i

        return ret

    def gcd(self, a,b,c):
        
        
        
        
        temp_ = self.gcd_(a,b)
        temp_ = self.gcd_(temp_, c)
        
        return temp_

    def gcd_(self,a,b):
        l = (min(a,b), max(a,b))
        if l in self.numlist:
            return self.numlist[l]
        else:
            mod = max(a,b) % min(a,b)
            if mod == 0:
                ret = min(a,b)
                self.numlist[l] = ret
                return ret
            else:
                ret = self.gcd_(min(a,b), mod)
                self.numlist[l] = ret
                return ret

K = int(input())
gcd = GCD()
ret = gcd.culc(K)
print(ret)

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

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

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

import time
from functools import wraps


def stop_watch(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        elapsed_time = time.time() - start
        print(f'{elapsed_time} [ms]')
        return result

    return wrapper


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


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

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


def main():
    K = int(input())
    num_list = range(1, K + 1)

    total = 0

    for a in range(1, K + 1):
        if a == 1:
            total += K ** 2
            continue
        for b in range(1, K + 1):
            if b == 1:
                total += K
                continue
            tmp = gcd(a, b)
            if tmp == 1:
                total += K
                continue
            tmp_b = is_prime(tmp)
            for c in range(1, K + 1):
                if c == 1:
                    total += 1
                    continue
                if tmp_b:
                    if c % tmp == 0:
                        total += tmp
                        continue
                    else:
                        total += 1
                        continue
                else:

                
                
                    total += gcd(tmp, c)
    print(total)


if __name__ == '__main__':
    main()

Solution (Python):

import numpy as np

def get_gcd(a, b, c):
    min_ab = min(a, b)
    max_ab = max(a, b)
    amari = max_ab % min_ab
    if amari == 0:
        gcd1 = min_ab
    else:
        x = min_ab
        y = amari
        while y != 0:
            amari = x % y
            if amari == 0:
                gcd1 = y
                break
            x = y
            y = amari
    min_2 = min(c, gcd1)
    max_2 = max(c, gcd1)
    amari = max_2 % min_2
    if amari == 0:
        gcd2 = min_2
    else:
        x = min_2
        y = amari
        while y != 0:
            amari = x % y
            if amari == 0:
                gcd2 = y
                break
            x = y
            y = amari

    return gcd2


k = int(input())
ans = 0

for a in range(1, k+1):
    for b in range(a+1, k+1):
        if b == a:
            continue
        for c in range(b+1, k+1):
            if c == a or c == b:
                continue
            ans += get_gcd(a, b, c) * 6

for a in range(1, k+1):
    b = a
    for c in range(1, k+1):
        if c == a:
            continue
        ans += get_gcd(a, b, c) * 3

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

print(ans)

Solution (Python):
from fractions import gcd
k=int(input())
l=[]
dp=[[0]*(k+1) for i in range(k+1)]

for i in range(1,k+1):
    for j in range(1,k+1):
        dp[i][j]=gcd(i,j)
        l.append(gcd(i,j))
sum=0
for x in range(1,k+1):
    for y in range(len(l)):
        sum += int(dp[l[y]][x])
print(sum)

Solution (Python):
import numpy as np
import math
K = int(input())
memo = np.zeros((K,K),int)
s = 0
for i in range(K):
  for j in range(i,K):
    for k in range(j,K):
      if i == j and j == k:
        rate = 1
      elif i == j or j == k or i == k:
        rate = 3
      else:
        rate = 6
      s += math.gcd(math.gcd(i+1,j+1),k+1) * rate
print(s)

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

def gcd(n,m,l):
    if n < m :
        n , m = m , n
    while n % m != 0 :
        n , m = m , n % m
    
    if m < l :
        m , l = l , m
    while m % l != 0 :
        m , l = l , m % l
    
    return l
  
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(a, b, c)

print ( S )

Solution (Python):
import math

k =int(input())
ans = 0
temp_list =[]


for i in range(k):
    for j in range(k):
        temp_list.append(math.gcd(i+1,j+1))

temp_set= list(set(temp_list))

memo = [0]*len(temp_set)

for i in temp_list:
    for j in range(len(temp_set)):
        if i == temp_set[j]:
            memo[j] += 1
            break

for i in range(k):
    for j in range(len(temp_set)):
        ans += memo[j] * math.gcd(temp_set[j],i+1)

print(ans)

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


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

    ans = 0
    for a in range(1,K+1):
        for b in range(a,K+1):
            ab = gcd(b,a)
            for c in range(b,K+1):
                n = 6
                if a==b or b==c: n = 3
                if a==b and b ==c : n=1
                ans += gcd(c,ab)*n

    print(ans)


    return

if __name__ == "__main__":
    main()

Solution (Python):
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions


num = int(stdin.readline().rstrip())
count = 0
arr = []
for a in range(1, num + 1):
    for b in range(1, num + 1):
        arr.append(math.gcd(a, b))
for c in range(1, num + 1):
    for d in arr:
        count += math.gcd(c, d)
print(count)

Solution (Python):
K = list(range(1,int(input())+1))

import itertools

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

result = 0


if len(K)>=3:
    for i in list(itertools.combinations(K,3)):
        result += gcd(gcd(i[0],i[1]),i[2])*6

if len(K)>=2:
    for i in list(itertools.combinations(K,2)):
        result += gcd(i[0],i[1])*6

for i in list(itertools.combinations(K,1)):
    result += i[0]

print(result)

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

Solution (Python):
from itertools import product
from math import gcd

k = int(input())

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

print(res)

Solution (Python):
from itertools import combinations
from math import gcd
from functools import reduce


def gcd3(cmb):
    return reduce(gcd, cmb)


k = int(input())
s1 = sum(range(1, k + 1))
s2 = 0
cmb2 = combinations(range(1, k + 1), 2)
for i, j in cmb2:
    s2 += gcd(i, j)
s3 = 0
cmb3 = combinations(range(1, k + 1), 3)
for i in cmb3:
    s3 += gcd3(i)

print(s1 + s2 * 6 + s3 * 6)

Solution (Python):
import math

k = int(input())

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


print(ans)

Solution (Python):
from __future__ import division, print_function

def gcd(a,b):
	while(b):
		a,b = b,a%b
	return a
def main():
	k = int(input())
	ans=0
	for a in range(1,k+1):
		for b in range(1,k+1):
			for c in range(1,k+1):
				ans+=gcd(a,gcd(b,c))

	print(ans)

py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange
 
import os, sys
from io import IOBase, BytesIO
 
BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None
 
    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
 
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()
 
    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s:self.buffer.write(s.encode('ascii'))
            self.read = lambda:self.buffer.read().decode('ascii')
            self.readline = lambda:self.buffer.readline().decode('ascii')
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
 

import sys
class ostream:
    def __lshift__(self,a):
        sys.stdout.write(str(a))
        return self
cout = ostream()
endl = '\n'
 

def readnumbers(zero = 0):
    conv = ord if py2 else lambda x:x
    A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
    try:
        while True:
            if s[i] >= b'0' [0]:
                numb = 10 * numb + conv(s[i]) - 48
            elif s[i] == b'-' [0]: sign = -1
            elif s[i] != b'\r' [0]:
                A.append(sign*numb)
                numb = zero; sign = 1
            i += 1
    except:pass
    if s and s[-1] >= b'0' [0]:
        A.append(sign*numb)
    return A
 
if __name__ == '__main__':
	main()

Solution (Python):
import math


K = int(input())

sum = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        sum += math.gcd(i, j)
        if j == K:
            break
        else:
            for k in range(j + 1, K + 1):
                sum += math.gcd(i, math.gcd(j, k)) * 2

print(sum)

Solution (Python):
import math
from functools import reduce

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

k = int(input())
num = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        for c in range(b,k+1):
          x = gcd(a,b,c)
          bai = sum([a==b,b==c,a==c])
          if bai == 0:
            num += x*6
          elif bai == 1:
            num += x*3
          else:
            num += x
            
print(num)

Solution (Python):
import numpy as np


n = int(input())
table = [[0 for i in range(n+10)] for j in range(n+10)]

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

ab_gcd = [0 for i in range(n+10)]
for i in range(1, n+1):
    for j in range(1, n+1):
        ret = gcd(i, j)
        table[i][j] = ret
        ab_gcd[ret] += 1

table = np.array(table)
ans = 0
for i in range(n+10):
    if ab_gcd[i] != 0:
        ans += int(np.sum(table[i, :]))*ab_gcd[i]
print(ans)

Solution (Python):
def kk(n,m):
    if n<m:
        n,m=m,n
    while  n%m!=0:
        n=n%m
        n,m=m,n
    return m


k= int(input())
count=0
for p in range(1,k+1):
    for q in range(p,k+1):
        pq=  kk(p,q)
        for r in range(q,k+1):
            pqr= kk(pq,r)
            if p==r:
                 pqr=pqr
            elif p==q or q==r:
                pqr=pqr*3
            else:
                pqr=pqr*6
            count= count+ pqr
print(count)

Solution (Python):
import math
from functools import reduce


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


n = int(input())

ans = 0

for i in range(1, n + 1):
    for j in range(i, n + 1):
        for k in range(j, n + 1):
            cnt = gcd(i, j, k)
            if i == j and j == k:
                ans += cnt
            elif (i == j and i != k) or (i == k and i != j) or (j == k and j != i):
                ans += cnt * 3
            else:
                ans += cnt * 6

print(ans)

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

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

if __name__ == "__main__":
    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 and c == a:
                    ans += gcd3(a, b, c)
                elif a == b or b == c or c == a:
                    ans += gcd3(a, b, c) * 3
                else:
                    ans += gcd3(a, b, c) * 6
    
    print(ans)

Solution (Python):
import math
from functools import reduce

k = int(input())
total = 0

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

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

Solution (Python):

import sys
import itertools as it
import numpy as np
from collections import Counter
d = {}
def _gcd(a,b):
    a, b = max(a,b), min(a,b)
    while b:
        if d.get((a, b), False):
            return d[(a, b)]
        else:
            a, b = b, a%b

    d[(a, b)] = a
    return a













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

def solve(K: int):
    
    
    r = range(1, K+1)

    arr = []
    for (a,b) in it.product(r,r):
        arr.append(_gcd(a,b))
        
        

    ans = 0
    cnt_dict = Counter(arr)
    for i in r:
        for key, cnt in cnt_dict.items():
            ans += _gcd(i,key)*cnt

    print(ans)
    return



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

if __name__ == '__main__':
    main()

Solution (Python):
import math
k = int(input())
ansA, ansB, ansC = 0, 0, 0
for a in range(k):
    ansC += a+1
    for b in range(k-a-1):
        GCDab = math.gcd(a+1, a+b+2)
        ansB += GCDab
        for c in range(k-a-b-2):
            GCDabc = math.gcd(GCDab, a+b+c+3)
            ansA += GCDabc
ans = ansA * 6 + ansB * 6 + ansC
print(ans)

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

	return b

N = int(input())

ans_sum = 0

for x in range(1, N+1):
	for y in range(x, N+1):
		for z in range(y, N+1):
			if x == y == z:
				ans_sum += z
			else:
				ans = gcd(x, y)
				ans = gcd(ans, z)
				if x == y or x == z or y == z:
					ans_sum += ans * 3
				else:
					ans_sum += ans * 6

print(ans_sum)

Solution (Python):
import math
k = int(input())
ans = []
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
           xy = math.gcd(x,y)
           ans.append(math.gcd(xy,z))
print(sum(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())
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)

Solution (Python):
k=int(input())
import math
if k==1:
    print(1)
else:
    s1=k*(k+1)//2
    s2=k-1
    for i in range(2,k):
        for j in range(i+1,k+1):
                s2+=math.gcd(i,j)
    s2=s2*6
    s3=0
    if k>2:
        for a in range(1,k-1):
            for b in range(a+1,k):
                for c in range(b+1,k+1):
                    s3+=math.gcd(math.gcd(a,b),c)
        s3=s3*6
    print(s1+s2+s3)

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

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

def main():
  K = int(input())
  S = 0
  
  S_0 = 0
  for a in range(1,K+1):
    S_0 = S_0 + tri_gcd(a,a,a)
  S = S_0
  
  S_1 = 0
  for a in range(1,K+1):
      for c in range(a+1,K+1):
          S_1 = S_1 + tri_gcd(a,a,c)
  S += 6*S_1

  
  S_3 = 0
  for a in range(1,K+1):
      for b in range(a+1,K+1):
        for c in range(b+1,K+1):
            S_3 = S_3 + tri_gcd(a,b,c)
  S += S_3 * 6
  
  print(S)

  return 0
 
if __name__ == "__main__":
  main()

Solution (Python):
from collections import defaultdict

K = int(input())


def calcGcd(a, b):
    large = max(a, b)
    small = min(a, b)
    while (True):
        num = large % small
        large = small
        small = num
        if small == 0:
            return large


ans = 0
dd = defaultdict(int)
for i in range(1, K + 1):
    for j in range(1, K + 1):
        dd[calcGcd(i, j)] += 1

for num in dd.keys():
    for k in range(1, K + 1):
        ans += calcGcd(num, k) * dd[num]

print(ans)

Solution (Python):

import math
import itertools
k = int(input())
ans = 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):
            e = math.gcd(d,c)
            ans += e
print(ans)

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


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


K = int(input())
ans = 0
for comb in itertools.combinations_with_replacement(list(range(1, K + 1)), 3):
    dup = len(set(comb))
    if dup == 1:
        ans += comb[0]
    elif dup == 2:
        ans += gcd(*comb) * 3
    elif dup == 3:
        ans += gcd(*comb) * 6
print(int(ans))

Solution (Python):
import math
from functools import reduce

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

k = int(input())
total = 0

for a in range(1, k+1):
    for b in range(a, k+1):
        for c in range(b, k+1):
            n = gcd(a, b, c)
            uni = len(set([a, b, c]))
            if uni == 1:
                total += n
            elif uni == 2:
                total += n * 3
            else:
                total += n * 6
print(total)

Solution (Python):
import sys
import math

N = int(input())

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 == k:
                ans += math.gcd(math.gcd(i,j),k)
            elif i == j or j== k:
                ans += 3*math.gcd(math.gcd(i,j),k)
            else:
                ans += 6*math.gcd(math.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())
count=0
for i in range(1,k+1):
    for j in range(i,k+1):
        for h in range(j,k+1):
            if i==j==h:
                x=gcd(i,j,h)
            elif i==j:
                x=3*gcd(i,j,h)
            elif j==h:
                x=3*gcd(i,j,h)
            else:
                x=6*gcd(i,j,h)
            count+=x
print(count)

Solution (Python):
def gcd(a,b):
	while(b):
		a,b = b,a%b
	return a
def main():
	k = int(input())
	ans=0
	for a in range(1,k+1):
		for b in range(1,k+1):
			for c in range(1,k+1):
				ans+=gcd(a,gcd(b,c))

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

print(s)

Solution (Python):
K=int(input())
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

Sum1=0
for i in range(1,K+1):
    for j in range(1,i+1):
        Ans=0
        Ans=gcd(i,j)
        for k in range(1,j+1):
            if i==j==k:
                Sum1+=gcd(Ans,k)
            elif i!=j!=k:
                Sum1+=6*gcd(Ans,k)
            else:
                Sum1+=3*gcd(Ans,k)

print(Sum1)

Solution (Python):
import math

k = int(input())
kl = [i for i in range(1, k+1)]


total = 0
dp = {}
for i in kl:
    for j in kl:
        tmp = math.gcd(i, j)
        for k in kl:
            total += math.gcd(tmp, k)


print(total)

Solution (Python):
import math
K=int(input())
c = 0
for i in range(1,K+1):
  for j in range(1,K+1):
    if math.gcd(i,j) == 1:
      c += K
    else:
      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):
from functools import reduce
import math


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


K = int(input())
gcdsum = K*K*K
gcd_ab = []
ab_list = []
for a in range(1, K + 1):
    for b in range(1, K + 1):
        
        if gcd(a, b) != 1:
            ab_list.append(a)
            ab_list.append(b)
            gcd_ab.append(ab_list)
            ab_list = []
for ab in gcd_ab:
    ab = gcd(ab[0], ab[1])
    for c in range(1, K+1):
        if gcd(ab, c) != 1:
            
            gcdsum += gcd(ab, c)-1

print(gcdsum)

Solution (Python):
import itertools
import math


def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)


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


K = int(input())
data = [k+1 for k in range(K)]
result = list(itertools.combinations_with_replacement(data, 3))

s = 0

for r in result:
    r0, r1, r2 = r
    g0 = gcd(r0, r1)
    g = gcd(g0, r2)
    if r0 == r1 == r2:
        s += g
    elif r0 != r1 and r1 != r2:
        s += g * 6
    else:
        s += g * 3

print(s)

Solution (Python):

def Euclid(m,n):
    while True:
        p = m%n
        m = n
        n = p
        if p == 0:
            return m
            break

K = int(input())
sum = 0

sub1_sum = 0
for i in range(3,K+1):
    for j in range(2,i):
        temp = Euclid(i,j)
        for k in range(1,j):
            sub1_sum += Euclid(temp,k)

sum += sub1_sum*6

sum += round(K*(K+1)/2)

sub2_sum = 0
for i in range(2,K+1):
    for j in range(1,i):
        sub2_sum += Euclid(i,j)

sum += sub2_sum*6
print(sum)

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

Solution (Python):
import math

k = int(input())
sum = 0
gcds = [[0 for i in range(k)] for i in range(k)]
for p in range(1,k+1):
    for q in range(1,k+1):   
        gcds[p-1][q-1] = math.gcd(p,q)          
        for r in range(1,k+1):
            sum += math.gcd(gcds[p-1][q-1], r)
            


print(sum)

Solution (Python):
import math

k = int(input().rstrip())


ans = 0
for a in range(1, k + 1):
    if a == 1:
        ans += k * k
    else:
       for b in range(1, k + 1):
            if b == 1:
               ans += k
            else:
                if a == b:
                    tmp = a
                    if tmp == 1:
                        ans += k
                    else:
                        for c in range(1, k + 1):
                            if c == 1:
                                ans += 1
                            else:
                                ans += math.gcd(tmp, c)
                else:
                    tmp = math.gcd(a, b)
                    if tmp == 1:
                        ans += k
                    else:
                        for c in range(1, k + 1):
                            if c == 1:
                                ans += 1
                            else:
                                ans += math.gcd(tmp, c)

print(ans)

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