Problem:
 Score : 300 points    Problem Statement Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)} .  Here \gcd(a,b,c) denotes the greatest common divisor of a , b , and c .      Constraints  1 \leq K \leq 200  K is an integer.         Input Input is given from Standard Input in the following format:  K       Output Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)} .        Sample Input 1 2      Sample Output 1 9  \gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)  +\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)  =1+1+1+1+1+1+1+2=9  Thus, the answer is 9 .       Sample Input 2 200      Sample Output 2 10813692

Solution (Python):
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

def gcd(a, b):  
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
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 sys
input = sys.stdin.buffer.readline


from collections import defaultdict
from fractions import gcd

con = 10 ** 9 + 7

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


def main():
	K = int(input())
	ans = 0
	D = defaultdict(int)
	for i in range(1, K + 1):
		for j in range(1, K + 1):
			s = gcd(i, j)
			D[s] += 1

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

	print(ans)

if __name__ == '__main__':
	main()

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

Solution (Python):
from itertools import product
from collections import Counter
K = int(input())

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

def list_gcd(l):
    return functools.reduce(gcd,l)

ans = 0
l = []
for i,j in product(range(1,K+1),repeat=2):
    l.append(gcd(i,j))
dic = Counter(l)

for k,v in dic.items():
    for i in range(1,K+1):
        ans += gcd(k,i)*v

print(ans)

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

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

dp = [[0] * (K+1) for _ in range(K+1)]
for i in range(1,K+1):
	for j in range(1,K+1):
		dp[i][j] = gcd(i,j)

ans = 0
for a in range(1,K+1):
	for b in range(1,K+1):
		for c in range(1,K+1):
			tmp = dp[a][b]
			ans += dp[tmp][c]
print(ans)

Solution (Python):
def gcd(x, y):
  b = max(x, y)
  s = min(x, y)
  r = b % s
  while r != 0:
    b = s
    s = r
    r = b % s
  return s

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

K = int(input()) + 1
gcDP = [[0] * K for _ in range(K)]

for i in range(1, K):
  for j in range(1, K):
    gcDP[i][j] = gcd(i, j)

ans = 0
for i in range(1, K):
  for j in range(1, K):
    for k in range(1, K):
      ans += gcd(gcDP[i][j], 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):
        tmp = math.gcd(i, j)
        for la in range(1, k + 1):
            ans += math.gcd(la, tmp)
print(ans)

Solution (Python):
import math
from functools import reduce

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

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

main()

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

K = int(input())

total = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        x = gcd(a, b)
        for c in range(1, K+1):
            total += gcd(x, c)
print(total)

Solution (Python):
from math import gcd

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

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

memo = [[-1] * (k+1) for _ in range(k+1)]

total = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        if memo[a][b] != -1:
            ab = memo[a][b]
        else:
            ab = math.gcd(a, b)
            memo[a][b] = ab
            memo[b][a] = ab
        for c in range(1, k+1):
            if memo[ab][c] != -1:
                total += memo[ab][c]
            else:
                abc = math.gcd(ab, c)
                memo[ab][c] = abc
                memo[c][ab] = abc
                total += abc
print(total)

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

memo = [[None] * (K + 1) for _ in range(K + 1)]

def mygcd(a, b):
    if memo[a][b] != None: return memo[a][b]
    else:
        if a < b:
            ret = mygcd(b, a)
        elif b == 0:
            ret = a
        else:
            ret = mygcd(b, a % b)
        
        memo[a][b] = ret
        memo[b][a] = ret
        return ret

ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        tmp = mygcd(a, b)
        for c in range(1, K + 1):
            ans += mygcd(tmp, 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):
        for k in range(1, k+1):
            temp = math.gcd(i, j)
            ans += math.gcd(temp, k)

print(ans)

Solution (Python):
import math

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

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

Solution (Python):

MOD = 10 ** 9 + 7
INFI = 10**10

import math

import itertools
import heapq


import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default




def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    n=int(input())
    total=0
    for a,b,c in itertools.product(range(1,n+1),repeat=3):
        temp1=math.gcd(a,b)
        temp2=math.gcd(temp1,c)
        total+=temp2
    print(total)



