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:
import functools
import itertools
import math


@functools.lru_cache(maxsize=None)
def gcd(a, b):
    return math.gcd(a, b)


@functools.lru_cache(maxsize=None)
def gcd3(a, b, c):
    return gcd(gcd(a, b), c)


# 文字列1行
K = int(input().strip())
Kr = [range(1, K+1) for i in range(3)]

items = itertools.product(*Kr)

sum_ = 0
for p in items:
    p = sorted(p)
    sum_ += gcd3(*p)
print(sum_)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b,c):
    #1
    seq  = [a,b,c]
    seq.sort()
    while b > 0:
        r = c%b
        c = b
        b = r
    #2
    seq_2 = [a,c]
    seq_2.sort()
    while a > 0:
        r = c%a
        c = a
        a = r
    return c
    
from itertools import product

k = int(input())
ans=[]

i = range(1,k+1)
j = range(1,k+1)
k = range(1,k+1)

for a,b,c in product(i,j,k):
    p = gcd(a,b,c)
    ans.append(p)

print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
from functools import reduce
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm_base(a, b):
    return (a * b) // gcd(a, b)
 
 
def main():
  k = int(input())
  ans = 0
  for a in range(1, k+1):
    for b in range(a, k+1):
      x = gcd(a,b)
      for c in range(b,k+1):
        if a==b and b==c:
          ans += a
        elif a==b or b==c:
          ans += gcd(x,c) * 3
        else:
          ans += gcd(x,c) * 6
  print(ans)
  
  
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
    for i in range(100):
      a, b = b, a%b
      if b == 0:
        return a
  
K = int(input())
A = B = C = [i for i in range(1, K+1)]
cnt = sum([gcd(gcd(a,b),c) for a in A for b in B for c in C])   
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math


N = int(input())

N_i = N
N_j = N
N_k = N

# def 

list = []
for i in range(N_i):
    for j in range(N_j):
        m = math.gcd(i+1,j+1)
        for k in range(N_k):
            if m == 1:
                list.append(1)
            else:
                list.append(math.gcd(m,k+1))

result = 0
for l in list:
    result += int(l)

print(str(result))

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):
        if i == j:
            n = i
        else:
            n = gcd(max(i, j), min(i,j))

        for k in range(1, N+1):
            if n == k:
                sums += n
            elif n == 1 or k == 1:
                sums += 1
            else:
                sums += gcd(max(n, k), min(n, k))

print(sums)

Submission status: Time Limit Exceeded

Solution code:
import math
k=int(input())
ans=0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
           if math.gcd(x,y)==1:
               ans+=1
           elif math.gcd(math.gcd(x,y),z)==1:
               ans+=1
           else:
              ans+=math.gcd(math.gcd(x,y),z)
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):
        if a == b:
            gcd = a
        else:
            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
def gcd_list(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 m in range(1, (k + 1)):
            li = [i, j, m]
            li = list(set(li))
            if 1 in li:
                ans += 1
            else:
                if len(li) == 2:
                    ans += math.gcd(li[0], li[1])
                else:
                    ans += gcd_list(li)                  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

K = int(input())

output = 0

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

print(output)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import itertools
from scipy.special import comb

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

k = int(input())
cnt = 0
b = list(range(1, k+1))
b = list(map(str, b))

c = itertools.combinations_with_replacement(b, 3)

c = list(c)

f = []
for i in range(len(c)):
    d = list(map(int, c[i]))
    dd = list(set(d))
    e = gcd(*d)
    if len(dd) == 1:
        cnt += e
    elif len(dd) == 2:
        cnt += e*3
    else:
        cnt += e*6

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def gcd(x,y,z):
  p=[x,y,z]
  p.sort()
  for hh in range(p[0],0,-1):
    if x%hh==0 and y%hh==0 and z%hh==0:
      return(hh)
      break
n=int(input())
s=0
f={}
for i in range(1,n+1):
  for j in range(1,n+1):
    for k in range(1,n+1):
      u=[i,j,k]
      u.sort()
      if 1 in u:
        s+=1
      else:
        o=''
        o+=str(u[0])
        o+=str(u[1])
        o+=str(u[2])
        if o in f:
          s+=f[o]
        else:
          f[o]=gcd(u[0],u[1],u[2])
          s+=f[o]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
import itertools as itr
sum1 =  [0]
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))

