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 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
import sys

def main():
    n = int(sys.stdin.readline())

    total = 0

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

    print(total)

main()

Submission status: Accepted

Solution code:
from math import gcd

K=int(input())

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

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

Submission status: Accepted

Solution code:
from itertools import product

K = int(input())

def gcd_ab(a, b):
    # print(a, b)
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    return gcd_ab(b, a%b)

def gcd(*args):
    r = args[0]
    for arg in args[1:]:
        r = gcd_ab(r, arg)
    return r

sum_num = 0
for a, b, c in product(range(1, K+1), repeat=3):
    sum_num += gcd(a, b, c)

print(sum_num)

Submission status: Accepted

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

Submission status: Accepted

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

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

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:
K = int(input())
ans = 0
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)
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:

k = int(input())

gcd_cnt = {}

for n in range(k, 0, -1):
    div_all = (k // n) ** 3
    div_dup = 0
    for m in range(n * 2, k + 1, n):
        div_dup += gcd_cnt[m]
    gcd_cnt[n] = div_all - div_dup

ans = 0
for gcd in gcd_cnt.keys():
    ans += gcd * gcd_cnt[gcd]

print(ans)

Submission status: Accepted

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


def main():
    input = sys.stdin.buffer.readline
    k = int(input())
    dp = [1]*(k+1)
    for i in range(2, k+1):
        dp[i] = dp[i-1]+i
        for e in combinations(range(1, i), 2):
            dp[i] += 6*gcd(gcd(e[0], e[1]), i)
        for j in range(1, i):
            dp[i] += 6*gcd(i, j)
    print(dp[k])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd
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(c,gcd(a,b))
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
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(a,gcd(b,c))
print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())

su = 0

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

Submission status: Accepted

Solution code:
#C - Sum of gcd of Tuples (Easy)
# 1<=K<=200
# Kは整数
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):
            gcg=math.gcd(math.gcd(a,b),c)
            ans+=gcg
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(gcd(i,j),k)
print(ans)

Submission status: Accepted

Solution code:
import itertools, math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
    
K = int(input())
if K == 1:
    print(1)
    exit()
elif K ==2:
    print(9)
    exit()
l_2 = itertools.combinations(range(1, K + 1), 2)
l_3 = itertools.combinations(range(1, K + 1), 3)
sum = int(K * (K + 1) / 2)
for x in l_2:
    sum += math.gcd(x[0], x[1]) * 6
for x in l_3:
    sum += gcd(x[0], x[1], x[2]) * 6
print(sum)

Submission status: Accepted

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

sum=0

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

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

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

print(sum)

Submission status: Accepted

Solution code:
import sys 
from functools import reduce
import math
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(readline())
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:
import math
from math import gcd

K = int(input())
result = 0

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

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

if K >= 3:
    for i in range(1, K-1):
        for j in range(i+1, K):
            for k in range(j+1, K+1):
                result += gcd(gcd(i,j),k) * 6

print(result)

Submission status: Accepted

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

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

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:
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):
      t_gcd=gcd(i,j)
      ans+=gcd(t_gcd,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):
		tmp = math.gcd(i,j)
		for n in range(1,K+1):
			ans += math.gcd(tmp,n)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

from math import gcd
def solve():
    K = II()

    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 = ans + gcd(g, c)
    print(ans)
if __name__ == '__main__':
    solve()

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:
n=int(input())
from math import gcd
import itertools
lst=[i for i in range(1,n+1)]
kumi=list(itertools.combinations_with_replacement(lst,3))
ans=0
for i in range(len(kumi)):
    a=kumi[i][0]
    b=kumi[i][1]
    c=kumi[i][2]
    g=gcd(gcd(a,b),c)
    if a==b==c:
        ans+=g
    elif a==b or a==c or b==c:
        ans+=3*g
    else:
        ans+=6*g

print(ans)

Submission status: Accepted

Solution code:
k=int(input())

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

def gcd3(a,b,c):
  gcd_ab = gcd(a,b)
  return(gcd(gcd_ab,c))


sum_gcd = 0
for i in range(1,k+1):
  for j in range(1,k+1):
    for n in range(1,k+1):
      sum_gcd += gcd3(i,j,n)
print(sum_gcd)

Submission status: Accepted

Solution code:
import math

k = int(input())

total = 0
for a in range(1, k+1):
  for b in range(1, k+1):
    if math.gcd(a,b) == 1:
      total += k
    else:
      for c in range(1, k+1):
        total += math.gcd(a,math.gcd(b,c))
    
print(total)

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

Submission status: Accepted

Solution code:
import math
k=int(input())
ans= 0
for s in range(1,k+1):
    for t in range(1,k+1):
        if math.gcd(s,t) == 1:
            ans=ans+k
        else:
            for u in range(1,k+1):
                ans= ans + math.gcd(math.gcd(s,t),u)
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):
    v = gcd(i,j)
    for m in range(1,k+1):
      ans += gcd(v,m)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
