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:
# -*- coding: utf-8 -*-
a = int(input())

#リストgcd
def list_gcd(l):
    from math import gcd
    if len(l)==1:
        raise
    else:
        ret = l[0]
        for i in l[1::]:
            ret = gcd(ret,i)
            if ret==1:
                break
    return ret

ret = 0
ret_1 = 0
ret_2 = 0
for i in range(1,a+1):
    ret_1+=i
for i in range(1,a+1):
    for j in range(1,a+1):
        if i!=j:
            ret_2+=(list_gcd([i,j]))*3
for i in range(1,a+1):
    for j in range(i+1,a+1):
        for k in range(j+1,a+1):
            ret+=list_gcd([i,j,k])
ret*=6
ret+=(ret_1+ret_2)
print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
K = int(input())
l = list(range(1,K+1))
c = itertools.combinations_with_replacement(l, 3)
import math
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
result = 0
for i in c:
    if i[0] == i[1] and i[2] == i[1]:
        result += gcd(*i)
    elif i[0] == i[1] or i[2] == i[1] or i[0] == i[2]:
        result += (gcd(*i)*3)
    else:
        result += (gcd(*i)*6)
print(result)

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 main():
    from math import gcd
    from itertools import product
    k = int(input())
    l = range(1,k+1)
    ans = k*(k+1)//2
    for i,j,k in product(l,l,l):
        if i==j==k:
            continue
        else:
            g = gcd(i,j)
            g = gcd(g,k)
            ans += g

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
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(fractions.gcd, numbers)

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

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

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)
"""
n = onem()

s = input()[:-1]

R = [0 for _ in range(n)]
B = [0 for _ in range(n)]
G = [0 for _ in range(n)]

co = 0

if s[0] == "R":
  R[0] = 1
elif s[0] == "G":
  G[0] = 1
else:
  B[0] = 1

for i in range(1,n):
  if s[i] == "R":
    R[i] += 1
  elif s[i] == "G":
    G[i] += 1
  else:
    B[i] += 1
  R[i] += R[i-1]
  G[i] += G[i-1]
  B[i] += B[i-1]

for i in range(1,n-1):
  if s[i] == "G":
    for j in range(i):
      if s[j] == "R":
        co += B[-1] - B[i] - (1 if (j + (j - i) < n) and s[j + (j - i)] == "B" else 0)
print(co)

"""
n = onem()



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[n-1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math


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


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
import math

k = int(input())

ans = 0
res = 0


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

print(ans)

Submission status: Accepted

Solution code:
import math

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

print(ans)

Submission status: Accepted

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

ans = 0

k = int(input())
for i in range(1,k+1):
	for j in range(1,k+1):
		ab = gcd(i,j)
		if ab == 1:
			ans += k
		else:
			for l in range(1,k+1):
				abc = gcd(ab,l)
				ans += abc
print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())

CACHE = {}
def gcd_with_memo(i, j):
    if j > i:
        i, j = j, i
    key = str(i) + ":" + str(j)
    if CACHE.get(key):
        return CACHE[key]
    else:
        CACHE[key] = math.gcd(i, j)
        return CACHE[key]

total = 0
r = range(1, K + 1)
for i in r:
    for j in r:
        for k in r:
            total += gcd_with_memo(gcd_with_memo(i, j), k)

print(total)

Submission status: Accepted

Solution code:
K = int(input())
count = 0
def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b
for i in range(1, K+1):
    for j in range(1, K+1):
        ijgcd = gcd(i, j)
        for k in range(1, K+1):
            count += gcd(ijgcd,k)
print(count)

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

Submission status: Accepted

Solution code:
import math
ans=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):
      g1=math.gcd(i,j)
      g=math.gcd(g1,k)
      if i==j==k:
        n=1
      elif i<j<k:
        n=6
      else:
        n=3
      ans+=g*n
print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
import math
from functools import reduce

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

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

    print(ans)

if __name__ == '__main__':
    resolve()

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 l in range(1,k+1):
            b = math.gcd(a,l)
            ans += b
print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())

ans = 0

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

