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



Sample input:
2


Sample output:
9

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcdli(xl):
    #xlはlist
    return reduce(math.gcd , xl)

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

def c162(k):

    count = 0
    li = []
    for i in range(1 , k + 1):
        for w in range(1 , k + 1):
            for q in range(1 , k + 1):
                li = [i , w , q]
                count += gcdli(li)
    """
    for i in range(1 , k + 1):
        for w in range(1 , k + 1):
            count += gcd(i , w)
    """
    return count

def main():
    k = int(input())
    print(c162(k))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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


k = int(input())

ans = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        ab = GCD(a, b)
        if ab == 1:
            ans += k
            continue
        for c in range(1, k + 1):
            ans += GCD(ab, c)

print(ans)

Submission status: Accepted

Solution code:
from functools import reduce

# from collections import defaultdict


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


def gcddd(*numbers):
    return reduce(gcd, numbers)


def reducef(x, y):
    return gcdd[(x, y)]


def gcd2(*numbers):
    return reduce(reducef, numbers)


K = int(input())
ans = 0
# gcdd=defaultdict()
gcdd = {}
for a in range(1, K + 1):
    for b in range(1, K + 1):
        if (a, b) not in gcdd:
            gcdd[(a, b)] = gcddd(a, b)

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

Submission status: Accepted

Solution code:
import math
N = int(input())
all_gcd = []
for i in range(1, N+1):
  for j in range(1, N+1):
    x = math.gcd(i, j) if i != j else i
    for m in range(1, N+1):
      y = math.gcd(x, m) if x != m else x
      all_gcd.append(y)

print(sum(all_gcd))

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

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

Submission status: Accepted

Solution code:
import math
k=int(input())
A=[[0 for i in range(k+1)] for j in range(k+1)]
s2=0
for i in range(1,k+1):
    for j in range(1,k+1):
        if i!=j:
            s2=s2+math.gcd(i,j)
            A[i][j]=math.gcd(i,j)
s1=0
for i in range(1,k-1):
    for j in range(i+1,k):
        for l in range(j+1,k+1):
            s1=s1+A[A[i][j]][l]

