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:
# C - Sum of gcd of Tuples (Easy)

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(a, math.gcd(b, c))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input()) + 1
NumberList = list()
for i in range(1,N):
    for j in range(i,N):
        for k in range(j,N):
            NumberList.append([i,j,k])

Answer = 0

def getGCD(abc):
    a = int(abc[2])
    b = int(abc[1])
    c = int(abc[0])
    if a == b and b == c:
        d = 1
    elif a == b or b == c or c == a:
        d = 3
    else:
        d = 6
        
    surplus = b % c
    while surplus != 0:
        b = int(c)
        c = int(surplus)
        surplus = b % c
    surplus = a % c
    while surplus != 0:
        a = int(c)
        c = int(surplus)
        surplus = a % c
    return c * d

for abc in NumberList:
    Answer += getGCD(abc)
print(Answer)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
def gcd(*numbers):
  return reduce(math.gcd,numbers)
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+=gcd(a,b,c)
print(s)

Submission status: Time Limit Exceeded

Solution code:
#C
#入力
K=int(input())+1


#デバッグ用
def pront(*n):
    if debug==1:
        print(n)
        
debug=1#0:本番,1:デバッグ

#処理
import math as m

Z=[ [ [-1 for i in range(K)] for j in range(K)] for l in range(K)]
def C_1(i,j,k):
    xx=[i,j,k]
    xx.sort()
    if Z[xx[0]][xx[1]][xx[2]]!=-1:
        return Z[xx[0]][xx[1]][xx[2]]
    else:
        Z[xx[0]][xx[1]][xx[2]]=m.gcd(m.gcd(xx[0],xx[1]),xx[2])
        return Z[xx[0]][xx[1]][xx[2]]


x=0
for i in range(1,K):
    for j in range(1,K):
        if m.gcd(i,j)==1:
            x+=K-1
        else:
            for k in range(1,K):
                x+=C_1(i,j,k)
print(x)

Submission status: Time Limit Exceeded

Solution code:
N = int(input()) + 1
NumberList = list()
for i in range(1,N):
    for j in range(1,N):
        for k in range(1,N):
            NumberList.append([i,j,k])

Answer = 0

def getGCD(abc):
    abc.sort(reverse=True)
    a = int(abc[0])
    b = int(abc[1])
    c = int(abc[2])
    surplus = b % c
    while surplus != 0:
        b = int(c)
        c = int(surplus)
        surplus = b % c
    surplus = a % c
    while surplus != 0:
        a = int(c)
        c = int(surplus)
        surplus = a % c
    return c

for abc in NumberList:
    Answer += getGCD(abc)
print(Answer)

Submission status: Time Limit Exceeded

Solution code:

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


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

print(total)

Submission status: Time Limit Exceeded

Solution code:
#C
#入力
K=int(input())+1


#デバッグ用
def pront(*n):
    if debug==1:
        print(n)
        
debug=1#0:本番,1:デバッグ

#処理
import math as m

Z=[ [ [-1 for i in range(K)] for j in range(K)] for l in range(K)]
def C_1(i,j,k):
    xx=[i,j,k]
    xx.sort()
    if Z[xx[0]][xx[1]][xx[2]]!=-1:
        return Z[xx[0]][xx[1]][xx[2]]
    else:
        Z[xx[0]][xx[1]][xx[2]]=m.gcd(m.gcd(xx[0],xx[1]),xx[2])
        return Z[xx[0]][xx[1]][xx[2]]


x=0
for i in range(1,K):
    for j in range(1,K):
        for k in range(1,K):
            x+=C_1(i,j,k)
print(x)

Submission status: Time Limit Exceeded

Solution code:
N = int(input()) + 1
NumberList = list()
for i in range(1,N):
    for j in range(1,N):
        for k in range(1,N):
            NumberList.append([i,j,k])

Answer = 0

def getGCD(abc):
    abc.sort(reverse=True)
    a = int(abc[0])
    b = int(abc[1])
    c = int(abc[2])
    surplus = a % b
    while surplus != 0:
        a = int(b)
        b = int(surplus)
        surplus = a % b
    if b > c:
        b,c = c,b
    surplus = c % b
    while surplus != 0:
        c = int(b)
        b = int(surplus)
        surplus = c % b
    return b

for abc in NumberList:
    Answer += getGCD(abc)
print(Answer)

Submission status: Time Limit Exceeded

Solution code:

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


