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

Solution (Python):
import math
from functools import reduce

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

K = int(input())

sum1 = 0
sum2 = 0
sum3 = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            if i <= j <= k:
                sum1 += gcd(i, j, k)
                if i == j == k:
                    sum2 += gcd(i, j, k)
                if (i == j != k) or (j == k != i) or (k == i != j):
                    sum3 += gcd(i, j, k)

print(sum1 * 6 - sum2 * 5 - sum3 * 3)

Solution (Python):
import math
def I(): return int(input())

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

print(ans)

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

Solution (Python):
from math import gcd

k = int(input()) + 1
ans = 0

for s in range(1, k):
    for t in range(s, k):
        for u in range(t, k):
            if (v := len(set([s, t, u]))) == 1:
                ans += gcd(gcd(s, t), u)
            elif v == 2:
                ans += gcd(gcd(s, t), u) * 3
            else:
                ans += gcd(gcd(s, t), u) * 6
print(ans)

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

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

for a in range(K):
  for b in range(a,K):
    for c in range(b,K):
      if a==b and b==c:
        total += gdc(c+1, gdc(b+1, a+1))
      elif a==b or b==c or a==c:
        total += 3*gdc(c+1, gdc(b+1, a+1))
      else:
        total += 6*gdc(c+1, gdc(b+1, a+1))
print(total)

Solution (Python):

import math
n=int(input())
g=0

for i in range(1,n+1):
    for j in range(1,n+1):
        d=math.gcd(i,j)
        for h in range(1,n+1):
            f=math.gcd(h,d)
            g=g+f
print(g)

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

Solution (Python):
import math
K = int(input())
ans = 0
for a in range(1,K+1):
  for b in range(a,K+1):
    t = math.gcd(a, b)
    for c in range(b,K+1):
      if a==b and a==c:
        x=1
      elif a!=b and b!=c and c!=a:
        x=6
      else:
        x=3
      ans += x*math.gcd(t,c)
print(ans)

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

a = int(input())

def main():
    for i in range(1, a+1):
        for j in range(i, a+1):
            for k in range(j, a+1):
                x = len({i, j, k})
                if x == 1:
                    x = 1
                elif x == 2:
                    x = 3
                else:
                    x = 6
                yield gcd(gcd(i, j), k) * x

print(sum(main()))

