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())
if k == 1:
    print(1)
if k == 2:
    print(9)
if k == 3:
    print(30)
if k == 4:
    print(76)
if k == 5:
    print(141)
if k == 6:
    print(267)
if k == 7:
    print(400)
if k == 8:
    print(624)
if k == 9:
    print(885)
if k == 10:
    print(1249)
if k == 11:
    print(1590)
if k == 12:
    print(2208)
if k == 13:
    print(2689)
if k == 14:
    print(3411)
if k == 15:
    print(4248)
if k == 16:
    print(5248)
if k == 17:
    print(6081)
if k == 18:
    print(7485)
if k == 19:
    print(8530)
if k == 20:
    print(10248)
if k == 21:
    print(11889)
if k == 22:
    print(13687)
if k == 23:
    print(15228)
if k == 24:
    print(17988)
if k == 25:
    print(20053)
if k == 26:
    print(22569)
if k == 27:
    print(25242)
if k == 28:
    print(28588)
if k == 29:
    print(31053)
if k == 30:
    print(35463)
if k == 31:
    print(38284)
if k == 32:
    print(42540)
if k == 33:
    print(46581)
if k == 34:
    print(50893)
if k == 35:
    print(55362)
if k == 36:
    print(61824)
if k == 37:
    print(65857)
if k == 38:
    print(71247)
if k == 39:
    print(76884)
if k == 40:
    print(84388)
if k == 41:
    print(89349)
if k == 42:
    print(97881)
if k == 43:
    print(103342)
if k == 44:
    print(111528)
if k == 45:
    print(120141)
if k == 46:
    print(128047)
if k == 47:
    print(134580)
if k == 48:
    print(146316)
if k == 49:
    print(154177)
if k == 50:
    print(164817)
if k == 51:
    print(174438)
if k == 52:
    print(185836)
if k == 53:
    print(194157)
if k == 54:
    print(207927)
if k == 55:
    print(218812)
if k == 56:
    print(233268)
if k == 57:
    print(245277)
if k == 58:
    print(257857)
if k == 59:
    print(268182)
if k == 60:
    print(288216)
if k == 61:
    print(299257)
if k == 62:
    print(313635)
if k == 63:
    print(330204)
if k == 64:
    print(347836)
if k == 65:
    print(362973)
if k == 66:
    print(383709)
if k == 67:
    print(397042)
if k == 68:
    print(416448)
if k == 69:
    print(434025)
if k == 70:
    print(456967)
if k == 71:
    print(471948)
if k == 72:
    print(499740)
if k == 73:
    print(515581)
if k == 74:
    print(536073)
if k == 75:
    print(559758)
if k == 76:
    print(583960)
if k == 77:
    print(604833)
if k == 78:
    print(633651)
if k == 79:
    print(652216)
if k == 80:
    print(683712)
if k == 81:
    print(709065)
if k == 82:
    print(734233)
if k == 83:
    print(754734)
if k == 84:
    print(793188)
if k == 85:
    print(818917)
if k == 86:
    print(846603)
if k == 87:
    print(874512)
if k == 88:
    print(909496)
if k == 89:
    print(933081)
if k == 90:
    print(977145)
if k == 91:
    print(1006126)
if k == 92:
    print(1041504)
if k == 93:
    print(1073385)
if k == 94:
    print(1106467)
if k == 95:
    print(1138536)
if k == 96:
    print(1187112)
if k == 97:
    print(1215145)
if k == 98:
    print(1255101)
if k == 99:
    print(1295142)
if k == 100:
    print(1342852)
if k == 101:
    print(1373253)
if k == 102:
    print(1422195)
if k == 103:
    print(1453816)
if k == 104:
    print(1502376)
if k == 105:
    print(1553361)
if k == 106:
    print(1595437)
if k == 107:
    print(1629570)
if k == 108:
    print(1691292)
if k == 109:
    print(1726717)
if k == 110:
    print(1782111)
if k == 111:
    print(1827492)
if k == 112:
    print(1887772)
if k == 113:
    print(1925853)
if k == 114:
    print(1986837)