def word_edit(word_list):
    new_words = []
    for word in word_list:
        new_word = str(word).lower()
        new_words.append(new_word)

    return new_words

for a in word_edit(aa):
    for b in word_edit(bb):
        for c in word_edit(cc):
            sum1[0] += gcd(int(a),int(b),int(c))
print(sum1[0])

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
from functools import reduce
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm_base(a, b):
    return (a * b) // gcd(a, b)
 
 
def main():
  k = int(input())
  ans = 0
  for a in range(1, k+1):
    for b in range(a, k+1):
      for c in range(b,k+1):
        if a==b and b==c:
          ans += a
        elif a==b or b==c:
          ans += gcd(gcd(a,b),c) * 3
        else:
          ans += gcd(gcd(a,b),c) * 6
  print(ans)
  
  
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

import math
from functools import reduce

sum = 0

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

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

#n個の最大公約数を求める
def gcd(nums):
	ans=0
	ans=nums[0]
	for num in nums[1:]:
		ans = fractions.gcd(ans,num)
	return ans


k=input1()
result=0
for i in range(1,k+1):
	for j in range(1,k+1):
		for k in range(1,k+1):
			result+=gcd([i,j,k])
print(result)

Submission status: Time Limit Exceeded

Solution code:
import math

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

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
from functools import reduce
import itertools

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

def gcd_list(l):
    return reduce(gcd, l)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

K = i_i()

h = {}
ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            tmp = "".join([str(y) for y in sorted([a,b,c])])
            if h.get(tmp):
                ans += h[tmp]
            else:
                h[tmp] = gcd_list([a,b,c])
                ans += h[tmp]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import itertools

if __name__ == "__main__":
    K = int(input())+1
    ret = 0
    for a, b, c in itertools.product(range(1,K), range(1,K), range(1,K)):
        ret += np.gcd.reduce(list({a,b,c}))
    print(ret)

Submission status: Time Limit Exceeded

Solution code:
def gcd(x, y):
    while(y):
        x, y = y, x % y 
    return x
k=int(input())
s=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for z in range(1,k+1):
            s=s+gcd(z,gcd(i,j))
print(s)

Submission status: Time Limit Exceeded

Solution code:
import itertools
from fractions import gcd
 
K = int(input())
l = range(1, K+1)
ans = sum(l)
 
for a, b in itertools.combinations(l, 2):
    ans += gcd(a, b) * 6
 
for a, b, c in itertools.combinations(l, 3):
    ans += gcd(gcd(a, b), c) * 6
        
print(ans)

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):
    a = find_gcd(x, y)
    if a ==1:
        return 1
    else:
        return find_gcd(a , z)

##### impl

s = 0

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

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

i=1
list=[]
while(i<=K):
    list.append(i)
    i=i+1

sum=0
a=1
b=1
c=1

for a in list:
    for b in list:
        for c in list:
            sum+=gcd(gcd(a,b),c)

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
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(i==j and j==k):
        r+=i
      elif(f==1 and g==1 and h==1):
        r+=1
      elif(f==1 or g==1 or h==1):
        r+=1
      elif(i==j):
        r+=math.gcd(j,k)
      elif(j==k):
        r+=math.gcd(i,j)
      elif(i==k):
        r+=math.gcd(i,j)
      else:
        r+=gcd(i, j, k)
      k+=1
    j+=1
  i+=1

print(r)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from itertools import combinations

def gcd_memo(memo, a, b):
    if memo[a][b] > 0:
        return memo[a][b]
    res = np.gcd(a, b)
    memo[a][b] = res
    memo[b][a] = res
    return res

