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: Time Limit Exceeded

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


def tri_gcd(p, q, r):
    tmp_gcd = fractions.gcd(p, q)
    return fractions.gcd(tmp_gcd, r)


k = int(input())
res = 0
for a in range(1, k+1):
    for b in range(a, k+1):
        for c in range(b, k+1):
            tk = tri_gcd(a, b, c)
            if a == b:
                if b == c:
                    res += tk
                else:
                    res += tk * 3
            else:
                if b == c:
                    res += tk*3
                else:
                    res += tk*6

print(res)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

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

K = int(input())

a = 1
b = 1
c= 1
ans = 0

while a <= K:
  b = 1
  while b <= K:
    c = 1
    while c <=K:
      if gcd(a, b) == 1:
        ans += 1
      else:
        ans += gcd(a, b, c)
      c += 1
    b += 1
  a += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
a=[0,0,0]
N = int(input())

wa=0
for i in range(1, N+1):
    a[0]=i
    for j in range(1,N+1):
        a[1]=j
        for k in range(1,N+1):
            a[2]=k
            
            ans = a[0]

            for l in range(1, 3):
                ans = math.gcd(ans, a[l])

            wa=wa+ans

print(wa)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import itertools as itr

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

n = int(input()) 
aa = list(range(1, n+1 ))
bb = list(range(1, n+1))
cc = list(range(1, n+1))
sum1 = 0


for a,b,c in itr.product(aa,bb,cc):
    sum1= sum1 + gcd(a,b,c)
            
print(sum1)

Submission status: Time Limit Exceeded

Solution code:
import fractions
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 = fractions.gcd(a ,b)
            g_abc = fractions.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: Time Limit Exceeded

Solution code:
import math

K = int(input())
ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def get_divisor(n):
    d = []
    i = 1
    while i*i <= n:
        if n % i == 0:
            d.append(i)
            if i != n // i:
                d.append(n // i)
        i += 1
    return sorted(d)

k = int(input())
d = [[0],]
for i in range(1, k+1):
    d.append(get_divisor(i))
ans = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        for k in range(1, k+1):
            if i == j == k:
                ans += i
            else:
                counter = dict(Counter(d[i] + d[j] + d[k]))
                keys = sorted(counter.keys(), reverse = True)
                for l in keys:
                    if counter[l] == 3:
                        ans += l
                        break

print(ans)

Submission status: Time Limit Exceeded

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):
            a = list(set([i,j,k]))
            if len(a)==1: ans+=a[0]
            elif len(a)==2:
                a = gcd(max(a), min(a))
                ans+=a
            else:
                tmp = gcd(i,j)
                tmp = gcd(tmp, k)
                ans+=tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

k=int(input())
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            x=math.gcd(i,j)
            y=math.gcd(x,l)
            ans+=y
            #print(gcd(i,j,l))
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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


K = int(input())
result = 0

temp = []

for i in range(1, K+1):
    for j in range(1, K+1):
        temp.append(gcd(i, j))

for k in range(1, K+1):
    for t in temp:
        add = gcd(t, k)
        result += add

print(result)

Submission status: Time Limit Exceeded

Solution code:

k=int(input())

import math
from functools import reduce

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

res=0

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

print(res)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
from fractions import gcd
from functools import reduce

x = 0
for a in range(1, k+1):
  for b in range(a, k+1):
    for c in range(b, k+1):
      if len({a, b, c})==1:
        x += reduce(gcd, [a,b,c])
      elif len({a, b, c})==2:
        x += 3*reduce(gcd, [a,b,c])
      else:
        x += 6*reduce(gcd, [a,b,c])
      
print(x)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

x = 0
n = int(input())+1

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