if __name__ == "__main__":

    main()

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

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

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

Solution (Python):
import math 
k = int(input())
ans = 0 
dic = {}
for x in range(1, k+1):
  for y in range(1, k+1):
    dic[(x, y)] = math.gcd(x, y)

for x in range(1, k+1):
  for y in range(1, k+1):
    for z in range(1, k+1):
      ans += dic[(x, dic[(y, z)])]
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):
        x = math.gcd(a, b)
        for c in range(b, k + 1):
            y = math.gcd(x, c)
            if a == b and b == c:
                ans += y
            elif a == b or b == c:
                ans += y * 3
            else:
                ans += y * 6
print(ans)

Solution (Python):
import math
from functools import reduce

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

n = int(input())
ans = 0

for a in range(n):
  a += 1
  for b in range(n):
    b+=1
    for c in range(n):
      c+=1
      ans += gcd(a, b, c)

print(ans)

Solution (Python):
from math import gcd


G = [[gcd(l, r) for l in range(201)] for r in range(201)]
            
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 += G[G[a][b]][c]

print(S)

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

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

print(res)

Solution (Python):

from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math
from math import gcd


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

print(ans)

Solution (Python):
import math
N, = (int(x) for x in input().split())

sol = 0
for i in range(1, N+1):
    for j in range(1, N + 1):
        G = math.gcd(i, j)
        if G == 1:
            sol += N
            continue
        for k in range(1, N + 1):
            g = math.gcd(G, k)
            sol += g
print(sol)

Solution (Python):
from math import gcd
from itertools import product
K = int(input())
print(sum(gcd(gcd(a,b),c) for a,b,c in product(range(1,K+1), repeat=3)))

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

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

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

print(s)

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

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

cache = {}
def tgcd(a, b, c):
    def gcd(a, b):
        if a < b:
            a, b = b, a
        if (a, b) in cache:
            return cache[a, b]
        elif b == 0:
            cache[a, b] = a
            return a
        else:
            res = gcd(b, a % b)
            cache[a, b] = res
            return res
    ab = gcd(a, b)
    return gcd(c, ab)

total = 0
for a in range(1, k + 1):
    for b in range(a, k + 1):
        for c in range(b, k + 1):
            local = tgcd(c, b, a)
            if a == b and b == c:
                total += local
            elif a != b and b != c and c != a:
                total += 6 * local
            else:
                total += 3 * local


print(total)

Solution (Python):

import sys
input = sys.stdin.readline
try:
    from math import gcd
except:
    from fractions import gcd

k = int(input())

memo = [[0] * 201 for _ in range(201)]
for i in range(201):
    for j in range(201):
        memo[i][j] = gcd(i, j)
ret = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            val = memo[a][b]
            ret += memo[val][c]
print(ret)

Solution (Python):


import math
from functools import reduce

k = int(input())


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


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

print(res)

Solution (Python):
import math
from functools import reduce
import itertools
def gcd(*numbers):
    return reduce(math.gcd, numbers)
k = int(input())
a = [i for i in range(1, k+1)]
ans = 0
for v in itertools.combinations_with_replacement(a, 3):
    if len(set(v)) == 1:
        ans += gcd(v[0], v[1], v[2])
    elif len(set(v)) == 2:
        ans += gcd(v[0], v[1], v[2]) * 3
    else:
        ans += gcd(v[0], v[1], v[2]) * 6
print(ans)

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

import math
from functools import reduce

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

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):
import math
import fractions
import collections
import itertools
def ggcd(a,b):
    if(b==0):
        return a
    return ggcd(b,a%b)
K=int(input())
num=0
a=0
for i in range(1,K+1,1):
    for j in range(1,K+1,1):
        for k in range(1,K+1,1):
            a=ggcd(i,j)
            num=num+ggcd(a,k)

print(num)

Solution (Python):
import sys
import math
sys.setrecursionlimit(10 ** 6)  
to_index = lambda x: int(x) - 1  
print_list_in_2D = lambda x: print(*x, sep="\n")  


def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

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

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())


def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

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


def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

K = input_int()

