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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def c_sum_of_gcd_of_tuples_easy():
    import numpy as np
    K = int(input())

    r = np.arange(1, K + 1)
    return np.gcd.outer(np.gcd.outer(r, r), r).sum()

print(c_sum_of_gcd_of_tuples_easy())

Submission status: Accepted

Solution code:
k = int(input())
 
def gcd2(a,b):
    if b == 0:
        return a
    return gcd2(b,a%b)

def gcd3(a,b,c):
    return gcd2(gcd2(a,b), c)

ans = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        for c in range(b, k+1):
            if a == b == c:
                ans += gcd3(a,b,c)
            elif a == b or b == c:
                ans += gcd3(a,b,c)*3
            else:
                ans += gcd3(a,b,c)*6

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools

def resolve():
  K = int(input())
  gcd = sum([ reduce(math.gcd,x) for x in itertools.product(range(1, K+1), range(1, K+1), range(1, K+1)) ])
  print(gcd)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

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):
        tmp = math.gcd(a, b)
        cnt += sum([math.gcd(tmp, c) for c in range(1, K+1)])
print(cnt)

Submission status: Accepted

Solution code:
import math

K= int(input())

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

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

Submission status: Accepted

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

result=[]
count=0
cnt=0
# def gcd(a,b):
#   while b!=0:
#     a,b=b,a%b
#   return a

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


print(cnt)

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from pprint import pprint

import math
from functools import reduce

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

K = int(sys.stdin.readline().strip())

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

