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


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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

from sys import stdin
import math

def I(): return int(stdin.readline().rstrip())


k = I()

ans = 0

for i in range(1,k+1):
    for j in range(1,k+1):
        tmp = math.gcd(i,j)
        if tmp != 1 :
            for w in range(1,k+1):
                ans += math.gcd(tmp,w)
        else:
            ans += k

print(ans)

Submission status: Accepted

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

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

def main():
    K = int(input())
    memo = [[-1] * 201 for i in range(201)]
    
    for a in range(1, 201):
        for b in range(1, 201):
            memo[a][b] = gcd(a, b)
    
    ans = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                ans += memo[memo[a][b]][c]
    print(ans)

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

Submission status: Accepted

Solution code:
k = int(input())
ans=0
def gcd(x,y):
  while y:
    x,y=y,x%y
  return x
 
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:
from sys import stdin, setrecursionlimit


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


def main():
    input = stdin.buffer.readline
    k = int(input())
    g = []
    for i in range(1, k + 1):
        gi = []
        for j in range(1, k + 1):
            gi.append(gcd(i, j))
        g.append(gi)
    ans = 0
    for a in range(k):
        for b in range(k):
            for c in range(k):
                tmp = g[a][b] - 1
                ans += g[tmp][c]
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
res = 0

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

print(res)

Submission status: Accepted

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

K=int(input())
ans=0
for a in range(1,K+1):
  for b in range(1,K+1):
    ab=gcd(a,b)
    if ab == 1:
      ans += K
    else:
      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
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
K = int(input())

s=0
for i in range(1,K+1):
    for j in range(1, K+1):
        for l in range(1, K+1):
            s += gcd(i, gcd(j,l))

print(s)

Submission status: Accepted

Solution code:
import math

K = int(input())
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

v = int(input())
sum = 0

for i in range(1, v + 1):
    for j in range(1, v + 1):
        for k in range(1, v + 1):
            sum += gcd(gcd(i, j), k)

print(sum)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())

total = 0
for l in range(1,K+1):
  for m in range(1,K+1):
    for n in range(1,K+1):
      total += gcd([l, m, n])

print(total)

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

Submission status: Accepted

Solution code:
import fractions
K = int(input())
ans = 0
dp = []
for i in range(0,K+1):
    dpp = []
    for j in range(0,K+1):
        dpp.append(0)
    dp.append(dpp)
def dpgcd(a,b):
    if dp[a][b] != 0:
        return dp[a][b]
    else:
        gg = fractions.gcd(a,b)
        dp[a][b] = gg
        dp[b][a] = gg
        return gg
for i in range(0,K):
    for j in range(0,K):
        g = dpgcd(i+1,j+1)
        for k in range(0,K):
            ans += dpgcd(g,k+1)
print(ans)

Submission status: Accepted

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

for a in range(1,K+1):
  for b in range(a+1,K+1):
    for c in range(b+1,K+1):
      total += math.gcd(a, math.gcd(b,c) ) * 6
      
for a in range(1,K+1):
  for b in range(a+1,K+1):
    total += math.gcd(a, b)*6

for a in range(1,K+1):
  total += a

print(total)

Submission status: Accepted

Solution code:
import math
from functools import reduce

ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

k=ni()

ans = 0
for a in range(1,k+1):
  for b in range(1,k+1):
    for c in range(1,k+1):
      arr = [a,b,c]
      r = reduce(math.gcd, arr)
      ans+=r

print(ans)

Submission status: Accepted

Solution code:
import math
k=int(input())
ans=0
for p in range(1,k+1):
    for q in range(1,k+1):
        gcd=math.gcd(p,q)
        for r in range(1,k+1):
            ans=ans+math.gcd(gcd,r)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
 
input = sys.stdin.readline
 
K = int(input())
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        n = math.gcd(i, j)
        for l in range(1, K + 1):
            ans += math.gcd(n, l)
 
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import math

def main():
    number_of_digit = int(input())
    
    result = 0
    for first in range(1,number_of_digit+1):
        for second in range(1,number_of_digit+1):
            first_gcd = math.gcd(first,second)
            if first_gcd == 1:
                result += number_of_digit
            else:
                for third in range(1,number_of_digit+1):
                    second_gcd = math.gcd(first_gcd,third)
                    result += second_gcd
    print(result)
            
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

import math
from functools import reduce

tmp = input().split()
K = int(tmp[0])

SGCD = 0


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

# def gcd(a,b,c):
#     for ga in range(a+1)[1:]:
#         if a % ga == 0:
#             Ga = a/ga
#             if b % Ga == 0 and c % Ga == 0:
#                 return Ga
        
#         if ga > a/2:
#             return 1

for c in range(K+1)[1:]:
    for b in range(c+1)[1:]:
        for a in range(b+1)[1:]:
            if not a == b and not b == c:
                SGCD = SGCD + 6*gcd(a,b,c)
            elif a == b and not b == c:
                SGCD = SGCD + 3*gcd(a,b,c)
            elif not a == b and b == c:
                SGCD = SGCD + 3*gcd(a,b,c)
            elif a == b and b == c:
                SGCD = SGCD + gcd(a,b,c)

