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 fractions import gcd


def memo(fnc):

    cache = {}

    def inner(*args):

        if args in cache:
            return cache[args]
        cache[args] = fnc(*args)
        return cache[args]

    return inner


@memo
def my_gcd(m, n):

    return gcd(m, n)


k = int(input())
ans = 0
for i in range(1, k+1):
    for j in range(i, k+1):
        g1 = my_gcd(i, j)
        for l in range(j, k+1):
            if i == j == l:
                w = 1
            elif i == j or j == l:
                w = 3
            else:
                w = 6
            ans += w * my_gcd(g1, l)


print(ans)

Submission status: Accepted

Solution code:
import numpy as np

K = int(input())
 
x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
print(nums.sum())

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
 
K = int(read())
 
x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
print(nums.sum())

Submission status: Accepted

Solution code:
from functools import reduce
from math import gcd
 
 
k = int(input())
B=list()
D=list()
 
for i in range(1,k+1):
  for j in range(1,k+1):
        a=[i,j]
        b = reduce(gcd,a)
        B.append(b)
 
for i in B:
    for k in range(1,k+1):
        c = [i,k]
        d = reduce(gcd,c)
        D.append(d)
print(sum(D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b != 0 :
        t = a % b
        a = b
        b = t
    return a
        
sum = 0
N=int(input(""))
for a in range(1, N + 1):
    for b in range(1, N + 1):
        x = gcd(a, b)
        if x == 1:
            sum = sum + N
        elif x == 2:
            sum = sum + (N + 1) // 2 + N // 2 * 2
        elif x == 3:
            sum = sum + N // 3 * 3 + (N - N // 3)
        elif x == 5:
            sum = sum + N // 5 * 5 + (N - N // 5)
        else:
            for c in range(1, N + 1):
                sum = sum + gcd(x, c)
print(sum)

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b != 0 :
        t = a % b
        a = b
        b = t
    return a
        
sum = 0
N=int(input(""))
for a in range(1, N + 1):
    for b in range(1, N + 1):
        x = gcd(a, b)
        if x == 1:
            sum = sum + N
        elif x == 2:
            sum = sum + (N + 1) // 2 + N // 2 * 2
        elif x == 3:
            sum = sum + N // 3 * 3 + (N - N // 3)
        else:
            for c in range(1, N + 1):
                sum = sum + gcd(x, c)
print(sum)

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b != 0 :
        t = a % b
        a = b
        b = t
    return a
        
sum = 0
N=int(input(""))
for a in range(1, N + 1):
    for b in range(1, N + 1):
        x = gcd(a, b)
        if x == 1:
            sum = sum + N
        elif x == 2:
            sum = sum + (N + 1) // 2 + N // 2 * 2
        else:
            for c in range(1, N + 1):
                sum = sum + gcd(x, c)
print(sum)

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b != 0 :
        t = a % b
        a = b
        b = t
    return a
        
sum = 0
N=int(input(""))
for a in range(1, N + 1):
    for b in range(1, N + 1):
        x = gcd(a, b)
        if x == 1:
            sum = sum + N
        else:
            for c in range(1, N + 1):
                sum = sum + gcd(x, c)
print(sum)

Submission status: Accepted

Solution code:
from numba import jit

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

@jit
def main2(K):
    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)
    return ans    


K = int(input())
print(main2(K))

Submission status: Accepted

Solution code:
from numba import jit

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

@jit
def main2(K):
    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)
    return ans    


K = int(input())
print(main2(K))

Submission status: Accepted

Solution code:
from math import gcd

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

N = int(input())
wa = 0
for i in range (1,N+1):
  for j in range (1,N+1):
    for h in range (1,N+1):
      wa += gcd(gcd(i,j),h) 
print(wa)

Submission status: Accepted

Solution code:
import math

def nyu():
    K = int(input())
    return K


def check(K ):
    sum =0

    for k1 in range(1,K+1):
        for k2 in range(1,K+1):
            for k3 in range(1,K+1):
                sum +=  (math.gcd(k1,math.gcd(k2,k3)))
    print(sum)
     
            