print(6*s1+3*s2+(k*(k+1))//2)

Submission status: Accepted

Solution code:
import functools
from math import gcd
k = int(input())
cnt = 0
for i in range(1,k+1):
    for j in range(i,k+1):
        for l in range(j,k+1):
            if i == j and j == l:
                cnt += i
            elif i == j and j != l:
                li = [i,l]
                cnt += functools.reduce(gcd,li)*3
            elif i != j and j == l:
                li = [i,j]
                cnt += functools.reduce(gcd,li)*3
            else:
                li = [i,j,l]
                cnt += functools.reduce(gcd,li)*6
print(cnt)

Submission status: Accepted

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

ans=0
for i in range(k):
    ans+=i+1
    for j in range(i+1, k):
        x=gcd(i+1,j+1)
        ans+=6*gcd(i+1,j+1)
        for l in range(j+1, k):
            ans+=6*gcd(x,l+1)
print(ans)

Submission status: Accepted

Solution code:
def main():
    from math import gcd
    k=int(input())
    ans=0
    for a in range(1,k+1):
        for b in range(1,k+1):
            g=gcd(a,b)
            for c in range(1,k+1):
                ans+=gcd(g,c)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from fractions import gcd

K = int(input())

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


for a in range(1,K+1):
    if L[a] == 0:
        continue
    for c in range(1,K+1):
        ans += L[a]*gcd(a,c)

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

k = int(input())

import functools
from fractions import gcd

cache = dict()

ab = []
for a in range(1, k + 1):
    for b in range(1, k + 1):
        i1, i2 = min(a, b), max(a, b)
        if (i1, i2) in cache:
            ab.append(cache[(i1, i2)])
            continue
        v = gcd(i1, i2)
        cache[(i1, i2)] = v
        ab.append(v)

ans = 0
for d in ab:
    for c in range(1, k + 1):
        i1, i2 = min(d, c), max(d, c)
        if (i1, i2) in cache:
            ans += cache[(i1, i2)]
            continue
        v = gcd(i1, i2)
        cache[(i1, i2)] = v
        ans += v

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

import math

# input
K = int(input())

# initialization
ans = 0

# count
for a in range(1, K+1):
    for b in range(1, K+1):
        tmp = math.gcd(a, b)
        for c in range(1, K+1):
            tmp_1 = math.gcd(tmp, c)
            ans += tmp_1

# output
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
K = int(input())

def Gcd(x, y): #a>=b>=c
    if x < y:
        x, y = y, x
    a = x
    b = y
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b

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

for a in range(1, K + 1):
    for b in range(1, a):
        sum += Gcd(a, b) * 6

for a in range(1, K + 1):
    for b in range(1, a):
        for c in range(1, b):
            sum += Gcd(Gcd(a, b), c) * 6
print(sum)

Submission status: Accepted

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

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


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

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

Submission status: Accepted

Solution code:
def gcd(m, n):
    while n:
        m, n = n, m % n
    return m

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
def gcd(*nums):
    return reduce(math.gcd, nums)

K = int(input())
s = 0

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

Submission status: Accepted

Solution code:
import itertools
import math
from functools import reduce

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

k = int(input())
l = list(range(1, k+1))                                                
s = 0

for i in itertools.product(l, repeat=3):
  s += gcd(*i)
print(s)

Submission status: Accepted

Solution code:
import math

K = int(input())

sum = 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:
                sum += i

            else:
                ans = i
                ans = math.gcd(ans,j)
                ans = math.gcd(ans,k)

                if i!=j and j!=k and k!=i:
                    sum += 6*ans
                else:
                    sum += 3*ans
print(sum)

Submission status: Accepted

Solution code:
def main():
    import math
    x = int(input())
    a = 0
    for i in range(x):
        for j in range(x):
            for k in range(x):
                a += math.gcd(math.gcd(i+1, j+1), k+1)
    print(a)

main()

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

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



N = int(input())
ans = 0

for i in range(1, N+1):
    for j in range(i, N+1):
        for k in range(j, N+1):
            n = len(set([i, j, k]))
            if n == 3:
                ans += gcd(i, j, k) * 6
            elif n == 2:
                ans += gcd(i, j, k) * 3
            else:
                ans += gcd(i, j, k)
print(ans)

Submission status: Accepted

Solution code:
def euclid(a, b):
    while b:
        a, b = b, a%b
    return a
  
K = int(input())

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:
        kake = 1
      elif i == j or j == k or i == k:
        kake = 3
      else:
        kake = 6
      count += euclid(euclid(i, j), k) * kake

print(count)

Submission status: Accepted

Solution code:
import itertools
import math

K = int(input())


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


print(sum(map(lambda x: _gcd(*x), itertools.product(range(1, K + 1), repeat=3))))

Submission status: Accepted

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

ans = 0
for a in range(1, k+1):
  for b in range(1, k+1):
      m = math.gcd(a, b)
      for c in range(1, k+1):
          ans += math.gcd(m, c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())
gcd_list = []
for i in range(1, k+1):
    for j in range(1, k+1):
        gcd_list.append(math.gcd(i, j))

sum_num = 0
for i in gcd_list:
    for j in range(1, k+1):
        sum_num += math.gcd(i, j)
print(sum_num)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
K=int(input())

cache = [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, 
10962213]

print(cache[K-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=[0,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(s[int(input())])

Submission status: Accepted

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

K = int(input())
ans=0
lst = [0]*201
for i in range(1,K+1):
  for j in range(1,K+1):
    lst[gcd(i,j)]+=1
for i in range(1,K+1):
  for j in range(1,K+1):
    ans += lst[i]*gcd(i,j)
print(ans)

Submission status: Accepted

Solution code:
import math
import random
import sys
import heapq
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, UserString
from functools import lru_cache, cmp_to_key, reduce
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations, product)

sys.setrecursionlimit(10**7)
MAX_INT = 2**62-1

input = sys.stdin.buffer.readline

@lru_cache(None)
def gcd(a, b):
    if not b:
        return a
    return gcd(b, a % b)

if __name__ == "__main__":
    n = int(input())
    ans = (n + 1) * n // 2

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

Submission status: Accepted

Solution code:

from math import gcd

k=int(input())

kij=dict()

for i in range(1,k+1):
    for j in range(1,k+1):
        gij=gcd(i,j)
        if gij in kij:
            kij[gij]=kij[gij]+1
        else:
            kij[gij]=1

isum=0
for kk in kij.items():
    for m in range(1,k+1):
        isum=isum+gcd(m,kk[0])*kk[1]

print(isum)

Submission status: Accepted

Solution code:
import math

k = int(input())

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math

def main():

    K = int(input())
    K_list = list(range(1, K+1))
    sum = 0

    combi=  list(itertools.combinations_with_replacement(K_list, 3))

    for tup_ in combi:
        
        dup = len(set(tup_))
        if dup == 3:
            coef = 6
        elif dup == 2:
            coef = 3
        else:
            coef = 1
        i, j, k = tup_
        sum += coef * math.gcd(math.gcd(i,j), k)

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

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
counter = 0
dic_gcd = {}

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

Submission status: Accepted

Solution code:
import math
def gcdf(a, b):
    if b == 0:
        return a
    return gcdf(b, a % b)
memo = {}
k = int(input())
ans = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            #print(i, j, l)
            if (i, j) not in memo.keys():
                memo[(i, j)] = gcdf(i, j)
                gcd = memo[i, j]
            else:
                gcd = memo[(i, j)]
            if (gcd, l) not in memo.keys():
                memo[(gcd, l)] = gcdf(gcd, l)
                ans += memo[(gcd, l)]
            else:
                ans += memo[(gcd, l)]
print(ans)

Submission status: Accepted

Solution code:
result = 0

def gcdAB(A, B):
    if(B == 0):
        return A
    return gcdAB(B, A%B)

def gcdABC(A, B, C):
    ABgcd = gcdAB(A, B)
    return gcdAB(ABgcd, C)


K = int(input())
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            result += gcdABC(a,b,c)
            
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
n = int(input())
 
 
sum = 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)
            tmp2 = math.gcd(j,k)
            tmp3 = math.gcd(tmp,tmp2)
            sum += tmp3
print(sum)

Submission status: Accepted

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

k = int(input())

ans_1 = 0
# pattern1: 'aaa'
for a in range(1, k + 1):
    ans_1 += a

# pattern2: 'aab' or 'abb'
ans_2 = 0
for a in range(1, k):
    for b in range(a + 1, k + 1):
        ans_2 += gcd(a, b)
ans_2 *= 6

# pattern3: 'abc'
ans_3 = 0
for a in range(1, k - 1):
    for b in range(a + 1, k):
        for c in range(b + 1, k + 1):
            d = gcd(a, b)
            ans_3 += gcd(c, d)
ans_3 *= 6

ans = ans_1 + ans_2 + ans_3
print(ans)

Submission status: Accepted

Solution code:
import math
K = int(input())
N = list()
ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        N.append(math.gcd(a, b))

for n in N:
    for c in range(1, K+1):
        ans += math.gcd(n, c)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math

def solve(K: int):
    answer = 0
    GCDLIST = [[1]*(K+1) for _ in range(K+1)]

    for i in range(1,K+1):
        for j in range(1,K+1):
            g = math.gcd(i,j)
            GCDLIST[i][j] = g
            GCDLIST[j][i] = g
    
    for i in range(1,K+1):
        for j in range(1,K+1): 
            for k in range(1,K+1):
                answer += GCDLIST[GCDLIST[i][j]][k]
    
    print(answer)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    solve(K)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

GCD = [[1 for x in range(K+1)] for y in range(K+1)]

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

# print(GCD)

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)

Submission status: Accepted

Solution code:
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):
            j = math.gcd(c,b)
            ans += math.gcd(a,j)