def main():
    k = int(input())
    res = 0
    memo = np.zeros((k+1, k+1), int)
    for i in range(1, k+1):
        memo[i][i] = i 
        res += i

    for c in combinations(range(1,k+1),2):
        ab = gcd_memo(memo, c[0], c[1])
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = gcd_memo(memo, c[0], c[1])
        abc = gcd_memo(memo, ab, c[2])
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math


N = int(input())

N_i = N
N_j = N
N_k = N

# def 

list = []
for i in range(N_i):
    for j in range(N_j):
        m = math.gcd(i+1,j+1)
        for k in range(N_k):
            list.append(math.gcd(m,k+1))

result = 0
for l in list:
    result += int(l)

print(str(result))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

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

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


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_list(i,j,k)

print(count)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
from functools import reduce
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm_base(a, b):
    return (a * b) // gcd(a, b)
 
def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)
 
 
def main():
  k = int(input())
  ans = 0
  for a in range(1, k+1):
    for b in range(a, k+1):
      for c in range(b,k+1):
        if a==b and b==c:
          ans += a
        elif a==b or b==c:
          ans += gcd(gcd(a,b),c) * 3
        else:
          ans += gcd(gcd(a,b),c) * 6
  print(ans)
  
  
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

List=range(1,K+1)          
D=0
for k, l, n in product(List, repeat=3):
    D=D+math.gcd(k,math.gcd(l,n))
    

print(D)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
k = int(input())

a = 0
for i in np.arange(1,k+1):
  for j in np.arange(1,k+1):
    for l in np.arange(1,k+1):
      a +=np.gcd.reduce([i,j,l]) 
print(a)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math 
n = np.arange(int(input()))+1

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import math 
  
w, h = 201, 201;
m = [[0 for x in range(w)] for y in range(h)] 
m3 = [[[0 for x in range(w)] for y in range(h)] for y in range(h)]
c = 0
e = 0
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):
			x1, x2, x3 = sorted([a, b, c], reverse = True)
			if m3[x1][x2][x3] == 0:
				if m[x2][x3] == 0:
					m[x2][x3] = math.gcd(x2,x3)
					c+=1
				m3[x1][x2][x3] = math.gcd(x1, m[x2][x3])
				e+=1
			s += m3[x1][x2][x3]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import functools
import fractions
 
def gcd(*numbers):
  return reduce(gcd_, numbers)

def gcd_(a, b):
  if (b == 0):
    return a
  return gcd(b, a % b)
 
sum = 0
K = int(input())
 
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if (i == j) & (j == k):
        sum += gcd(i,j,k)
      elif (i == j) & (j != k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j == k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j != k):
        sum += gcd(i,j,k)*6

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import itertools

k=int(input())
tot=0
for l,m,n in itertools.product(range(1,k+1),range(1,k+1),range(1,k+1)):
    ans=fractions.gcd(m,l%m)
    ans=fractions.gcd(ans,n)
    tot+=ans
print(tot)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
cnt = 0
import numpy as np
gcdlist = []
for i in range(1,n+1):
    for j in range(1,n+1):
        gcdlist.append(np.gcd(i,j))
for i in range(len(gcdlist)):
    for j in range(1,n+1):
        cnt += np.gcd(gcdlist[i],j)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
dp2 =np.zeros((n+1,n+1))

def gcd(x, y):
    x=int(x)
    y=int(y)
    #print(dp2)
    if y == 0:
        return x
    else:
        if dp2[x,y]!=0:
            return dp2[x,y]
        else:
            dp2[x,y] = gcd(y, x % y) 
            return dp2[x,y]

def saidai(la):
    #print(la[0],la[1],la[2])
    ans = la[0]
    for i in range(1, 3):
        if dp2[ans,la[i]]==0:
            ans = gcd(ans, la[i])
            ans = int(ans)
        else:
            ans = dp2[ans,la[i]]
            ans = int(ans)
          
    #print(ans)
    return ans




dp = np.zeros((n,n,n))


