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

Submission status: Accepted

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

G = []
i,j = 1,1
while i < 201:
    j = 1
    g0 = []
    while j < 201:
        g0.append(gcd(i,j))
        j += 1
    g0 = tuple(g0)
    G.append(g0)
    i += 1
n = int(input())
i = 1
j = 1
k = 1
ans = 0
while i < n+1:
    j = 1
    while j < n+1:
        k = 1
        p = G[i-1][j-1]
        while k < n+1:
            ans += G[k-1][p-1]
            k += 1
        j += 1
    i += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

k=int(input())
ans=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):
                ans+=math.gcd(a,math.gcd(b,c))
            if ((a!=b) and (b==c)) or ((a==b) and (b!=c)):
                ans+=3*math.gcd(a,math.gcd(b,c))
            if ((a!=b) and (b!=c)):
                ans+=6*math.gcd(a,math.gcd(b,c))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(x,y):
    if y == 0:
        return x
    elif x == 1 or y == 1:
        return 1
    else:
        return gcd(y,x%y)

G = []
i,j = 1,1
while i < 201:
    j = 1
    g0 = []
    while j < 201:
        g0.append(gcd(i,j))
        j += 1
    g0 = tuple(g0)
    G.append(g0)
    i += 1
n = int(input())
i = 1
j = 1
k = 1
ans = 0
while i < n+1:
    j = 1
    while j < n+1:
        k = 1
        p = G[i-1][j-1]
        while k < n+1:
            ans += G[k-1][p-1]
            k += 1
        j += 1
    i += 1
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:
from math import gcd
 
def main():
    k = int(input())
    ans = 0
 
    for i in range(k):
        for j in range(k):
            for ii in range(k):
                ans += gcd(i+1,gcd(j+1,ii+1))
 
    print(ans)
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd

def main():
    k = int(input())
    ans = 0

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys
import pprint
from math import gcd


sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	k = int(sys.stdin.readline())
	s = 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):
				s += gcd(x, c)
	print(s)
	


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

Submission status: Accepted

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

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:
K = int(input())
mod = int(1e9) + 7
X = [0 for _ in range(K)]
def doubling(n, m):
  y = 1
  base = n
  while m != 0:
    if m % 2 == 1:
      y *= base
      y %= mod
    base *= base
    base %= mod
    m //= 2
  return y
SS = 0
for i in range(K, 0, -1):
  d = K // i
  S = doubling(d, 3)
  if d > 1:
    for j in range(2, d+1):
      S -= X[j*i-1]
      S %= mod
  X[i-1] = S
  SS += i * S
  SS %= mod
print(SS)

Submission status: Accepted

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

import sys
import pprint
from math import gcd


sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	k = int(sys.stdin.readline())
	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)
	


main()

Submission status: Accepted

Solution code:
k = int(input())

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

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

Submission status: Accepted

Solution code:
def mapint_inp():
    return map(int, input().split())
 
def intinp():
    return int(input())
 
import math
import itertools
  
K = intinp()
 
ans = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        ij = math.gcd(i, j)
        for k in range(1, K+1):
            ans += math.gcd(ij, k)
 
print(ans)

Submission status: Accepted

Solution code:
k = int(input())
import math
import fractions
ans = 0
e = 0
d = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        e = math.gcd(a,b)
        #e = fractions.gcd(a,b)
        for c in range(b,k+1):
            d = math.gcd(e,c)
            #d = fractions.gcd(e,c)
            if a != b and b!= c and a!= c:
                ans+= d*6
            elif (a==b and a!=c) or (a==c and a!= b) or (b == c and a!= b):
                ans+= d*3
            else:
                ans+= d
print(ans)

Submission status: Accepted

Solution code:
k = int(input())
import math
import fractions
ans = 0
e = 0
d = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        #e = math.gcd(a,b)
        e = fractions.gcd(a,b)
        for c in range(b,k+1):
            #d = math.gcd(e,c)
            d = fractions.gcd(e,c)
            if a != b and b!= c and a!= c:
                ans+= d*6
            elif (a==b and a!=c) or (a==c and a!= b) or (b == c and a!= b):
                ans+= d*3
            else:
                ans+= d
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import gcd
ans = 0
n = int(input())

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

Submission status: Accepted

Solution code:
k = int(input())


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


ans = 0
for p in range(1, k + 1):
    for q in range(1, k + 1):
        for r in range(1, k + 1):
            ans += gcd(gcd(p, q), r)
print(ans)

Submission status: Accepted

Solution code:
# 最大公約数を格納するリストを初期化
max_codiv = []
 
# 繰り返す数を入力
n = int(input())
 