print(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import itertools
import numpy as np


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


K = int(input())

l = [i for i in range(1, K + 1)]

temp_sum = np.array([])
for j in itertools.product(l, repeat=3):
    temp_sum=np.append(temp_sum,gcd_list(list(j)))


print(int(np.sum(temp_sum)))

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

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

s = 0

N = int(input())
for a in range(N):
	for b in range(N):
		for c in range(N):
			#print(a+1,b+1,c+1)
			if a+1 ==1 or b+1 ==1 or c+1 ==1:
				s += 1
			else:
				s += gcd(a+1, b+1, c+1)
			#print(s)
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import math as mt

num = int(input())
list = []

for i in range(1, num+1):
    for j in range(1, num+1):
        for k in range(1, num+1):
            twoGcd = mt.gcd(i, j)
            threeGcd = mt.gcd(twoGcd, k)
            list.append(threeGcd)
            
print(sum(list))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import fractions

k = int(input())
d = dict()
ans = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        for c in range(1, k + 1):
            if 1 in [a, b, c]:
                ans += 1
            else:
                z = tuple(set([b, c]))
                if z not in d:
                    d[z] = fractions.gcd(b, c)
                    x = tuple(set([a, d[z]]))
                    if x not in d:
                        d[x] = fractions.gcd(a, d[z])
                        ans += d[x]
                    else:
                        ans += d[x]

                else:
                    x = tuple(set([a, d[z]]))
                    if x not in d:
                        d[x] = fractions.gcd(a, d[z])
                        ans += d[x]
                    else:
                        ans += d[x]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

K = int(input())

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

answer = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            lis = [a,b,c]
            temp = gcd(lis)
#            print(temp)
            answer += temp
            
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

n,ans=int(input()),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)

Submission status: Time Limit Exceeded

Solution code:
import fractions
K = int(input())
A = [i for i in range(1, K+1)]
B = [i for i in range(1, K+1)]
C = [i for i in range(1, K+1)]
 
cnt = 0
for a in A:
  for b in B:
    for c in C:
      N = [a,b,c]
      ans = N[0]
      for i in range(1, 3):
        ans = fractions.gcd(ans, N[i])
      cnt += ans
print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0

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

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

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
K = int(input())
sum = 0
for a in range(1, K+1):
  for b in range(a+1, K+1):
    for c in range(b+1, K+1):
      sum += gcd(gcd(a, b), c)
sum *= 6
for j in range(1, K+1):
  for k in range(j+1, K+1):
    sum += gcd(j, k)*6
for l in range(1, K+1):
  sum += l 
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math


n = int(input())


c = 0
for i in range(n):
    for j in range(n):
        for k in range(n):
            temp = math.gcd(i+1,j+1)
            c += math.gcd(temp,k+1)

print(c)

Submission status: Time Limit Exceeded

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

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

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

            cnt += gcd_list([i,j,k])
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

K = int(input())
gcd_list = []

for i in range(1, K+1):
    for ii in range(1, K+1):
        for iii in range(1, K+1):
            gcd_list.append(gcd(i, ii, iii))

print(sum(gcd_list))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import (
    accumulate,  # 累積和のイテレータ
    groupby,  # 値をkey,同じ値の集まりをgroup
    permutations,  # 順列
    combinations,  # 組み合わせ
    product,  # 重複あり順列（直積, 深いネストにも使える）
    combinations_with_replacement,  # 重複あり組み合わせ
)

import fractions
from functools import reduce

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

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


N = int(input())
ans = 0
for v in product(range(1, N+1), repeat=3):
    ans += gcd_list(v)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

limit_num = int(input())

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

result = 0
num_list =[1, 1, 1]

for num_1 in range(num_list[0], limit_num + 1):
	for num_2 in range(num_list[1], limit_num + 1):
		for num_3 in range(num_list[2], limit_num + 1):
			result = result + gcd([num_1, num_2, num_3])

print(int(result))

Submission status: Time Limit Exceeded

Solution code:
k=int(input())
ans=0
import itertools
import math
from functools import reduce
s=list(range(1,k+1))
s=s+s+s
def gcd(*numbers):
    return reduce(math.gcd, numbers)
