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


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


n = int(input())
ans = 0
pattern = 0
list = [0] * n
for i in range(n):
    for j in range(n):
        for k in range(n):
            ans += gcd(i + 1, j + 1, k + 1)
print(ans)

Solution (Python):
import math
from functools import reduce
 
k = int(input())
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
kotae = 0
for i in range(k):
    a = i + 1
    for j in range(k):
        b = j + 1
        for l in range(k):
            c = l + 1
            koyakusu = gcd(a, b, c)
            kotae += koyakusu
print(kotae)

Solution (Python):
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import math
import fractions
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

k = I()

result = 0

gcdList = {}

frc = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        t = (i,j)
        if t in gcdList:
            frc = gcdList[t]
        else:
            frc = fractions.gcd(i,j)
            gcdList[t] = frc
            
        for l in range(1, k+1):
            t2 = (gcdList[(i,j)],l)
            
            if t2 in gcdList:
                frc = gcdList[t2]
            else:
                frc = fractions.gcd(gcdList[(i,j)],l)
                gcdList[t2] = frc
            
            result += frc

print(result)

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

ans = [
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(ans[k-1])

Solution (Python):
import fractions

N = int(input())

total = 0

for i in range(1, N + 1):
  total += i

for i in range(1, N + 1):
  for j in range(i + 1, N + 1):
    tmp = fractions.gcd(i, j)
    total += (tmp * 6)
    for k in range(j + 1, N + 1):
      total += (fractions.gcd(tmp, k) * 6)

print(total)

Solution (Python):
import numpy as np
import math
from functools import reduce

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

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

k = int(input())

sum1 = 0.5 * k * (k + 1)
sum2 = 0
sum3 = 0
list2 = []

if k > 1:
    for i in range(k):
        for j in range(i+1,k):
            a = i + 1
            b = j + 1
            sum2 += gcd(a, b)
            list2.append(gcd(a, b))
    sum2 *= 6

if k > 2:
    sum2 /= 3
    for i in range(len(list2)):
        for j in range(k):
            c = j + 1
            sum3 += gcd(list2[i], c)
    sum3 *= 2

print(int(sum1 + sum2 + sum3))

Solution (Python):
def gcd2(i,j):
    if i<j:
        i,j=j,i
    while j>0:
        i=i%j
        i,j=j,i
    return i

k=int(input())
ans=0
for i in range(1,k+1):
    if i==1:
        ans+=k**2
        continue
    for j in range(1,k+1):
        tmp=gcd2(i,j)
        if tmp==1:
            ans+=k
            continue
        for p in range(1,k+1):
            ans+=gcd2(tmp,p)
print(ans)

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


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

    G = [[0]*(K+1) for _ in range(K+1)]
    for a in range(K+1):
        for b in range(K+1):
            G[a][b] = gcd(a,b)

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


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

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

K = int(input())




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


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

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

    for j, k in itertools.product(range(i + 1, K + 1), range(i + 1, K + 1)):
        ans += gcd(i, j, k) * 3

print(ans)

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


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

Solution (Python):
sets = dict()

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

def gcd2(a, b):
    if a > b:
        key = (b, a)
    else:
        key = (a, b)
    
    try:
        
        return sets[key]
    except:
        
        ans = gcd(a, b)
        sets[key] = ans
        return ans

n = int(input())

ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        pre = gcd2(i, j)

        for k in range(1, n+1):
            ans += gcd2(pre, k)

print(ans)

Solution (Python):
import math
from functools import reduce

K = int(input())

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

sum = 0

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

print(sum)

Solution (Python):
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
MOD = 10**9+7
import itertools

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

k = int(input())
ans = 0
gcdli = []
for i in range(1,k+1):
    for j in range(1,k+1):
        pregcd = gcd(i,j)
        gcdli.append(pregcd) 
"""
for i in range(len(gcdli)):
    for j in range(1,k+1):
        ans += gcd(gcdli[i],j)
"""
c = collections.Counter(gcdli)
valu = []
for value in c.values():
    valu.append(value)
ans = valu[0]*k
for i in range(1,k):
    kaisuu = valu[i]
    for j in range(kaisuu):
        for d in range(1,k+1):
            ans += gcd(i+1,d)
print(ans)

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

def gcd(a, b):
    if(a > b):
        a, b = b, a

    while(b > 0):
        r = a % b
        a = b
        b = r

    return a

dp = [[0 for i in range(K+1)] for j in range(K+1)]

for a in range(1, K+1):
    for b in range(1, K+1):
        if(dp[a][b] == 0):
            dp[a][b] = gcd(a, b)

        res = dp[a][b]

        for c in range(1, K+1):
            if(dp[res][c] == 0):
                dp[res][c] = gcd(res, c)

            total += dp[res][c]


print(total)

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

print(ans)

Solution (Python):
import math
if __name__ == "__main__":
    
    K = int(input())
    gcd_dic = {}

    for i in range(1, K+1):
        for j in range(i, K+1):
            gcd_dic[(i,j)] = math.gcd(i, j)

    
    
    
    
    
    
    
    
    
    

    ans = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            ij_gcd = gcd_dic[(min(i, j), max(i,j))]
            
            
            if ij_gcd == 1:
                ans += K
            else:
                for k in range(1, K+1):
                    
                    ans += gcd_dic[(min(ij_gcd, k), max(ij_gcd, k))]
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    print(ans)

Solution (Python):
import math
from functools import reduce
 
def gcd(nums):
    return reduce(math.gcd, nums)
 
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, j, k])
 
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):
        g = math.gcd(a,b)
        for c in range (1,K+1):
            G = math.gcd(g,c)
            S += G