import math
# 1からkまでの総当たりで最大公約数を調べる
for i in range(1, n+1):
	for j in range(1, n+1):
		i_j = math.gcd(i, j)
		for k in range(1, n+1):
			i_j_k = math.gcd(i_j, k)
			max_codiv.append(i_j_k)
print(sum(max_codiv))

Submission status: Accepted

Solution code:
mod = 10**9 + 7
import math 
from bisect import bisect_left 
from collections import Counter
from itertools import combinations
 
def ain():
    return map(int, input().split())
def fain():
    return map(float, input().split())
 
def mc2(s):
    return (s*(s-1))//2 + s
    
# for tc in range(int(input())):
#     n = int(input())
    
#     print(ans)

k = int(input())
sum=0
for a in range(1,k+1):
    for b in range(1,k+1):
        g1 = math.gcd(a,b)
        for c in range(1,k+1):
            sum += math.gcd(g1,c)
print(sum)
# python3 pan3.py

Submission status: Accepted

Solution code:
from math import gcd

K=int(input())
ans=0

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

Submission status: Accepted

Solution code:
import sys
import itertools
import math


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    k = [i for i in range(1, int(readline()) + 1)]
    t = 0
    for v in itertools.product(k, repeat=3):
        t += math.gcd(math.gcd(v[0], v[1]), v[2])
    print(t)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
K = int(input())

def func_gcd(a, b):
    if b > a:
        a, b = b, a
    
    while b > 0:
        r = a % b
        a = b
        b = r

    return a

ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for l in range(1, K + 1):
            temp = func_gcd(j, l)
            temp2= func_gcd(i, temp)
            ans += temp2

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

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
ans=0
array = [0] * 201

for X in range(1,K+1):
    for c in range(1,K+1):
        array[X] += gcd(X,c)

for a in range(1,K+1):
    for b in range(1,K+1):
        X = gcd(a,b)
        ans += array[X]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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):
            # print("{} {} {} gcd{}".format(a, b, c, gcd((a, b, c))))
            ans += gcd(a, gcd(b, c))

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
from math import gcd
from itertools import product
from functools import reduce
def resolve():
    n = int(input())
    res = 0
    for i, j, k in product(range(1, n + 1), repeat = 3):
        res += reduce(gcd, (i, j, k))
    print(res)
resolve()

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

count = 0

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

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

res = 0
mul = (0, 1, 3, 6)
for a in range(1, k+1):
    for b in range(a, k+1):
        for c in range(b, k+1):
            res += gcd(gcd(a, b), c) * mul[len({a, b, c})]

print(res)

Submission status: Accepted

Solution code:

k=int(input())
 
from math import gcd
s6=0
for i in range(2,k-1):
    for j in range(i+1,k):
        for jj in range(j+1,k+1):
            s6+=gcd(gcd(i,j),jj)
 
s3=0
for i in range(2,k):
    for j in range(i+1,k+1):
        s3+=gcd(i,j)
 
s1=int((k+1)*k/2)
 
 
print(s1+s3*6+s6*6+(k-1)*6+(k-2)*(k-1)*3)

Submission status: Accepted

Solution code:
import math
from itertools import combinations_with_replacement

K = int(input())

ans = 0


for i, j, k in combinations_with_replacement(list(range(1,K+1)), 3):
    lstset = {i, j, k}
    if len(lstset)==3: ans += math.gcd(math.gcd(i,j), k) * 6
    elif len(lstset)==2: ans += math.gcd(math.gcd(i,j), k) * 3
    else: ans +=  math.gcd(math.gcd(i,j), k)
    
print(ans)

Submission status: Accepted

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

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

if __name__ == "__main__":
    resolve()

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

Submission status: Accepted

Solution code:
import math


K=int(input())

ans=0

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

print(ans)

Submission status: Accepted

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

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:
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 h in range(1,k+1):
      ans+=math.gcd(tmp,h)
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:
N = int(input()) + 1

Answer = 0

def getGCD(a,b,c):
    if a == b == c:
        d = 1
    elif a == b or b == c:
        d = 3
    else:
        d = 6
        
    surplus = a % c
    while surplus != 0:
        a,c = c,surplus
        surplus = a % c
    surplus = b % c
    while surplus != 0:
        b,c = c,surplus
        surplus = b % c
    return c * d


for i in range(1,N):
    for j in range(i,N):
        for k in range(j,N):
            Answer += getGCD(k,j,i)
            
print(Answer)

Submission status: Accepted

Solution code:
import math


K=int(input())

ans=0

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input()) + 1

Answer = 0