print(int(SGCD))

Submission status: Accepted

Solution code:
from math import gcd
from itertools import product


def solve(string):
    k = int(string)
    ans = 0
    for a, b, c in product(range(1, k + 1), repeat=3):
        ans += gcd(gcd(a, b), c)
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
import math


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

print(S)

Submission status: Accepted

Solution code:
import math 
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
    
 
a = int(input())
 
ans = 0
 
for i in range(1,a+1):
    for j in range(1,i+1):
        for k in range(1,j+1):
            if i == j and i == k:
                ans += gcd(i,j,k)
            elif i == j or i == k or j == k:
                ans += gcd(i,j,k)*3
            else:
                ans += gcd(i,j,k)*6
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K = int(input())

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

print(S)

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

print(ans)

Submission status: Accepted

Solution code:
import math

def main():
    K = int(input())
    r = 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):
                r += math.gcd(t, k)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

main()

Submission status: Accepted

Solution code:
import numpy as np
k=int(input())
a=np.arange(1,k+1)
b=np.gcd.outer(np.gcd.outer(a,a),a)
c=b.sum()
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
K=int(input())
ans=0
for i in range(1,K+1):
    ans += i

for i in range(1,K):
    for j in range(i+1,K+1):
        ans += math.gcd(i,j)*6
        
for i in range(1,K-1):
    for j in range(i+1,K):
        for k in range(j+1,K+1):
            ans += math.gcd(i,math.gcd(j,k))*6
print(ans)

Submission status: Accepted

Solution code:
import unittest
from io import StringIO
import sys
import itertools
import math


def gcd(a, b, c):
    n = 1
    if a == b:
        if b != c:
            n = 3
    else:
        if b != c:
            n = 6
        else:
            n = 3
    return math.gcd(math.gcd(a, b), c) * n


def resolve():
    K = int(input())
    T = [i for i in range(1, K + 1)]
    Tr = itertools.combinations_with_replacement(T, 3)
    sumT = 0
    for a, b, c in Tr:
        sumT += gcd(a, b, c)
    print(sumT)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2"""
        output = """9"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """200"""
        output = """10813692"""
        self.assertIO(input, output)

    def test_add(self):
        input = """195"""
        output = """9997134"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

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

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

K = int(input())

t = []

for i in range(1, K+1):
    s = 0
    
    for j in range(1, K+1):
        s += gcd(i, j)
    t.append(s)
    
ans = 0

for i in range(1, K+1):
    for j in range(1, K+1):
        ans += t[gcd(i, j)-1]
        
print(ans)

Submission status: Accepted

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

sum_num=0
for i in range(1,k+1):
  for j in range(1,k+1):
    tmp=math.gcd(j,i)
    for l in range(1,k+1):
      s=math.gcd(l,tmp)
      sum_num+=s
      
print(sum_num)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import lru_cache, reduce
from math import gcd

K = int(input())

memo_b = [[None for _ in range(K)] for _ in range(K)]

def c_gcd(a, b):
    if memo_b[a-1][b-1] is not None:
        return memo_b[a-1][b-1]
    else:
        res = gcd(a, b)
        memo_b[a-1][b-1] = res
    return res

ans = 0
for i in range(1, K+1):
    for j in range(i+1, K+1):
        ans += 6 * c_gcd(i, j)
        for k in range(j+1, K+1):
            ans += 6*c_gcd(c_gcd(i, j), k)


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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import itertools

k = int(input())
comb = list(itertools.combinations_with_replacement([i for i in range(1,k+1)], 3))
ans = 0
for a,b,c in comb:
  permutation = len(set([a,b,c]))
  r = b % a
  while r > 0:
    b = a
    a = r
    r = b % a
    
  r = c % a
  while r > 0:
    c = a
    a = r
    r = c % a
    
  if permutation == 3:
    ans += 6 * a
  elif permutation == 2:
    ans += 3 * a
  else:
    ans += a
    
print(ans)

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

mem = dict()
s = 0


def gcd(n1, n2):
    while n2 != 0:
        n3 = n1 % n2
        n1 = n2
        n2 = n3
    return n1


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)
            s += gcd(tmp, c)


print(s)

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

Submission status: Accepted

