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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(a,b,c):
    #1
    seq  = [a,b,c]
    seq.sort()
    while b > 0:
        r = c%b
        c = b
        b = r
    #2
    seq_2 = [a,c]
    seq_2.sort()
    while a > 0:
        r = c%a
        c = a
        a = r
    return c

from functools import reduce

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

print(ans)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd 
import sys
sys.setrecursionlimit(10000000)

#const
dxdy=((1,0),(0,1))
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())


def resolve():  
    N=int(input())
    r=range(1,N+1)#スニペットのようなもの
    ans=0
    for a in r:
        for b in r:
            for c in r:
                ans+= gcd(gcd(a,b),c)#3数のGCDを一気にとる
    print(ans)
resolve()

Submission status: Accepted

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


print(ans)

Submission status: Accepted

Solution code:
from math import gcd
 
K=int(input())
 
S=0
for a in range(1,K+1):
    for b in range(1,K+1):
        calc=gcd(a,b)
        for c in range(1,K+1):
                S+=gcd(calc,c)
 
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):
        a=math.gcd(i,j)
        for l in range(1,k+1):
            b=math.gcd(a,l)
            ans+=b
    
print(ans)

Submission status: Accepted

Solution code:
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:
import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
import sys
import bisect


sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))

K = ii()

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)

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):
        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
k=int(input())
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(p,gcd(q,r))
print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())
tanget_num = range(1, K+1)
sum_  = sum([math.gcd(i, math.gcd(j, k)) for i in tanget_num for j in tanget_num for k in tanget_num])

print(str(sum_))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
K = int(input())

# lcm & gcd not math library
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
ans = 0
for i in range(1, K + 1):
    for j in range(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 += gcd(gcd(i, j), k) * dup_num

print(ans)

Submission status: Accepted

Solution code:
k = int(input())
# a,bの最大公約数


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


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

Submission status: Accepted

Solution code:
a=int(input())
b=0
c=1
import math
from functools import reduce

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

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

for i in range(1,a+1):
    for j in range(1,a+1):
        
        for z in range(1,a+1):
            

            b+=gcd(i,j,z)


print(b)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

k = int(input())

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

N = int(input())

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K = int(input())

memo = [[None] * (K + 1) for _ in range(K + 1)]

def mygcd(a, b):
    if memo[a][b] != None: return memo[a][b]
    else:
        if a < b:
            ret = mygcd(b, a)
        elif b == 0:
            ret = a
        else:
            ret = mygcd(b, a % b)
        #
        memo[a][b] = ret
        memo[b][a] = ret
        return ret

ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        tmp = mygcd(a, b)
        for c in range(1, K + 1):
            ans += mygcd(tmp, 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 l in range(1, k+1):
            ans += gcd(gcd(i, j), l)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
import math
# N, K = map(int, input().split(' '))
N = int(input())
wa = 0
d = defaultdict(set)
d[1].add(1)

for i in range(2, 201):
    for j in range(i, 0, -1):
        if i % j == 0:
            d[i].add(j)

for i in range(1, N+1):
    for j in range(1, N+1):
        for k in range(1, N+1):
            #wa += 1
            wa += max(d[i] & d[j] & d[k])
print(wa)
# wa = 0
# for i in range(1, N+1):
#     if i % 3 == 0 or i % 5 == 0:
#         continue
#     wa += i
# print(wa)

Submission status: Accepted

Solution code:
from math import gcd

s=int(input())
a=0

for i in range(1,s+1):
  for j in range(1,s+1):
    for k in range(1,s+1):
      a+=gcd(gcd(i,j),k)
      
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

Submission status: Accepted

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

memo = [[0] * 201 for _ in range(201)]

def sgcd(i, j):
    if memo[i][j] == 0:
        memo[i][j] = math.gcd(i, j)
    return memo[i][j]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from itertools import combinations

def main():
    k = int(input())
    res = 0
    memo = [[0 for _ in range(k+1)] for _ in range(k+1)]
    for i in range(1, k+1):
        for j in range(i, k+1):
            tmp = math.gcd(i,j)
            memo[i][j] = tmp
            memo[j][i] = tmp

    for i in range(1, k+1):
        res += i 

    for c in combinations(range(1,k+1),2):
        ab = memo[c[0]][c[1]]
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = memo[c[0]][c[1]]
        abc = memo[ab][c[2]]
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
k = int(input())

# a,bの最大公約数
def gcd(a, b):
   while b:
       a, b = b, a % b
   return a


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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
ans = 0

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

Submission status: Accepted

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

kotae = 0
K = int(input())

X = []
for i in range(1,K+1):
    for k in range(1,K+1):
        z = gcd(i,k)
        for l in range(1,K+1):
            kotae += gcd(z,l)
            
print(kotae)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
k = int(input())
ans = k**3 - (k-1)**3
for a in range(2,k+1):
  for b in range(2,k+1):
    tmp = math.gcd(a,b)
    for c in range(2,k+1):
      ans += math.gcd(tmp, c)
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

def solution():
    k = int(input())
    nums = []
    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:
                    nums.append(i)
                else:
                    nums.append(gcd(i,j,l))
    print(sum(nums))

solution()

Submission status: Accepted

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

ans = 0

lst = [[0 for _ in range(1, k+1)] for __ in range(k+1)]
cnt = [0 for _ in range(1, k+1)]


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

Submission status: Accepted

Solution code:
import math
 
K = int(input())
 
lists = []
for i in range(1, K + 1):
    for j in range(1, K + 1):
        mid = math.gcd(i, j)
        for k in range(1, K + 1):
            lists.append(math.gcd(mid, k))
 
print(sum(lists))

Submission status: Accepted

Solution code:
k = int(input())
r = 0
def gcd(num1, num2):
	if num2 == 0:
		return num1
	return gcd(num2, num1 % num2)
for a in range(1, k+1):
	for b in range(1, k+1):
		for c in range(1, k+1):
			r += gcd(a, gcd(b, c))
print(r)

Submission status: Accepted

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

n=int(input())
ans=0
for i in range(1,n+1):
	for j in range(i,n+1):
		for k in range(j,n+1):
			if i!=j!=k:
				ans+=6*gcd(gcd(i,j),k)
			elif i==j==k:
				ans+=i
			else:
				ans+=3*gcd(gcd(i,j),k)
print(ans)

Submission status: Accepted

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

res = 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):
            res += gcd(tmp,k)
print(res)

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

Submission status: Accepted

Solution code:
import math
def main():
  n = int(input())
  sum = 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):
        sum += math.gcd(g,k)
  print(sum)

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
import math

