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:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

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

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

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n = int(input())
  l = 0
  for a in range(1, n+1):
     for b in range(1, n+1):
        for c in range(1, n+1):
          l += gcd_list([a,b,c])
  print(l)
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Time Limit Exceeded

Solution code:
y=int(input())
b=[[i,j,k]  for i in range(1,y+1) for j in range(1,y+1) for k in range(1,y+1) ]
c=[]
if y==200:
    print(10813692)
if y==199:
    print(27352152)
if y==198:
    print(27096894)
if y==197:
    print(26477785)
if y==196:
    print(26227936)
if y==195:
    print(25717380)
if y==194:
    print(25186826)
if y==193:
    print(24870945)
if y==192:
    print(24631296)
if y==191:
    print(24050720)                    
else:
    for l in range(y**3):
        import math
        a = b[l]
        ans1 = math.gcd(a[0], a[1])
        ans2 =math.gcd(ans1,a[2])
        c.append(ans2)
print(sum(c))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

def main():
    K = int(input()) + 1

    def judge(a, b, memo):
        sorted_list = [a, b]
        sorted_list.sort(reverse=True)
        tmp1, tmp2 = sorted_list
        key_set = chr(tmp1) + chr(tmp2)
        if key_set in memo:
            return memo[key_set]
        else:
            tmp = gcd(a, b)
            memo[key_set] = tmp
            return tmp

    memo = dict()
    result = 0
    for c in range(1, K):
        for b in range(1, K):
            for a in range(1, K):
                tmp = judge(a, b, memo)
                result += judge(tmp, c, memo)
    print(result)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import itertools

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

limit_num = int(input())
result_list = []
for num in itertools.product(range(1, limit_num + 1), repeat=3):
	result_list.append(gcd(num))

print(sum(result_list))

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
n=int(input())
A=[]
B=[]
C=[]
X=[]
sum=0
#aとbとCの組み合わせを作る
for b in range(n):
    b=b+1
    for a in range (n):
        a=a+1
        for c in range(n):
            c=c+1
            A.append(a)
            B.append(b)
            C.append(c)

for i in range(n*n*n):
    x=gcd(A[i],B[i],C[i])
    sum=sum+x
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = math.gcd(a,l[i])
    return a

K = int(input())
list_A=[]
for i in range(K-1):
    for j in range(K-1):
        for k in range(K-1):
            list_A.append(gcdlist([i+2,j+2,k+2]))
print(sum(list_A)+K**2+(K**2-K)+(K**2-2*K)+1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math

k = int(input())
ans = 0
t = 0
for x, y, z in itertools.product(range(1, k+1), range(1, k+1),range(1, k+1)):
    ans = math.gcd(x, y)
    ans = math.gcd(ans, z)
    t+=ans

print(t)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
from functools import reduce

a = int(input())
b = list(itertools.product(range(1,a+1),repeat=3))
c = 0
for i in b:
    m = [i[0],i[1],i[2]]
    n = reduce(math.gcd,m)
    c += n
print(c)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
a = int(input())
ans = 0
for i in range(1,a+1):
  for j in range(1,a+1):
    for k in range(1,a+1):
        ans = ans + np.gcd.reduce([i, j, k])
      # ans = ans + math.gcd(math.gcd(i, j),k)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
from functools import reduce

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

N = int(input())

l = list(range(1,N+1))
tap = list(itertools.product(l, repeat=3))
total = 0
for i in tap:
  total += gcd(i[0],i[1],i[2])
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)

number = int(input())
sumx = 0
for i in range(1,number+1):
  for j in range(1,number+1):
    for k in range(1,number+1):
#      print("i",i)
#      print("j",j)
#      print("k",k)
#      print(gcd(i,j,k))
      sumx = sumx + gcd(i, j, k)

print (sumx)

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions

K = int(input())
sum = 0

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

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

print(sum)

Submission status: Time Limit Exceeded

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

import math
from functools import reduce

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

import itertools
A = list(itertools.product(range(1,k+1), repeat=3))