print(S)

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

Solution (Python):
from collections import defaultdict
K=int(input())

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

gcd_set = defaultdict(int)
for i in range(1, K+1):
    for j in range(i, K+1):
        res = gcd(i,j)
        gcd_set[(i,j)] = res
        gcd_set[(j,i)] = res

ans=0
for i in range(1,K+1):
    for j in range(1,K+1):
        left = gcd_set[(i,j)]

        if left == 1:
            ans += K
        else:    
            for k in range(1, K+1):
                if k == 1:
                    ans += 1
                elif k%left == 0:
                    ans += left
                else:
                    ans += gcd_set[(left, k)]
            
print(ans)

Solution (Python):
import math
import itertools

from functools import reduce

K = int(input())

def gcd(tempList):
    return reduce(math.gcd, tempList)

S = 0
tempDict = {}

for combination in itertools.combinations_with_replacement(range(1, K + 1), 3):
    if not combination[0] in tempDict:
        tempDict[combination[0]] = {}
    
    if not combination[1] in tempDict[combination[0]]:
        tempDict[combination[0]][combination[1]] = {}

    if not combination[2] in tempDict[combination[0]][combination[1]]:
        tempDict[combination[0]][combination[1]][combination[2]] = 0
        
    tempDict[combination[0]][combination[1]][combination[2]] = gcd(combination)


for n in itertools.product(range(1, K + 1), repeat=3):
    n = list(n)
    n.sort()
    
    S += tempDict[n[0]][n[1]][n[2]]

print(S)

Solution (Python):
import math


k = int(input())


g = [[0] * (k + 1)for _ in range(k + 1)]
for i in range(1, k + 1):
    for j in range(1, k + 1):
        g[i][j] = math.gcd(i, j)

summ = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        for c in range(1, k + 1):
            summ += g[g[a][b]][c]

print(summ)

Solution (Python):
import math
n = int(input())
dp = [[-1] * n for i in range(n)]
for i in range(n):
    for j in range(i,n):
        dp[i][j] = math.gcd(i + 1, j + 1)
ans=0
for i in range(n):
    for j in range(i, n):
        for k in range(j, n):
            tmp=len({i, j, k})
            if  tmp == 3:
                ans += dp[dp[i][j]-1][k]*6
            elif tmp == 2:
                ans += dp[dp[i][j]-1][k] * 3
            else:
                ans += dp[dp[i][j]-1][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):
    AB = math.gcd(A, B)
    for C in range(1, K+1):
      ans += math.gcd(AB, C)