print(ans)

Submission status: Accepted

Solution code:
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 j in range(1,k+1):
        for l in range(1,k+1):
            ans += gcd(i,j,l)
 
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

k = int(input())
ans = 0

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

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

print(ans + k * (k + 1) // 2)

Submission status: Accepted

Solution code:
import math
import functools

k = int(input())
sum = 0

for i in range(1, k+1):
  for j in range(1, k+1):
    for l in range(1, k+1):
      sum += functools.reduce(math.gcd, (i, j, l))
print(sum)

Submission status: Accepted

Solution code:
import math

K=int(input())

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

k = int(input())
sum = 0

for a in range(k):
    for b in range(k):
        aandb = math.gcd(a+1, b+1)
        for c in range(k):
            sum += math.gcd(aandb, c+1)

print(sum)

Submission status: Accepted

Solution code:
import math

K = int(input())

gcd = [[[0]* K for _ in range(K)] for _ in range(K)]

ans = 0
for a in range(K):
    for b in range(K):
        for c in range(K):
            if gcd[a][b][c] == 0:
                res = math.gcd(math.gcd(a+1,b+1),c+1)
                gcd[a][b][c] = res
                gcd[b][c][a] = res
                gcd[c][a][b] = res
                gcd[b][a][c] = res
                gcd[c][b][a] = res
                gcd[a][c][b] = res
                ans += res
            else:
                ans += gcd[a][b][c]

print(ans)

Submission status: Accepted

Solution code:
import math
K = int(input())
result = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        tmp = math.gcd(i,j)
        for k in range(1,K+1):
            result += math.gcd(tmp,k)
print(result)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())

