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:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N = I()
 
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:
from math import gcd
K = int(input())
ans = 0
for a in range(1, K + 1) :
  for b in range(1, K + 1) :
    for c in range(1, K + 1) :
      ans += gcd(gcd(a, b), c)
      
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
K = int(input())
g = [[0 for i in range(201)] for j in range(201)]


def gcd(x, y):
    if x*y == 0:
        return x+y
    if x == y:
        return x
    elif x > y:
        return gcd(y, x)
    else:
        return gcd(y%x, x)
    
for i in range(201):
    for j in range(201):
        g[i][j] = gcd(i, j)
    
s = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            s += g[a][g[b][c]]
print(s)

Submission status: Accepted

Solution code:
import math

n=int(input())
 
def diffs(n):
  total=0
  for k in range(1,n):
    for l in range(k+1,n):
      total+=math.gcd(n,math.gcd(k,l))*6
  for k in range(1,n):
    total+=math.gcd(n,k)*6
  total += n
  return total

sum=0
for i in range(1,n+1):
  sum += diffs(i)

print(sum)

Submission status: Accepted

Solution code:
K = int(input())
def gcd(a, b):
    while b != 0:
        r = a%b
        a = b
        b = r
    return a
gcd_ab = [gcd(a,b) for a in range(1, K+1) for b in range(1, K+1)]
gcd_abc = [gcd(a_b,c) for a_b in gcd_ab for c in range(1, K+1)] 
print(sum(gcd_abc))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())
sum = 0

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

print(sum)

Submission status: Accepted

Solution code:
import math
import numpy as np
import numba
import math

n = int(input())

@numba.jit
def main(n):
  lis = np.zeros((n, n, n))
  for i in range(n):
    for j in range(n):
      for k in range(n):
        lis[i][j][k] = math.gcd(math.gcd(i+1, j+1), k+1)
  return int(np.sum(lis))

print(main(n))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

N = int(input())
num_list = [[i+1 for i in range(N)],[i+1 for i in range(N)],[i+1 for i in range(N)]]
sum = 0
for a in num_list[0]:
    for b in num_list[1]:
        for c in num_list[2]:
            sum = sum + gcd(a,b,c)

print(sum)

Submission status: Accepted

Solution code:
import numpy as np


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

print(ans)

Submission status: Accepted

Solution code:
import io


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

    return a


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

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(i,K+1):
        for k in range(1,K+1):
            if i != j:
                count += 2 * math.gcd(math.gcd(i,j),k)
            else:
                count += math.gcd(math.gcd(i,j),k)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

print(ret)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:

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

def permutation(n):
    if n == 1:
        return 1
    elif n == 2:
        return 3
    else:
        return 6

def solve():
    n = int(input())
    s = 0
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            for k in range(j, n + 1):
                diff = 1
                if j != k:
                    diff += 1
                if i != j:
                    diff += 1
                s += gcd(i, gcd(j, k)) * permutation(diff)

    print(s)
    return s

solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 9)

K = int(input())

def gcd(x,y):
    while y:
        x, y = y, x % y
    return x

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 9)

K = int(input())

def gcd(x,y):
    while y:
        x, y = y, x % y
    return x

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)

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:
from math import gcd
import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)

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

n = int(input())

res = 0
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 sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)

K = int(input())

def gcd(x,y):
    while y:
        x, y = y, x % y
    return x

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

print(ans)

Submission status: Accepted

Solution code:
K = int(input())

def gcd(x,y):
    while y:
        x, y = y, x % y
    return x

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

print(ans)

Submission status: Accepted

Solution code:
import math
K = int(input())
N = 0
 
for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            g = math.gcd(math.gcd(a,b),c)
            if(a != b or a != c):
                if(a != b and a != c and b != c):
                    g *= 6
                else:
                    g *= 3
            N += g
 
print(N)

Submission status: Accepted

Solution code:
import math
K = int(input())
N = 0
 
for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            g = math.gcd(a,b)
            g = math.gcd(g,c)
            if(a != b or a != c):
                if(a != b and a != c and b != c):
                    g *= 6
                else:
                    g *= 3
            N += g
 
print(N)

Submission status: Accepted

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

for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            g = math.gcd(a,b)
            g = math.gcd(g,c)
            if(a != 1 or b != 1 or c != 1 ):
                if(a != b or a != c):
                    if(a != K or b != K or c != K ):
                        if(a != b and a != c and b != c):
                            g *= 6
                        else:
                            g *= 3
            N += g