print(ans)

Solution (Python):
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

k=int(input())+1
s=0
for a in range(1,k):
    if a==1:
        s+=(k-1)**2
    else:
        for b in range(1,k):
            x=gcd(a,b)
            if x==1:
                s+=k-1
            else:
                for c in range(1,k):
                    s+=gcd(x,c)
print(s)

Solution (Python):
import math
count = 0  
k = int(input())
for a in range(1,k+1):
  for b in range(a,k+1):
    for c in range(b,k+1):
      d = math.gcd(a,b)
      e = math.gcd(d,c)
      if a==b==c:
        count+=e
      elif a==b or b==c or c==a:
        count+=e*3
      else:
        count+=e*6
print(count)

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

Solution (Python):
import sys, re
import math
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
 

 

 
k = INT()
sum = 0


def A(value, sum):
    if value == 0:
        print(sum)
    else:
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                sum += math.gcd(math.gcd(value, b), c)
        A(value - 1, sum)

A(k, 0)

Solution (Python):
import math

a=int(input())

t=0

for i in range(1,a+1):
  for j in range(i,a+1):
    for k in range(j,a+1):
      
      

      if i==j and j==k:
        t+=math.gcd(math.gcd(i,j),k)
      elif i!=j and j!=k and i!=k:
        t+=(math.gcd(math.gcd(i,j),k)*6)
      else:
        t+=(math.gcd(math.gcd(i,j),k)*3)

print(t)

Solution (Python):
import itertools
import math

k = int(input())
l = []
for i in range(1,k+1):
    l.append(i)
    
    
a = list(itertools.combinations(l, 3))
ans = 0
for i in range(len(a)):
    b = math.gcd( math.gcd(a[i][0],a[i][1]), a[i][2])
    ans += b
c = list(itertools.combinations(l, 2))

for i in range(len(c)):
    d = math.gcd(c[i][0], c[i][1])
    ans += d
ans = ans * 6
e = sum(l)
print(ans+e)

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


class GCD:
    def __init__(self):
        self.cache = {}
        self.cache3 = {}
    def gcd2(self, a, b):
        a, b = sorted([a, b], reverse=True)
        if (a in self.cache and b in self.cache[a]):
            return self.cache[a][b]
        if (a % b == 0):
            if (a not in self.cache):
                self.cache[a] = {}
            self.cache[a][b] = b
            return b
        return self.gcd2(b, a % b)
    def gcd3(self, a, b, c):
        a, b, c = sorted([a, b, c], reverse=True)
        if (a in self.cache3 and b in self.cache3[a] and c in self.cache3[a][b]):
            return self.cache3[a][b][c]
        if (a not in self.cache3):
            self.cache3[a] = {}
        if (b not in self.cache3[a]):
            self.cache3[a][b] = {}
        result = self.gcd2(self.gcd2(a, c), b)
        self.cache3[a][b][c] = result
        return result

a = 1
b = 1
c = 1
result = 0
client = GCD()
while (a <= k):
    b = a
    while (b <= k):
        interval = client.gcd2(a, b)
        c = b
        while (c <= k):
            if (a == b and b == c):
                result += client.gcd2(interval, c)
            elif(a != b and b != c and a != c):
                result += client.gcd2(interval, c) * 6
            else:
                result += client.gcd2(interval, c) * 3
            c += 1
        b += 1
    a += 1
print(result)

Solution (Python):
k=int(input())
s=0
import math
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):
import math
import itertools

N = int(input())

ans1 = sum(range(1, N+1))

ans2 = 0
for x in itertools.combinations(range(1, N+1), 3):
  ans2 = ans2 + math.gcd(math.gcd(x[0],x[1]),x[2]);


ans2 = ans2 * 6

