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

memo = []
memolist = []
for i in range(1,K+1):
    if i != 1:
        memo.append(memolist)
    memolist = []
    for j in range(1,K+1):
        memolist.append(gcd(i,j))
memo.append(memolist)

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(memo[i-1][j-1],k)

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    ret = 0
    for a in range(1, N + 1):
        for b in range(1, N + 1):
            q = math.gcd(a, b)
            for c in range(1, N + 1):
                ret += math.gcd(q, c)
    print(ret)


if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
import math

K = int(input())

sum = 0
# for i in range(1,K+1):
#     for j in range(1,K+1):
#         for l in range(1,K+1):
#             sum += math.gcd(math.gcd(i,j),l)
for i in range(1,K+1):
    for j in range(1,K+1):
        if math.gcd(i,j)==1:
            sum += K
        else:
            for l in range(1,K+1):
                sum += math.gcd(math.gcd(i,j),l)


print(sum)

Submission status: Accepted

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

for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            a=math.gcd(i,j)
            a=math.gcd(a,k)
            if i==j and i==k:
              ans=ans+a
            elif i==j or i==k or j==k:
              ans=ans+3*a
            else:
              ans=ans+6*a
print(ans)

Submission status: Accepted

Solution code:
def gcd(m, n):
	if m % n == 0:
		return n
	return gcd(n, m % n)

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

resolve()

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

print(s)

Submission status: Accepted

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

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

K = int(input())

if K == 1:
    ans = 1
elif K == 2:
    ans = 9
else:
    ans = 0
    for i in range(1, K+1):
        ans += i
        for j in range(i+1, K+1):
            ans += gcd(i, j) * 6
            for k in range(j+1, K+1):
                ans += gcd(i, j, k) * 6

print(ans)

Submission status: Accepted