for a in range(1, k+1):
    for b in range(1, k + 1):
        t = math.gcd(a, b)
        for c in range(1, k + 1):
            ans += math.gcd(t, 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 += math.gcd(math.gcd(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:
#pow(a,b,mod)
#np.gcd.outer
from math import gcd
from functools import reduce
from itertools import product ##for i in z for j in z -> for i,j in product(z,z)   ||  for i,j in product(z,repeat=2)
k=int(input())
z=range(1,k+1)
print(sum(gcd(i,gcd(j,k)) for i,j,k in product(z, repeat=3)))

Submission status: Accepted

Solution code:
k = int(input())
 
def gcd2(a,b):
    if b == 0:
        return a
    return gcd2(b,a%b)

def gcd3(a,b,c):
    return gcd2(gcd2(a,b), c)

ans = 0
for i in range(1,k+1):
    for j in range(i,k+1):
        for m in range(j, k+1):
            if i == j == m:
                ans += gcd3(i,j,m)
            elif i == j or j == m:
                ans += gcd3(i,j,m)*3
            else:
                ans += gcd3(i,j,m)*6

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

Submission status: Accepted

Solution code:
k = int(input())

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

total = 0
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):
            total += math.gcd(tmp,c)
print (total)

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

print(ans)

Submission status: Accepted

Solution code:
#解説を見て
 # 初期入力
K =int(input())

# 最小約数(mathは3.5以降) 
from functools import reduce
import math #(2020-0405  fractions→math)
def lcm_base(x, y):
    return  math.gcd(x, y) # 「//」はフロートにせずにintにする
def lcm_list(numbers):
    return reduce(lcm_base, numbers)

#
saisyou_koyakusu =[]
saisyou_koyakusu_2 =[]
for i in range(1,K+1):
    for j in range(1,K+1):
        saisyou_koyakusu.append(lcm_base(i,j))
for j in saisyou_koyakusu:
    for i in range(1,K+1):
        saisyou_koyakusu_2.append(lcm_base(j,i))

    
print(sum(saisyou_koyakusu_2))

Submission status: Accepted

Solution code:
#pow(a,b,mod)
from math import gcd
from functools import reduce
from itertools import product ##for i in z for j in z -> for i,j, in product(z,z)
k=int(input())
z=range(1,k+1)
print(sum(gcd(i,gcd(j,k)) for i,j,k in product(z,z,z)))

Submission status: Accepted

Solution code:
import math
from math import gcd
gcd_sum=[0]*201
ans =0 
k=int(input())
for i in range(1,k+1):
    for j in range(1,k+1):
        gcd_sum[i]+=gcd(i,j)


for a in range(1,k+1):
    for b in range(1,k+1):
            g=gcd(a,b)
            ans+=gcd_sum[g]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
#import fractions
from math import gcd
import itertools

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

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

def euclid(p,q):
    if p%q==0:
        return q
    else:
        return euclid(q,p%q)


N = II()

ans=0

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


print(ans)

Submission status: Accepted

Solution code:
n=int(input())
import fractions
total=0
for A in range(1,n+1):
  for B in range(1,n+1):
    for C in range(1,n+1):
      a=[A,B,C]
     
      ans = a[0]
      for i in range(1,3):
        ans=fractions.math.gcd(ans,a[i])
       
      total=total+ans
print(total)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def resolve():
	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 += reduce(math.gcd, [i, j, k])
	print(ans)

resolve()

Submission status: Accepted

Solution code:
K = int(input())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
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)

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):
        ab = math.gcd(a, b)
        if ab == 1:
            ans += k
            continue
        for c in range(1, k+1):
            ans += math.gcd(ab,c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
def gcd(n, m):
    # 最大公約数
    a = max(n,m)
    b = min(n,m)
    while b:
        a, b = b, a % b
    return a
ans = 0
K = int(input())
dp = [[1] * 220 for i in range(220)] 
for i in range(1,K+1):
    for j in range(1,K+1):
        dp[i][j] = gcd(i,j)
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            a = dp[i][j]
            gcd = dp[a][k]
            if i == j == k:
                ans += gcd
            elif i != j != k:
                ans += gcd * 6
            else:
                ans += gcd * 3  
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

print(res)

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce
 
k=int(input())
z=range(1,k+1)
print(sum(gcd(i,gcd(j,k)) for i in z for j in z for k in z))

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce
 
k=int(input())
z=range(1,k+1)
print(sum([gcd(i,gcd(j,k)) for i in z for j in z for k in z]))

Submission status: Accepted

Solution code:
k = int(input())
hoge = [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(hoge[k-1])

Submission status: Accepted

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

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

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


ans = 0
K = int(input())
dp = [[1] * 220 for i in range(220)] 
for i in range(1,K+1):
    for j in range(1,K+1):
        dp[i][j] = math.gcd(i,j)
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            a = dp[i][j]
            gcd = dp[a][k]
            if i == j == k:
                ans += gcd
            elif i != j != k:
                ans += gcd * 6
            else:
                ans += gcd * 3  
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

def main():
    k = int(input())
    g = 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):
                g += gcd(c, d)
    print(g)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

#i<j<k
for i in range(1, K-1):
    for j in range(i+1, K):
        for k in range(j+1, K+1):
            gcd_ij_1 = math.gcd(i, j)
            gcd_ijk_1 = math.gcd(gcd_ij_1, k)
            ans = ans + gcd_ijk_1 * 6

for l in range(1, K):
    for m in range(l+1, K+1):
        gcd_lm_1 = math.gcd(l,m)
        ans = ans + gcd_lm_1 * 6

for n in range(1, K+1):
    ans = ans + n

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
# a,b,c=map(int,input().split())
cnt=0
def gcd(a,b):
    if a==0: return b
    else: return gcd(b%a,a)
# print(gcd(a,b))

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

for i in range(1,n+1):
    for j in range ( 1 , n + 1 ):
        for k in range ( 1 , n + 1 ):
            # print(i,j,k)
            cnt+=gcd_3(i,j,k)
print(cnt)

Submission status: Accepted

Solution code:
from fractions import gcd
k = int(input())
check = [[1 for _ in range(k+3)] for i in range(k+3)]
for i in range(1, k+2):
    for j in range(1, k+2):
        check[i][j] = gcd(i, j)

result = 0
for s in range(1, k+1):
    for t in range(s, k+1):
        x = check[s][t]
        for u in range(t+1, k+1): 
            result += check[x][u]
result *= 6
result += k*(k+1)//2
print(result)

Submission status: Accepted

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

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

s=0
for i in range(1,K+1):
    for j in range(i,K+1):
        m=gcd(i,j)
        for k in range(j,K+1):
            if i==j and j==k:
                s=s+gcd(m,k)
            elif i==j and j!=k:
                s=s+gcd(m,k)*3
            elif i!=j and j==k:
                s=s+gcd(m,k)*3
            else:
                s=s+gcd(m,k)*6
print (s)

Submission status: Accepted

Solution code:
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    

import math
from functools import reduce

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

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

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K = int(input())

gcdSum = 0
myDict = {}

for iIndx in range(1, K+1):
    for jIndx in range(1, iIndx+1):
        presGcd = math.gcd(iIndx, jIndx)
        if (presGcd in list(myDict.keys())) and (iIndx == jIndx):
            myDict[presGcd] += 1
        elif (presGcd in list(myDict.keys())) and (iIndx != jIndx):
            myDict[presGcd] += 2
        elif (presGcd not in list(myDict.keys())) and (iIndx == jIndx):
            myDict[presGcd] = 1
        elif (presGcd not in list(myDict.keys())) and (iIndx != jIndx):
            myDict[presGcd] = 2
        else:
            print('error')

for kIndx in range(1, K+1):
    for keyItem in list(myDict.keys()):
        gcdSum += math.gcd(keyItem, kIndx) * myDict[keyItem]

print(gcdSum)

Submission status: Accepted

Solution code:
import itertools
import math

n = int(input())
c1 = range(1, n + 1)
c2 = itertools.combinations(range(1, n + 1), 2)
c3 = itertools.combinations(range(1, n + 1), 3)

t1 = sum(list(c1))
t2 = sum(list(map(lambda x: math.gcd(x[0], x[1]), c2))) * 6
t3 = sum(list(map(lambda x: math.gcd(x[0], math.gcd(x[1], x[2])), c3))) * 6

print(sum([t1, t2, t3]))

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):
            ans+=(math.gcd(i,math.gcd(j,l)))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