Solution code:
import math
k = int(input())
d = [math.gcd(a, b) for a in range(1, k + 1) for b in range(1, k + 1)]
ans = [math.gcd(c, di) for c in range(1, k + 1) for di in d]
print(sum(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):
        a = math.gcd(i, j)
        for l in range(1,k+1):
            ans += math.gcd(a,l)
print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True

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


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

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
# sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True

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


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

Submission status: Accepted

Solution code:
import math, itertools


def g(a, b, c):
    return math.gcd(math.gcd(a, b), c)


k = int(input())
print(
    sum(
        g(a, b, c)
        for a, b, c in itertools.product(range(1, k + 1), repeat=3)))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
k = int(input())



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

print(sum)

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools

K = int(input())
 
res = 0
for i in range(1, K+1):
  for j in range(1, K+1):
    gcd1 = math.gcd(i, j)
    for k in range(1, K+1):
      if gcd1 == 1:
        res += 1
      else:
        res += math.gcd(gcd1, k)
print(res)

Submission status: Accepted

Solution code:
k=int(input())
ans=0

import math
for i1 in range(k):
    for i2 in range(k):
        for i3 in range(k):
            ans+=math.gcd(math.gcd(i1+1,i2+1),i3+1)
    
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):
    gcd1 = math.gcd(i, j)
    for k in range(1, K+1):
      if gcd1 == 1:
        res += 1
      else:
        res += math.gcd(gcd1, k)
print(res)

Submission status: Accepted

Solution code:
import math

K = int(input())

r = 0

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

print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math
ans = 0
for a,b,c in itertools.combinations_with_replacement(range(1,int(input())+1),3):
    G = math.gcd(a,math.gcd(b,c))
    if a==b==c:
        ans += G
    elif a==b or b==c:
        ans += 3*G
    else:
        ans += 6*G
    
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

K = int(input())

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    import math
    k=int(input())
    answer=0
    l=list(range(1,k+1))
    for x in range(1,k+1):
        for y in range(1,k+1):
            for z in range(1,k+1):
                answer+=math.gcd(math.gcd(x,y),z)
    print(answer)
resolve()

Submission status: Accepted

Solution code:
## C
from math import gcd
K = int(input()) +1
rt = 0
for x in range(1, K):
    for y in range(1, K):
        for z in range(1, K):
            rt += gcd(gcd(x,y), z)
print(rt)

Submission status: Accepted

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

map_list = [[0 for i in range(k+1)]for j in range(k+1)]
for i in range(1,k+1):
    for j in range(1,k+1):
        map_list[i][j]=math.gcd(i,j)

ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        gcd1 = map_list[i][j]
        for k in range(1,k+1):
            ans+=map_list[k][gcd1]
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

print(solve())

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

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

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

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

print(c)

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())
sum = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        AB_gcd = math.gcd(a, b)
        for c in range(1, K+1):
            sum += math.gcd(AB_gcd, c)

print(sum)

Submission status: Accepted

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

from math import gcd
ans = 0
for a in range(1, k):
    for b in range(1, k):
        ab = gcd(a, b)
        for c in range(1, k):
            ans += gcd(ab, c)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf
# @pysnooper.snoop()
#import pysnooper # debug
from functools import lru_cache

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect

def mutual_division_method(a, b):
    while not (a == 0 or b == 0):
        if a < b:
            b = b % a
        else:
            a = a % b
    if a == 0:
        return b
    else:
        return a



def gcd(a, b, c):
    #print('a, b, c', a, b, c) # debug
    d = mutual_division_method(a, b)
    e = mutual_division_method(d, c)
    #print('e', e) # debug
    return e

if __name__ == '__main__':
    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):
                ans = gcd(a, b, c)
                s += ans
    #print('s') # debug
    print(s)

    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Accepted

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

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):
        for z in range(1,k+1):
            
            ans += gcd(gcd(x,y),z)
print(ans)

Submission status: Accepted

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

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

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(a,K+1):
    for c in range(b,K+1):
      if a==b==c:
        ans += gcd(gcd(a,b),c)
      elif a==b or b==c or c==a:
        ans += gcd(gcd(a,b),c)*3
      else:
        ans += gcd(gcd(a,b),c)*6
      
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import product

def gcd(a, b):
    while a > 0 and b > 0:
        if a >= b:
            a %= b
        else:
            b %= a
    return max(a, b)

k = int(stdin.readline())
ls = [[0 for j in range(k+1)] for i in range(k+1)]
for i in range(1, k+1):
    for j in range(i, k+1):
        res = gcd(i, j)
        ls[i][j] = res
        ls[j][i] = res
_sum = 0
for i in product(range(1,1+k), repeat=3):
    _sum += ls[ls[i[0]][i[1]]][i[2]]
print(_sum)

Submission status: Accepted

Solution code:
from math import gcd as 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(j, k), i)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

def gcd_3(a, b, c):
    return gcd(gcd(a, b), c)
sum_gcd = 0
K = int(input())
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for l in range(1, K + 1):
            sum_gcd += gcd_3(i, j, l)

print(sum_gcd)

Submission status: Accepted

Solution code:
from math import gcd

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

    for i in range(1,K+1):
        for k in range(1,K+1):
            b = gcd(i, k)
            for m in range(1,K+1):
                ans += gcd(b, m)
    return ans

print(solve())

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

print(c)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
ans = 0

for i in range(1,K+1):
    for k in range(1,K+1):
        b = gcd(i, k)
        for m in range(1,K+1):
            ans += gcd(b, m)

print(ans)