Submission status: Accepted

Solution code:
import math
import itertools

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

ans = 0

add = (0, 1, 3, 6)

for c in itertools.combinations_with_replacement(l, 3):
    ans += math.gcd(math.gcd(c[0], c[1]), c[2]) * add[len(set(c))]
    
print(ans)

Submission status: Accepted

Solution code:
import math

K=int(input())
out=0
for i in range(1,K+1):
    for ii in range(1,K+1):
        tmp=math.gcd(i,ii)
        for iii in range(1,K+1):
            out+=math.gcd(tmp,iii)

print(out)

Submission status: Accepted

Solution code:
from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from fractions import gcd
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix

# strlist = "abcdefghijklmnopqrstuvwxyz"
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):
            num = gcd(i,j)
            num = gcd(num,k)
            ans += num

print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
# C - Sum of GCD of Tuples (Easy)

k = int(input())

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

s = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        s += gcd(a, b)
        for c in range(b + 1, k + 1):
            s += gcd(gcd(a, b), c) * 2
print(s)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix

from math import gcd


def solv(K):
    ans = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                ans += gcd(a, gcd(b, c))
    return ans

if __name__ == "__main__":

    # K
    #
    K = int(input())
    ans = solv(K)

    print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
from math import gcd

def solve():
    K = int(input())
    ans = 0

    # 3つ同じ値のとき
    for i in range(1, K+1):
        ans += i

    # 2つ同じ値のとき
    for i in range(1, K+1):
        for j in range(1, K+1):
            if i==j: continue
            ans += gcd(i, j)*3

    # すべて異なる値のとき
    for i in range(1, K+1):
        for j in range(i+1, K+1):
            for k in range(j+1, K+1):
                ans += gcd(gcd(i,j),k)*6

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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


def main():
    K = int(input())
    result = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            ab = gcd(a, b)
            for c in range(1, K + 1):
                tmp = gcd(ab, c)
                result += tmp

    print(result)


if __name__ == "__main__":
    main()

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

print(ans)

Submission status: Accepted

Solution code:
import math


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


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


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


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    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)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
K = int(input())


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


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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
def solve(i,j,k):
    return gcd(gcd(i,j),k)
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+=solve(i,j,k)
print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())

lists = []
for i in range(1, K + 1):
    for j in range(1, K + 1):
        mid = math.gcd(i, j)
        for k in range(1, K + 1):
            lists.append(math.gcd(mid, k))

print(sum(lists))

Submission status: Accepted

Solution code:
from math import gcd

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

main()

Submission status: Accepted

Solution code:
import math
from itertools import combinations as com


k = int(input())
num = [i for i in range(1, k+1)]
cnt = sum(num)

for a,b in com(num, 2):
    cnt += 6 * math.gcd(a, b)

for a,b,c in com(num, 3):
    cnt += 6*math.gcd(c,math.gcd(a, b))

print(cnt)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
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,b,c)
print(s)

Submission status: Accepted

Solution code:
import math

K = int(input())
count = 0

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

for k in range(1, K+1):
    for j in range(1, K+1):
        temp = A[k][j]
        for i in range(1, K+1):
            count += A[temp][i]
print(count)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
s = 0
for x in range(1, k + 1):
    for y in range(1, k + 1):
        t = gcd(x, y)
        if t == 1:
            s += k
            continue
        for z in range(1, k + 1):
            s += gcd(t, z)
print(s)

Submission status: Accepted

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


def solve(K: int):
    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 and b != c:
                    ans += 6 * math.gcd(a,math.gcd(b,c))
                elif a != c:
                    ans += 3 * math.gcd(a,math.gcd(b,c))
                else:
                    ans += math.gcd(a,math.gcd(b,c))
    print(ans)


    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
def count(i,j,k):
    val= math.gcd(i,math.gcd(j,k))

    if i==j==k:
        return val
    else:
        if i==j or i==k or j==k:
            return 3*val
        else:
            return 6*val
n=int(input())+1
sum=0
for i in range(1,n):
    for j in range(i,n):
        for k in range(j,n):
            sum+=count(i,j,k)