print(N)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
valSum = 0

for A in range(1,K+1):
    for B in range(1,K+1):
        for C in range(1,K+1):
            valSum += gcd(gcd(A,B),C)

print(valSum)

Submission status: Accepted

Solution code:
i=int(input())
import math
list=[]
box=[]
for a in range(1,i+1):
    for b in range(1,i+1):
        x=math.gcd(a,b)
        for c in range(1,i+1):
            list.append(math.gcd(x,c))
print(sum(list))

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());a=0
x=[]
for i in range(k):
    d=0
    for j in range(k):
        d+=gcd(i+1,j+1)
    x.append(d)
for i in range(k):
  for j in range(k):
      a+=x[gcd(i+1,j+1)-1]
print(a)

Submission status: Accepted

Solution code:
from math import gcd
K=int(input())
ans=0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            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(i, K+1):
        for k in range(j, K+1):
            if i==j and i==k: g = i
            elif (i==j and i!=k):
                g = math.gcd(i,math.gcd(j,k))*3
            elif (i==k and i!=j):
                g = math.gcd(i,math.gcd(j,k))*3
            elif (j==k and i!=j):
                g = math.gcd(i,math.gcd(j,k))*3
            else : g = math.gcd(i,math.gcd(j,k))*6
            ans += g
print(ans)

Submission status: Accepted

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

for a in range(1,K+1):
  for b in range(1,K+1):
    for c in range(1,K+1):
      sum += gcd(gcd(a,b),c);

print(sum)

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:
# gcdをmathからimport
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):
            # gcd(i,j)とkのgcdを計算
            # 1番小さい数字が制限になる
            ans += gcd(gcd(i, j), k)
 
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
sum = (n - 1) * (n - 1) + n * n + n * (n - 1)
for i in range(2, n + 1):
    for j in range(i, n + 1):
        for k in range(j, n + 1):
            otv = math.gcd(math.gcd(i, j), k)
            if i != j and j != k:
                otv *= 6
            elif i != j or j != k:
                otv *= 3
            sum += otv
print(sum)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
from sys import stdin
import itertools

def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
def main():
  N = int(stdin.readline())
  N_list = list(range(1, N+1))
  
  ans = 0
  
  for xs in itertools.combinations_with_replacement(N_list, 3):
    if len(set(xs)) == 1:
      ans += gcd(xs[0], xs[1], xs[2])
    elif len(set(xs)) == 2:
      ans += 3*gcd(xs[0], xs[1], xs[2])
    else:
      ans += 6*gcd(xs[0], xs[1], xs[2])
      
  print(ans)

if __name__ == '__main__':
  main()

Submission status: Accepted

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

    
def main():
	num = int(input())
	sum = 0
	for i in range(num):
		a = i+1
		for j in range(num):
			b = j+1
			for k in range(num):
				c = k+1
				sum += math.gcd(math.gcd(a,b),c)
	print (sum)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
k = int(input())
import itertools
def GCD(a,b):
    if(a%b == 0):
        return b
    else:
        return GCD(b,a%b)
total = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            total += GCD(GCD(a,b),c)
print(total)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

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

print(ans)

Submission status: Accepted

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

n = int(input())+1
a = int(0)
for i in range(1, n):
  for j in range(1, n):
    b = math.gcd(i,j)
    for k in range(1, n):
      a = a + math.gcd(b,k)
print("{}".format(a))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
# import numpy as np
from math import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10 ** -7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)


K = inp()

def gcd_3(x, y, z):
    return gcd(gcd(x, y), z)

dic = {} # key: (i, j, k), el: gcd(i, j, k)
ans = 0
for i in range(1, K + 1):
    for j in range(i, K + 1):
        # tmp = gcd(i, j)
        for k in range(j, K + 1):
            # a = gcd(tmp, k)
            # a = gcd(gcd(i, j), k)
            a = gcd_3(i, j, k)
            if i == j == k:
                ans += a
            elif i == j or j == k:
                ans += a * 3
            else:
                ans += a * 3 * 2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
# import numpy as np
from math import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10 ** -7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)


K = inp()

