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(i,gcd(j,l)))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import math
K = int(input())
sum_gcd = 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 :
                sum_gcd += math.gcd(math.gcd(a,b),c)
            elif a == b or b == c or c == a:
                sum_gcd += 3 * math.gcd(math.gcd(a,b),c)
            else:
                sum_gcd += 6 * math.gcd(math.gcd(a,b),c)
print(sum_gcd)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

def resolve():
    N = int(input())
    A = np.arange(1, N+1)
    ans = np.gcd.outer(np.gcd.outer(A, A),A)
    return print((ans.sum()))

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd
def main(): #startline-------------------------------------------
    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)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Accepted

Solution code:
from math import gcd 

num = int(input()) + 1

count = 0
for a in range(1, num):
    for b in range(1, num):
        t = gcd(a,b)
        for c in range(1, num):
                count += gcd(t, c)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math as np
K=int(input())
s3=0
s2=0
s1=0
s0=0
for a in range(1,K-1):
    for b in range(a+1,K):
        d=np.gcd(a,b)
        for c in range(b+1,K+1):
            s0+=np.gcd(c,d)
for a in range(1,K):
    for c in range(a+1,K+1):
        s1+=np.gcd(a,c)
for a in range(1,K):
    for b in range(a+1,K+1):
        s3+=np.gcd(a,b)
for k in range(K+1):
    s2+=k
print(s0*6+s1*3+s3*3+s2)

Submission status: Accepted

Solution code:
n=int(input())
import math
a=[0]*3
sum=0
for i in range(n):
  for j in range(n):
    for k in range(n):
      a[0]=i+1
      a[1]=j+1
      a[2]=k+1
      ans=a[2]
      for l in range(2):
        ans = math.gcd(ans, a[l])
      sum+=ans
print(int(sum))

Submission status: Accepted

Solution code:
import math

K = int(input())
G = [[0] *(K+1) for i in range(K+1)]

for a in range(1,K+1):
  t = 0
  for b in range(1,K+1):
    w = math.gcd(a,b)
    G[a][b] = w
    t += w
  G[a][0] = t
#
t = 0
for a in range(1,K+1):
  for b in range(1,K+1):
    w = G[a][b]
    t += G[w][0]
#
print(t)

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

Submission status: Accepted

Solution code:
from math import gcd
n=int(input())
sum1=0
for i in range(1,n+1):
	for j in range(1,n+1):
		gcd1=gcd(i,j)
		for k in range(1,n+1):
			gcd2=gcd(gcd1,k)
			sum1+=gcd2
print(sum1)

Submission status: Accepted

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

def resolve():
    N = int(input())
    ans = 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):
                ans += math.gcd(g,k)

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import gcd, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
# from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
c = 0
K = INT()
ans = 0
 
for i in range(1, K + 1):
    for j in range(1, K + 1):
        a = gcd(i, j)
        for k in range(1, K + 1):
            b = gcd(a, k)
            ans += b
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 h in range(1,k+1):
            ans += gcd(gcd(i,j),h)

print(ans)

Submission status: Accepted

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

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

K=int(input())
S=0

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

print(S)

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:
K = int(input())  #スペース抜きの複数行,１文字ずつ
import math
from functools import reduce

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

A = []
for i in range(1, K + 1):
    for j in range(i, K+1):
        for k in range(j, K + 1):
            if i == j and j == k and k == i:
                #print(i,j,k)
                A.append(gcd_list([i, j, k]))
            elif i != j and j != k and k != i:
                A.append(gcd_list([i, j, k])*6)
            else:
                #print(i,j,k)
                A.append(gcd_list([i, j, k])*3)           

print(sum(A))

Submission status: Accepted

Solution code:
import math
from functools import reduce

gcd = lambda *numbers: reduce(math.gcd, numbers)

K = int(input())

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

print(answer)

Submission status: Accepted

Solution code:
def main():
    import math
    import sys

    input = sys.stdin.readline

    n = int(input())
    c_1 = 0
    c_2 = 0
    c_3 = 0

    for i in range(n):
        for j in range(i,n):
            for k in range(j,n):
                if i == j and j == k:
                    c_1  += i+1
                elif i != j and j != k and k != i:
                    c_2 += math.gcd(math.gcd(i+1,j+1),k+1)
                else:
                    c_3 += math.gcd(i+1,k+1)

    print(c_1 + c_2 * 6 + c_3 * 3)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
p = int(input())
counta = 0

def mygcd(a_,b_,c_):
    return math.gcd(math.gcd(a_, b_),c_)

for i in range(1,p+1):
    for j in range(1,p+1):
        for k in range(1,p+1):
            counta += mygcd(i,j,k)
print(counta)