count = 0
for a in range(K):
    ap = a + 1

    for b in range(K):
        bp = b + 1
        gcd_ab = math.gcd(ap, bp)
        for c in range(K):
            count += math.gcd(gcd_ab, c + 1)

print(count)

Solution (Python):


"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
flush= sys.stdout.flush




def iin(): return int(input())


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





def main():
    from math import gcd
    n = iin()
    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)

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

Solution (Python):
from math import gcd
import itertools
K=int(input())
res=0
li=[]
for x in itertools.product(range(1,K+1),repeat=2):
    li.append(gcd(x[0],x[1]))
for x,y in itertools.product(range(1,K+1),li):
    res+=gcd(x,y)
print(res)

Solution (Python):
import math as m

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

Solution (Python):
K = int(input())
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
ans = 0
a = []
glist = [[0 for i in range(K+1)] for j in range(K+1)]
for i in range(1,K+1):
    for j in range(1,K+1):
        g = gcd(i,j)
        a.append(g)
        glist[i][j] = g

for i in a:
    for j in range(1,K+1):
        ans += glist[i][j]
print(ans)

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(1,k+1):
        for n in range(1,k+1):
            ans += gcd(i, j, n)
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):
        a = gcd(i, j)
        if a == 1:
            ans += K
            continue
        for k in range(1, K + 1):
            ans += gcd(a, k)
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)

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

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

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



import sys
from math import gcd
from itertools import product

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    K = i2n()
    r = sum(gcd(gcd(i, j), k) for (i, j, k) in product(range(1, K+1), range(1, K+1), range(1, K+1)))
    print(r)
    return

main()

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

sub = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        mn = math.gcd(a, b)
        for c in range(1, k+1):
            sub+=math.gcd(mn, c)
print(sub)

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

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

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

print(ans)

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

K = int(readline())


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

    return 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(a, 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):
k = int(input())

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


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(gcd(a, b), c)

print(s)

Solution (Python):
import math
from functools import reduce

K=int(input())
sum=0

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

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

Solution (Python):
from collections import Counter

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

K = int(input())

Gs = []
for a in range(1, K+1):
    for b in range(1, K+1):
        Gs.append(gcd(a, b))

cnt = Counter(Gs)


ans = 0
for G, num in cnt.items():
    for c in range(1, K+1):
        ans += gcd(G, c) * num

print(ans)

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

res = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        cand = math.gcd(i, j)
        for l in range(1, k+1):
            res += math.gcd(l, cand)
print(res)

Solution (Python):

import math

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

k = int(input())

c = 0

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

            t = gcd(i, j, h)

            c += t

print (c)

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

Solution (Python):
def main():
    K = int(input())
    from math import gcd
    total = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            ab = gcd(a, b)
            if ab == 1:
                total += K
            else:
                for c in range(1, K+1):
                    total += gcd(ab, c)
    print(total)

main()

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):
            s = math.gcd(i,j)
            for t in range(1,k+1):
                ans+=math.gcd(s,t)
    
    print(ans)
if __name__ == "__main__":
    main()

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

total = 0

for i in range(1, n+1):
  for j in range(1, n+1):
    for k in range(1, n+1):
      tmp = math.gcd(i, j)
      total += math.gcd(tmp, k)
      
print(total)

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):
        _gcd = gcd(i, j)
        for k in range(1, k+1):
            ans += gcd(_gcd, k)
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
total=0
a=int(input())
for x in range(1,a+1):
    for y in range(1,a+1):
        for z in range(1,a+1):
            total+=math.gcd(math.gcd(x,y),z)
print(total)

Solution (Python):
import sys














readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from math import gcd
    k = int(input())
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

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

NN = 202020
MOD = 10**9+7

INF = float("inf")

n = int(input())
s = 0
for i in range(1, n+1):
    for j in range(i, n+1):
        for k in range(j, n+1):
            tmp = math.gcd(i, j)
            a = math.gcd(tmp, k)
            if i == j == k:
                s += a
            elif i == j or j == k or k == j:
                s += 3*a
            else:
                s += 6*a

print(s)

Solution (Python):
from collections import *
import sys
try: inp = raw_input
except: inp = input
def err(s):
    sys.stderr.write('{}\n'.format(s))

def ni():
    return int(inp())