if k == 115:
    print(2033674)
if k == 116:
    print(2089776)
if k == 117:
    print(2145333)
if k == 118:
    print(2197483)
if k == 119:
    print(2246640)
if k == 120:
    print(2332104)
if k == 121:
    print(2379085)
if k == 122:
    print(2434833)
if k == 123:
    print(2490534)
if k == 124:
    print(2554600)
if k == 125:
    print(2609625)
if k == 126:
    print(2693919)
if k == 127:
    print(2742052)
if k == 128:
    print(2813988)
if k == 129:
    print(2875245)
if k == 130:
    print(2952085)
if k == 131:
    print(3003306)
if k == 132:
    print(3096024)
if k == 133:
    print(3157249)
if k == 134:
    print(3224511)
if k == 135:
    print(3306240)
if k == 136:
    print(3388576)
if k == 137:
    print(3444609)
if k == 138:
    print(3533637)
if k == 139:
    print(3591322)
if k == 140:
    print(3693924)
if k == 141:
    print(3767085)
if k == 142:
    print(3842623)
if k == 143:
    print(3912324)
if k == 144:
    print(4027884)
if k == 145:
    print(4102093)
if k == 146:
    print(4181949)
if k == 147:
    print(4270422)
if k == 148:
    print(4361548)
if k == 149:
    print(4427853)
if k == 150:
    print(4548003)
if k == 151:
    print(4616104)
if k == 152:
    print(4718640)
if k == 153:
    print(4812789)
if k == 154:
    print(4918561)
if k == 155:
    print(5003286)
if k == 156:
    print(5131848)
if k == 157:
    print(5205481)
if k == 158:
    print(5299011)
if k == 159:
    print(5392008)
if k == 160:
    print(5521384)
if k == 161:
    print(5610705)
if k == 162:
    print(5739009)
if k == 163:
    print(5818390)
if k == 164:
    print(5930196)
if k == 165:
    print(6052893)
if k == 166:
    print(6156139)
if k == 167:
    print(6239472)
if k == 168:
    print(6402720)
if k == 169:
    print(6493681)
if k == 170:
    print(6623853)
if k == 171:
    print(6741078)
if k == 172:
    print(6864016)
if k == 173:
    print(6953457)
if k == 174:
    print(7094451)
if k == 175:
    print(7215016)
if k == 176:
    print(7359936)
if k == 177:
    print(7475145)
if k == 178:
    print(7593865)
if k == 179:
    print(7689630)
if k == 180:
    print(7886244)
if k == 181:
    print(7984165)
if k == 182:
    print(8130747)
if k == 183:
    print(8253888)
if k == 184:
    print(8403448)
if k == 185:
    print(8523897)
if k == 186:
    print(8684853)
if k == 187:
    print(8802826)
if k == 188:
    print(8949612)
if k == 189:
    print(9105537)
if k == 190:
    print(9267595)
if k == 191:
    print(9376656)
if k == 192:
    print(9574704)
if k == 193:
    print(9686065)
if k == 194:
    print(9827097)
if k == 195:
    print(9997134)
if k == 196:
    print(10174780)
if k == 197:
    print(10290813)
if k == 198:
    print(10493367)
if k == 199:
    print(10611772)
if k == 200:
    print(10813692)

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

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

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

if k == 1:
    print(1)
    exit()
elif k == 2:
    print(9)
    exit()

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

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

print(ans)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()

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

ret = 0
memo = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, n + 1):
        memo[i][j] = gcd(i, j)


for i in range(1, n + 1):
    for j in range(1, n + 1):
        for k in range(1, n + 1):
            ret += memo[memo[i][j]][k]



print(ret)

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

Solution (Python):
from functools import lru_cache
from math import gcd

cgcd = lru_cache(maxsize=None)(gcd)

k = int(input())
ans = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        d = cgcd(a, b)
        if d == 1:
            ans += k
            continue
        for c in range(1, k + 1):
            ans += cgcd(d, c)
print(ans)

Solution (Python):
import sys, math
def input(): return sys.stdin.readline().strip()