from  itertools import combinations
q=[]
for i in range(1,k+1):
    for j in range(1,k+1):
        q.append(gcd(i,j))
for i in q:
    for j in range(1,k+1):
        ans+=gcd(i,j)
    
print(ans)

Submission status: Time Limit Exceeded

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

n = int(input())
ree = list(itertools.combinations_with_replacement(range(1, n+1), 3))

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

re = []
for i in range(len(ree)):
  k = len(collections.Counter(ree[i]))
  if k == 3:
    re.append(gcd_list(list(ree[i])) * 6)
  elif k == 2:
    re.append(gcd_list(list(ree[i])) * 3)
  else:
    re.append(gcd_list(list(ree[i])))
print(sum(re))

Submission status: Time Limit Exceeded

Solution code:
import math as mt

p = input()
n = 0
for i in range(1, int(p)+1):
    for j in range(1, int(p)+1):
        for k in range(1, int(p)+1):
            q = mt.gcd(i,j)
            n += mt.gcd(q,k)
print(n)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

print(su)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
lists = []
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            def gcd(a,b): 
                if(b==0): 
                    return a 
                else: 
                    return gcd(b,a%b)
            
            ans = gcd(a, gcd(b, c))
            lists.append(ans)
print(sum(lists))

Submission status: Time Limit Exceeded

Solution code:
ans = 0
k = int(input())
def f(x,y):
  x,y= min(x,y),max(x,y)
  while y%x != 0:
    z = y%x
    y = x
    x = z
  else:
    return x
def ff(a,b,c):
  return f(c,f(a,b))
for i in range(1,k+1):
    for j in range(1,k+1):
        for k in range(1,k+1):
            c = sorted([i,j,k])
            ans += ff(c[0],c[1],c[2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math 
from functools import reduce
def gcd(*numbers):
  return reduce(math.gcd,numbers)
K=int(input())
A=[]
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      s=gcd(i,j,k)
      A.append(s)
print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

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

Submission status: Time Limit Exceeded

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

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):
      n = gcd(a, b)
      m = gcd(n, c)
      result += m

print(result)

Submission status: Time Limit Exceeded

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

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
  
for a in range(K):
  for b in range(K):
    for c in range(K):
      ans+=gcd(a+1,b+1,c+1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce


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


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

N = int(input())

sum = 0
for i in range(1, N+1):
  for j in range(1, N+1):
    for k in range(1, N + 1):
      if i == j or j==k:
        hoge = gcd(i, k)
      elif i == k:
        hoge = gcd(j, k)
      elif i == j == k:
        hoge = 1
      else:
        hoge = gcd(i, j, k)
      sum+=hoge

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

k=int(input())
ml=[i for i in range(k+1)]
n=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            if i==j==l:
                continue
            n+=gcd(i,j,l)         
print(n+sum(ml))

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

import fractions

arr = [[0 for i in range(200)] for j in range(200)]

def gcd(a, b):
    if arr[a-1][b-1] != 0:
        return arr[a-1][b-1]
    else:
        ans = fractions.gcd(a, b)
        arr[b-1][a-1] = ans
        arr[a-1][b-1] = ans
        return ans

K = 200
lis = []
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            lis.append(gcd(gcd(i, j), gcd(j, k)))


print(sum(lis))

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions
#import time

def in_ctrl():
    _input = [_.rstrip() for _ in sys.stdin.readlines()]
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    K = int(_input[0])
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return K


def out_ctrl(s):
    print(s)


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


if __name__ == "__main__":
    #t1 = time.time()
    K = in_ctrl()
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    #cnt = 0
    ## 全部同じ数字(a=b=c)
    #for a in range(1,K+1):
    #    cnt += a
    ## 1つ同じ数字(a<b=c)
    #for a in range(1,K+1):
    #    for b in range(a+1,K+1):
    #        ans = fractions.gcd(a,b)
    #        cnt += 6*ans
    ## 全部違う数字(a<b<c)
    #for a in range(1,K+1):
    #    for b in range(a+1,K+1):
    #        for c in range(b+1,K+1):
    #            if a !=b and b!=c:
    #                ans = fractions.gcd(a,b)
    #                cnt += 6*fractions.gcd(ans, c)
    #            else:
    #                pass
    cnt = 0
    for a in range(1,K+1):
        cnt += a
        for b in range(a+1,K+1):
            ans = fractions.gcd(a,b)
            cnt += 6*ans
            for c in range(b+1,K+1):
                if a !=b and b!=c:
                    cnt += 6*fractions.gcd(ans, c)
                else:
                    pass
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    out_ctrl(cnt)
    #print(time.time()-t1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np
ans=0
K=int(input())
ls = [i for i in range(1,K+1)]

for v in itertools.combinations_with_replacement(ls, 3):
    if len(set(v))==1:
        ans += 1*np.gcd(v[0],np.gcd(v[1],v[2]))
    elif len(set(v))==2:
        ans += 3*np.gcd(v[0],np.gcd(v[1],v[2]))
    elif len(set(v))==3:
        ans += 6*np.gcd(v[0],np.gcd(v[1],v[2]))
print(ans)

Submission status: Time Limit Exceeded

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(K)] for j in range(K)]
answ=0
for i in range(1,K+1):
    for j in range(1,K+1):
        ans[i-1][j-1]+=gcd(i,j)

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

