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):
K = int(input())
g = [[] for i in range(K+1)]
for i in range(1, K+1):
    for j in range(i, K+1, i):
        g[j].append(i)
sigma = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            for d in reversed(range(len(g[i]))):
                if g[i][d] in g[j] and g[i][d] in g[k]:
                    sigma += g[i][d]
                    break
print(sigma)

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

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import gcd
def main():
    k = int(input())
    r = 0
    for i1 in range(1, k + 1):
        for i2 in range(1, k + 1):
            for i3 in range(1, k + 1):
                t1 = gcd(i1, i2)
                r += gcd(t1, i3)
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
import math

n = int(input())

c=0
for i in range(1,n+1):
    if i==1:
        c += (n*n)
        continue
    for j in range(1,n+1):
        t = math.gcd(i,j)
        if t==1:
            c += n
            continue
        for k in range(1,n+1):
            c += math.gcd(t,k)

print(c)

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

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

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

Solution (Python):
from math import gcd

K = int(input())

ans = 0

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

print(ans)

Solution (Python):

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
score = int(input())
answer = 0
gcd2 = 0
for i in range(score):
    for j in range(score):
        gcd2 = math.gcd(i+1,j+1)
        for k in range(score):
            answer += math.gcd(gcd2,k+1)
print(answer)

Solution (Python):
l=[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(l[int(input())-1])

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

from math import gcd

ans = 0

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

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

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

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):
K=int(input())
def gcd(p,q):
    if p%q==0:
        return q
    return gcd(q,p%q)
S=0
for i in range(K):
    for j in range(K):
        for k in range(K):
            S+=gcd(i+1,gcd(j+1,k+1))
print(S)

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

Solution (Python):

import math



K = int(input())









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

print(tot)

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

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

Solution (Python):
import math
from functools import reduce

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

n = int(input()) + 1
ans = 0
for i in range(1,n):
    for j in range(1,i+1):
        for k in range(1,j+1):
            if i == j == k:
                m = 1
            elif i == j or j == k or k == i:
                m = 3
            else:
                m = 6
            a = [i,j,k]
            ans += gcd_list(a) * m
print(ans)

Solution (Python):
import math
from functools import reduce

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

n = int(input()) + 1
ans = 0
for i in range(1,n):
    for j in range(1,i+1):
        for k in range(1,j+1):
            if i == j == k:
                m = 1
            elif i == j or j == k or k == i:
                m = 3
            else:
                m = 6
            a = [i,j,k]
            ans += gcd_list(a) * m
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):
from math import gcd
import itertools

k = int(input())

lst = [int(i) for i in range(1, k+1)]
all_array = list(itertools.combinations_with_replacement(lst, 3))
num = 0
for element in all_array:
  a = element[0]
  b = element[1]
  c = element[2]
  d = gcd(gcd(a, b), c)
  if a != b and b != c:
    num += d * 6
  elif a == b and b != c:
    num += d * 3
  elif a != b and b == c:
    num += d * 3
  elif a == b and b == c:
    num += d
print(num)

Solution (Python):
from math import gcd
K = int(input())
ans = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        L = gcd(i,j)
        for k in range(j,K+1):
            M = gcd(L,k)
            if i == j == k:
                ans += M
            elif i == j or j == k:
                ans += M*3
            else:
                ans += M*6
print(ans)

Solution (Python):
import itertools
import math
import functools

K = int(input())

ans = 0

for i in itertools.product(range(1,K+1),repeat=3):
    ans += functools.reduce(math.gcd,i)
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
def readinput():
    k=int(input())
    return k

def main(k):
    memo=[]
    for _j in range(k+1):
        memo.append([0 for _i in range(k+1)])
    sum=0
    for a in range(1,k+1):
        for b in range(1,k+1):
            if memo[a][b]!=0:
                temp=memo[a][b]
            else:
                temp=gcd(a,b)
                memo[a][b]=temp
                memo[b][a]=temp
            for c in range(1,k+1):
                if memo[temp][c]!=0:
                    sum+=memo[temp][c]
                else:
                    temp2=gcd(temp,c)
                    memo[temp][c]=temp2
                    memo[c][temp]=temp2
                    sum+=temp2
    return sum

if __name__=='__main__':
    k=readinput()
    ans=main(k)
    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):
        for c in range(1,k+1):
            ans+=math.gcd(math.gcd(a,b),c)
print(ans)

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

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

sum = 0

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

print(sum)

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


def main():
    listSum = [1, 3, 6]
    k = int(input())
    sumGCD = 0
    for a in range(1, k + 1):
        for b in range(a, k + 1):
            for c in range(b, k + 1):
                sumGCD += gcd(a, b, c) * listSum[len(set([a, b, c])) - 1]
    print(sumGCD)


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


if __name__ == '__main__':
    s = time.time()
    main()

Solution (Python):







































global FLAG_LOG
FLAG_LOG = False