K = int(input())
map = [[[None for _ in range(K)] for _ in range(K)] for _ in range(K)]
total = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            x, y, z = sorted([a, b, c])
            if map[x-1][y-1][z-1] != None:
                total += map[x-1][y-1][z-1]
            else:
                tmp = gcd(x, y)
                total += gcd(tmp, z)

print(total)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import math
K=int(input())
result=0
for i in range(1,K+1):
	for j in range(1,K+1):
		for k in range(1,K+1):
			result+=math.gcd(math.gcd(i,j),k)

print(result)

Submission status: Time Limit Exceeded

Solution code:
k=int(input())
ans=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for h in range(1,k+1):
      p=i
      q=j
      r=h
      while q>=1:
        bk=p%q
        p=q
        q=bk
      while p>=1:
        bk=r%p
        r=p
        p=bk
      ans+=r
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import math
K=int(input())
result=0
for i in range(K):
	for j in range(K):
		for k in range(K):
			result+=math.gcd(math.gcd(i+1,j+1),k+1)

print(result)

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

print(total)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(gcd_total)

Submission status: Time Limit Exceeded

Solution code:
K=int(input())
 
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a
 
ans=0
 
for a in range(1,K+1):
    for b in range(1,K+1):
        hoge=gcd(a,b)
        for c in range(1,K+1):
            g=gcd(hoge,c)
            ans += g
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
def my_gcd(a, b):
    if a < b: a, b = b, a
    c = a % b
    if c == 0: return b
    for _ in range(100):
        c = a % b
        a = b
        b = c
        if c == 0: return a

ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        t = my_gcd(a, b)
        for c in range(1, k+1): ans += my_gcd(t, c)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
    while True:
            if a >= b:
                if a % b == 0:
                    c = b
                    break
                else:
                    c = a % b
                    a = b
                    b = c
            else:
                a, b = b, a

    return c

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

print(gcd_total)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

# from fractions import gcd
from math import gcd

def f(x, y, z):
    r = gcd(gcd(x, y), z)
    return r



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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math

k = int(input())
klis = list(range(1,k+1))
lis = list(itertools.product(klis, klis, klis))

def gcd2(x):
    return math.gcd(x[0],math.gcd(x[1],x[2]))

result = sum(list(map(gcd2, lis)))
print(result)

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

K = int(input())

if __name__ == "__main__":
    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:
#!/usr/bin/env python3

def main():
    k = int(input())
    print(getresult(k))

def getresult(k):
    res = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                res += gcd(gcd(a, b), c)
    return res

def gcd(x, y):
    if x < y:
        x, y = y, x  # x >= y
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b):
    a, b = max([a,b]), min([a,b])
    if a % b == 0:
        return b
    else:
        return gcd(b, a % b)

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

x=0
k=int(input())
r = range(1, k + 1)
x = sum([gcd(a, b, c) for a in r for b in r for c in r])
print(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

x=0
k=int(input())

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

print(x)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

from fractions import gcd
# return gcd(x, y)

d = {}
d2 = {}

def f(x, y, z):
    s = gcd(x, y)
    t = gcd(s, z)
    return t

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b):
    if b<a:a,b=b,a
    r=b%a
    return a if r==0else gcd(a,r)
k=int(input())+1
print(sum(gcd(c,gcd(a,b))for a in range(1,k)for b in range(1,k)for c in range(1,k)))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce


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


def solve():

    K = int(input())

    rangeK = range(1, K+1)
    ans = 0

    for a in rangeK:
        for b in rangeK:
            for c in rangeK:
                ans += gcd(a, b, c)

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import math

k=int(input())
ans=3*k**2-3*k+1
for i in range(2,k+1):
    for j in range(2,k+1):
        for l in range(2,k+1):
            ans+=math.gcd(math.gcd(i,j), l)
            #print(gcd(i,j,l))
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from math import gcd
k = int(input())
 
sum = 0
for x1 in range(1,k+1):
  for x2 in range(1,k+1):
    for x3 in range(1,k+1):
        ans = gcd(x3,gcd(x1,x2))
        sum += ans
 
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math

from collections import deque 
from functools import reduce
from functools import lru_cache

input = sys.stdin.readline

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

def gcd_list2(numbers):
    if (numbers[0] == 1 or numbers[1] == 1 or numbers[2] == 1):
        return 1
    return reduce(math.gcd, numbers)
  
def gcd_list3(numbers):
    g = math.gcd(numbers[0], numbers[1])
    return math.gcd(g, numbers[2])