ans = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for l in range(1, K+1):
            ans += gcd_get(i, j, l)
print(ans)

Submission status: Accepted

Solution code:
from math import *
g=[[0]*202 for _ in range(202)]
for i in range(1,201):
    for j in range(1,201):
        g[i][j]=gcd(i,j)
n=int(input())
a=0
for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            a+=g[g[i][j]][k]
print(a)

Submission status: Accepted

Solution code:
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 k in range(1, K + 1):
            ans += math.gcd(k, tmp)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

# import time
# start = time.time()

import math
from functools import reduce



def gcd(*numbers):
    return reduce(math.gcd, numbers)
ans = 0
for a in range(1,N+1):
    for b in range(1,N+1):
        for c in range(1,N+1):
            ans+=gcd(a,b,c)
print(ans)

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

k = ii()

#a>b
dp = [[0]*201 for _ in range(201)]
def gcd(a,b):
    if a < b:
        a,b = b,a
    if dp[a][b] != 0:
        return dp[a][b]
    while b!=0:
        a,b=b,a%b
    dp[a][b] = a
    return a

def gcds(a,b,c):
    d = gcd(a,b)
    e = gcd(d,c)
    return e
ans = 0
for i in range(1,k+1):
    for l in range(1,k+1):
        for t in range(1,k+1):
            ans += gcds(i,l,t)
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k=int(input())
l=[]
for a in range(1,k):
    for b in range(a+1,k+1):
        l.append(gcd(a,b))
s=2*sum(l)+(k*(k+1))//2
for m in l:
    for c in range(1,k+1):
        s=s+2*gcd(c,m)
print(s)

Submission status: Accepted

Solution code:
from functools import reduce
from math import gcd

#a = [12, 18, 24]
#b = reduce(gcd, a)


K = int(input())
cnt = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            a = [i,j,k]
            b = reduce(gcd,a)
            cnt = cnt+b
print(cnt)

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest
import math
from functools import reduce

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

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #n,m,k=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    k=int(readline())
    #ss=readline().rstrip()
    #yn(1)
    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)



    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


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


def main():
    K = si()
    ans = 0
    q = [0]*201
    for i in range(1, K+1):
        for j in range(1, K+1):
            q[gcd(i, j)] += 1

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math

def solve(K: int):
    sum = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            for c in range(1, K + 1):
                sum += math.gcd(math.gcd(a,b), c)

    print(sum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    solve(K)

if __name__ == '__main__':
    main()