def nl():
    return [int(_) for _ in inp().split()]

K = ni()

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


DP = {}
def gcd_SUM(K, a):
    if (K, a) in DP: return DP[K, a]
    sm = 0
    for i in range(1, K+1):
        sm += gcd(i, a)
    DP[K, a] = sm
    return sm


S = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        S += gcd_SUM(K, gcd(a, b))
print(S)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def gcd(a, b):
    """
    return gcd of a,b
    ユークリッド
    """
    a,b =max(a,b),min(a,b)
    while b:
        a, b = b, a % b
    return a



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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


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


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

    return a


K = int(input())


ans = 0
dif3 = 0
for a in range(1, K + 1):
    for b in range(a + 1, K + 1):
        ab = gcd(a, b)
        for c in range(b + 1, K + 1):
            dif3 += gcd(ab, c)

dif3 *= 6

dif2 = 0
for a in range(1, K + 1):
    for c in range(1, K + 1):
        if a == c:
            continue
        dif2 += gcd(a, c)
dif2 *= 3

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

ans = dif1 + dif2 + dif3
print(ans)

Solution (Python):
import sys
import math

input = sys.stdin.readline

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

print(ans)

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

from itertools import product
from functools import reduce
ans = [reduce(gcd, tmp) for tmp in product(range(1,n+1), repeat=3)]
print(sum(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 k in range(1,K+1):
            ans += math.gcd(math.gcd(i,j),k)

print(ans)

Solution (Python):
from math import gcd

K = int(input())

gcds = [[None] * (K+1) for _ in range(K+1)]
for i in range(1, K+1):
    for j in range(i, K+1):
        gcds[i][j] = gcds[j][i] = gcd(i, j)

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

print(ans)

Solution (Python):
K = int(input())
from fractions import gcd
from math import ceil
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations

sum_gcd = [0]*K
for i in range(1,K+1):
    c = 0
    for j in range(1,K+1):
        c += gcd(i,j)
    sum_gcd[i-1] = c
ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        ans += sum_gcd[gcd(a,b)-1]

print(ans)

Solution (Python):


import sys
import pprint
from math import gcd


sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	k = int(sys.stdin.readline())
	s = 0
	for a in range(1, k+1):
		for b in range(a, k+1):
			for c in range(b, k+1):
				if len(set([a, b, c])) == 3:
					s += gcd(gcd(a, b), c) * 6
				elif len(set([a, b, c])) == 2:
					s += gcd(gcd(a, b), c) * 3
				elif len(set([a, b, c])) == 1:
					s += gcd(gcd(a, b), c)
	print(s)
	


main()

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

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
ans = 0
k = int(input())
D = [0]*201
for a in range(1, k+1):
    for b in range(1, k+1):
        D[gcd(a, b)] += 1
for c in range(1, k+1):
    for g in range(201):
        ans += D[g] * gcd(c, g)
print(ans)

Solution (Python):
from math import gcd

K = int(input())

sum_ = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        g1 = gcd(i, j)
        for k in range(1, K + 1):
            sum_ += gcd(g1, k)

print(sum_)

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

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

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

K = int(input())

g = [[0] * 201 for i in range(201)]

for i in range(201):

    for j in range(201):

        g[i][j] = math.gcd(i,j)

ans = 0

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

    for j in range(1,K+1):

        for x in range(1,K+1):

            ans += g[g[i][j]][x]

            

print (ans)

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

def main():
    K = int(input())
    T = [[gcd(a, b) for b in range(K+1)] for a in range(K + 1)]

    return sum(T[T[a][b]][c] for a, b, c in product(range(1, K + 1), repeat=3))

print(main())

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



import math 
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
count = 0
for i in range(1,k+1):
    for j in range(1, k+1):
        for l in range(1,k+1):
            count += gcd(i,j,l)

print(count)

Solution (Python):
import math

K = int(input())
count = {}
for a in range(1, K + 1):
    for b in range(1, K + 1):
        d = math.gcd(a, b)
        if d in count:
            count[d] += 1
        else:
            count[d] = 1
res = 0
for c in range(1, K + 1):
    for d, cnt in count.items():
        res += math.gcd(c, d) * cnt
print(res)