@lru_cache(maxsize=None)
def gc(a, b):
    if (a % b == 0):
        return b
    return(gc(b, a%b))  

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):
#                g = gc(i, j)
#            else:
#                g = gc(j, i)
#            if (g < k):
#                sum += gc(g, k)
#            else:
#                sum += gc(k, g)
            sum += gc(gc(i, j), k)
#            sum += gcd_list3([i, j, k])
print(sum)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b):
    a,b=max(a,b),min(a,b)
    while b>0:
        a,b=b,a%b
    return(a)

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

Submission status: Time Limit Exceeded

Solution code:
from math import gcd
from functools import reduce

K = int(input())

res = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            res += reduce(gcd,(a,b,c))

print(res)

Submission status: Time Limit Exceeded

Solution code:
import math

k = int(input())

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

print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math
# for AtCoder
# import fractions
from collections import deque 
from functools import reduce
from functools import lru_cache

# input = sys.stdin.readline

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):
            sum += gcd_list([i, j, k])
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
k = int(input())
ans = []
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
           xy = math.gcd(x,y)
           ans.append(math.gcd(xy,z))
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
def lcm(a,b):
    if a < b:
        a,b = b,a
    while a % b != 0:
        a,b = b,a%b
    return b

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

Submission status: Time Limit Exceeded

Solution code:
def euclidean(x,y):
    while y != 0:
        buf = y    
        y = x % y
        x = buf
    return x

K = int(input())
gcd = 0
s = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            #n = sorted([a,b,c])
            gcd_ab = euclidean(a,b)
            gcd_abc = euclidean(c,gcd_ab)
            s += gcd_abc
            #print(str(n)+","+str(gcd_abc))

print(s)

Submission status: Time Limit Exceeded

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

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

    return x

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(a, math.gcd(b, c))

print(sum)

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

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

    return x

sum = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            sum += computeGCD(a, computeGCD(b, c))

print(sum)

Submission status: Time Limit Exceeded

Solution code:
def euclidean(x,y):
    while y != 0:
        buf = y    
        y = x % y
        x = buf
    return x

K = int(input())
gcd = 0
s = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            n = sorted([a,b,c])
            gcd_ab = euclidean(n[1],n[0])
            gcd_abc = euclidean(n[2],gcd_ab)
            s += gcd_abc
            #print(str(n)+","+str(gcd_abc))

print(s)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce 
import itertools
k = int(input())
l = [i for i in range(1,k+1)]
ans = 0
def gcd(*numbers):
    return reduce(math.gcd, numbers)
for x in itertools.product(l,repeat=3):
    ans += gcd(x[0],x[1],x[2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math


K = int(input())
cnt = 0
for i in range(1, K+1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            cnt += math.gcd(k, math.gcd(i, j))


print(cnt)

Submission status: Time Limit Exceeded

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

result = 0
for k in range(1, K + 1):
  for l in range(1, K + 1):
    for m in range(1, K + 1):
      gcd_res = gcd(gcd(k, l), m)
      result += gcd_res

print(result)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
def GCD(p,q):
    if (p%q==0):
        return q
    return GCD(q,p%q)
s = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for m in range(1,k+1):
            s += GCD(GCD(i,j),m)
print(s)

Submission status: Time Limit Exceeded

Solution code:
x = int(input())
allval = 0
def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a%b)

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

Submission status: Time Limit Exceeded

Solution code:
x = int(input())
allval = []
def gcd(a, b, c):
    if a < b:
        if a < c:
            small = a
        else:
            small = b
    else:
        if b < c:
            small = b
        else:
            small = c
    hcf = small
    while hcf > 0:
        if a % hcf == 0 and b % hcf == 0 and c % hcf == 0:
            allval.append(hcf)
            break
        else:
            hcf -= 1

for i in range(1, x+1):
    for j in range(1, x+1):
        for k in range(1, x+1):
            gcd(i, j, k)
print(sum(allval))

Submission status: Time Limit Exceeded

Solution code:
import math
'''s=input()
c=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        for k in range(j+1,len(s)):
            if(j-i!=k-j):
                if(s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]):
                    c=c+1
print(c%1000000007)  '''
def find_gcd(x, y): 
    while(y): 
        x, y = y, x % y 
  
    return x 
n=int(input())
c=0
c1={}
for i in range(1,n+1):
    for j in range(1,n+1):
        for k in range(1,n+1):
            v=str([i,j,k])
            if v in c1:
                c=c+c1[v]
            else:
                c1[str(v)]=math.gcd(k,math.gcd(i,j))
                c=c+c1[v]
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def gcd(p, q):  # p > qのこと！
    if p % q == 0:
        # print('result: ' + str(q))
        return q
    else:
        return gcd(q, p % q)


K = int(input())

ans = 0

for i in range(1, K+1):
    for j in range(1, K+1):
        gcd_ij = gcd(max(i, j), min(i, j))
        for k in range(1, K+1):
            gcd_ijk = gcd(max(gcd_ij, k), min(gcd_ij, k))
            # print(i, j, k, gcd_ij, gcd_ijk)
            ans += gcd_ijk

print(ans)

# print(max(2, 3))
# print(min(2, 3))
#
# print(gcd(max(2, 3), min(2, 3)))

Submission status: Time Limit Exceeded

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

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

	return ans

n = int(input())
print(contains(n + 1))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
    if a < b:
        a, b = b, a
    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 k in range(1, k + 1):
            sum += gcd(gcd(i, j), k)
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
def getN():
    return int(input())

def gcd(p, q):
    if p % q == 0: return q
    return gcd(q, p % q)

def main():
    
    K = getN()

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

    return

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def solve(a,b,c):

    return gcd(gcd(a,b),c)

ans = 0

k=int(input())
for i in range(1,k+1):
    for j in range(1,k+1):
        for k in range(1,k+1):

            ans+=solve(i,j,k)
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)


K = int(input())

result = 0
for i in range(1, K + 1):
    for p in range(1, K + 1):
        for q in range(1, K + 1):
            result += gcd(i, p, q)
            
print(result)

Submission status: Time Limit Exceeded

Solution code:
import scipy as sp
import numpy as np
import math
 
 
N = int(input())
 
ans = 0
for a in range(1, N+1):
    for b in range(1, N+1):
        d = sp.gcd(a, b)
        for c in range(1, N+1):
 
            ans += sp.gcd(c, d)
 
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):
            ans += math.gcd(i, math.gcd(j, k))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(p, q):
    while (q != 0):
        r = p % q
        p = q
        q = r
    return p