print(sum)

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

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 sys import stdin, stdout
from math import gcd
def main():
    K = int(stdin.readline())
    sm = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            for k in range(1, K+1):
                sm += gcd(i, gcd(j, k))
    stdout.write(str(sm)+"\n")
main()

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())

SUM = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        tmp = math.gcd(i,j)
        for k in range(1,K+1):
            if not i == j:
                SUM += math.gcd(tmp,k) * 2
            else:
                SUM += math.gcd(tmp,k)


print(SUM)

Submission status: Accepted

Solution code:
def GCD(a, b):
    r = a % b
    while(r!=0):
        a = b
        b = r
        r = a % b
    
    return b

def main():
    ans = 0 
    K = int(input())
    for i in range(1, K+1):
        for j in range(1, K+1):
            if(i < j):
                gcd1 = GCD(j, i)
            else:
                gcd1 = GCD(i, j)
            for k in range(1, K+1):
                ans += GCD(gcd1, k)

    print(ans)



if __name__ == main():
    main()

Submission status: Accepted

Solution code:
import sys
from math import gcd
from itertools import combinations


def input():
    return sys.stdin.readline().strip()


def main():

    N = int(input())
    if N == 1:
        print(1)
    elif N == 2:
        print(9)
    else:
        A = list(combinations(range(1, N + 1), 2))
        B = list(combinations(range(1, N + 1), 3))
        ans = N * (N + 1) // 2
        for a in A:
            a = list(a)
            ans += gcd(a[0], a[1]) * 6
        for b in B:
            b = list(b)
            x = gcd(b[0], b[1])
            x = gcd(x, b[2])
            ans += x * 6
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
def main():
    K = int(input())
    mae = []
    ans = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            mae.append(math.gcd(i,j))
    for m in mae:
        for k in range(1, K+1):
            ans += math.gcd(m, k)
    print(ans)
main()

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(i, k+2):
        check[i][j] = gcd(i, j)

result = 0
for s in range(1, k+1):
    for t in range(1, k+1):
        for u in range(1, k+1): 
            n, m = sorted([s, t])
            x = check[n][m]
            x, y = sorted([x, u])
            result += check[x][y]
print(result)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
k = int(input())

import math
from functools import reduce

def gcd(*i):
    return reduce(math.gcd, i)
j = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            j += gcd(a,b,c)

print(j)

Submission status: Accepted

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import Counter
def resolve():
    K = ir()
    def GCD(a, b):
        if b == 0:
            return a
        else:
            return GCD(b, a % b)
    ab = []
    for i in range(1, K+1):
        for j in range(1, K+1):
            ab.append(GCD(i, j))
    A = Counter(ab)
    ans = 0
    for k in A.keys():
        for i in range(1, K+1):
            abc = GCD(k, i)
            ans += abc*A[k]
    print(ans)
resolve()

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
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,b,c)
      
      
print(s)

Submission status: Accepted

Solution code:
import math


def main(K=None):
    if not K:
        K = int(input())
    ans = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            for k in range(1, K+1):
                ans += math.gcd(math.gcd(i, j), k)
    print(ans)
    return ans


if __name__ == "__main__":
    # assert main(2) == 9
    # assert main(200) == 10813692
    main()

Submission status: Accepted

Solution code:
import math

k=int(input())

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

print(count)

Submission status: Accepted

Solution code:
import sys,math
def input():
    return sys.stdin.readline()[:-1]
K = int(input())
ANS = 0

for i in range(1,K+1):
    for j in range(1,K+1):
        elm = math.gcd(i,j)
        if elm==1:
            ANS += K
            continue
        for k in range(1,K+1):
            ANS += math.gcd(elm,k)
print(ANS)

Submission status: Accepted

Solution code:
K = int(input())

import math

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

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

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

input = raw_input

k = int(input())
out = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        g1 = gcd(a, b)
        for c in range(1, k+1):
            g2 = gcd(g1, c)
            out += g2

print(out)

Submission status: Accepted

Solution code:
n = int(input())


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