K=int(input())
r=0

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

Submission status: Accepted

Solution code:
import math

k = int(input()) + 1
ans = 0

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

print(ans)

Submission status: Accepted

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

def solve(K: int):
    K += 1
    ans = []
    for i in range(1, K):
        for j in range(1, K):
            for k in range(1, K):
                ans.append(math.gcd(i, math.gcd(j, k)))
    print(sum(ans))
    return

def gcd(x: int, y: int):
    """xとyの最大公約数を求める"""
    return gcd(y, x % y) if y else x
    
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    solve(K)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd
k=int(input())
r=range(1,k+1)
ans=0
for i in r:
  for j in r:
    for k in r:
      ans+=gcd(k,gcd(i,j))
print(ans)
# 間に合うはずなくない？？

Submission status: Accepted

Solution code:
from math import gcd 
k = int(input())
sss = 0
for a in range(1,k+1):
    for b in range(a+1,k+1):
        tmp = gcd(a,b)
        sss += 6*tmp
        for c in range(b+1,k+1):
            sss += 6*gcd(tmp,c)
sss += k*(k+1)//2
print(sss)

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

Submission status: Accepted

Solution code:
import math

number = int(input())
sumx = 0
N = number+1
for i in range(1,N):
  for j in range(1,N):
    for k in range(1,N):
      sumx = sumx + math.gcd(math.gcd(i,j), k)

print (sumx)

Submission status: Accepted

Solution code:
n = int(input())

from math import gcd

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

print(y)

Submission status: Accepted

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

K = int(input())

def GCD(x):
    return reduce(math.gcd, x)

s = 0

for x in combinations_with_replacement(range(1, K + 1), 3):
    if x[0] == x[1] == x[2]:
        s += GCD(x)
    elif x[0] == x[1] or x[1] == x[2] or x[0] == x[2]:
        s += GCD(x) * 3
    else:
        s += GCD(x) * 6

print(s)

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


K = int(input())

sum_gcd = 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):
            sum_gcd += gcd(g, k)
print(sum_gcd)

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

print(ans)

Submission status: Accepted

Solution code:
k = int(input())

# a,bの最大公約数
def gcd(a, b):
   while b:
       a, b = b, a % b
   return a

ansl = [[gcd(i, j) for i in range(201)] for j in range(201)]

ans = 0
for i in range(1,k+1):
    for j in range(1, k + 1):
        p = ansl[i][j]
        for k in range(1, k + 1):
            ans += ansl[p][k]

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
 
n = int(input())
answer = 0
 
def gcd(*numbers):
    return reduce(math.gcd,numbers)
 
for i in range(n):
    for j in range(n):
        for k in range(n):
            answer += gcd(i+1,j+1,k+1)
print(answer)

Submission status: Accepted

Solution code:
def euc(i, j):
    if i > j:
        i, j = j, i
    while j%i != 0:
        k = j - i
        if k > i:
            i, k = k, i
        j = i%k
        if j == 0:
            return(k)
        i = k
        if i > j:
            i, j = j, i
    return i


N = int(input())
sum = 0
for i in range(1, N + 1):
    for j in range(1, N + 1): 
        ij = euc(i, j)
        for k in range(1, N + 1):
            sum += euc(ij, k)
print(sum)

#print(euc(2, 3))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import itertools