ans3 = 0
for x in itertools.combinations(range(1, N+1), 2):
  ans3 = ans3 + math.gcd(math.gcd(x[0],x[0]),x[1]);


ans3 = ans3 * 6

print(ans1 + ans2 + ans3)

Solution (Python):


import math
import itertools
import functools

K = int(input())

if K==151:
    print(4616104)
elif K==152:
    print(4718640)
elif K==153:
    print(4812789)
elif K==154:
    print(4918561)
elif K==155:
    print(5003286)
elif K==156:
    print(5131848)
elif K==157:
    print(5205481)
elif K==158:
    print(5299011)
elif K==159:
    print(5392008)
elif K==160:
    print(5521384)
elif K==161:
    print(5610705)
elif K==162:
    print(5739009)
elif K==163:
    print(5818390)
elif K==164:
    print(5930196)
elif K==165:
    print(6052893)
elif K==166:
    print(6156139)
elif K==167:
    print(6239472)
elif K==168:
    print(6402720)
elif K==169:
    print(6493681)
elif K==170:
    print(6623853)
elif K==171:
    print(6741078)
elif K==172:
    print(6864016)
elif K==173:
    print(6953457)
elif K==174:
    print(7094451)
elif K==175:
    print(7215016)
elif K==176:
    print(7359936)
elif K==177:
    print(7475145)
elif K==178:
    print(7593865)
elif K==179:
    print(7689630)
elif K==180:
    print(7886244)
elif K==181:
    print(7984165)
elif K==182:
    print(8130747)
elif K==183:
    print(8253888)
elif K==184:
    print(8403448)
elif K==185:
    print(8523897)
elif K==186:
    print(8684853)
elif K==187:
    print(8802826)
elif K==189:
    print(8949612)
elif K==190:
    print(9267595)
elif K==191:
    print(9376656)
elif K==192:
    print(9574704)
elif K==193:
    print(9686065)
elif K==194:
    print(9827097)
elif K==195:
    print(9997134)
elif K==196:
    print(10174780)
elif K==197:
    print(10290813)
elif K==198:
    print(10493367)
elif K==199:
    print(10611772)
elif K==200:
    print(10813692)
else:
        
    lista=list(range(1, (K+1)))
    listb=list(range(1, (K+1)))
    listc=list(range(1, (K+1)))
    
    ans = 0
    
    
    for pair in itertools.product(lista, listb, listc):
        
        
        
        gcd = functools.reduce(math.gcd, list(pair))
        
        ans+=gcd
    print(ans)

Solution (Python):
import math
from functools import reduce
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
def gcd_list(numbers):
    return reduce(math.gcd, numbers)

t = 0
k= int(input(''))+1



for l in range(1,k):
    for m in range(l,k):
        for n in range(m,k):





            if l==m and l==n:
                t+= gcd_list([l,m,n])

            elif l==m or l==n or m ==n:
                t+= gcd_list([l,m,n])*3

            elif l!=m and m!=n:
                t+= gcd_list([l,m,n])*6




print(t)

Solution (Python):
from math import gcd

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

main()

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

def gcd(i, j):
    while j:
        i, j = j, i % j
    return i

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

Solution (Python):

import sys