K = nyu()
check(K)
#print(S)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
 
def max(a,b):
    if a>b:
        return a
    else:
        return b
 
def min(a,b):
    if a<b:
        return a
    else:
        return b
 
def gcd(a,b):
    p = max(a,b)
    q = min(a,b)
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d
 
 
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:
import math
from math import gcd
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 += gcd(gcd(a,b),c)
print(ans)

Submission status: Accepted

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

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

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 j == k:
        ans += i
      elif i == j:
        ans += gcd(i, k) * 3
      elif j == k:
        ans += gcd(i, j) * 3
      else:
        ans += gcd3(i, j, k) * 6
print(ans)

Submission status: Accepted

Solution code:
from math import*
K=int(input())
N=0
k=K*K
d1=[0]*int(k)
i=0
j=0
for b in range(K):
    for c in range(K):
        d1[i]=gcd((c+1),(b+1))
        i+=1
for a in range(K):
    for x in d1:
        N+=gcd(x,(a+1))
print(N)

Submission status: Accepted

Solution code:
import math
K=int(input())
N=0
k=K*K
d1=[0]*int(k)
i=0
j=0
for b in range(K):
    for c in range(K):
        d1[i]=math.gcd((c+1),(b+1))
        i+=1
for a in range(K):
    for x in d1:
        N+=math.gcd(x,(a+1))
print(N)

Submission status: Accepted

Solution code:
from scipy import gcd
from collections import Counter

def sum_of_gcd(N):
    temp = [gcd(i, k) for i in range(1, N+1) for k in range(1, N+1)]
    counts = Counter(temp)

    return sum([gcd(c, i)*counts[c] for i in range(1, N+1) for c in counts.keys()])

print(sum_of_gcd(int(input())))

Submission status: Accepted

Solution code:
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial
def gcd(a,b):
	a,b=max(a,b),min(a,b)
	while a%b!=0:
		a,b=b,a%b
	return b
n=int(input())
su=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):
			su+=gcd(a,k)
print(su)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

s=0

def check(a, b):
  judge=False
  while(judge==False):
    a,b=b,a%b
    if(b==0):
      judge = True
      return a
    elif(b==1):
      return 1
    
for i in range(1, n+1):
  for j in range(1, n+1):
    for k in range(1, n+1):
      s+=check(k,check(i,check(j,k)))


print(s)

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

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:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
from queue import PriorityQueue
from fractions import gcd
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def gcd(*numbers):
    return functools.reduce(math.gcd, numbers)
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort(reverse=True)
    return divisors

K = _I()

ans = 0
d = {}
vals = []
for i in range(1, K+1):
    for j in range(1, K+1):
        nums = [i,j]
        nums.sort()
        key = str(i) + '-' + str(j)
        if key in d:
            vals.append(d[key])
        else:
            val = gcd(i,j)
            d[key] = val
            vals.append(val)
#  print(vals)
#  print(d)
for val in vals:
    for k in range(1, K+1):
        #  print(val, k)
        nums = [i,j]
        nums.sort()
        key = str(val) + '-' + str(k)
        if key in d:
            ans += d[key]
        else:
            val = gcd(val, k)
            ans += val
            d[key] = val
print(ans)
exit()

Submission status: Accepted

Solution code:
n = int(input())
 
def gcd(a,b):
  ma = max(a,b)
  mb = min(a,b)
  jy = ma % mb
  if jy == 0 :
    return mb
  else:
    return gcd(mb,jy)
sum = 0
for i in range(1,n+1):
  sum += i

for i in range(1,n):
  for j in range(i+1,n+1):
    sum += 6 * gcd(i,j)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import math
import collections

k = int(input())

gcd_sum_dict = collections.defaultdict(int)
for i in range(1, k+1):
    gcd_sum_dict[i] = 0
    for j in range(1, k+1):
        gcd_sum_dict[i] += math.gcd(i, j)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

def gcd(a,b):
  ma = max(a,b)
  mb = min(a,b)
  jy = ma % mb
  if jy == 0 :
    return mb
  else:
    return gcd(mb,jy)