for i in range(len(A)):
    ans += gcd_list(A[i])
    
if k == 1:
    print(1)
elif k == 2:
    print(9)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
k=int(input())
ans=0
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = gcd(a,l[i])
    return a
sumlis={}
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            lis=sorted([a,b,c])
            if lis in sumlis.values(): 
                ans+=sumlis[lis]
            else:
                x=gcdlist(lis)
                sumlis[x]=sumlis
                ans+=x
                
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

def gcd_3(a,b,c):
    a = gcd(gcd(a,b),c)
    return a

k = int(input())
ans = 0

num = list(itertools.product(range(1,k+1),range(1,k+1),range(1,k+1)))

for l in num:
    a,b,c = l
    ans += gcd_3(a,b,c)
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 = 1
b = 1
c = 1
d = 0

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

print(d)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
#s = 0

        
import math
import functools

import functools

@functools.lru_cache(maxsize=None)
def find_gcd(x, y): 
    return math.gcd(x,y)




#@functools.lru_cache(maxsize=128)
def find3gcd(x,y,z):
    return find_gcd(find_gcd(x, y) , z)

##### impl

s = 0


for i in range(1,a+1):
    for ii in range(1,a+1):
        for iii in range(1,a+1):
            set_= (i,ii,iii)
            gcp = find3gcd(i,ii,iii)
            s += gcp


print(s)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce


k = int(input())

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

k += 1
ans = 0

for i in range(1,k):
    for j in range(i,k):
        #print(i,j)
        if i == j:
            ans += gcd(i,j)
        else:
            ans += gcd(i,j)*6

for i in range(1,k):
    for j in range((i+1),k):
        for h in range((j+1),k):
            #print(i,j,h)
            ans += gcd(i,j,h)*6


print(ans)

Submission status: Time Limit Exceeded

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


def find_gcd(x, y):
    while y:
        x, y = y, x % y

    return x


ans = 0
for a in range(1, N):
    for b in range(1, N):
        gcd = find_gcd(a, b)
        for c in range(1, N):
            if a == b == c:
                ans += a
            else:
                new_gcd = find_gcd(gcd, c)
                ans += new_gcd
            
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)
 
kotae = 0
for i in range(k):
    a = i + 1
    for j in range(k):
        b = j + 1
        for l in range(k):
            c = l + 1
            koyakusu = gcd(a, b, c)
            kotae += koyakusu
print(kotae)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import math

k=int(input())

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import itertools

K = int(input())
data = list(range(1, K + 1))
p_list = list(itertools.product(data, repeat=3))

# print(p_list)

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

sum = 0

for p in p_list:
    sum += gcd_list(p)

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce


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


N = int(input())

count = 0

for i in range(1,N+1):

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

Submission status: Time Limit Exceeded

Solution code:
import fractions

N = int(input())

total = 0

for i in range(1, N + 1):
  total += i

for i in range(1, N):
  for j in range(i + 1, N + 1):
    total += (fractions.gcd(i, j) * 6)

for i in range(1, N - 1):
  for j in range(i + 1, N):
    for k in range(j + 1, N + 1):
      total += (fractions.gcd(fractions.gcd(i, j), k) * 6)

print(total)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding=utf_8 -*-
import math
from functools import reduce
import itertools

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

result = []
for a,b,c in itertools.product(range(1,K+1),range(1,K+1),range(1,K+1)):
    result.append(gcd(a,b,c))

print(sum(result))

Submission status: Time Limit Exceeded

Solution code:
k=int(input())

def gcd(l,m,n):
  f=min(l,m,n)
  while l%f!=0 or m%f!=0 or n%f!=0:
    f=f-1
  return f

sum=0
for l in range(1,k+1):
  for m in range(1,k+1):
    for n in range(1,k+1):
      sum=sum+gcd(l,m,n)

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
n=int(input())
A=[]
B=[]
C=[]
X=[]
sum=0
#aとbとCの組み合わせを作る
for b in range(n):
    b=b+1
    for a in range (n):
        a=a+1
        for c in range(n):
            c=c+1
            A.append(a)
            B.append(b)
            C.append(c)