Solution code:
import math
"""
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
"""
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 = ans + gcd(a,b,c)
       """
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())

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

Submission status: Accepted

Solution code:
K = int(input())
sum = 0
class gcd:
    def __init__(self,a,b,c):
        self.a=a
        self.b=b
        self.c=c
        
    def gcds(self):
        if self.a>self.b:
            x=self.a
            y=self.b
        else:
            x=self.b
            y=self.a
            
        while x % y!=0:
            tem_y = y
            y = x % y 
            x = tem_y
       
        gcd_1 = y
        
        if gcd_1>self.c:
            x=gcd_1
            y=self.c
        else:
            x=self.c
            y=gcd_1
            
        while x % y!=0:
            tem_y = y
            y = x % y 
            x = tem_y
       
        return int(y)

for i in range(1,K+1):
    for j in range(i,K+1):
        for l in range(j,K+1):
            if i==j and j==l:
                sum = sum + gcd(i,j,l).gcds()
            elif i != j and j==l:
                sum = sum + gcd(i,j,l).gcds()*3
            elif i==j and j!= l:
                sum = sum + gcd(i,j,l).gcds()*3
            elif i!=j and j!=l:
                sum = sum + gcd(i,j,l).gcds()*6         
            
print(sum)

Submission status: Accepted

Solution code:
def main():
    s = 0
    n = int(input())
    for i in range(1, n+1):
        for j in range(1, n+1):
            gcd_tmp = gcd(i, j)
            for k in range(1, n+1):
                s += gcd(gcd_tmp, k)
    print(s)


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
K=int(sys.stdin.readline()[:-1])
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(k,math.gcd(i,j))

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    K = int(_in[0])  # type:int
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    x_arr = np.arange(1,K+1)
    cnt = np.gcd.outer(np.gcd.outer(x_arr,x_arr),x_arr).sum()
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

K = int(input()) + 1
d = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

def main():
  K=int(input())
  T=[[0]*-~K for _ in range(K+1)]

  for x in range(1,K+1):
    for y in range(1,K+1):
      T[x][y]=gcd(x,y)

  t=0
  for a in range(1,K+1):
    for b in range(1,K+1):
      for c in range(1,K+1):
        t+=T[a][T[b][c]]

  print(t)

main()

Submission status: Accepted

Solution code:
import math
from functools import lru_cache
from itertools import combinations


@lru_cache(maxsize=None)
def gcd2(a: int, b: int) -> int:
    return math.gcd(a, b)


def gcd3(a: int, b: int, c: int) -> int:
    return gcd2(gcd2(a, b), c)


def main() -> None:
    k = int(input())

    one_to_k = range(1, k + 1)
    s1 = sum(one_to_k)
    s2 = sum(gcd2(a, b) for a, b in combinations(one_to_k, 2))
    s3 = sum(gcd3(a, b, c) for a, b, c in combinations(one_to_k, 3))
    print(s1 + 6 * s2 + 6 * s3)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
import math


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


def main() -> None:
    k = int(input())

    one_to_k = range(1, k + 1)
    s1 = sum(one_to_k)
    s2 = sum(math.gcd(a, b) for a, b in itertools.combinations(one_to_k, 2))
    s3 = sum(gcd(a, b, c) for a, b, c in itertools.combinations(one_to_k, 3))
    print(s1 + 6 * s2 + 6 * s3)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
K = int(input())

ans = 0

import math

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
from math import gcd


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    K = int(_in[0])  # type:int
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    for a in range(1,K+1):
        cnt += a
        for b in range(a+1,K+1):
            ab = gcd(a,b)
            cnt += 6*ab
            for c in range(b+1,K+1):
                cnt += 6*gcd(ab, c)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    s = 0
    n = int(input())
    gd1s = []
    combs = {}
    '''
    for i in range(1, n+1):
        for j in range(1, n+1):
            try:
                gd1 = combs[(i, j)]
                combs[(j, i)] = gd1
                gd1s.append(gd1)
            except:
                gd1 = gcd(i, j)
                gd1s.append(gd1)
                combs[(i, j)] = gd1

    for k in range(1, n+1):
        for g in gd1s:
            s += combs[(g,k)]
    print(s)
    '''
    for i in range(1, n+1):
        for j in range(1, n+1):
            gcd_tmp = gcd(i, j)
            for k in range(1, n+1):
                s += gcd(gcd_tmp, k)
    print(s)

                

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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


def main():
    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):
                tmp = gcd(a, b)
                ans += gcd(tmp, c)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd

def gcdx(x,y,z):
    return gcd(gcd(x,y),z)
n = int(input())
a = 0
longp = []

for i in range(1,n+1):
    for j in range(1,n+1):
        for p in range(1,n+1):
            a += gcdx(i,j,p)
           
print(a)

Submission status: Accepted

Solution code:
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a
  
def gcd3(a,b,c):
    return gcd(gcd(a,b),c)
    
k= int(input())    
total = 0
for i in range(1,k+1):
    for ii in range(1,k+1):
        for iii in range(1,k+1):
            total += gcd3(i,ii,iii)
 
print(total)

Submission status: Accepted

Solution code:
import math

k = int(input())

ans = 0

for i in range(k):
  for j in range(i,k):
    for h in range(j, k):
      if i == j == h:
        ans += math.gcd(math.gcd(i+1, j+1), h+1)
      elif (i != j) and (j != h) and (i != h):
        ans += math.gcd(math.gcd(i+1, j+1), h+1) * 6
      else:
      	ans += math.gcd(math.gcd(i+1, j+1), h+1) * 3
      
print(ans)

Submission status: Accepted

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

def main():
	mp = {}
	k = int(input())	
	s = 0
	for i in range(1, k + 1):
		for j in range(1, k + 1):
			d = gcd(j, i)
			if d not in mp:
				mp[d] = 0 
			mp[d] += 1 
	for key in mp:
		for j in range(1, k+1):
			s += gcd(key, j) * mp[key]
	print(s)		
	return 0

main()

Submission status: Accepted

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

ans = 0
if n == 1:
    ans = 1
else:
    for i in range(n):
        i = i + 1
        for j in range(n):
            j = j + 1
            for k in range(n):
                k = k + 1
                ij = math.gcd(i,j)
                ans = ans + math.gcd(ij,k)

print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())
s = 0

i = 1
j = 1
k = 1

while i <= N:
	j = i + 1
	while j <= N:
		k = j + 1
		l = math.gcd(i,j)
		while k <= N:
			s = s + math.gcd(l,k)*6
			k = k + 1
		s = s + math.gcd(i,j)*6
		j = j + 1
	s = s + math.gcd(i,i)
	i = i + 1

print(s)

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0
if n == 1:
    ans = 1
else:
    for i in range(n):
        i = i + 1
        for j in range(n):
            j = j + 1
            for k in range(n):
                k = k + 1
                ij = gcd(i,j)
                ans = ans + gcd(ij,k)

print(ans)

Submission status: Accepted

Solution code:
import math


N = int(input())

s = 0

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

print(s)

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

print(res)

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

gcd_sum = [0] * 201
res = 0

for i in range(1, k + 1):
	for j in range(1, k + 1):
		gcd_sum[i] += gcd(i, j)

for a in range(1, k + 1):
	for b in range(1, k + 1):
		g = gcd(a, b)
		res += gcd_sum[g]

print(res)

Submission status: Accepted

Solution code:

#lista2=[16,100,4000]
#ans2=lista2[0]

def gcd2(a,b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b

def gcd3(lista):
    ans=lista[0]
    for i in range(1,3):
        ans=gcd2(ans,lista[i])
    return ans

K=int(input())
sums=0
for i in range(K):
    for j in range(K):
        for k in range(K):
            sums+=gcd3([1+i,1+j,1+k])
print(sums)

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

k = int(input())

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
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
n = int(input())
print(sum([gcd(gcd(i,j),k) for i in range(1,n+1) for j in range(1,n+1) for k in range(1,n+1)]))

Submission status: Accepted

Solution code:

# -*- coding: utf-8 -*-
import math
K=int(input())

count=0

for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            cal=math.gcd(math.gcd(a,b),c) 
            #print(cal)
            if a!=b and b!=c:
                count+=cal*6
            elif a==b==c:
                count+=cal
            else:
                count+=cal*3
            

print(count)

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):
      x=math.gcd(i,j)
      for h in range(1,n+1):
        p=math.gcd(x,h)
        sum=sum+p
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):
        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, 1):
    for b in range(1, k + 1, 1):
        g = gcd(a, b)
        for c in range(1, k + 1, 1):
            ans += gcd(g, c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
t = 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):
            t += gcd(c, d)

print(t)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def gcd(x, y, z):
    ex = math.gcd(x, y)
    return math.gcd(ex, z)

def comb(x):
    num = len(set(x))
    if num == 1:
        return 1
    elif num == 2:
        return 3
    else:
        return 6

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

k = int(input())

res = 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):
            res += math.gcd(x, l)

print(res)

Submission status: Accepted

Solution code:
from math import gcd
n = int(input())
s = 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):
			s += gcd(x,k)
print (s)

Submission status: Accepted

Solution code:
n = int(input())
import math
from fractions import gcd
s = 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):
			s += math.gcd(x,k)
print (s)

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
#input
K = int(input())

#output
import math

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

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import fractions

k = input()

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

Submission status: Accepted

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

print(result)

Submission status: Accepted

Solution code:
import math
gcd1 = 0
gcd3 = 0
gcd6 = 0
N = int(input())
for a in range(1, N + 1):
  for b in range(1, N + 1):
    for c in range(1, N + 1):
      if a < b < c:
        gcd6 += math.gcd(a,math.gcd(b, c))
      elif a == b < c or a < b == c:
        gcd3 += math.gcd(a,math.gcd(b, c))
      elif a == b == c:
        gcd1 += math.gcd(a,math.gcd(b, c))
print(6 * gcd6 + 3 * gcd3 + gcd1)

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
ans = 0
N = int(input())
if N <= 5:
  for i in range(N):
    for j in range(N):
        for k in range(N):
            ans += math.gcd(math.gcd(i+1,j+1),math.gcd(j+1,k+1))
else:
    for i in range(N):
        ans += i+1
    for i in range(N-1):
        for j in range(i+1,N):
            ans += 6*math.gcd(i+1,j+1)
    for i in range(N-2):
        for j in range(i+1,N-1):
            for k in range(j+1,N):
                ans += 6*math.gcd(math.gcd(i+1,j+1),math.gcd(j+1,k+1))
print(ans)

Submission status: Accepted

Solution code:
K = int(input())

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

ans = 0

for a in range(1, K+1):
  for b in range(1, K+1):
    r = gcp(a, b)
    for c in range(1, K+1):
      ans += gcp(r, c)
      
print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())
li = []
ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        li.append(math.gcd(i,j))
for g in li:
    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())
sum = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for m in range(1, k + 1):
            sum += gcd(gcd(i, j), m)
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K = int(input())

ans = 0
'''
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
           ab = math.gcd(i,j)
           abc = math.gcd(ab,l)
           print(i,j,l)
           ans += abc
'''
           
for i in range(1,K+1):
    for j in range(i,K+1):
        for l in range(j,K+1):
           ab = math.gcd(i,j)
           abc = math.gcd(ab,l)
           #print(i,j,l)
           if i == j and j == l:#３つすべて同じ数字のとき
               abc = abc
               #print('abc',abc)
           elif i == j or j == l or i == l:#２つの数字が同じ時
               abc = 3*abc
               #print('abc',abc)
           elif i != j and i != l and j != l:#３つの数字がすべて違うとき
               abc = 6*abc
               #print('abc',abc)
           
           ans += abc
           #print(ans,abc)


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

from math import gcd
 
res=0
 
for n in range(1,k+1):
    for i in range(1,k+1):
        for j in range(1,k+1):

            res+=gcd(gcd(n,i),j)

print(res)

Submission status: Accepted

Solution code:
from math import gcd

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

print(ans)

Submission status: Accepted

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

gcd = []
for i in range(210):
    gcd.append([-1] * 210)
for i in range(205):
    for j in range(205):
        gcd[i][j] = GCD(i, j)

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

Submission status: Accepted

Solution code:
import math
k = int(input())
a = []
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):
            y = math.gcd(l, x)
            a.append(y)
print(sum(a))

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
gcd_sum=[0]*201
ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        gcd_sum[i] += gcd(i,j)
for i in range(1,k+1):
    for j in range(1,k+1):
        g = gcd(i,j)
        ans += gcd_sum[g]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def gcd(n, m):
    if m == 0:
        return n
    return gcd(m, n % m)

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

Submission status: Accepted

Solution code:
n = int(input())
sum = 0

def gcd(a, b):
    if a < 1 or b < 1 :
        if a > 0:
            return a
        return b
    remainder = 0
    if a % b == 0 :
        return b
    remainder = a % b
    a = b
    b = remainder
    return gcd(a, b)

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

gcd = [[0]*K for i in range(K)]
L = [0]*K

for a in range(1,K+1):
    for b in range(1,K+1):
        A = fractions.gcd(a,b)
        gcd[a-1][b-1] = A
        L[A-1] +=1

sum=0
for c in range(1,K+1):
    for i in range(1,K+1):
        B = gcd[c-1][i-1]
        sum +=  B * L[i-1]

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K=int(input())
sum=0


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

print(sum)

Submission status: Accepted

Solution code:
import math
from functools import reduce

k = int(input())

answer = (1+k)*k/2

for i in range(1, k):
  for j in range(i+1, k+1):
    answer += 6*math.gcd(i, j)
    
for x in range(1, k-1):
  for y in range(x+1, k):
    for z in range(y+1, k+1):
      answer += 6*reduce(math.gcd, [x, y, z])
      
print(int(answer))

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

print(total)

Submission status: Accepted

Solution code:
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math
from math import gcd


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

print(ans)