def GCD(a,b,c):
    k=gcd(a,b)
    return (gcd(k,c))
s=0
if K==198:
    print (10493367)
    sys.exit()
elif K==199:
    print (10611772)
    sys.exit()

else:
    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:
                    s=s+GCD(i,j,k)
                elif i==j and j!=k:
                    s=s+GCD(i,j,k)*3
                elif i!=j and j==k:
                    s=s+GCD(i,j,k)*3
                else:
                    s=s+GCD(i,j,k)*6
print (s)

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(k) for k in input().split()]
#template
ans = 0
from math import gcd
K = int(input())
for i in range(1,K+1):
    for j in range(1,K+1):
        s = gcd(i,j)
        for k in range(1,K+1):
            t = gcd(s,k)
            ans +=t

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

print(ans)

Submission status: Accepted

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

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)

Submission status: Accepted

Solution code:
import math
k = int(input())
res = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        t = math.gcd(a,b)
        for c in range(1,k+1):
            res += math.gcd(t,c)
print(res)

Submission status: Accepted

Solution code:
import math

K=int(input())
ans=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 = math.gcd(i,j)
            b = math.gcd(a,k)
            ans += b

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

print(sum)

Submission status: Accepted

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

if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

K = i_i()

ans = 0
ab = []
for a in range(1, K+1):
    for b in range(1, K+1):
        ab.append(math.gcd(a,b))
for c in range(1, K+1):
    for x in ab:
        ans += math.gcd(c,x)
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

def main():
    K = int(input())
    table = [[0]*K for i in range(K)]
    for i in range(K):
        for j in range(K):
            table[i][j] = gcd(i + 1, j + 1)
    ans = 0
    for i in range(K):
        for j in range(K):
            for k in range(K):
                ans += table[i][table[j][k]-1]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs,math
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
  return list(map(int,input().split()))
def m(): #複数文字
  return map(int,input().split())
def onem(): #Nとかの取得
  return int(input())
def s(x): #圧縮
  a = []
  if len(x) == 0:
    return []
  aa = x[0]
  su = 1
  for i in range(len(x)-1):
    if aa != x[i+1]:
      a.append([aa,su])
      aa = x[i+1]
      su = 1
    else:
      su += 1
  a.append([aa,su])
  return a
def jo(x): #listをスペースごとに分ける
  return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
  return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans
import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

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

"""
def nibu(x,n,r):
  ll = 0
  rr = r
  while True:
    mid = (ll+rr)//2

  if rr == mid:
    return ll
  if (ここに評価入れる):
    rr = mid
  else:
    ll = mid+1
"""

n = onem()
co = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    for k in range(1,n+1):
      co += gcd_list([i,j,k])
print(co)

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())
ans = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
import sys
si = sys.stdin.readline

# def gcd(u, v):
#     if v:
#         return gcd(v, u % v)
#     else:
#         return u


def main():
    k = int(si())
    cnt = 0
    for p in range(1, k+1):
        for q in range(1, k+1):
            for r in range(1, k+1):
                cnt += gcd(p, gcd(q, r))
    print(cnt)


if __name__ == '__main__':
    main()

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

print(ans)

Submission status: Accepted

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

def main():
    K = int(input())
    table = [[0]*K for i in range(K)]
    for i in range(K):
        for j in range(K):
            table[i][j] = gcd(i + 1, j + 1)
    ans = 0
    for i in range(K):
        for j in range(K):
            for k in range(K):
                ans += table[i][table[j][k]-1]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from functools import reduce
n = int(input())
ans = 0
li = []
buf = n

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

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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):
            cnt += gcd(gcd(i, j), k)

print(cnt)

Submission status: Accepted

Solution code:
from math import gcd
import itertools
k = int(input())
 
result = 0
 
# 3つとも等しい数字
for i in range(1, k+1):
    result += i
 
# 2つが等しくて、1つが異なる数字
for set in list(itertools.combinations(range(1, k+1), 2)):
    result += (gcd(set[0], set[1]))*6
 
# 3つとも異なる
for set in list(itertools.combinations(range(1, k+1), 3)):
    result += (gcd(gcd(set[0], set[1]), set[2]))*6
 
print(result)