def log(value):
    
    
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    
    K = int(lines[0])
    
    
    
    
    
    
    
    
    

    import math

    dic = dict()
    for x in range(1, K):
        for y in range(x+1, K+1):
            dic[(x, y)] = math.gcd(x, y)

    su = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                
                
                tmp = [a, b, c]
                tmp.sort()
                x, y, z = tmp
                
                su += get_gcd_by_dic(dic, x, y, z)

    return [su]


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


def calculation2(lines):
    K = int(lines[0])

    gcdlist = [[0 for i in range(K)] for j in range(K)]
    

    for i in range(K):
        for j in range(i, K):
            tmp = gcd(i+1, j+1)
            gcdlist[i][j] = tmp
            gcdlist[j][i] = tmp
    

    su = 0
    for a in range(K):
        for b in range(K):
            for c in range(K):
                if a == b == c:
                    tmp = a+1
                elif a == b:
                    tmp = gcdlist[a][c]
                elif a == c:
                    tmp = gcdlist[a][b]
                elif b == c:
                    tmp = gcdlist[a][b]
                else:
                    tmp = gcdlist[a][gcdlist[b][c]-1]
                
                su += tmp

    return [su]


def calculation3(lines):
    K = int(lines[0])
    su = 0
    for a in range(K):
        for b in range(K):
            for c in range(K):
                su += gcd(gcd(a+1, b+1), c+1)

    return [su]







def get_gcd_by_dic(dic, a, b, c):
    if a == b:
        x = a
    else:
        x = dic[(a, b)]
    if a == c:
        y = a
    else:
        y = dic[(a, c)]
    if x == y:
        ret = x
    elif x > y:
        ret = dic[(y, x)]
    else:
        ret = dic[(x, y)]
    return ret



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['2']
        lines_export = [9]
    if pattern == 2:
        lines_input = ['200']
        lines_export = [10813692]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation2(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

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

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

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

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

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

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

Solution (Python):
import math
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):
            ij = math.gcd(i, j)
            s += math.gcd(ij, k)
print(s)

Solution (Python):
from math import *
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):
k = int(input())

sum = 0

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

for i in range(1,k+1):
    for m in range(1,k+1):
        for l in range(1,k+1):
            sum += gcd(gcd(i,m),l)
print(sum)

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

sum = 0

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

import math

for i in range(1,k+1):
    for m in range(1,k+1):
        sub = gcd(i,m)
        for l in range(1,k+1):
            sum += gcd(sub,l)
print(sum)

Solution (Python):
import math

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

Solution (Python):
import math

N = int(input())
n = 0
m = 0
res =0 
List1 = ["INF"]*(N+1)
for i in range(1,N+1):
  for j in range(1,N+1):
    n = math.gcd(i,j)
    if List1[n] == "INF":
      for k in range(1,N+1):
        m += math.gcd(n,k)
      res += m
      List1[n] = m
      m = 0
    else:
      res += List1[n]
print(res)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

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

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


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


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

Solution (Python):
def gcd(x,y):
    while y>0:
        x,y = y,x%y
    return x
K = int(input())
cnt = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            x = a
            x = gcd(x,b)
            x = gcd(x,c)
            cnt += x
print(cnt)

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

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

print(num)

Solution (Python):
def gcd(x,y):
    if x%y!=0:
        return gcd(y,x%y)
    else:
        return y
def GCD(x,y,z):
    return gcd(gcd(x,y),z)
ans=0
K=int(input())
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            ans+=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(b,c),a)
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(b,c),a)

print(ans)

Solution (Python):
def calc(a, b):
    m = min(a, b)
    l = max(a, b)
    while m != 0:
        s = m
        m = l % m
        l = s
    return l

K = int(input())
ans = 0
for i in range(K):
    for j in range(i+1):
        for k in range(j+1):
            if i == j == k:
                ans += i+1
            elif i == j:
                ans += 3 * calc(i+1, k+1)
            elif j == k:
                ans += 3 * calc(i+1, j+1)
            else:
                ans += 6 * calc(calc(i+1, j+1), k+1)

print(ans)

Solution (Python):
def calc(a, b):
    m = min(a, b)
    l = max(a, b)
    while m != 0:
        s = m
        m = l % m
        l = s
    return l

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

print(ans)

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

n = int(sys.stdin.read())
print(sum(gcd(gcd(abc[0], abc[1]), abc[2]) for abc in itertools.product(range(1, n + 1), repeat=3)))

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

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

Solution (Python):
import math

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy






import functools
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
k = inp()
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans += functools.reduce(euclid,[a,b,c])

print(ans)

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

l1 = [i for i in range(1, int(input()) + 1)]
print(sum(reduce(gcd, l) for l in product(l1, repeat=3)))

Solution (Python):
import math
from functools import reduce

K = int(input())

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

ans = 0
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if i == j  == k:
        ans += gcd(i,j,k)
      elif i == j or j == k:
        ans += 3*gcd(i,j,k)
      else:
        ans += 6*gcd(i,j,k)
      
print(ans)

Solution (Python):
import math