def getGCD(a,b,c):
    if a == b and b == c:
        d = 1
    elif a == b or b == c or c == a:
        d = 3
    else:
        d = 6
        
    surplus = a % c
    while surplus != 0:
        a = int(c)
        c = int(surplus)
        surplus = a % c
    surplus = b % c
    while surplus != 0:
        b = int(c)
        c = int(surplus)
        surplus = b % c
    return c * d


for i in range(1,N):
    for j in range(i,N):
        for k in range(j,N):
            Answer += getGCD(k,j,i)
            
print(Answer)

Submission status: Accepted

Solution code:
k = int(input())
cnt = [0] * (k + 1)

for i in range(k, 0, -1):
    cnt[i] = (k // i) ** 3
    for j in range(i * 2, k + 1, i):
        cnt[i] -= cnt[j]

print(sum((i* cnt[i] for i in range(1, k + 1))))

Submission status: Accepted

Solution code:
k = int(input())
n = 3
cnt = [0] * (k + 1)

for i in range(k, 0, -1):
    cnt[i] = (k // i) ** n
    for j in range(i * 2, k + 1, i):
        cnt[i] -= cnt[j]

print(sum((i* cnt[i] for i in range(1, k + 1))))

Submission status: Accepted

Solution code:
def gcd(p, q):
    if p % q == 0:
        return q
    else:
        return gcd(q, p % q)
k = int(input())
ans = 0
for m in range(1, k+1):
    for n in range(1, k+1):
        for l in range(1, k+1):
            ans += gcd(gcd(m, n), l)
print(ans)

Submission status: Accepted

Solution code:
#C
#入力
K=int(input())


#デバッグ用
def pront(*n):
    if debug==1:
        print(n)
        
debug=1#0:本番,1:デバッグ

#処理
import math as m
Z=[ [-1 for i in range(K+1)] for j in range(K+1)]
def C_1(i,j):
    if Z[i][j]!=-1:
        return Z[i][j]
    else:
        x=m.gcd(i,j)
        Z[i][j]=x
        Z[j][i]=x
        return Z[i][j]

x=0
for i in range(1,K+1):
    for j in range(1,K+1):
        y=C_1(i,j)
        if y==1:
            x+=K
        else:
            for k in range(1,K+1):
                x+=C_1(y,k)
print(x)

Submission status: Accepted

Solution code:
from numpy import*;f=gcd.outer;r=range(1,int(input())+1);print(sum(f(f(r,r),r)))

Submission status: Accepted

Solution code:
k = int(input())
n = 3
cnt = [0] * (k + 1)

for i in range(k, 0, -1):
    cnt[i] = (k // i) ** n
    multiple = i * 2
    while multiple <= k:
        cnt[i] -= cnt[multiple]
        multiple += i

print(sum((i* cnt[i] for i in range(1, k + 1))))

Submission status: Accepted

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

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import math


def main():
    k = int(input())
    ans = 0
    gcd = math.gcd
    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)

main()

Submission status: Accepted

Solution code:
import math
s = input()
k = int(s)
n = 3
ans={}
for i in reversed(range(1, k+1)):
    ans[i]=math.floor(k/i)**n
    l=2
    j=i*l
    while j<=k:
        ans[i]=ans[i]-ans[j]
        l +=1
        j = i*l
res=0
for i in ans:
    res+=i*ans[i]
print(res)

Submission status: Accepted

Solution code:
import math
k=int(input())
tot=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):
            tot+=math.gcd(tmp,c)
print(tot)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
total = 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):
            total += gcd(tmp, c)

print(total)

Submission status: Accepted

Solution code:
import math


def main():
    k = int(input())
    ans = 0
    gcd = math.gcd
    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)

main()

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8
from math import*
K=int(input())
result=0
for i in range(1,K+1):
	for j in range(1,K+1):
		for k in range(1,K+1):
			result+=gcd(gcd(i,j),k)

print(result)

Submission status: Accepted

Solution code:
k = int(input())
from math import gcd
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, gcd(b,c))
print(sum)

Submission status: Accepted

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

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:
import numpy as np
K=int(input())
x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
print(nums.sum())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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 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)
            ans += res
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
k=int(input())
a=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      if i<=j<=l:
        if i==j==l:
          a=a+i
        elif i==j and j!=l:
          a=a+3*math.gcd(i,l)
        elif i!=j and j==l:
          a=a+3*math.gcd(i,l)
        elif i!=j and j!=l:
          a=a+6*math.gcd(i,math.gcd(j,l))
      else:
        a=a+0
print(a)

Submission status: Accepted

Solution code:
import math

n = int(input())
ans = 0
a = {}
for i in range(1,n+1):
    for j in range(1,n+1):
        tmp = math.gcd(i,j)
        a[tmp] = a.get(tmp, 0)
        a[tmp] += 1