ans = 0
d = [0] * (n+1)
for i in range(1,n+1):
  for j in range(1,n+1):
    d[gcd(i,j)] += 1

for i in range(1,n+1):
    for j in range(1,n+1):
        ans += gcd(i,j) * d[i]
print(ans)

Submission status: Accepted

Solution code:
def main():
    import math
    K = int(input())
    value = 0
    for a in range(1 , K+1) :
        for b in range(1 , K+1) :
            gcd_ab = math.gcd(a,b)
            for c in range(1,K+1) :
                gcd_abc = math.gcd(gcd_ab,c)
                value = value + gcd_abc
    print(value)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import math
from functools import reduce
#import itertools
#import statistics
#import numpy as np
#import collections
n = int(input())
#h  = list(map(int, input().split()))
#h, w = list(map(int, input().split()))


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

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(i, j, k)



#print(gcd(27, 18, 3))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

l = [0 for i in range(k+1)]

for a in range(1, k+1):
    for b in range(1, k+1):
        l[math.gcd(a, b)-1] += 1

sum = 0
for i in range(1, k+1):
    for c in range(1, k+1):
        sum += l[i-1] * math.gcd(i, c)

print(sum)

Submission status: Accepted

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

ans=0
for i in range(1,k+1):
    for j in range(1,1+k):
        ans+=gcd(i,j)*l[i-1]
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_gcd = math.gcd(a, b)
        for c in range(1, k + 1):
            ans += math.gcd(ab_gcd, c)

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
K = int(input())
res = 0
dp = {}
for a in range(K+1)[1:]:
  for b in range(K+1)[1:]:
    i = a
    j = b
    if b < a:
      i = b
      j = a
    if not i + j * (K+1) in dp:
      dp[i + j * (K+1)] = gcd(i, j)
    
    d = dp[i + j * (K+1)]

    for c in range(K+1)[1:]:
        

      s = c
      t = d
      if d < c:
        s = d
        t = c
      
      if not s + t * (K+1) in dp:
        dp[s + t * (K+1)] = gcd(s, t)
      res += dp[s + t * (K+1)]

print(res)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())
cnt = 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 and b == c :
                cnt += gcd(a,b,c)
            elif (a == b and b != c) or (a == c and c != a) or (b == c and a != b) :
                cnt += 3 * gcd(a,b,c)
            else :
                cnt += 6 * gcd(a,b,c)

print(cnt)

Submission status: Accepted

Solution code:
import math


def gcd3(n1, n2, n3):
    return math.gcd(n1, math.gcd(n2, n3))


if __name__ == "__main__":
    k = int(input())
    res = dict()
    sum_ = 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:
                    sum_ += gcd3(i, j, l)
                elif i != j and j != l and i != l:
                    sum_ += gcd3(i, j, l) * 6
                else:
                    sum_ += gcd3(i, j, l) * 3

    print(sum_)

Submission status: Accepted

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

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

print(k)

Submission status: Accepted

Solution code:
import math

def ggcd(p,q,r):
    s = math.gcd(p,q)
    return math.gcd(r,s)

def main():
    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 += ggcd(i,j,k)
    print(ans)

if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

def gcd(x, y):
    if x < y: x, y = y, x
    while y:
        n = x%y
        x = y
        y = n
    return x
    
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            ret = gcd(i, j)
            ans += gcd(ret, 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):
        tmp_ab=math.gcd(a,b)
        for c in range(1, K + 1):
            S+=math.gcd(tmp_ab,c)
print(S)

Submission status: Accepted

Solution code:
from math import gcd

def main():
    k = int(input())
    d = 0
    for a in range(1, k + 1):
        for b in range(a, k + 1):
            for c in range(b, k + 1):
                d += gcd(gcd(a, b), c) * [0, 1, 3, 6][len({a, b, c})]
    print(d)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import functools as f

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

	

k = int(input())

s = 0

for i in range(1,k+1):
	for j in range(1,k+1):
		t = math.gcd(i,j)
		for l in range(1,k+1):
				s += math.gcd(t,l)

print(s)