Submission status: Accepted

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

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

    tmp = []
    for a in range(1, k+1):
        for b in range(1, k+1):
            tmp.append(gcd(a,b))

    ans = 0
    for c in range(1,k+1):
        for i in tmp:
            ans += gcd(i, c)
    
    print(ans)
    
resolve()

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

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)
        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())
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

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

print(cnt)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import math

k = ni()
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
K=int(input())
from math import gcd
# サイズは最大8*10^6だけどOK(これくらいが限界ギリギリOKだよぉ)
lis = [gcd(gcd(a,b),c) for a in range(1,K+1) for b in range(1,K+1) for c in range(1,K+1)]
print(sum(lis))

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

print(ans)

Submission status: Accepted

Solution code:
import math

a=0

K=int(input())
for i in range(1,K+1):#(1,2,3)
    for j in range(i,K+1):
        for r in range(j,K+1):
            if i==j and i==r:#(1,1,1)のような場合
                a+=math.gcd(i,math.gcd(j,r))
            elif i==j or j==r:#(1,1,2),(1,2,2)のような場合。各3通り
                a+=math.gcd(i,math.gcd(j,r))*3
            else:
                a+=math.gcd(i,math.gcd(j,r))*6
print(a)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce

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

def main():
    K = int(input())
    g = 0
    for i in range(1, K+1):
        for j in range(1, K+1):
            for k in range(1, K+1):
                g += gcd(gcd(i, j), k)         
    print(g)
main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(1, k + 1):
    for x in range(1, k + 1):
        for z in range(1, k + 1):
            ans += gcd(z, gcd(i, x))
print(ans)

Submission status: Accepted

Solution code:

import math
K=int(input())
ans=0
A=0

for n in range(1,K+1):
    for m in range(1,K+1):
        x=math.gcd(n,m)
        for l in range(1,K+1):
            A=math.gcd(x,l)    
            ans+=A
            
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 g in range(1,k+1):
      ans += gcd(gcd(i,j), g)

print(ans)

Submission status: Accepted

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

def main():
    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(gcd(i, j), k)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

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

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

cache = [[gcd(a, b) for b in range(K + 1)] for a in range(K + 1)]
  
sum = 0

for a in range(K):
	for b in range(K):
		ab = cache[a + 1][b + 1]
		for c in range(K):
			sum += cache[ab][c + 1]

print(sum)

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):
        if gcd(i,j) == 1: ans += K
        else:
            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
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
ans = 0
k = int(input())
for i in range(1,k+1):
    for j in range(1,k+1):
        for p in range(1,k+1):
            ans += gcd(i,j,p)
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(a, K + 1):
        ab = math.gcd(a, b)
        for c in range(b, K + 1):
            abc = math.gcd(ab, c)
            if a == b == c:
                m = 1
            elif a == b or b == c or c == a:
                m = 3
            else:
                m = 6
            ans += m * abc

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

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(i,k+1):
    for m in range(j,k+1):
      tem=[]
      tem = [i,j,m]
      
      if  len(set(tem)) == 1 :
        ans += i
      elif len(set(tem)) == 2:
        ans += gcd(i,j,m) * 3
      elif len(set(tem)) == 3:
        ans += gcd(i,j,m) * 6

print(ans)

Submission status: Accepted

Solution code:
K = int(input())


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


result = 0
for i in range(1, K + 1):
    for j in range(1, i + 1):
        for k in range(1, j + 1):
            if i == j == k:
                result += i
            elif i == j:
                result += gcd(i, k) * 3
            elif j == k:
                result += gcd(i, k) * 3
            else:
                result += gcd(gcd(i, j), k) * 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):
        ab = gcd(i,j)
        if ab == 0:
            continue
        for k in range(1,K+1):
            ans += gcd(ab,k)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from math import gcd


t = int(input())
ans = 0
for i in range(1, t + 1):
    for j in range(1, t + 1):
        r = gcd(i, j)

        for k in range(1, t + 1):
            ans += gcd(r, k)

print(ans)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import numpy as np
K=int(input())
s3=0
s2=0
s1=0
s0=0
for a in range(1,K-1):
    for b in range(a+1,K):
        d=np.gcd(a,b)
        for c in range(b+1,K+1):
            s0+=np.gcd(c,d)
for a in range(1,K):
    for c in range(a+1,K+1):
        s1+=np.gcd(a,c)
for a in range(1,K):
    for b in range(a+1,K+1):
        s3+=np.gcd(a,b)
for k in range(K+1):
    s2+=k
print(s0*6+s1*3+s3*3+s2)

Submission status: Accepted

Solution code:
import math

def main():
    #n, k = map(int,input().split())
    k = int(input())
    lists = []
    sum = 0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for l in range(1,k+1):
                #print(i,j,l)
                sum = sum + math.gcd(math.gcd(i,j),l)
    print(sum)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(input())