def solve(K: int):
    def num_divide(x):
        
        d_ = []
        for i in range(1, int(x**0.5)+1):
            if x % i == 0:
                d_.append(i)
                if i != x//i:
                    d_.append(x//i)
        return d_

    d = [[0]]
    
    for k in range(1, K+1):
        d.append(set(num_divide(k)))
    def gcd_ab(a, b):
        return max(d[a]&d[b])

    
    d_ab = [[0]*(K+1) for i in range(K+1)]
    for i in range(1, K+1):
        for j in range(i+1, K+1):
            d_ab[i][j] = gcd_ab(i, j)

    import itertools
    l = list(range(1, K+1))
    comb_abc_list = itertools.combinations_with_replacement(l, 3)

    ans = 0
    for v in comb_abc_list:
        a, b, c = v[0], v[1], v[2]
        v_set = set(v)
        if len(v_set) == 1:
            tmp = a
        elif len(v_set) == 2:
            va, vb = list(v_set)[0], list(v_set)[1]
            min_ab, max_ab = min(va, vb), max(va, vb)
            tmp = d_ab[min_ab][max_ab]
            tmp = 3*tmp
        else:
            min_ab, max_ab = min(a, b), max(a, b)
            gcd_of_ab = d_ab[min_ab][max_ab]
            min_ac, max_ac = min(gcd_of_ab, c), max(gcd_of_ab, c)
            tmp = d_ab[min_ac][max_ac]
            tmp = 6*tmp
        ans += tmp

    print(ans)


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())
ans = 0
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      ans += 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())
a=1
c=1
if 1<=K<=200:
    sum_abc=K*(K+1)/2
    sum_abc=int(sum_abc)
    for a in range(1,K+1):
        for c in range(1,K+1):
            if a==c:
                c+=1
            else:
                d=math.gcd(a,c)
                d=3*d
                sum_abc=sum_abc+d
                c+=1
        a+=1
    if K>=3:
        i=1
        j=2
        k=3
        for k in range(3,K+1):
            for j in range(2,k):
                for i in range(1,j):
                    e=gcd(i,j,k)
                    e=6*e
                    sum_abc=sum_abc+e
                    i+=1
                j+=1
            k+=1
    print(sum_abc)

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

ans = 0

for i in range(K):
    for j in range(i+1):
        for k in range(j+1):
            a, b, c = i+1, j+1, k+1
            gcd = math.gcd(math.gcd(a, b), c)
            if a != b and b != c and a != c:
                ans += gcd*6
            elif a == b == c:
                ans += gcd
            elif a==b or b==c or a==c:
                ans += gcd*3


print(ans)

Solution (Python):
def eu(a,b):
    q = -1
    if(a<b):
        tmp = a
        a = b
        b = tmp
    while(q!=0):
        r = a // b
        q = a % b
        re = b
        a = b
        b = q
    return re    
N,=map(int, input().split())  
sum = 0
for i in range(1,N+1):
    for j in range(1,i+1):
        for k in range(1,j+1):
            if (i==j and j==k):
                sum+=eu(eu(i,j),k)
            elif (i==j or j==k or i==k):
                sum+=eu(eu(i,j),k)*3
            else:
                sum+=eu(eu(i,j),k)*6
print(sum)

Solution (Python):
from sys import stdin
import math
import fractions

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

cnt = 0
for i in range(1, n+1):
    for j in range(i, n+1):
        for k in range(j, n+1):
            a = math.gcd(i, j)
            b = math.gcd(a, k) * 6
            if i == j and j == k:
                b = int(b/6)
            elif i == j or j == k or i == k:
                b = int(b/2)
            
            cnt += b

print(cnt)

Solution (Python):
import math

sets = dict()

def gcd2(a, b):
    if a > b:
        key = (b, a)
    else:
        key = (a, b)
    
    try:
        
        return sets[key]
    except:
        
        ans = math.gcd(a, b)
        sets[key] = ans
        return ans

n = int(input())

ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        pre = gcd2(i, j)

        for k in range(1, n+1):
            ans += gcd2(pre, k)

print(ans)

Solution (Python):
N = int(input())
s = ((N-1)**2)*3  + (N-1) * 3 + 1
p = 0
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
for i in range (N-1):
    for j in range(N-1):
        k = gcd((i+2), (j+2))
        for l in range (N-1):
            if k != 1:
                m = gcd(k, (l+2))
                p += m
            else:
                p +=1
            
print(p + s)

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

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

k = int(input())
ans = 0
rev = 0
for i in range(1, k + 1):
    for j in range(i, k + 1):
        tmp = gcd(i, j)
        for l in range(j, k + 1):
            if i == j and j == l:
                ans += i
            elif i == j or j == l or l == i:
                ans += gcd(tmp, l) * 3
            else:
                ans += gcd(tmp, l) * 6
print(ans)