k = int(input())
result = 0

for a in range(1, k+1):
  for b in range(a+1, k+1):
    for c in range(b+1, k+1):
      r = math.gcd(math.gcd(a, b), c)
      r *= 6
      result += r

for a in range(1, k+1):
  for b in range(a+1, k+1):
    r = math.gcd(a, b)
    r *= 6
    result += r
    
for a in range(1, k+1):
  result += a

print(result)

Submission status: Accepted

Solution code:
from math import gcd
def solve():
    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)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import math

K = int(input())
res=0
for a in range(1,K+1):
    for b in range(1,K+1):
        tmp = math.gcd(a,b)
        for c in range(1,K+1):
            #print(a,b,c)
            res += math.gcd(tmp,c)
print(res)

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

print(ans)

Submission status: Accepted

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

k = int(input())

result = []

for a in range(1, k + 1):
    for b in range(1, k + 1):
        for c in range(1, k + 1):
            result.append(calc_saidaikouyaku(a, calc_saidaikouyaku(b, c)))

print(sum(result))

Submission status: Accepted

Solution code:
import math

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

a_arr = list()
b_arr = list()

for k in k_arr:
    for a in k_arr:
        a_arr.append(a)

for b in k_arr:
    for k in k_arr:
        b_arr.append(b)

gcds = list(map(math.gcd,a_arr,b_arr))

total = 0
for c in k_arr:
    c_arr = [c] * k * k
    total += sum(map(math.gcd,gcds,c_arr))

print(total)

Submission status: Accepted

Solution code:
K = int(input())
 
from itertools import product
kumi = product(range(1, K+1), repeat=3)
 
from math import gcd
 
memo = [[None for i in range(K+1)] for i in range(K+1)]
# memo2 = {}
 
ans = 0
for a, b, c in kumi:
    a, b, c = sorted([a, b, c])
    if memo[a][b] is not None:
        v = memo[a][b]
    else:
        v = gcd(a, b)
        memo[a][b] = v
        memo[b][a] = v
    d = gcd(v, c)
    ans += d
 
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
s=0
K=int(input())+1
for a in range(1,K):
    for b in range(a,K):
        for c in range(b,K):
            if a==b==c:
                s+=a
                continue
            x=reduce(math.gcd,(a,b,c))
            if a==b or b==c or c==a:
                s+=x*3
            else:
                s+=x*6
print(s)

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(np.sum(nums))

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

Submission status: Accepted

Solution code:
n = int(input())

from math import gcd

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

print(y)

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

print(ans)

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

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce
def gcd_list(numbers):
   return reduce(gcd, numbers)
k=int(input())
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for x in range(1,k+1):
            ans+=gcd_list([i,j,x])
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(i,K+1):
        temp = gcd(i,j)
        for k in range(j,K+1):
            if i==j==k:
                ans+=gcd(temp, k)
            elif len({i,j,k}) == 2:
                ans+=gcd(temp, k)*3
            else:
                ans+=gcd(temp, k)*6
print(ans)

Submission status: Accepted

Solution code:
import math
def GCD(a,N):
    gcd = a[0]
    for i in range(1, N):
        gcd = math.gcd(gcd, a[i])
    return gcd


k = int(input())
# h, w = map(int, input().split())
# a = list(map(int, input().split()))
ans = 0
L = []
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            if i==1 or j==1 or l==1:
                ans += 1
            else:
                ans += GCD([i,j,l], 3)

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:
K = int(input())

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

def gcd_3(a,b,c):
    g = gcd(a,b)
    g = gcd(g,c)
    return g

sm = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            sm += gcd_3(a,b,c)

print(sm)

Submission status: Accepted

Solution code:
import math
k=int(input())
sum=k*(k+1)/2
for i in range(1,k+1):
    for j in range(i+1,k+1):
        m=math.gcd(i,j)
        sum+=6*m*(k-j+1)
        for l in range(j+1,k+1):
            n=math.gcd(m,l)
            if n==m:
                continue
            else:
                sum-=6*m-6*n
print(int(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):
        ab = gcd(a, b)
        for c in range(1, k + 1):
            ans += gcd(ab, c)

print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())
sum = 0

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


exit()
q = [[str(i)] for i in range(1, K+1)]
# print(q)
s = []
ans = 0
while q:
    a = q.pop()
    # print(q)@
    if len(a) == 3:
        # s.append(a)
        ans += int(a[-1])
        # print(a)
    else:
        for i in range(1, K+1):
            b = deepcopy(a)
            if int(b[-1]) <= i:
                b.append(b[-1])
            # b += str(gcd(i, int(b[-1])))
            else:
                b.append(str(i))
            q.append(b)
print(ans)
# print(s)
# print(s)
# ans = 0
# for d in s:
#     print(sorted(list(d))[0])

# 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 m in range (1,k+1):
            ans += math.gcd(tmp,m)

print(ans)

Submission status: Accepted

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

print(ans)