Submission status: Time Limit Exceeded

Solution code:
import fractions

K = input()

Sum = 0

for A in range(int(K)):
    a = A + 1
    for B in range(int(K)):
        b = B + 1
        for C in range(int(K)):
            c = C + 1

            Sum = Sum + fractions.gcd(fractions.gcd(a, b), c)

print(Sum)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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):
            s += reduce(math.gcd, (a, b, c))
            
print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
K=int(input())
s=0
for a in range(1,K+1):
    for b in range(1,K+1):
        d=np.gcd(a,b)
        for c in range(1,K+1):
            s+=np.gcd(c,d)
print(s)

Submission status: Time Limit Exceeded

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


def gcd(*numbers):
    return reduce(math.gcd, numbers)
ans = 0
for i1 in range(1,i+1):
    for i2 in range(1,i+1):
        for i3 in range(1,i+1):
            num = gcd(i1,i2,i3)
            ans += num
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions
from collections import Counter, defaultdict, deque
from functools import reduce

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)


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


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    k = I()

    def gcgcd(a, b, c):
        return fractions.gcd(fractions.gcd(a, b), c)

    sum = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                sum += gcgcd(a, b, c)

    print(sum)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n=int(input())
ans=0
for i in range(1,n+1):
  for k in range(i,n+1):
    for _ in range(k,n+1):
      g=gcd(gcd(i,k),_)
      if i==_:
        ans+=g
      elif i==k or k==_:
        ans+=g*3
      else:
        ans+=g*6
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

k = int(input())
a = 0

for i in range(1, k + 1):
    for l in range(1, k + 1):
        for m in range(1, k + 1):
            a += reduce(math.gcd, [i, l, m])
print(a)

Submission status: Time Limit Exceeded

Solution code:
K=int(input())
def gcd(a,b):
    if a <b:
        a,b=b,a
    while a%b != 0:
        a,b= b,a%b
    return b
x=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
            if (i==1) or (j==1) or (l==1):
                x+=1
            else:
                x += gcd(gcd(i,j),l)
print(x)

Submission status: Time Limit Exceeded

Solution code:
n=int(input(''))
def e(x,y):
    z=1
    if x<y:
        z=x
        x=y
        y=z

    while x!=0 and y!=0 and z!=0 and x!=y:
        z=x%y
        x=y
        y=z
    return x