def main():
    K = int(input())
    ans = 0
    add = 0
    for a in range(1, K + 1):
        for b in range(a + 1, K + 1):
            d = math.gcd(a, b)
            add += d
            for c in range(b + 1, K + 1):
                ans += math.gcd(d, c)
    ans *= 6
    ans += add * 6
    ans += K * (K + 1) // 2
    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):
        for l in range(1, k + 1):
            ans += gcd(i, gcd(j, l))
print(ans)

Solution (Python):
from math import gcd

K = int(input())

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

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

out = 0
K = int(input())
for i in range(1, K + 1):
    count = K//i
    val = 0
    for j in range(1,i + 1):
        if gcd(i,j) == 1:
            val += 1
    out += val * count * count * count
print(out)

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


ans = 0
for a in range(1, K+1):
    for b in range(a, K+1):
        for c in range(b, K+1):
            size = len(set([a, b, c]))
            gd = gcd(a, gcd(b, c))
            if size == 1:
                ans += gd
            elif size == 2:
                ans += 3 * gd
            else:
                ans += 6 * gd
print(ans)

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

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

    return 0


if __name__ == '__main__':
    main()

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

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

Solution (Python):

"""
Created on Sun Apr 12 20:58:46 2020

@author: Kanaru Sato
"""

import math

k = int(input())

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

sum2 = 0
for i in range(1,k):
    for j in range(i+1,k+1):
        sum2 += math.gcd(i,j)
sum2 = 6*sum2

sum3 = 0
for i in range(1,k-1):
    for j in range(i+1,k):
        for k in range(j+1,k+1):
            sum3 += math.gcd(math.gcd(i,j),k)
sum3 = 6*sum3

ans = sum1 + sum2 + sum3

print(ans)

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


def input():
    return sys.stdin.readline().strip()


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


def main():
    K = int(input())
    diff = 0
    diff1 = 0
    same = 0

    for a in range(1, K + 1):
        for b in range(a + 1, K + 1):
            for c in range(b + 1, K + 1):
                diff += gcd_list([a, b, c])

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

    for a in range(1, K + 1):
        same += a

    print(diff * 6 + diff1 * 6 + same)


if __name__ == "__main__":
    main()

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


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

temp = []
for i in range(1, k + 1):
    for j in range(1, k + 1):
        temp.append(gcd(i, j))
from collections import Counter
c = Counter(temp)
ans = 0
for t, v in c.items():
    for n in range(1, k + 1):
        ans += gcd(t, n) * v
print(ans)