sum = 0
for i in range(n):
  for j in range(n):
    for k in range(n):
      sum += gcd(i+1,gcd(j+1,k+1))

print(sum)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

k = inp()
res = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        xx = gcd(i,j)
        for l in range(1,k+1):
            res += gcd(xx,l)
print(res)

Submission status: Accepted

Solution code:
from math import gcd
from sys import stdin
K = int(stdin.readline().rstrip())
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:
import numpy as np
a = np.arange(1, int(input()) + 1)
print(np.sum(np.gcd(np.gcd(a, a.reshape((-1, 1))), a.reshape((-1, 1, 1)))))

Submission status: Accepted

Solution code:
import sys
from math import gcd
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            g1 = gcd(a, b)
            for c in range(1, k + 1):
                g2 = gcd(g1, c)
                res += g2

    print(res)


if __name__ == '__main__':
    resolve()

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import math
from collections import Counter

K = int(input())

memo = {frozenset((i, j)): math.gcd(i, j) 
        for i in range(1, K+1) for j in range(i, K+1)}

counter = Counter([memo[frozenset((i, j))] 
                   for i in range(1, K+1) for j in range(1, K+1)])

total = 0
for i in range(1, K+1):
  for value, count in counter.items():
    total += memo[frozenset((i, value))] * count
    
print(total)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k = int(input())
sum = 0
def gcd(p,q):
  if p%q == 0:
    return q
  return gcd(q,p%q)
  
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:
import math

K = int(input())

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

Submission status: Accepted

Solution code:
import math
from functools import reduce


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


k = int(input())
s = 0

# a = b = c -> gcd(a,a,a) -> a
for a in range(1, k+1):
    s += a

# a = b, a < c -> gcd(a, c)と同じ -> gcd(c, a)も同じ 6倍
for a in range(1, k+1):
    for b in range(a+1, k+1):
        s += gcd(a, a, b) * 6

# a < b < c -> gcd(a, b, c)と同じ -> 6通り

for a in range(1, k+1):
    for b in range(a+1, k+1):
        for c in range(b+1, k+1):
            s += gcd(a, b, c) * 6

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

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

Submission status: Accepted

Solution code:
import math
 
def main():
    n = int(input())
    ans = 0
    for i in range(1,n+1):
        for j in range(1,n+1):
            tmp = math.gcd(i,j)
            for k in range(1,n+1):
                ans += math.gcd(tmp,k)
 
    print(ans)
 
if __name__ == "__main__":
    main()

Submission status: Accepted

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

main()

Submission status: Accepted

Solution code:

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

n = int(input())

li = [[0 for i in range(n+1)] for j in range(n+1)]
for i in range(n+1):
  for j in range(0, i+1):
    li[i][j] = gcd(i, j)

ans = 0
for i in range(1, n+1):
  for j in range(1, i+1):
    for k in range(1, j+1):
      if i == j and j == k:
        ans += li[i][li[j][k]]
      elif i == j or j == k:
        ans += 3*li[i][li[j][k]]
      else:
        ans += 6*li[i][li[j][k]]
print(ans)

Submission status: Accepted

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


K = int(input())
count = 0

# a, b, cが全て一致する場合
count += K * (K+1) / 2

# a, b, cのうち、いずれか二つが一致する場合
for a in range(1, K):
  for b in range(a+1, K+1):
    count += gcd(b, a) * 6

# a, b, cが全て異なる場合
for a in range(1, K-1):
  for b in range(a+1, K):
    ab = gcd(b, a)
    
    for c in range(b+1, K+1):
      count += gcd(c, ab)  * 6

print(int(count))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
"""
"""
def gcd(a, b):
    if b == 0: return a
    return gcd(b, a % b)