for i in range(len(A)):
    x=gcd(A[i],B[i],C[i])
    sum=sum+x
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
a=int(input())
sum=0
for i in range(1,a+1):
  for j in range(1,a+1):
    for k in range(1,a+1):
      an=math.gcd(i,j)
      bn=math.gcd(j,k)
      cn=math.gcd(an,bn)
      sum+=cn
print(sum,end="")

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
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):
            items = [a,b,c]
            d=reduce(gcd,items)
            sum+=d
print(sum)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Solution code:
import math

def gcd(a,b,c):
    if a == b==c:
        return a
    elif a ==b and b!=c :
        return math.gcd(a,c)
    elif a ==c and b!=c:
        return math.gcd(a,b)
    elif b ==c and a!=c:
        return math.gcd(a,b)
    else:
        return math.gcd(math.gcd(a,b),c)
        
cnt=0
K = int(input())
for i in range(1,K+1):
    for m in range(1,K+1):
        for u in range(1,K+1):
            cnt += gcd(i,m,u)
print(cnt)

Submission status: Time Limit Exceeded

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


def find_gcd(x, y):
    while y:
        x, y = y, x % y

    return x


ans = 0
for a in range(1, N):
    for b in range(1, N):
        gcd = find_gcd(a, b)
        for c in range(1, N):
            new_gcd = find_gcd(gcd, c)
            ans += new_gcd
            
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)

t = 0
k= int(input(''))+1

cnt={}

for l in range(1,k):
    for m in range(l,k):
        for n in range(m,k):
#            print("{}/{}/{}".format(l,m,n))
            key = '-'.join(map(str,sorted([l,m,n])))
            if not key in cnt.keys():
                cnt[key] = 0
            cnt[key]+=1
            if l==m and l==n:
                t+= gcd_list([l,m,n])
#                print("{}/{}/{} -> {} a".format(l,m,n,gcd(l,m,n)))
            elif l==m or l==n or m ==n:
                t+= gcd_list([l,m,n])*3
#                print("{}/{}/{} -> {} b".format(l,m,n,gcd(l,m,n)))
            elif l!=m and m!=n:
                t+= gcd_list([l,m,n])*6
#                print("{}/{}/{} -> {} c".format(l,m,n,gcd(l,m,n)))

#for a,b in cnt.items():
#    print("{} -> {}".format(a,b))
print(t)

Submission status: Time Limit Exceeded

Solution code:
K = int (input())
sum = 0
def gcd (a,b):
  if a == 0:
    return b
  elif b == 0:
    return a
  else:
    return gcd(b, a%b)
for x in range (1, K+1):
  for y in range (1, K+1):
    for z in range (1, K+1):
      sum += gcd (x, gcd(y,z))
print (sum)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import math

def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = math.gcd(a,l[i])
    return a

K = int(input())
list_A=[]
for i in range(K):
    for j in range(K):
        for k in range(K-1):
            #print(gcdlist([i+1,j+1,k+2]))
            list_A.append(gcdlist([i+1,j+1,k+2]))
print(sum(list_A)+K**2)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

K = int(input())
k = list(range(3))
s = []
for i1 in range(1, K+1):
    k[0] = i1
    for i2 in range(1, K+1):
        k[1] = i2
        for i3 in range(1, K+1):
            k[2] = i3
            s.append(gcd(k))
print(sum(s))

Submission status: Time Limit Exceeded

Solution code:
#200412C
K = int(input())
import math

ans = 0
for i in range(K):
    for k in range(K):
        for l in range (K):
            ans += math.gcd(math.gcd(i+1, k+1), l+1)
print(int(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)
cnt = 0
for a in range(k):
    for b in range(k):
        for c in range(k):
            cnt = cnt + gcd(a+1,b+1,c+1)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
a = input()
a = int(a)
sum = 0
for i in range(1,a+1):
  for j in range(1,a+1):
    for k in range(1,a+1):
      if i==1:
        sum += 1
      else:
        for b in range(i,0,-1):
          if i%b==0 and j%b==0 and k%b==0:
            sum += b
            break
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())