for a in range(1,n+1):
  for b in range(1,n+1):
    d=math.gcd(a,b)
    if d==1:
      ans+=n
      continue
    if d==2:
      ans+=n//2*3+n%2
      continue
    if d==3:
      ans+=n//3*5+n%3
      continue
    if d==5:
      ans+=n//5*9+n%5
      continue
    for c in range(1,n+1):
      ans += math.gcd(c,d)

print(ans)

Submission status: Accepted

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

ans=0
for a in range(1,n+1):
  for b in range(1,n+1):
    d=np.gcd(a,b)
    if d==1:
      ans+=n
      continue
    if d==2:
      ans+=n//2*3+n%2
      continue
    if d==3:
      ans+=n//3*5+n%3
      continue
    if d==5:
      ans+=n//5*9+n%5
      continue
    for c in range(1,n+1):
      ans += np.gcd(c,d)

print(ans)

Submission status: Accepted

Solution code:
#162C
#Sum of gcd of Tuples (Easy)

import math

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from functools import reduce
from itertools import combinations
from math import gcd

K = int(input())

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

for i in combinations(range(K + 1), 3):
    sum += 6 * reduce(gcd, i)

print(sum)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from functools import reduce
from itertools import combinations
from math import gcd

K = int(input())

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

for i in combinations(range(1, K + 1), 2):
    sum += 6 * reduce(gcd, i)

for i in combinations(range(1, K + 1), 3):
    sum += 6 * reduce(gcd, i)

print(sum)

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

Submission status: Accepted

Solution code:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    K = I()
    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)


main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


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


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    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)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from functools import reduce

k=int(input())

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

ans=0  
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      ans+=gcd(i,j,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):
    aa=math.gcd(i,j)
    for l in range(1,k+1):
      ans+=math.gcd(aa,l)

print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())
sum_num = 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):
            sum_num += math.gcd(tmp, k)
print(sum_num)

Submission status: Accepted

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

d = [[] for _ in range(k)]

for i in range(1, k+1):
    n = 1
    while i*n <= k:
        d[i*n-1].append(i)
        n += 1

seq = [i for i in range(1, k+1)]
ans = 0
A = list(itertools.combinations_with_replacement(seq, 3))
for a in A:
    a1 = a[0]
    a2 = a[1]
    a3 = a[2]
    if a1 == a2 == a3:
        m = 1
    elif a1 == a2 or a1 == a3 or a2 == a3:
        m = 3
    else:
        m = 6
    ans += max(set(d[a1-1]) & set(d[a2-1]) & set(d[a3-1])) * m

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) :
    ans += i
for i in range(1,K) :
    for j in range(i+1,K+1) :
        ans += math.gcd(i,j) * 6
if K >= 3 :
    for i in range(1,K-1) :
        for j in range(i+1,K) :
            for k in range(j+1,K+1) :
                ans += gcd(i,j,k) * 6
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:
import math
K = int(input())
sum = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        r = math.gcd(i,j)
        for k in range(1,K+1):
            sum+=math.gcd(r,k)
print(sum)

Submission status: Accepted

Solution code:
import math


K = int(input())
result = 0

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
 
sum = 0
for i in range(1, N+1):
    for j in range(1, N+1):
        tmp = math.gcd(i,j)
        for k in range(1, N+1):
            sum += math.gcd(tmp, k)
 
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):
        num=math.gcd(i,j)
        for l in range(1,K+1):
            ans+=math.gcd(num,l)
print(ans)

Submission status: Accepted

Solution code:
import math
k = int(input());
    
sum = 0;
sum += (k*k*k-(k-1)*(k-1)*(k-1))

for p in range(2,k+1):
    for q in range(2,k+1):
        g = math.gcd(p,q);
        if g == 1:
            sum += k-1
        else:
            for r in range(2,k+1):
                sum += math.gcd(g,r);
            
print(sum);

Submission status: Accepted

Solution code:
import math
# from functools import reduce
import itertools


def gcd(a, b, c):
    ans = math.gcd(a, b)
    ans = math.gcd(ans, c)

    return ans

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

k = int(input())

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

combs = itertools.combinations_with_replacement(elem, 3)

tmp = [1, 2, 3]

s = 0
for comb in combs:
    s += sum(tmp[:len(set(comb))]) * gcd(comb[0], comb[1], comb[2])
# 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:
K = int(input())

ans = 0

import math

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

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 itertools
import functools
import math

def OK():
    N = int(input())

    sum = 0
    cnt = 0
    for i in range(1,N+1):
        for j in range(1,N+1):
            chk = math.gcd(i,j)
            for k in range(1,N+1):
                sum += math.gcd(chk,k)

    print(sum)

OK()

Submission status: Accepted

Solution code:
import math
K = int(input())
total = 0
for x in range(1, K+1):
    for y in range(1, K+1):
        a=math.gcd(x, y)
        for z in range(1, K+1):
            total = total+math.gcd(z, a)
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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)

Submission status: Accepted

Solution code:
k=int(input())
l=list(range(1, k+1))
import itertools
import math
from functools import reduce
def gcd(*numbers):
  return reduce(math.gcd, numbers)