"""
import math

k = int(input())


cnt=0

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

print(cnt)

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

Submission status: Accepted

Solution code:
from  math import gcd

 
k = int(input())
sum_c = 0
 
for a in range(1, k + 1):
    for b in range(1, k + 1):
        for c in range(1, k + 1):
            sum_c += gcd(gcd(a, b), c)
            
print(sum_c)

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys
import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    k = ii()
    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)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
for a in range(1, K + 1):
    ans += a
    for b in range(a + 1, K + 1):
        tmp = gcd(a, b)
        ans += tmp * 6
        for c in range(b + 1, K + 1):
            ans += gcd(tmp, c) * 6
print(ans)

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

import fractions

dicts={}

K=one_int()

def reg(a,b):
    key = str(a)+","+str(b)
    if key not in dicts:
        dicts[key] = fractions.gcd(a,b)
        dicts[str(b)+","+str(a)] = dicts[key]
    temp = dicts[key]
    return temp

sums = 0
count=0
for a in range(1,K+1):
    for b in range(1,K+1):
        temp1 = reg(a,b)
        for c in range(1,K+1):
            temp = reg(temp1,c)
            sums += temp

print(sums)

Submission status: Accepted

Solution code:
from numba import jit
from math import gcd

k = int(input())

@jit
def main(k):
    point = 0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for k in range(1,k+1):
                point += gcd(gcd(i,j),k)
    return point

print(main(k))

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

k = int(input())

sum = 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):
            sum += gcd(gcd_ab, c)

print(sum)

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

Submission status: Accepted

Solution code:
k = int(input())
arr =  [0]*(k+1)
count = [0]*(k+1)
sum = 0
n = 3
for i in range(k,1,-1):
    x = k//i
    p = int(pow(x,n))
    arr[i]=i*p 
    count[i]=p
    for j in range(x-1):
        arr[i]-=i*count[i*(j+2)]
        count[i]-=count[i*(j+2)]
csum = 0
for i in range(2,k+1):
    sum+=arr[i]
    csum+=count[i]
sum+=(int(pow(k,n)) - csum)
print(int(sum%(1e9+7)))

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

ir = range(1, k+1)
jr = range(1, k+1)
lr = range(1, k+1)

count = 0
for i in ir:
  for j in jr:
    for l in lr:
      count += gcd(i, gcd(j, l))

print(count)

Submission status: Accepted

Solution code:

from math import gcd
k = int(input())
ans = 0
for i in range(1,k+1):
	for p in range(1,k+1):
		for q in range(1,k+1):
			ans+=gcd(q,gcd(i,p))

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

k = int(input())

print(sum([gcd3(a,b,c) for a in range(1,k+1)
 for b in range(1,k+1)
 for c in range(1,k+1)]))

Submission status: Accepted

Solution code:
from collections import deque
import math


k = int(input())


def equal_num(dq):
    equal_sum = -1
    dq_len = len(dq)

    for _ in range(dq_len):
        if dq[0] != dq[1]:
            equal_sum += 1
        dq.rotate()

    equal_sum *= 3

    if equal_sum <= 0:
        equal_sum = 1

    # print(dq, equal_sum)
    return equal_sum


gcd_sum = 0
for h in range(1, k + 1):
    for i in range(h, k + 1):
        for j in range(i, k + 1):
            gcd_tmp = math.gcd(j, math.gcd(h, i))
            gcd_sum += gcd_tmp * equal_num(deque([h, i, j]))

print(gcd_sum)

Submission status: Accepted

Solution code:
import math
K = int(input())
total = 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 == k:
        times = 1
      elif i == j or j == k or k == i:
        times = 3
      else:
        times = 6
      total += math.gcd(math.gcd(i,j), k) * times
print(total)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from math import gcd
input = sys.stdin.readline
def main():
    K = int(input())
    total = 0
    for a in range(1,K+1):
        for b in range(1,K+1):
            tmp = gcd(a,b)
            for c in range(1,K+1):
                total+=gcd(tmp,c)
    print(total)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

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

n = int(input())
x = n*(n+1)//2
y = sum([math.gcd(i, j) for i in range(1, n+1) for j in range(i+1, n+1)])*6
z = sum([gcd(i, j, k) for i in range(1, n+1) for j in range(i+1, n+1) for k in range(j+1, n+1)])*6
print(x+y+z)