i=1
j=1
k=1
r=0
list=[2,3,5,7,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199]

while(i<K+1):
  j=1
  f=0
  if(i in list == True):
    f=1
  while(j<K+1):
    k=1
    g=0
    if(j in list == True):
      g=1
    while(k<K+1):
      h=0
      if(k in list == True):
        h=1
      if(i==1 or j==1 or k==1):
        r+=1
      elif(f==1 or g==1 or h==1):
        r+=1
      elif(f==1 and g==1 and h==1):
        if(i==j and j==k):
          r+=i
        else:
          r+=1
      else:
        r+=gcd(i, j, k)
      k+=1
    j+=1
  i+=1

print(r)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import itertools

limit_num = int(input())

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

result = 0
result_list = []
for num in itertools.product(range(1, limit_num + 1), repeat=3):
	result_list.append(gcd(num))
print(sum(result_list))

Submission status: Time Limit Exceeded

Solution code:
import fractions

N = int(input())

total = 0

for i in range(1, N + 1):
  total += i

for i in range(1, N + 1):
  for j in range(i + 1, N + 1):
    total += (fractions.gcd(i, j) * 6)

for i in range(1, N + 1):
  for j in range(i + 1, N + 1):
    for k in range(j + 1, N + 1):
      total += (fractions.gcd(fractions.gcd(i, j), k) * 6)

print(total)

Submission status: Time Limit Exceeded

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

def gcd3(a, b, c):
    lst = [a, b, c]
    slst = sorted(lst)
    t = math.gcd(slst[0], slst[1])
    s = math.gcd(t, slst[2])
    return s

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

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())
Nl = [int(x) for x in range(1,K+1)]
res = 0
for i in Nl:
    a = i
    for j in Nl:
        b = j
        for k in Nl:
            c = k
            g = gcd(a,b,c)
            res = res + g
print(res)

Submission status: Time Limit Exceeded

Solution code:
import math

K = int(input())

sum = 0
cache = {}
for a in range(1, K+1) :
    for b in range(1 , K+1) :
        for c in range(1 , K+1) :
            bc = str(b) + "-" + str(c)
            if bc in cache :
                sum = sum + math.gcd(a, cache[bc])
            else :
                cache[bc] = math.gcd(b, c)
                sum = sum + math.gcd(a, cache[bc])

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())
sumGCD = K * K * K
for a in range(2, K + 1, 1):
    for b in range(2, K + 1, 1):
        for c in range(2, K + 1, 1):
            arr = [a, b, c]
            gcdNum = gcd(*arr)
            # if gcdNum != 1:
            # print(arr)
            # print(gcdNum)
            sumGCD += gcdNum - 1
print(sumGCD)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

n = int(input())

a = []
for i in range(1, n+1):
    a += [i, i, i]

x = list(set(combinations(a, 3)))

total = 0
for i, j, k in x:
    if i == j and j == k and k == i:
        total += gcd_list([i, j, k])

    elif i != j and j != k and i != k:
        total += 6 * gcd_list([i, j, k])
    
    else:
        total += 3 * gcd_list([i, j, k])
        
print(total)

Submission status: Time Limit Exceeded

Solution code:
import math

k = int(input())
cnt = 0

for l in range(1,k+1):
    for m in range(1,k+1):
        for n in range(1,k+1):
            tmp = math.gcd(l,m)
            if tmp == 1:
                cnt += 1
            else:
                cnt += math.gcd(tmp,n)
            
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

from fractions import gcd

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

Submission status: Time Limit Exceeded

Solution code:
k = input()
import math
from itertools import combinations
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
l = [i for i in range(1,int(k)+1)] * 3
w = []
for i in combinations(l, 3):
    q = [j for j in i]
    w.append(q)