total = K*K
for i in range(2,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      total += gcd(i,j,k)
print(total)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import gcd, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
# from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
K = INT()
 
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 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(1,K+1):
        for k in range(1,K+1):
            a = dp[i][j]
            ans += dp[a][k]
print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())
S = 0

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


print(S)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import itertools

K = int(input())
s = 0
l = range(K)

for i in itertools.combinations_with_replacement(l, 3):
    if i[0] == i[1] == i[2]:
        s += math.gcd(math.gcd(i[0]+1, i[1]+1), i[2]+1)
    elif i[0] != i[1] != i[2]:
        s += (math.gcd(math.gcd(i[0]+1, i[1]+1), i[2]+1)*6)
    else:
        s += (math.gcd(math.gcd(i[0]+1, i[1]+1), i[2]+1)*3)
print(s)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

import math

def main():
    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(math.gcd(i,j),l)

    print(ans)


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

Submission status: Accepted

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

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
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:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import gcd, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
# from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
K = INT()
 
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

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

K = int(input())
box = []
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:
                box.append(gcd(a,b,c))
            elif a!=b and b!=c and c!=a:
                box.append(gcd(a,b,c)*6)
            else:
                box.append(gcd(a,b,c)*3)
print(sum(box))

Submission status: Accepted

Solution code:
from functools import reduce
import math
import sys
 
K = int(input())
 
def gcd(*args):
  return reduce(math.gcd, args)
 
if K==1:
  print("1")
  sys.exit()
elif K==2:
  print("9")
  sys.exit()
 
ans = K * (K+1)//2
for i in range(1,K):
  for j in range(i+1,K+1):
    ans += 6*math.gcd(i,j)
  
for i in range(1,K-1):
  for j in range(i+1, K):
    for k in range(j+1, K+1):
      ans += 6 * 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(a+1,K):
        for c in range(b+1,K+1):
            r = math.gcd(math.gcd(a,b),c)
            r *= 6
            ans += r
for a in range(1,K):
    for b in range(a+1,K+1):
        r = math.gcd(a,b)
        r *= 6
        ans += r
for a in range(1,K+1):
    ans += a
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 l in range(1, k+1):
            ans += gcd(ij, l)

print(ans)

Submission status: Accepted

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

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())

import fractions

ans = 0

import itertools

gcd_dic = {}

for i in itertools.combinations(range(1, K + 1), 2):
    ans_m = fractions.gcd(i[0], i[1])
    ans += ans_m * 6
    gcd_dic[(i[0], i[1])] = ans_m
    gcd_dic[(i[1], i[0])] = ans_m

for i in itertools.combinations(range(1, K+1), 3):
    ans_m = i[0]
    for l in i[1:]:
        if (ans_m, l) in gcd_dic:
            ans_m = gcd_dic[(ans_m, l)]
        else:
            ans_m = fractions.gcd(ans_m, l)
    ans += ans_m*6

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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
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 l in range(1,k+1):
            ans+=math.gcd(tmp,l)

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools
import numpy as np

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):
            if i == j and j == k:
                dup_num = 1
            elif i == j or j == k:
                dup_num = 3
            else:
                dup_num = 6
            ans += math.gcd(math.gcd(i, j), k) * dup_num

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())+1
n = 0
for i in range(1,k):
  for j in range(i,k):
    for l in range(j, k):
      if i == j == l:
        n += gcd(i,gcd(j,l))
      elif i == j or j == l or l == i:
        n += gcd(i,gcd(j,l))*3
      else:
        n += gcd(i,gcd(j,l))*6
print(n)

Submission status: Accepted

Solution code:
K = int(input())

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

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 12
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  itertools import permutations
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 l in range(1,k + 1):
                ans += gcd(gcd(i,j),l)
    print(ans)
if __name__ =='__main__':
    main()

Submission status: Accepted

Solution code:
K = int(input())
cnt = 0
import math
from functools import reduce
 
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
 
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
 
            cnt += gcd_list([i,j,k])
print(cnt)

Submission status: Accepted

Solution code:
from math import gcd
key = int(input())
sin = 0
for i in range(1,key+1):
    for j in range(1,key+1):
        temp = gcd(i,j)
        for m in range(1,key+1):
            sin += gcd(m,temp)
print(sin)

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
import math

#n = map(int,input().split())

k = int(input())


def gcd(a,b):
    if a > b:
        a , b = b , a

    # q = b / a
    r = b % a

    if r == 0:
        return a
    else:
        return gcd(a,r)
    

sum = 0



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

print(sum)

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
from collections import Counter
def printYes():
    print("YES")
def printNo():
    print("NO")

t = int(input())
ans=0
for i in range(1,t+1):
    for j in range(1,t+1):
        r=gcd(i,j)
        for s in range(1,t+1):
            ans=ans+gcd(r,s)
print(ans)