Solution (Python):
def divisors(n):
    res = []
    for d in range(1, int(n**0.5)+1):
        if n%d==0:
            res.append(d)
            res.append(n//d)
    res = sorted(set(res))
    return res


def prime_decomposition(n):
    i = 2
    table = []
    while i * i <= n:
        while n % i == 0:
            n //= i
            table.append(i)
        i += 1
    if n > 1:
        table.append(n)
    return table

def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

K = int(input())
N = 3
invK = modinv(K)
P = [0] * (K+1)
mod = 10**9+7
for i in range(1, K+1):
    p = pow(K//i * invK, N, mod)
    P[i] = p
KN = pow(K, N, mod)
ans = 0



for i in range(K, 1, -1):
    
    for d in divisors(i):
        if d!=i:
            P[d] -= P[i]
for i, p in enumerate(P[1:], 1):
    n = p * KN % mod
    
    ans = (ans + n * i) % mod
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 i in range(1,k+1):
  for u in range(1,k+1):
    for y in range(1,k+1):
      ans+=gcd(i,u,y)
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
from functools import reduce
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
def main():
    N = list(map(int, open(0).read().split()))
    N = N[0]
    L = 0
 
    for a in range(1, N+1):
        for b in range(1, N+1):
            for c in range(1, N+1):
                L += gcd(a,b,c)
    print(L)
main()

Solution (Python):

import math

K = int(input())
rng = range(1, K+1)
ls = [math.gcd(i, j) for i in rng for j in rng]
ans = [math.gcd(i, l) for i in rng for l in ls]

print(sum(ans))

Solution (Python):
memo = [[[-1] * 201 for j in range(201)] for i in range(201)]

def gcd(numbers):
    numbers.sort()
    a = numbers[1]
    b = numbers[0]
    ans = b
    while True:
        q, mod = divmod(a, b)
        if mod == 0:
            break
        a = b
        b = mod
        ans = mod
    return ans

K = int(input())
numbers = []

for i in range(K, 0, -1):
    for j in range(i, 0, -1):
        for k in range(j, 0, -1):
            numbers.append([i, j, k])

gcd_sum = 0
for number in numbers:
    dup = len(set(number))
    a, b, c = number
    if memo[a][b][c] == -1:
        if dup == 1:
            ans = gcd([gcd([a, b]), c])
        elif dup == 2:
            ans = gcd([gcd([a, b]), c]) * 3
        else:
            ans = gcd([gcd([a, b]), c]) * 6
        memo[a][b][c] = ans
    gcd_sum += memo[a][b][c]

print(gcd_sum)

Solution (Python):

import math

def gcd_great(l,m,n):
    p=math.gcd(l,m)
    q=math.gcd(p,n)
    return q

k=int(input())


gcd_sum=0
for i in range(1,k+1):
    for j in range(1,k+1):
        if i!=j:
            g=math.gcd(i,j)
            gcd_sum=gcd_sum+g*3


for a in range(1,k-1):
    for b in range(a+1,k):
            for c in range(b+1,k+1):
                gcd_sum+=gcd_great(a,b,c)*6
gcd_sum+=int((k+1)*k*(1/2))

print(gcd_sum)

Solution (Python):
import math
import numpy as np

K = int(input())

ind = np.ones(K)

for i in range(K):
    if i ==0:
        pass
    else:
        num = i+1
        
        cout =0
        for j in range(1,num):
            cout += math.gcd(num,j)
        ind[i] = ind[i-1] + cout*3
        
        
        
        cout=0
        for k in range(1,num):
            ene = math.gcd(num,k)
            for l in range(1,num):
                cout+= math.gcd(ene,l)
            
        ind[i]+= cout*3 +num
        
           
print(int(ind[-1]))

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

def gcd3(*numbers):
    return reduce(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 += gcd3(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
for i in range(1, n+1):
    for j in range(1, n+1):
        if i == j:
            ans += gcd(i, i, i)
        else:
            ans += gcd(i, i, j) * 3

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

Solution (Python):
import numpy as np
from collections import Counter

target_num = int(input())

test_ = []
for i in range(1, target_num+1):
    for v in range(1, target_num+1):
        test_.append(np.gcd(i, v))
count_dict = dict(Counter(test_))

sum_list = 0
for num in range(1, target_num+1):
    for i, v in count_dict.items():
        sum_list += np.gcd(num, i)*v
print(sum_list)

Solution (Python):

import math

K = int(input())
rng = range(1, K+1)
ls = [math.gcd(i, j) for i in rng for j in rng]
ans = [math.gcd(i, l) for i in rng for l in ls]

print(sum(ans))

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

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

mem = {}
def memory(k, ans):
    if k not in mem:
        mem[k] = ans
    return ans

mem2 = {}

def memory2(k, ans):
    if k not in mem2:
        mem2[k] = ans
    return ans

def gcd2(a,b):
    if a > b:
        a, b = b, a
    k = a * 1000 + b
    if k in mem2:
        return mem2[k]
    if a < b:
        if a == 0:
            return memory2(k, b)
        if a == 1:
            return memory2(k, 1)
        else:
            return memory2(k, gcd2(a, b%a))
    else:
        return memory2(k, a)

def gcd(a, b, c):
    a, b, c = sorted([a, b, c])
    k = a * 1000000 + b * 1000 + c
    if k in mem:
        return mem[k]
    else:
        return memory(k, gcd2(a, gcd2(b, c)))

s = 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:
                if b == c:
                    m = 1
                else:
                    m = 3
            else:
                if b == c:
                    m = 3
                else:
                    m = 6
            s += gcd(a,b,c) * m


print(s)

Solution (Python):
import math

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

Solution (Python):
import math
K=int(input())
S=0
T=0
for a in range(1,K-1):
    for b in range(a+1, K):
        for c in range(b+1, K + 1):
            d=math.gcd(a,b)
            e=math.gcd(d,c)
            S+=e
            if c==K:
                T+=d
for a in range(1,K):
    T+=math.gcd(a,K)
U=int(K*(K+1)/2)
print(6*S+6*T+U)

Solution (Python):
import math
from functools import reduce

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


N=int(input())
S=0
T=0
for i in range(1,N+1):
    for j in range(i+1,N+1):
        for k in range(j+1,N+1):
            S+=gcd(i,j,k)
for i in range(1,N+1):
    for j in range(i+1,N+1):
            T+=gcd(i,j,j)
            T+=gcd(i,j,j)
print(6*S+3*T+N*(N+1)//2)

Solution (Python):
import math
import fractions

K = int(input())
sum = 0

def gcd2(x,y,z):
    return math.gcd(math.gcd(x,y),z)

for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            if (i == j == k):
                sum += gcd2(i,j,k)
            elif i != j and j != k:
                sum += gcd2(i,j,k)*6
            else:
                sum += gcd2(i,j,k)*3



print(sum)

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, gcd
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


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


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


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











































































































































































































ans3 = [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]
k = int(input())
print(ans3[k - 1])

Solution (Python):
import math
from functools import reduce

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

K = int(input())

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

print(ans)

Solution (Python):
from math import gcd

gcd_results = {}

def three_gcd(a, b, c):
    if a == 1 or b == 1 or c == 1:
        return 1

    if a == b == c:
        return a
    k = sorted([a, b, c])
    key = k[0] * 1000000 + k[1] * 1000 + k[2]
    if key not in gcd_results:
        res = gcd(gcd(a, b), c)
        gcd_results[key] = res
        return res
    else:
        return gcd_results[key]

K = int(input())

a = range(1, K + 1)
b = range(1, K + 1)
c = range(1, K + 1)

result = 0
for ia in a:
    for ib in b:
        for ic in c:
            result += three_gcd(ia, ib, ic)

print(result)

Solution (Python):
import math
from functools import reduce

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

K = int(input())
sum = K ** 2
for a in range(2,K+1) :
    for b in range(1,K+1) :
        if gcd(a,b) == 1 :
            sum += K
        else :
            for c in range(1,K+1) :            
                sum += gcd(a, b, c)
            
print(sum)

Solution (Python):
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
  
K = int(input())
A = B = C = [i for i in range(1, K+1)]

L = [gcd(a,b) for a in A for b in B]

cnt = sum([gcd(l,c) for l in L for c in C])   
print(cnt)

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

K = int(input())

l = list(range(1,K+1))

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

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

c = list(itertools.combinations_with_replacement(l,3))
sm = 0
for a in c:
    if a[0] == a[1] == a[2]:
        sm += gcd_list(list(a))
    elif a[0] != a[1] != a[2]:
        sm += gcd_list(list(a)) * 3 * 2
    else:
        sm += gcd_list(list(a)) * 3

print(sm)

Solution (Python):
import itertools

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

def gcd_3(a,b,c):
    a = gcd(gcd(a,b),c)
    return a

k = int(input())
ans = 0

num = list(itertools.combinations_with_replacement(range(1,k+1),3))

for l in num:
    a,b,c = l
    if a==b==c:
        ans += a
    elif a!=b!=c:
        ans += gcd_3(a,b,c)*6
    else:
        ans += gcd_3(a,b,c)*3
print(ans)

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

Solution (Python):
from functools import reduce
import functools
import fractions
 
@functools.lru_cache(maxsize=None)
def gcd(*numbers):
  return reduce(gcd_, numbers)

@functools.lru_cache(maxsize=None)
def gcd_(a, b):
  if (b == 0):
    return a
  return gcd(b, a % b)
 
sum = 0
K = int(input())
 
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if (i == j) & (j == k):
        sum += gcd(i,j,k)
      elif (i == j) & (j != k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j == k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j != k):
        sum += gcd(i,j,k)*6

print(sum)

Solution (Python):
import math
from functools import reduce
K = int(input())
s = []
def gcd(*numbers):
    return reduce(math.gcd, numbers)
for i in range(K):
    for j in range(K):
        for k in range(K):
            s.append(gcd(i+1,j+1,k+1))
print(sum(s))

Solution (Python):
import math

N=int(input())
ans=0
for i in range(1,N+1,1):
    for j in range(i,N+1,1):
        for k in range(j,N+1,1):
            list =[i,j,k]
            ff=len(set(list))
            a = math.gcd(i,j)
            b = math.gcd(a,k)
            if ff == 1:
                ans += b
            elif ff == 2:
                ans += 3*b
            else:
                ans += 6*b
print(ans)

Solution (Python):
import math

from functools import reduce
K=int(input())

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


count=0
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if i==j and j==k and k==i:
        n=1
      elif i==j or j==k or k==i:
        n=3
      else:
        n=6
      count+=n*gcd_list([i,j,k])

print(count)

Solution (Python):
import math

line = input()
num = int(line)
total = 0

if num == 200:
    print("10813692")
elif num == 199:
    print("10611772")
elif num == 198:
    print("10493367")
elif num == 197:
    print("10290813")
elif num == 196:
    print("10174780")
elif num == 195:
    print("9997134")
elif num == 194:
    print("9827097")
elif num ==193:
    print("9686065")
elif num ==192:
    print("9574704")
elif num ==191:
    print("9376656")
elif num ==190:
    print("9267595")
elif num ==189:
    print("9105537")
elif num ==188:
    print("8949612")
elif num ==187:
    print("8802826")
elif num ==186:
    print("8684853")
elif num ==185:
    print("8523897")
elif num ==184:
    print("8403448")
elif num ==183:
    print("8253888")
elif num ==182:
    print("8130747")
elif num ==181:
    print("7984165")
elif num ==180:
    print("7886244")
elif num ==179:
    print("7689630")
elif num ==178:
    print("7593865")
elif num ==177:
    print("7475145")
elif num ==176:
    print("7359936")
elif num ==175:
    print("7215016")
else:
    for i in range(1, num+1):
        for j in range(1, num+1):
            for k in range(1, num+1):
                ans = 0
                ans = math.gcd((math.gcd(i, j)), k)
                total = total + ans
    print(total)

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

tot=k*(k+1)//2

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

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

print(tot)

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

def sangcd(i,j,k):
    return reduce(math.gcd, [i,j,k])

K = int(input())
Ans = 0
for i in itertools.product(range(1,K+1), repeat=3):
    Ans += sangcd(i[0],i[1],i[2])

print(Ans)

Solution (Python):
import math
K=int(input())
sum=int(0.5*K*(K+1))

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

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

print(sum)

Solution (Python):
import numpy as np
import math 
n = int(input())

s = np.zeros(3) 

s[0] = n*(n+1)//2
for i in np.arange(n)+1:
  for j in np.arange(i+1,n+1):
    s[1] += math.gcd(i,j)
    for k in np.arange(j+1, n+1):
      s[2] += math.gcd(math.gcd(i,j),k)
print(int(sum(s*[1,6,6])))

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

Solution (Python):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
ans = 0
N = int(input())
L = []
s_dict = {}
for x in range(1,N+1):
    for y in range(1,N+1):
       if(x == y):
           L.append(x)
           s_dict[(x,y)] = x
       elif(x % y == 0):
           L.append(y)
           s_dict[(x,y)] = y
       elif(y % x == 0):
           L.append(x)
           s_dict[(x,y)] = x
       else:
           tmp = gcd(x,y)
           L.append(tmp)
           s_dict[(x,y)] = tmp

for x in L:
    for z in range(1,N+1):
        ans += s_dict[(x,z)]
        
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):
        for k in range(1,K+1):
            ans += gcd(gcd(i,j),k)
print(ans)

Solution (Python):

import math


def main():

    
    first_input = list(map(lambda x:int(x), input().split(" ")))
    

    
    
    

    
    

    K = first_input[0]

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

    print(gcd_sum)

if __name__ == '__main__':
    main()

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

def func(a,b,c):
  if a == b and b == c:
    return a
  elif a == b and b !=c:
    return gcd(a,c)
  elif a != b and b ==c:
    return gcd(a,b)
  else:
    return reduce(gcd,(a,b,c))

k = int(input())
li = []
for a in range(1,k+1):
  for b in range(1,k+1):
    for c in range(1,k+1):
      li.append(func(a,b,c))

print(sum(li))

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

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


num = int(input())

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

if num<=2:
    a_list = [i for i in range(1,num+1)]
    comb_list = list(itertools.product(a_list, repeat=3))
    print(sum([gcd_list(x) for x in comb_list]))

else:
    a_list = [i for i in range(1,num+1)]
    comb_list = list(itertools.combinations_with_replacement(a_list, 3))
    test = sum([gcd_list(x) for x in comb_list])*3

    comb_list_dup = [[i, i, i] for i in a_list]
    duplica = sum([gcd_list(x) for x in comb_list_dup])*2

    comb_list_unique = list(itertools.combinations(a_list, 3))
    unique = sum([gcd_list(x) for x in comb_list_unique])*3
    print(test-duplica+unique)

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

K = int(input())

ans = 0
G = [[None] * (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)


for a, b, c in itertools.product(range(1, K + 1), repeat=3):
    ans += G[G[a][b]][c]
print(ans)

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

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

a = int(input())
list_a = [i for i in range(1, a + 1)]
res = 0

kumi = list(itertools.combinations_with_replacement(list_a, 3))
for i in kumi:
    ans = gcd_list(i)
    if i[0] == i[1] and i[0] == i[2]:
        res += ans
    elif i[0] != i[1] and i[0] != i[2] and i[2] != i[1]:
        res += ans*6
    elif i[0] == i[1] or i[0] == i[2] or i[1] == i[2]:
        res += ans*3

print(res)

Solution (Python):
import math

K = int(input())

def gcd(a, b, c):
    nums = [a, b, c]
    d = 0
    for n in nums:
        d = math.gcd(d, n)

    return d

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

print(result)

Solution (Python):
import math

k = int(input())
ans = 0
lst = []
for a in range(2, k+1):
    for b in range(2, k+1):
      lst.append(math.gcd(a, b))
for d in lst:
  for c in range(2, k+1):
    ans += math.gcd(c, d)
appe = k**3 - (k-1)**3
ans += appe
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)

import itertools
a=int(input())
A=[]
for i in range(a):
    A.append(str(i+1))

li=list(itertools.combinations_with_replacement(A, 3))
s = 0
for lis in li:
    a = int(lis[0])
    b = int(lis[1])
    c = int(lis[2])
    
    if math.gcd(b,c) == 1:
        gcd1 = 1
    else:
        gcd1 = gcd(a,b,c)
    
    if a==b and b==c:
        s = s + gcd1
    elif a==b or b==c:
        s = s + 3*gcd1
    else:
        s = s + 6*gcd1
print(s)

Solution (Python):
from fractions import gcd
 
def main():
  k = int(input())
  ans = 0
  for a in range(1, k+1):
    for b in range(a, k+1):
      x = gcd(a,b)
      for c in range(b,k+1):
        if a==b and b==c:
          ans += a
        elif a==b or b==c:
          ans += gcd(x,c) * 3
        else:
          ans += gcd(x,c) * 6
  print(ans)
  
  
if __name__ == '__main__':
  main()

Solution (Python):
import math
from functools import reduce

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

import sys
K=input()
K=int(K)
S=0
a=1
b=1
c=1
for i in range(K):
    for j in range(K):
        if gcd(a,b)==1:
            S+=K
        else:
            S+=sum([gcd(a,b,c) for c in range(1,K+1)])
        c=1
        b=b+1
    b=1
    c=1
    a=a+1

print(S)

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

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


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


for i in range(1,K-1,1):
    for j in range(i+1,K,1):
        kari = math.gcd(i,j)
        for h in range(j+1,K+1,1):
            ans += 6*math.gcd(kari,h)

print(ans)

Solution (Python):
import numpy as np
from itertools import combinations

def main():
    k = int(input())
    res = 0
    memo = np.zeros((k+1, k+1), int)
    for i in range(1, k+1):
        for j in range(i, k+1):
            tmp = np.gcd(i,j)
            memo[i][j] = tmp
            memo[j][i] = tmp

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

    for c in combinations(range(1,k+1),2):
        ab = memo[c[0], c[1]]
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = memo[c[0], c[1]]
        abc = memo[ab, c[2]]
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()