dic = {} # key: (i, j, k), el: gcd(i, j, k)
ans = 0
for i in range(1, K + 1):
    for j in range(i, K + 1):
        # tmp = gcd(i, j)
        for k in range(j, K + 1):
            # a = gcd(tmp, k)
            a = gcd(gcd(i, j), k)
            if i == j == k:
                ans += a
            elif i == j or j == k:
                ans += a * 3
            else:
                ans += a * 3 * 2
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

k = int(input())

sum_count = 0
for i in range(1, k+1):
    a = i
    for j in range(1, k+1):
        b = j
        for l in range(1, k+1):
            c = l
            sum_count = sum_count + gcd(a,b,c)

print(sum_count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())

res = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    a = math.gcd(i,j)
    for k in range(1,n+1):
      res += math.gcd(a,k)
print(res)

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:
#ユークリッドの互除法

def euclid(m,n):
    # m > n となるように入れ替え
    if m < n:
        m, n = n, m
    while n != 0:
        m, n = n, m % n
    return m


num = int(input('')) #input()で入力を受け取ってint()で数字型に変換します。
ans_sum = 0

for a in range(1,num+1):
    for b in range(1,num+1):
        gcd_ab = euclid(a,b)
        for c in range(1,num+1):
            ans_sum = ans_sum + euclid(gcd_ab,c)

ans_sum = ans_sum

print(ans_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):
            x = math.gcd(math.gcd(i, j), k)
            if i == j == k:
                ans += x
            elif i != j != k:
                ans += x * 6
            else:
                ans += x * 3

print(ans)

Submission status: Accepted

Solution code:
def gcd(a, b):
    if a < b:
        a, b = b, a
    return b if a % b == 0 else gcd(b, a%b)
def solve(k):
    ans = 0
    for a in range(1, k+1):
        for b in range(1, k+1):
            d = gcd(a, b)
            if d in [1,2,3,5,7,11,13]:
                num = k // d
                ans += num * d + (k - num)
            else:
                for c in range(1, k+1):
                    ans += gcd(c, d)
    return ans

k = int(input())
print(solve(k))

Submission status: Accepted

Solution code:
import math

ans = 0
K = int(input())
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:
import itertools
import math
K = [i for i in range(1, int(input()) + 1)]
numbers = itertools.combinations_with_replacement(K, 3)

sum_gcd = 0
for i in numbers:
    a = math.gcd(i[0], i[1])
    b = math.gcd(a, i[2])

    if i[0] != i[1] != i[2]:
        sum_gcd += b * 6
    elif i[0] == i[1] == i[2]:
        sum_gcd += b
    else:
        sum_gcd += b * 3

print(sum_gcd)

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

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

Submission status: Accepted

Solution code:
import math


def main():
    k: longlong
    result: longlong

    k = int(input())

    result = 0

    for x in range(1, k + 1):
        for y in range(1, k + 1):
            for z in range(1, k + 1):
                temp = math.gcd(math.gcd(x, y), z)
                result += temp
    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math 

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

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(x,y,z)
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools

K = int(input())
K = [i for i in range(1, K+1)]
p = itertools.combinations_with_replacement(K, 3)
p = list(p)
g = 0

for i in p:
    a = math.gcd(i[0], i[1])
    b = math.gcd(a, i[2])
    if i[0] != i[1] != i[2]:
        g += b*6
    elif i[0] == i[1] == i[2]:
        g += b
    else:
        g += b*3

print(g)

Submission status: Accepted

Solution code:
import math

K = int(input())
g = 0
for i in range(1, K+1):
    for j in range(1, i+1):
        for k in range(1, j+1):
            a = math.gcd(i, j)
            b = math.gcd(a, k)
            if i != j != k:
                g += b*6
            elif i == j == k:
                g += b
            else:
                g += b*3

print(g)

Submission status: Accepted

Solution code:
#import numpy
from math import gcd

N = int(input())
ans = 0

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

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

Submission status: Accepted

Solution code:
k, = (int(x) for x in input().split())
 