ans=0
for v in itertools.combinations_with_replacement(l, 3):
  if v[0]==v[1]==v[2]:
    ans+=gcd(*v)
  elif v[0]==v[1]:
    ans+=gcd(*v)*3
  elif v[1]==v[2]:
    ans+=gcd(*v)*3
  elif v[0]==v[2]:
    ans+=gcd(*v)*3
  else:
    ans+=gcd(*v)*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(1,k+1):
        m=math.gcd(i,j)
        for k in range(1,k+1):
            n=math.gcd(m,k)
            ans+=n
print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())
ans = 0

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

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)

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce

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

print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())
ans = 0
for h in range(1,k+1):
    for i in range(1,k+1):
        for j in range(1,k+1):
            t = math.gcd(h, i)
            ans += math.gcd(t, j)

print(ans)

Submission status: Accepted

Solution code:
#inport
import sys
import numpy as np
from math import gcd

#input
K = int(sys.stdin.readline())
sum = 0
#3つ同じ場合..
sum = int((K+1)/2*K)
#print(sum)
#print(K) 
for i in range(1,K+1):
    for j in range(i+1,K+1):
        sum = sum + gcd(i,j) * 6
#        print(str(i) + ", " + str(j))
    #2つが同じ場合
        for l in range(j+1, K+1):
            sum = sum + gcd(gcd(i,j),l) * 6

print(int(sum))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

k = int(input())
k_range = range(1, k + 1)

sum_gcd = 0
for l in k_range:
    temp = 0
    for m in k_range:
        temp = math.gcd(l, m)
        for n in k_range:
            sum_gcd += math.gcd(n, temp)

print(sum_gcd)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd
k=int(input())
cnt=0
for i in range(1,k+1):
  for p in range(1,k+1):
    for q in range(1,k+1):
      gcd1=gcd(i,p)
      
      cnt+=gcd(gcd1,q)

      
print(cnt)

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

K=int(input())

#関数定義(最大公約数)
def gcd(*numbers):
    return reduce(math.gcd, numbers)

sum=0

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

print(sum)

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement


def euclidean(num_list: list):
    a, b = sorted(num_list)
    while b % a != 0:
        redi = b % a
        b = a
        a = redi
    return a


K = int(input())
k_range = list(range(1, K+1))

result = 0
for comb in combinations_with_replacement(k_range, 3):
    min_abc = min(comb)
    gcd = euclidean(comb[0:2])
    gcd = euclidean([gcd, comb[2]])
    if comb[0]==comb[1] and comb[1]==comb[2]:
        coef = 1
    elif (comb[0]!=comb[1] and comb[1]!=comb[2]) and comb[2]!=comb[0]:
        coef = 6
    else:
        coef = 3
    result += gcd * coef
print(result)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')
import math


def main():
	
	k=II()
	
	ans=0
	
	li=[0]*(k+1)
	
	for a in range(1,k+1):
		for b in range(1,k+1):
			li[math.gcd(a,b)]+=1
	
	for i in range(1,k+1):
		for c in range(1,k+1):
			if li[i]!=0:
				ans+=li[i]*math.gcd(i,c)
				
	
	print(ans)





	
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')
import math


def main():
	
	k=II()
	
	ans=0
	
	li=[0]*(k+1)
	
	for a in range(1,k+1):
		for b in range(1,k+1):
			li[math.gcd(a,b)]+=1
	
	for i in range(1,k+1):
		for c in range(1,k+1):
			ans+=li[i]*math.gcd(i,c)
				
	
	print(ans)





	
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')
import math


def main():
	
	k=II()
	
	ans=0
	
	li=[[0]*(k+1) for i in range(k+1)]
	
	for a in range(1,k+1):
		for b in range(1,k+1):
			for c in range(1,k+1):
				d=math.gcd(a,b)
				ans+=math.gcd(d,c)
				
	
	print(ans)





	
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')
import math


def main():
	
	k=II()
	
	ans=0
	
	li=[[0]*(k+1) for i in range(k+1)]
	
	for a in range(1,k+1):
		for b in range(1,k+1):
			li[a][b]=math.gcd(a,b)
	
	
	for a in range(1,k+1):
		for b in range(1,k+1):
			for c in range(1,k+1):
				ans+=math.gcd(li[a][b],c)
				
	
	print(ans)





	
if __name__ == "__main__":
	main()

Submission status: Accepted

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


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


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:
# aaa
from math import gcd
# gcd: to get 最大公約数

k = int(input())
gg = [0] * k
ans = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        temp = gcd(i, j)
        for l in range(1, k + 1):
            ans += gcd(temp, l)

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())

sum = 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:
                sum += gcd(a,b,c)
            elif a == b or b == c:
                sum += gcd(a,b,c) * 3
            else:
                sum += gcd(a,b,c) * 6
print(sum)

Submission status: Accepted

Solution code:
K = int(input())

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

total = 0
for i in range(1,K+1,1):
  for j in range(1,K+1,1):
    p = gcd(i,j)
    for k in range(1,K+1,1):
      q = gcd(k, p)
      total += q
print(total)