def sum_of_gcd_of_tuples():
    
    K = int(input())
    
    sum_gcd = 0
    
    for a in range(1,K+1):
        for b in range(1,K+1):
            for c in range(1,K+1):
                gcd_num = math.gcd(a,b)
                gcd_num = math.gcd(gcd_num,c)
                sum_gcd += gcd_num
    return sum_gcd

result = sum_of_gcd_of_tuples()
print(result)

Solution (Python):


import math

def main():

    K = int(input())

    ans = 0

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin
input = stdin.readline


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


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

Solution (Python):
import itertools
import math

K = int(input())
nums = range(1, K + 1)
comb = itertools.combinations_with_replacement(nums, 3)
ans = 0

for v in comb:
    n1 = math.gcd(v[0], v[1])
    n2 = math.gcd(n1, v[2])
    l = len(list(set(v)))
    if l == 3:
        ans += 6 * n2
    elif l == 2:
        ans += 3 * n2
    elif l == 1:
        ans += n2
print(ans)

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

Solution (Python):


from typing import *
import collections
import itertools
import functools
import math
import sys

INF = float('inf')


def solve(K: int):

    @functools.lru_cache(maxsize=K**3)
    def gcd(a: int, b: int) -> int:
        return math.gcd(a, b)

    return sum([gcd(ab, c+1) for ab in [gcd(a+1, b+1) for a in range(K) for b in range(K)] for c in range(K)])


def main():

    sys.setrecursionlimit(10 ** 6)

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    print(f'{solve(K)}')


if __name__ == '__main__':
    main()

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

Solution (Python):
from sys import stdin
import math
from functools import reduce

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

k = int(stdin.readline().rstrip())

d = 0
for i in range(1,k+1):
    for j in range(i+1,k+1):
        for l in range(j+1,k+1):
            d = d + 6*reduce(gcd, [i,j,l])

for i in range(1,k+1):
    for j in range(1,k+1):
        if j != i: d = d + 3*reduce(gcd, [i,j])

for i in range(1,k+1):
    d = d + i

print (d)

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

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

print(sum)

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

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

print(ans)

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

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

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

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

def gcd(*numbers):
    return reduce(math.gcd, numbers)
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,b,c)
      
print(ans)

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

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

print(ans)

Solution (Python):
from math import gcd

k = int(input())

wa = 0

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


print(wa)

Solution (Python):
import math
from functools import reduce
import sys
sys.setrecursionlimit(2000000)

k = int(input())

wa = 0

for a in range(1,k+1):
    for b in range(a+1,k+1):
        for c in range(b+1,k+1):
            li = [a,b,c]
            wa += reduce(math.gcd,li)*6

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

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

print(wa)

Solution (Python):
import math

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

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

s = 0






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


for i in range(1,k+1):
    for j in range(1,k+1):
        if i != j:
            s += gcd(i,gcd(i,j))*3


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

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

ans=0
import itertools
import math
for seq in itertools.combinations_with_replacement(range(1,k+1),3):
    if seq[0]==seq[1] or seq[1]==seq[2]:
        if  seq[0]!=seq[2]:
            ans+=(math.gcd(seq[0],math.gcd(seq[1],seq[2])))*3
        else:
            ans+=math.gcd(seq[0],math.gcd(seq[1],seq[2]))

    else:
        ans+=math.gcd(seq[0],math.gcd(seq[1],seq[2]))*6
print(ans)

Solution (Python):
def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a % b)
import collections
K = int(input())
ans = 0
cnt = collections.defaultdict(int)

for a in range(1, K + 1):
  for b in range(1, K + 1):
    cnt[gcd(a, b)] += 1
for c in range(1, K + 1):
  for gcd_of_ab in cnt.keys():
    ans += gcd(gcd_of_ab, c) * cnt[gcd_of_ab]
print(ans)

Solution (Python):
import math
from functools import reduce
k = int(input())
ans = 0
def gcd(*numbers):
    return reduce(math.gcd, numbers)

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

print(ans)

Solution (Python):
import math
import itertools

k = int(input())
l = list(range(1,k+1))
y = list(itertools.combinations_with_replacement(l,3))
n = len(y)
t = 0

for i in range(n):
    a = y[i][0]
    b = y[i][1]
    c = y[i][2]
    d = math.gcd(math.gcd(a,b),c)
    if a != b != c:
        t += d * 6
    elif a == b == c:
        t += d
    else:
        t += d * 3

print(t)

Solution (Python):


import math
from itertools import combinations_with_replacement as C

K = int(input())

ans = 0
ABC = list(C(range(1, K+1), 3))
for a, b, c in ABC:
  if a == b == c:
    ans += math.gcd(math.gcd(a, b), c)
  elif a == b or b == c or c == a:
    ans += math.gcd(math.gcd(a, b), c) * 3
  else:
    ans += math.gcd(math.gcd(a, b), c) * 6
print(ans)

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

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

print(ans)

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

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

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

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

print(ans)

Solution (Python):
import numpy as np
k = int(input())
x = np.arange(1,k+1)

a = np.gcd.outer(np.gcd.outer(x,x),x)
print(a.sum())