for key, val in a.items():
    for j in range(1, n+1):
        ans += math.gcd(key,j) * val

print(ans)

Submission status: Accepted

Solution code:
import math

def main():
    n = int(input())
    ans = 0
    a = {}
    for i in range(1,n+1):
        for j in range(1,n+1):
            tmp = math.gcd(i,j)
            a[tmp] = a.get(tmp, 0)
            a[tmp] += 1


    for key, val in a.items():
        for j in range(1, n+1):
            ans += math.gcd(key,j) * val

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

import sys
input = sys.stdin.readline
from math import gcd


def main():
    K=int(input())
    ret = 0
    gc=[[-1]*K for _ in range(K)]
    for a in range(K):
        for b in range(K):
            if gc[a][b]==-1:
                gc[a][b]=gcd(a+1,b+1)
                gc[b][a]=gc[a][b]
            x=gc[a][b]
            for c in range(K):
                if gc[x-1][c]==-1:
                    gc[x-1][c]=gcd(x,c+1)
                    gc[c][x-1]=gc[x-1][c]
                ret += gc[x-1][c]
    print(ret)

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

# これでは３重ループとなり200**3 = 8000000 ではLTEとなる

#　そこで、一番内側のループをなくすために、
# gcd(g,1) + gcd(g,2) + ・・・　+gcd(g,K) を先に計算しておく
L = [0] * 201

for g in range(1, K+1):
    for c in range(1, K+1):
        L[g] += gcd(g, c)


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

print(ans)

Submission status: Accepted

Solution code:
import math

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
import itertools
GCD_over_150=[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]
 
N=int(input())
if N<150:
	GCD=[]
	GCD1=[]
	for i in range (1,N+1):
		GCD.append(i)
	for i in itertools.product(GCD,repeat=3):
		GCD1.append(math.gcd(math.gcd(i[0],i[1]),i[2]))
	print(sum(GCD1))    
else:
    print(GCD_over_150[N-150])

Submission status: Accepted

Solution code:
K=int(input())
N=3
pr=10**9+7
a=0
l=[None]*(K+1)
for i in range(K,0,-1):
    t=pow(K//i,N,pr)
    c=i*2
    while c<=K:
        t-=l[c]
        c+=i
    l[i]=t
    a=(a+t*i)%pr
print(a)

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

Submission status: Accepted

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

import sys
input = sys.stdin.readline
from math import gcd

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
K=int(input())
r=np.arange(1,K+1)
print(np.sum(np.gcd.outer(np.gcd.outer(r,r),r)))

Submission status: Accepted

Solution code:
import math
if __name__ == "__main__":
    n = int(input())
    ans = 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):
                ans += math.gcd(tmp,k)

    print(ans)

Submission status: Accepted

Solution code:
import math

def main():
    n = int(input())
    ans = 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):
                ans += math.gcd(tmp,k)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
h = {}
for i in range(1, K+1):
  for j in range(1, K+1):
    x = gcd(i, j)
    h[x] = h.get(x, 0)
    h[x] += 1

keys = list(h.keys())
n = len(keys)
for i in range(n):
  tmp = 0
  key = keys[i]
  for j in range(1, K+1):
    tmp += gcd(key, j)
  ans += tmp * h[key]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = [[1]* 201 for _ in range(201)] 

def gcd (a,b):
  if a < b:
    a, b = b, a
  A, B = a, b
  if L[a][b] != 1:
    return L[a][b]
  while b:
    a,b = b, a % b
  L[A][B] = a
  return a

ans = 0
for x in range(1,N+1):
  for y in range(x,N+1):
    for z in range(y,N+1):
      t = gcd(x,y)
      u = gcd(t,z)
      if x==y==z:
        ans += u
      elif x!=y and y!= z:
        ans += u * 6
      else:
        ans += u * 3
      
print (ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
L = [[1]* 201 for _ in range(201)] 

def gcd (a,b):
  if a < b:
    a, b = b, a
  A, B = a, b
  if L[a][b] != 1:
    return L[a][b]
  while b:
    a,b = b, a % b
  L[A][B] = a
  return a

ans = 0
for x in range(1,N+1):
  for y in range(1,N+1):
    for z in range(1,N+1):
      t = gcd(x,y)
      u = gcd(t,z)
      ans += u
print (ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
K = int(input())

# from fractions import gcd
from math import gcd

# def f(x, y, z):
#     r = gcd(gcd(x, y), z)
#     return r

ans = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            # ans += f(i, j, k)
            ans += gcd(gcd(i, j), k)

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

    print(ans)

if __name__ == "__main__":
    main()