K = int(input())
cnt = 0
for a in range(K):
    for b in range(K):
        for c in range(K):
            g1 = 0
            g2 = 0
            g1 = gcd(a+1, b+1)
            g2 = gcd(g1, c+1)
            cnt += g2
print(str(cnt))

Submission status: Time Limit Exceeded

Solution code:
def gcd(p,q):
    while q != 0:
        r = p % q
        p = q
        q = r
    return p


k = int(input())
ans = 0
for s in range(1,k+1):
    for t in range(1,k+1):
        for u in range(1,k+1):
            ans += gcd(gcd(s,t,),u)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

N = I()

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
K = int(input())
sumval = 0
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)
            sumval += g
print(sumval)

Submission status: Time Limit Exceeded

Solution code:

k=int(input())

import numpy as np
import itertools as itr

s6=list(itr.combinations(list(range(2,k+1)),3))
s3=list(itr.combinations(list(range(2,k+1)),2))

ans=0
for i in s6:
    ans+=np.gcd.reduce(i)

for i in s3:
    ans+=np.gcd.reduce(i)

ans*=6

ans+=(k+1)*k/2

ans+=(k-1)*6+(k-1)*(k-2)*3

print(int(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

def gcd3(a,b,c):
    d = gcd(a,b)
    e = gcd(d,c)
    return(e)

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

print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
from math import gcd
from functools import reduce

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

K = ii()
ans = 0

memo = dp3(-1, K+1, K+1, K+1)

for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            #l = sorted([i, j, k])
            #l = [i, j, k]
            #a, b, c = l[0], l[1], l[2]
            a, b, c = i, j, k
            if memo[a][b][c] != -1:
                ans += memo[a][b][c]
            else:
                memo[a][b][c] = gcd_list([a, b, c])
                memo[i][k][j] = memo[k][i][j] = memo[k][j][i] = memo[j][i][k] = memo[j][k][i] = memo[i][j][k]
                #memo[i][k][j] = memo[i][j][k]
                #memo[k][i][j] = memo[i][j][k]
                #memo[k][j][i] = memo[i][j][k]
                #memo[j][j][i] = memo[i][j][k]
                #memo[j][i][k] = memo[i][j][k]
                ans += memo[a][b][c]
            #ans += gcd_list([i, j, k])


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
K = INT()
sum = 0
 
for n in range(K):
  for m in range(K):
    for l in range(K):
      sum += gcd(n+1, gcd(m+1, l+1))
 
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

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

print(ans)

Submission status: Time Limit Exceeded

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

result=[]
count=0
cnt=0


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