s=0
for a in range(n):
    for b in range(n):
        u=e(a+1,b+1)
        for c in range(n):
            v=e(b+1,c+1)
            w=e(u,v)
            s=s+w

print(s)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
n = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
from functools import reduce

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

k = int(input())

a = [i for i in range(1, k+1)]
b = [i for i in range(1, k+1)]
c = [i for i in range(1, k+1)]

product = list(itertools.product(a, b, c))

output = 0
for p in product:
    output += gcd_list(p)
    
print(output)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np

k = int(input())

answer = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            answer += np.gcd.reduce([a,b,c])

print(answer)

Submission status: Time Limit Exceeded

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


K = int(input())

L = [[[0] * K for _ in range(K)] for _ in range(K)]
ans = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        d = gcd(a, b)
        for c in range(1, K+1):
            x, y, z = sorted([a, b, c])
            x -= 1
            y -= 1
            z -= 1
            if L[x][y][z] != 0:
                ans += L[x][y][z]
            else:
                t = gcd(d, c)
                ans += t
                L[x][y][z] = t

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions

from functools import reduce
K=int(input())

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


count=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 and k==i:
        n=1
      elif i==j or j==k or k==i:
        n=3
      else:
        n=6
      count+=n*gcd(i,j,k)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

k = int(input())
sum = 0
for p in range(1,k+1):
    for q in range(1,k+1):
        for r in range(1,k+1):
            sum += np.gcd.reduce([p,q,r])

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
b=int(input())+1
sum=0
for x in range(1,b):
    for y in range(1,b):
        for z in range(1,b):
            if x==1 or y==1 or z==1:
                sum+=1
            elif x==y and x==z:
                sum+=x
            elif x==y or z==y:
                sum+=math.gcd(x, z)
            elif x==z:
                sum+=math.gcd(y, z)
            else:
                sum+=math.gcd(x, math.gcd(y, z))
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import time

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: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import itertools
import numpy as np


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


K = int(input())

#l = [i for i in range(1, K + 1)]

temp_sum = []
#for j in itertools.product(l, repeat=3):
#    temp_sum.append(gcd_list(list(j)))
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
            temp_sum.append(gcd_list([i,j,l]))

print(np.sum(temp_sum))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

def gcd(a,b):
    if b == 0:
        return a
    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: Time Limit Exceeded

Solution code:
import math
K=int(input())
ans = 0
for i1 in range(1,1+K):
  for i2 in range(1,1+K):
    for i3 in range(1,1+K):
      ans = ans + math.gcd(math.gcd(i1,i2),i3)
print(ans)

Submission status: Time Limit Exceeded

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

k = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
import math
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 += math.gcd(math.gcd(a, b), c)
print(sum)

Submission status: Time Limit Exceeded

Solution code:
def getInput():
    return [int(i) for i in input().rstrip().split(' ')]

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

N = getInput()[0]
sums = 0
for i in range(1, N+1):
    for j in range(1, N+1):
        n = gcd(max(i, j), min(i,j))
        for k in range(1, N+1):
            sums += gcd(max(n, k), min(n, k))

print(sums)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

k = int(input())
kl = [i for i in range(1, k+1)]


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


total = 0
for i in kl:
    for j in kl:
        for k in kl:
            total += gcd(i, j, k)


print(total)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import fractions
k = int(input())
a = [i for i in range(1,k+1)]

ans = 0
for i in itertools.combinations_with_replacement(a,3):
    if i[0] == i[1] and i[1]==i[2]:
        ans += fractions.gcd(i[2],fractions.gcd(i[0],i[1]))
    elif i[0] == i[1] or i[1]==i[2] or i[2]==i[0]:
        ans += (fractions.gcd(i[2],fractions.gcd(i[0],i[1]))*3)
    else:
        ans += (fractions.gcd(i[2],fractions.gcd(i[0],i[1]))*6)


print(ans)