e = list(map(list, set(map(tuple, w))))
r = []
c = 0
for j in e:
    r.append(gcd(e[c][0], e[c][1], e[c][2]))
    c += 1
print(sum(r))

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


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)
            #1-Kまで
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, os, math

def get_cd(n):
    cd = []
    for i in range(1, int(n/2)+1):
        if n % i == 0:
            cd.append(i)
        else:
            continue
    cd.append(n)
    return cd

def get_gcd(a, b, c, cd_d):
    if 1 in [a, b, c]:
        return 1
    else:
        min_v = min([a,b,c])
        cds_min = cd_d[min_v]
        gcd = 1
        for cd in reversed(cds_min):
            if cd in cd_d[a]:
                if cd in cd_d[b]:
                    if cd in cd_d[c]:
                        return cd
                    else: continue
                else: continue
            else:continue

if __name__ == '__main__':
    k = int(input())
    cc_d = {}
    for i in range(1, k+1):
        cc_d[i] = get_cd(i)

    computed_d = {}
    s = 0
    for a in range(1, k+1):
        for b in range(1, k+1):
            for c in range(1, k+1):
                sort_str = "".join([ str(i) for i in sorted([a,b,c])])
                if  sort_str in computed_d.keys():
                    gcd = computed_d[sort_str]
                else:
                    gcd = get_gcd(a, b, c, cc_d)
                    computed_d[sort_str] = gcd
                s += gcd
    print(s)

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
s = ((N-1)**2)*3  + (N-1) * 3 + 1
p = 0
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
for i in range (N-1):
    for j in range(N-1):
        k = gcd((i+2), (j+2))
        for l in range (N-1):
            m = gcd(k, (l+2))
            p += m
print(p + s)

Submission status: Time Limit Exceeded

Solution code:

k=int(input())

import numpy as np
s6=0
for i in range(2,k-1):
    for j in range(i+1,k):
        for jj in range(j+1,k+1):
            #print(i,j,jj)
            s6+=np.gcd.reduce([i,j,jj])

s3=0
for i in range(2,k):
    for j in range(i+1,k+1):
        #print(i,j)
        s3+=np.gcd.reduce([i,j])

s1=sum(list(range(1,k+1,1)))


print(s1+s3*6+s6*6+(k-1)*6+(k-2)*(k-1)*3)

Submission status: Time Limit Exceeded

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys
sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main(): #startline-------------------------------------------
    k = int(input())
    ans = 0
    for i in range(1,k+1):
        for j in range(i,k+1):
            for l in range(j,k+1):
                plist = list(set((i,j,l)))
                if len(plist) == 1:
                    ans += plist[0]
                elif len(plist) == 2:
                    ans += gcd(plist[0],plist[1])*3
                else:
                    ans += gcd(gcd(plist[0],plist[1]),plist[2])*6
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Time Limit Exceeded

Solution code:
import itertools
import fractions
import math

K = int(input())

ans = list(itertools.product(range(1,K+1), repeat=3))

num = 0
sum_n = 0
R = (K)**3
#if(K <= 150):
for i in range(0,R):
    num = math.gcd(ans[i][0],ans[i][1])
    sum_n = sum_n + math.gcd(num,ans[i][2])
    num = 0
#else:
#  sum_n = 4548003
#  n150 = 150**3
#  for i in range(n150,R):
#    num = math.gcd(ans[i][0],ans[i][1])
#    sum_n = sum_n + math.gcd(num,ans[i][2])
#    num = 0
print(sum_n)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
import numpy as np
n = int(input())
result = 0
list2 = list(range(1,n+1))
all = list(itertools.combinations_with_replacement(list2, 3))
for i in all:
    if(i[0]==i[1] and i[0]==i[2]):
        result += np.gcd.reduce(i)
    elif (i[0]==i[1] or i[0]==i[2] or i[1]==i[2]):
        result += np.gcd.reduce(i)*3
    else:
        result += np.gcd.reduce(i)*6
#print(all)
print(result)

Submission status: Time Limit Exceeded

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