Solution (Python):
import queue
import math
import copy
"""
N = int(input())



A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""
def T(a,b):
	s = a*b
	if a > b:
		b += a
		a = b - a
		b -= a
	while b % a != 0:
		b %= a
		b += a
		a = b - a
		b -= a
	return a

mp = {}

Ans = 0
K = int(input())
for a in range(1,K+1):
	for b in range(1,K+1):
		t = T(a, b)
		if not t in mp:
			one = 0
			for c in range(1,K+1):
				
				one += T(t,c)
			mp[t] = one
		Ans += mp[t]
print(Ans)

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

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

k = int(input())

sum = 0

for a, b, c in itertools.product(range(1, k+1), range(1, k+1), range(1, k+1)):

    sum += gcd(a, b, c)

print(sum)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False


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


def gcd_n(a, n):
    ans = a[0]
    for i in range(1, n):
        ans = gcd(ans, a[i])
    return ans


def divisor(n):
    ret = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            ret.append(i)
            if n // i != i:
                ret.append(n // i)
    return ret


def main():
    K = I()
    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_n([a, b, c], 3)

    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
from collections import defaultdict
def gcd(a, b) : 
    while a :
        a, b = b%a, a 
    return b 

def c(K) :
    dp = defaultdict(int)
    ans = 0
    for i in range(1, K+1) :
        for j in range(1, K+1) :
            if i > j and (j,i) not in dp :
                temp = dp[(j,i)]
            else : 
                dp[(i,j)] = gcd(i,j)
                temp = dp[(i,j)]  
            for k in range(1, K+1) : 
                if k > temp :
                    q = (temp, k)
                else :
                    q = (k, temp)
                if q not in dp :
                    dp[q] = gcd(temp,k )
                ans += dp[q]
    return ans 

if __name__ == "__main__" :
    k = int(input())
    print(c(k))

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

Solution (Python):
import math

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

print(ans)

Solution (Python):

''':'
docker run --rm -it -v $(cd $(dirname $0) && pwd):/vo:ro pypy:3-2.4.0 \
   /bin/bash -c "pypy3 /vo/$(basename $0) < /vo/input.txt"
exit $?
'''
import sys


def _i(): return int(sys.stdin.readline().strip())


def _gcd(a, b):
    a, b = max(a, b), min(a, b)
    if a % b == 0:
        return b
    return _gcd(b, a % b)


def main():
    gcd = [[1]*201 for _ in range(201)]
    for i in range(1, 201):
        for j in range(i, 201):
            g = _gcd(i, j)
            gcd[i][j] = gcd[j][i] = g

    k = _i() + 1
    ans = 0
    for l in range(1, k):
        for m in range(1, k):
            glm = gcd[l][m]
            for n in range(1, k):
                ans += gcd[glm][n]
    return ans


if __name__ == "__main__":
    print(main())

Solution (Python):
from math import gcd

k = int(input())

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

Solution (Python):
import math
from functools import reduce

K = int(input())

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

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

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

ans = 0

lst = [[0 for _ in range(1, k+1)] for __ in range(k+1)]
cnt = [0 for _ in range(1, k+1)]


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

ans = 0

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

print(ans)

Solution (Python):
import math
from itertools import product

K = int(input())
s = 0
for a, b, c in product(range(1, K + 1), repeat=3):
    s += math.gcd(math.gcd(a, b), 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):
        a_b_gcd = gcd(a, b)
        for c in range(1, K+1):
            ans += gcd(a_b_gcd, c)
print(ans)

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

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

Solution (Python):
import math

k=int(input())
tochu={}
for i in range(1,k+1):
    for q in range(1,k+1):
        res=math.gcd(i,q)
        if res in tochu:
            tochu[res]+=1
        else:
            tochu[res]=1

ans=0
for i in range(1,k+1):
    for ele in tochu:
        ans+=math.gcd(i,ele)*tochu[ele]
print(ans)

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

def lcm(x, y):
    return x//gcd(x, y)*y

K = int(input())

GCD = [[0 for _ in range(201)] for _ in range(201)]
for i in range(1, 201):
    for j in range(1, 201):
        GCD[i][j] = gcd(i, j)

ans = 0
for a in range(1, K+1):
    for b in range(a, K+1):
        for c in range(b, K+1):
            
            t = GCD[GCD[a][b]][c]
            if (a == b and b < c ) or (a < b and b == c):
                ans += 3*t
            elif a < b and b < c:
                ans += 6*t
            else:
                ans += t

print(ans)

Solution (Python):
from math import*
n=int(input())

ans=n*(n+1)//2

for i in range(1,n+1):
    for j in range(i+1,n+1):
        ans+=gcd(i,j)*6
        
for i in range(1,n+1):
    for j in range(i+1,n+1):
        for r in range(j+1,n+1):
            ans+=gcd(gcd(i,j),r)*6
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):
      tmp = gcd(a, b)
      if tmp == 1:
         ans += 1 * k
      else:
         for c in range(1, k+1):
            ans += gcd(tmp, c)

print(ans)

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)
from functools import reduce

K = int(input())

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

def gcd2(number):
    return reduce(gcd, number)

ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K+1):
            ans += gcd2([i,j,k])
print(ans)

Solution (Python):

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

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

print(s)

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



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


gcdl = [[0]*k*2 for _ in range(k*2)]
for a in range(1, k+1):
    for b in range(1, k+1):
        gcdl[a][b] = gcd(a, b)

ansl = [[[-1]*k*2 for _ in range(k*2)] for _ in range(k*2)]
ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            if ansl[a][b][c] == -1:
                t = gcd(c, gcdl[a][b])
                ansl[a][b][c] = t
                ansl[a][c][b] = t
                ansl[b][a][c] = t
                ansl[b][c][a] = t
                ansl[c][b][a] = t
                ansl[c][a][b] = t
            else:
                t = ansl[a][b][c]
            ans += t
print(ans)

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

ans = []
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            ans_temp = gcd(i, j)
            ans.append(gcd(ans_temp, k))
print(sum(ans))

Solution (Python):
import math


def read():
    K = int(input())
    return K,

def solve(K):
    ans = 0
    cache = [[math.gcd(i, j) for j in range(K+1)] for i in range(K+1)]
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                bc = cache[b][c]
                abc = cache[a][bc]  
                ans += abc
    return ans


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

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

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq


def gcd(m,n):
  x = max(m,n)
  y = min(m,n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z









K = int(input())
ans = 0

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

print(ans)

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

Solution (Python):


from math import gcd

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

     ans = 0

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

     print(ans)

if __name__ == "__main__":
    main()

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

a = 0
for i in range(1, N+1):
    for j in range(1, N+1):
        b = math.gcd(i, j)
        for k in range(1, N+1):
            a += math.gcd(b, k)
print(a)

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

n = int(input())
res = 0

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

print(res)

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

Solution (Python):
from math import gcd
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

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

    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)


if __name__ == "__main__":
    main()

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

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

Solution (Python):
import math
from numba import jit

K = int(input())


@jit
def apt(N):
    cnt = 0
    for i in range(1, K+1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                cnt += math.gcd(k, math.gcd(i, j))
    return cnt


print(apt(K))

Solution (Python):
import math
from functools import reduce

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

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

Solution (Python):
import math
K = int(input())
b = []
ans = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        b.append(math.gcd(i, j))
n = len(b)
for i in range(n):
    for k in range(1, K+1):
        ans += math.gcd(b[i], k)
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 math import gcd
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(gcd(a, b), c)
print(ans)

Solution (Python):
import math

K = int(input())

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

print(total_ans)

Solution (Python):
from math import gcd

K = int(input())
ans = 0
for a in range(1, K + 1):
    for b in range(a, K + 1):
        for c in range(b, K + 1):
            if a == b == c:
                n = 1
            elif a == b or b == c or c == a:
                n = 3
            else:
                n = 6
            ans += n * gcd(gcd(a, b), c)
print(ans)

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


K = int(input())

G = [[0] * (K + 1) for _ in range(K + 1)]

for a in range(1, K + 1):
    for b in range(1, K + 1):
        G[a][b] = gcd(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):
            if G[a][b] == 1:
                ans += 1
                continue
            ans += gcd(G[a][b], c)
print(ans)

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

Solution (Python):


import sys
import math

sys.setrecursionlimit(10 ** 6)

input_line = input()
inputs = input_line.split(' ')
K, = [int(s) for s in inputs]

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

Solution (Python):
import math
k=int(input())
ans=0
for a in range(1,k+1):
  for b in range(a,k+1):
    for c in range(b,k+1):
      ans+=math.gcd(math.gcd(a,b),c)*(1 if a==b==c else (3 if a==b or b==c else 6))
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):
        a1 = gcd(i, j)
        for k in range(1, K+1):
            ans += gcd(a1, k)
print(ans)

Solution (Python):

import math
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):
            if len(set([i,j,k])) == 3:
                cnt = 6
            elif len(set([i,j,k])) == 2:
                cnt = 3
            else:
                cnt = 1
            ans += math.gcd(math.gcd(i,j),k) * cnt
            
print(ans)

Solution (Python):
import math

k=int(input())
ans=0

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

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

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



def gcd(a, b, c):



	nums = (a, b, c)


	
	d = calc_gcd(nums[0], nums[1])
	d = calc_gcd(d, nums[2])
	

	return d

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

print(total)

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

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

import math
from functools import reduce

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

sum=0

for i in range(1,K+1):
    for j in range(1,K+1):
        for n in range(1,K+1):
            sum+=gcd(i,j,n)

print(sum)