an = 0
for i in range(n):
    for j in range(n):
        for k in range(n):
            #print(i,j,k)
            l = [i,j,k]
            l.sort()
            if dp[l[0],l[1],l[2]]!=0:
                an += dp[l[0],l[1],l[2]]
            else:
                dp[l[0],l[1],l[2]] = saidai([l[0]+1,l[1]+1,l[2]+1]) 
                an += dp[l[0],l[1],l[2]]
            #print(dp)
            #print('^^^^^^^^^^^^^^^^^^^^^^')

print(int(an))

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
K = int(input())

lis = [i for i in range(1,K+1)]
a = 0

n = list(itertools.product(lis, repeat=3))
for i in n:
    e,f,g = map(int,i)
    e_f = math.gcd(e,f)
    e_g = math.gcd(e,g)
    e_f_g = math.gcd(e_f,e_g)
    a += e_f_g
print(a)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

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(gcd(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 math

k = int(input())

res = 0

dict = [[0 for i in range(201)] for j in range(201)]
tdict = [[[0 for i in range(201)] for j in range(201)] for k in range(201)]
# for d in dict:
#     print(d)

def fun(a, b):
    if dict[a][b] != 0:
        # print('used')
        return dict[a][b]
    else:
        res = math.gcd(a, b)
        dict[a][b] = res
        dict[b][a] = res
        return res

def find_gcd(a, b, c):
    if tdict[a][b][c] != 0:
        # print('used')
        return tdict[a][b][c]
    if dict[a][b] != 0:
        res = fun(dict[a][b], c)
        tdict[a][b][c] = res
        tdict[a][c][b] = res
        tdict[b][a][c] = res
        tdict[b][c][a] = res
        tdict[c][a][b] = res
        tdict[c][b][a] = res
        return res
    elif dict[b][c] != 0:
        res = fun(a, dict[b][c])
        tdict[a][b][c] = res
        tdict[a][c][b] = res
        tdict[b][a][c] = res
        tdict[b][c][a] = res
        tdict[c][a][b] = res
        tdict[c][b][a] = res
        return res
    else:
        res = fun(a, fun(b, c))
        tdict[a][b][c] = res
        tdict[a][c][b] = res
        tdict[b][a][c] = res
        tdict[b][c][a] = res
        tdict[c][a][b] = res
        tdict[c][b][a] = res
        return res

for a in range(1, k+ 1):
    for b in range(1, k+1):
        for c in range(1, k+ 1):
            res += find_gcd(a, b, c)

# from itertools import combinations
# L = [i for i in range(1,k+1)]*3
# comblist = [comb for comb in combinations(L, 3)]
#
# print(comblist)

# for comb in comblist:
#     res += find_gcd(comb[0], comb[1], comb[2])*6


print(res)

Submission status: Time Limit Exceeded

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

def gcd(num):
    return reduce(math.gcd, num)
numlist = list(range(1, k+1))
l = list(itertools.product(numlist, repeat=3))
l2 = map(gcd, l)

ans = sum(l2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

def main():
  k = int(input())
  params = [1,1,0]
  count = 0
  
  while params[0] < k or params[1] < k or params[2] < k:
    if params[2] < k:
      params[2] = params[2]+1
    elif params[1] < k:
      params = [params[0],params[1]+1,1]
    elif params[0] < k:
      params = [params[0]+1,1,1]
      
    tmp = 0
    if 1 in params:
      tmp = 1
    else:
      tmp = math.gcd(params[0],params[1])
      if tmp > 1:
        tmp = math.gcd(tmp, params[2])
    
    count += tmp
    
  return count
 
def gcd(val1, val2):
  val3 = 0
  while True:
    val3 = val1 % val2
    val1 = val2
    if val3 == 0:
      break
    val2 = val3
  return val1

if __name__ == '__main__':
  print(main())

Submission status: Time Limit Exceeded

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

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(l,k+1):
    for n in range(m,k+1):
      if l==m==n:
        sum=sum+gcd(l,m,n)
      elif l==m or m==n:
        sum=sum+gcd(l,m,n)*3
      else:
        sum=sum+gcd(l,m,n)*6

print(sum)

Submission status: Time Limit Exceeded

Solution code:
from fractions import *

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

K = int(input())

res = 0
tmp = 0
d = []
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            if i == j and j == k:
                res += i
            elif i == j:
                res += gcd(i,k)
            elif i == k:
                res += gcd(j,k)
            elif j == k:
                res += gcd(i,j)
            else:
                res += gcd(gcd(i,j),k)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import math
K=int(input())
GCD=[[0]*(K+2) for _ in range(K+2)]

def divable(a,b):
    if a==b:
        return a
    if a<b:
        a,b=b,a
    if a%b>0:
        return 1
    return math.gcd(a,b)

def make_GCD(K):
    for a in range(1,K+1):
        for b in range(1,K+1):
            GCD[a][b]=math.gcd(a,b)            
def gcd3(a,b,c):
    if a<b:
        a,b=b,a
    a=GCD[a][b]
    return GCD[a][c]
make_GCD(K)
ans=0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            ans += gcd3(a,b,c)
            
print(ans)

Submission status: Time Limit Exceeded

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

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

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

print(sum(result))

Submission status: Time Limit Exceeded

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

k = int(input())

def extract_gcd(limit):
    ran = range(1, limit + 1)
    for i in ran:
        for j in ran:
            for k in ran:
                yield math.gcd(math.gcd(i, j), k)
total = 0
for n in extract_gcd(k):
    total += n
    
print(total)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())

def f_gcd(x,y):#x>y
    ans = y
    while x%y != 0:
        y = x%ans
        x = ans
        ans = y
    return ans

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

            if a == b:
                gcd_ab = a
            else:    
                if a >= b:
                    gcd_ab = f_gcd(a,b)
                else:
                    gcd_ab = f_gcd(b,a)
            
            if gcd_ab == c:
                gcd = c
            else:
                if gcd_ab >= c:
                    gcd = f_gcd(gcd_ab,c)
                else:
                    gcd = f_gcd(c,gcd_ab)
            s = s+gcd
print(s)

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(a, k+1):
            for c in range(b, k+1):
                sort_str = "".join([ str(i) for i in sorted([a,b,c])])
                gcd = get_gcd(a, b, c, cc_d)
                if a == b == c:
                    s += gcd
                elif a == b or a == c or b == c:
                    s += gcd*3
                else:
                    s += gcd*6
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
k=int(input())

import math
import functools 
import itertools

res=0

l=range(1,k+1)

for i in itertools.product(l, repeat=3):
    new_l = list(set(i))
    if len(new_l)==1:
        res+=int(new_l[0])
    else:
        res+=functools.reduce(math.gcd,new_l)

print(res)

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(i==j and j==k):
        r+=i
      elif(f==1 and g==1 and h==1):
        r+=1
      elif(f==1 or g==1 or h==1):
        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
def gcd(*numbers):
    return reduce(math.gcd, numbers)
sum=0
i1=1
i2=1
i3=1
gc=0
K=int(input())
for i in range(K):
  if i2==(K+1):
        i2=1
  for j in range(K):
    if i1==(K+1):
        i1=1
    for z in range(K):
      
      #print(i)
      sum+=gcd(i1,i2,i3)
      i1+=1
      
    i2+=1
    
  i3+=1
      
#sum+=gcd(K,K,K)
print(sum)
#print(gcd(5,10,20))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import fractions
k=int(input())
a=[0,0,0]
cnt=0

def gg(x,y,z):
    a[0]=int(x)
    a[1]=int(y)
    a[2]=int(z)
    ans=a[0]
    for i in range(1,3):
        ans = fractions.gcd(ans,a[i])
    return int(ans)
                
for i in range(1,k+1):
    for j in range(1,k+1):
        for m in range(1,k+1):
            cnt+=gg(i,j,m)
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())
sum = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            sum += (gcd(k,j,i))
print(sum)