n = 3
c = {} 
t = 0
for x in range(k, 0, -1):
    c[x] = (k // x) ** n
    c[x] = c[x] - sum(c[x * y] for y in range(2, k // x + 1))
    s = c[x] * x
    t += s
    # t = t % 1000000007
 
print(t)

Submission status: Accepted

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

Submission status: Accepted

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

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

for a in range(K):
  for b in range(K):
    tmp = gdc(b+1, a+1)
    for c in range(K):
      total += gdc(c+1, tmp)
print(total)

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

Submission status: Accepted

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

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

for a in range(K):
  for b in range(K):
    for c in range(K):
      total += gdc(c+1, gdc(b+1, a+1))
print(total)

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:
#!/usr/bin/python3

k = int(input())

c = [ 0 for i in range(k + 1) ]

for i in range(k, 0, -1):
    c[i] = (k // i) ** 3
    for j in range(2, k + 1):
        if i * j > k:
            break
        else:
            c[i] -= c[i * j]

s = 0
for i in range(1, k + 1):
    s += i * c[i]

print(s)

Submission status: Accepted

Solution code:
import math

k = input()
k = int(k)
total = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        g = math.gcd(i,j)
        for l in range(1,k+1):
            total += math.gcd(g,l)
print(total)

Submission status: Accepted

Solution code:
K = int(input())

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


gcds = [[gcd(i, j) for j in range(K + 1)] for i in range(K + 1)]

s = 0
for i in range(1, K + 1):
  for j in range(1, K + 1):
    for k in range(1, K + 1):
      s += gcds[gcds[i][j]][k]

print(s)

Submission status: Accepted

Solution code:
K = int(input())

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


gcds = [[0 for _ in range(K + 1)] for _ in range(K + 1)]

for i in range(1, K + 1):
  for j in range(1, K + 1):
    gcds[i][j] = gcd(i, j)

s = 0
for i in range(1, K + 1):
  for j in range(1, K + 1):
    for k in range(1, K + 1):
      s += gcds[gcds[i][j]][k]

print(s)

Submission status: Accepted

Solution code:
from math import gcd
import numpy as np


def sum_gcd(k: int) -> int:
    # a > b > c
    sum_p1 = 0
    for h in range(1, k + 1):
        for i in range(1, h):
            for j in range(1, i):
                sum_p1 += gcd(gcd(h, i), j)
                
    # a = b > c and a > b = c
    sum_p2 = 0
    for h in range(1, k + 1):
        for i in range(1, h):
            sum_p2 += gcd(h, i)

    # a = b = c
    sum_p3 = k * (k + 1) //2

    answer = sum_p1 * 6 + sum_p2 * 6 + sum_p3
    
    return answer


def main():
    k = int(input().rstrip())
    print(sum_gcd(k))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from typing import List, NoReturn


def sum_gcd(k: int) -> int:
    cache: List[List[List[int]]] = [[[0] * (k + 1) for i in range(k + 1)] for j in range(k + 1)]

    def cached_gcd(a: int, b: int, c: int) -> int:
        k0: int
        k1: int
        k2: int
        k0, k1, k2 = sorted([a, b, c])

        result: int = cache[k0][k1][k2]
        if result > 0:
            return result
        else:
            result = math.gcd(math.gcd(k0, k1), k2)
            cache[k0][k1][k2] = result
            return result

    answer: int = 0
    for h in range(1, k + 1):
        for i in range(1, k + 1):
            for j in range(1, k + 1):
                answer += cached_gcd(h, i, j)

    return answer


def main() -> NoReturn:
    k: int = int(input().rstrip())
    print(sum_gcd(k))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#author : dokueki

import sys
from math import gcd
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)
sys.setrecursionlimit(10**7)
mod = 1000000007


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def Gcd(a, b, c):
    return gcd(a, gcd(b, c))


def main():
    n = int(sys.stdin.readline())
    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, j, k)
    print(ans)


if __name__ == "__main__":
    try:
        IOE()
    except:
        pass
    main()

Submission status: Accepted

Solution code:
def gcd(a,b):
	i = a
	j = b
	while i!=j:
		if i > j:
			i = i-j
		else :
			j = j - i
	return i

k = int(input())

ret = 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,gcd(j,l))
			if (i == j) and (j == l):
				m = 1
			elif (i == j) or (j == l) or (l == i):
				m = 3
			else :
				m = 6
			#print("i:",i," j:",j," l:",l," gcd:", g," m:",m)
			ret += g*m
print(ret)

Submission status: Accepted

Solution code:
def gcd(a, b):
    if a%b==0: return b
    return gcd(b, a%b)
K=int(input())
sum=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: sum+=a
            elif a!=b!=c: sum+=gcd(gcd(a,b),c)*6 
            else: sum+=gcd(gcd(a,b),c)*3
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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