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 gcd(n, m):
	if m == 0:
		return n
	return gcd(m, n % m)


K = input()
s = 0

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

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
ans1 = 0
ans2 = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    for k in range(1,n+1):
      ans2 += gcd(gcd(k,j),i)

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

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

# 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
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
ans1 = 0
ans2 = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    ans1 = gcd(i,j)
    for k in range(1,n+1):
      ans2 = gcd(ans1,k)
      ans = ans+ans2
      
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

ans = 0
arr = []
for i in range(1,n+1):
  for j in range(1,n+1):
    for k in range(1,n+1):
      arr = np.array([i,j,k])
      ans = ans+(np.gcd.reduce(arr))
      #print(k)
      
print(ans)

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

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

print(mysum)

Submission status: Time Limit Exceeded

Solution code:
import math 

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

mysum = 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):
			mysum += mygcd(i,j,k)

print(mysum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

K = int(input())
result = 0

for a in range(1, K+1):
  for b in range(1, K+1):
    temp = np.gcd.reduce([a, b])
    for c in range(1, K+1):
      result += np.gcd.reduce([temp, c])

print(result)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

K = int(input())
result = 0

for a in range(1, K+1):
  for b in range(1, K+1):
    for c in range(1, K+1):
      result += np.gcd.reduce([a, b, c])

print(result)

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

Submission status: Time Limit Exceeded

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

#      divisors.sort(reverse=True)
#      return divisors

K = _I()

ans = 0
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)
exit()

Submission status: Time Limit Exceeded

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

#      divisors.sort(reverse=True)
#      return divisors

K = _I()

ans = 0
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)
exit()

Submission status: Time Limit Exceeded

Solution code:
import math

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

n = int(input())
s = sum([gcd3(a, b, c) for a in range(1, n + 1) for b in range(1, n + 1) for c in range(1, n + 1)])
print(s)

Submission status: Time Limit Exceeded

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

    divisors.sort(reverse=True)
    return divisors

K = _I()

ans = 0

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

Submission status: Time Limit Exceeded

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

    divisors.sort(reverse=True)
    return divisors

K = _I()

ans = 0

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

Submission status: Time Limit Exceeded

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

    divisors.sort(reverse=True)
    return divisors

K = _I()

ans = 0

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

Submission status: Time Limit Exceeded

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

    divisors.sort(reverse=True)
    return divisors

K = _I()

ans = 0
d = {}
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            nums = [i,j,k]
            nums.sort()

            key = '{}-{}-{}'.format(nums[0], nums[1], nums[2])
            if key in d:
                ans += d[key]
                continue

            min_num = nums[0]
            divisors = make_divisors(min_num)
            #  print(divisors)
            for divisor in divisors:
                #  if gcd(divisor, nums[1], nums[2]) == divisor:
                if nums[1] % divisor == 0 and nums[2] % divisor == 0:
                    d[key] = divisor
                    ans += divisor
                    break

            #  print(d)
            #  ans += val
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b,c):
    maxi=1
    for i in range(1,min([a,b,c])+1):
        if(a%i==0 and b%i==0 and c%i==0):
            maxi=i
    return maxi
x=int(input())
sum=0
for i in range(1,x+1):
    for j in range(1,x+1):
        for k in range(1, x+1):
            sum=sum+gcd(i,j,k)
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
import time
start = time.time()
sum = 0
k= 200
for i in range(k):
    for j in range(k):
        for x in range(k):
            sum += math.gcd(x+1, math.gcd(i+1, j+1))

print(sum)

#print(time.time()-start)

Submission status: Time Limit Exceeded

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

def gcd(a,b):
    if a>b:
        p = a
        q = b
    else:
        p = b
        q = a
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d


for i in range(1,n+1):
    for j in range(1,n+1):
        for k in range(1,n+1):
            if i==1 or j==1 or k==1:
                ans += 1
            elif abs(i-j)==1 or abs(j-k)==1 or abs(k-i)==1:
                ans += 1
            else:
                ans += gcd(gcd(i,j),k)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
K = int(input())
ans=0
num=0
a=1
while a<=K:
    b=1
    while b<=K:
        c=1
        while c<=K:
            num=math.gcd(a,b)
            ans+=math.gcd(num,c)
            c+=1
        b+=1
    a+=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
K = int(input())
ans=0
num=0
a=1
b=1
c=1
while a<=K:
    b=1
    while b<=K:
        c=1
        while c<=K:
            num=math.gcd(a,b)
            ans+=math.gcd(num,c)
            c+=1
        b+=1
    a+=1
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
K=int(input())
def gcd(a,b):
    if b==0:
        return a
    else:
        return gcd(b,a%b)
ans=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            t=gcd(i,gcd(j,k))
            ans+=t
print(ans)

Submission status: Time Limit Exceeded

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

def gcd(a,b):
    if a>b:
        p = a
        q = b
    else:
        p = b
        q = a
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d


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

print(ans)

Submission status: Time Limit Exceeded

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

def gcd(a,b):
    if a>b:
        p = a
        q = b
    else:
        p = b
        q = a
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d


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

print(ans)

Submission status: Time Limit Exceeded

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

def max(a,b):
    if a>b:
        return a
    else:
        return b

def min(a,b):
    if a<b:
        return a
    else:
        return b

def gcd(a,b):
    p = max(a,b)
    q = min(a,b)
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d


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

print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import math  
from itertools import product  

x = int(input())  

s = 0  
for a, b, c in product(range(1, x+1), repeat=3):  
  s += math.gcd(math.gcd(a, b), c)  
print(s)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import math

num = int(input())
a = 1
sum = 0

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

def loopC(a, b, num):
  sum = 0
  for c in range(1, num + 1):
      ans = gcd(a, b, c)
      sum += ans
  return sum

def loopB(a, num):
  sum = 0
  for b in range(1, num + 1):
    ans = loopC(a, b, num)
    sum += ans
  return sum

for a in range(1, num + 1):
  ans = loopB(a, num)
  sum += ans

print(sum)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import math

num = int(input())
a = 1
sum = 0

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

def loopC(a, b, num):
  c = 1
  sum = 0
  while c <= num:
      ans = gcd(a, b, c)
      # print(a, b, c, ans)
      sum += ans
      c += 1
  return sum

def loopB(a, num):
  b = 1
  sum = 0
  while b <= num:
    ans = loopC(a, b, num)
    b += 1
    sum += ans
  return sum

while a <= num:
  ans = loopB(a, num)
  sum += ans
  a += 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

num = int(input())

sum_gcd = 0

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

print(sum_gcd)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())

def divisor(num):
  divisor_set = {1, num}
  for i in range(2, num):
    if num % i == 0:
      divisor_set.add(i)
  return divisor_set

def gcd(a, b, c):
  a_div = divisor(a)
  b_div = divisor(b)
  c_div = divisor(c)
  common_div = a_div.intersection(b_div).intersection(c_div)
  gcd = max(common_div)
  return gcd

sum_gcd = 0

for i in range(1, num + 1):
  for j in range(1, num + 1):
    for k in range(1, num + 1):
      sum_gcd += gcd(i, j, k)
      
print(sum_gcd)

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
from functools import reduce

K = int(input())

memo = {}
def gcd(a, b):
  key = frozenset((a,b))
  if key in memo:
    return memo[key]
  memo[key] = math.gcd(a, b)
  return memo[key]
ans = 0
for i in range(1, K+1):
  for j in range(1, K+1):
    for k in range(1, K+1):
      ans += reduce(gcd, [i, j, k])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(_a, _b, _c):
    for i in range(1, min(_a, _b, _c) + 1):
        if _a % i == 0 and _b % i == 0 and _c % i == 0:
            gcd_result = i
        else:
            continue
    return gcd_result


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

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())
s = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for k in range(1, k + 1):
            s += gcd_list([i, j, k])
print(s)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
from fractions import gcd
def gcds(a,b,c):
  d=gcd(a,b)
  e=gcd(d,c)
  return e
    
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+gcds(i,j,k)
      
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

K = int(input())

sum = 0
for a in range(1, K+1, 1):
    for b in range(1, K+1, 1):
        for c in range(1, K+1, 1):
            sum = sum + math.gcd(a, math.gcd(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())
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:
def gcd(x, y):
  if x % y == 0:
    return y
  
  return gcd(y, x % y)


K = int(input())
count = 0
ab = []

for a in range(1, K+1):
  for b in range(1, K+1):
    ab.append(gcd(a, b))

for c in range(1, K+1):
  for i in ab:
    count += gcd(i, c)

print(count)

Submission status: Time Limit Exceeded

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


K = int(input())
count = 0

for a in range(1, K+1):
  for b in range(1, K+1):
    ab = gcd(a, b)

    for c in range(1, K+1):
      count += gcd(ab, c)

print(count)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
k = int(input())
s = 0
 
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
 
for a in range(1, k+1):
  for b in range(1, k+1):
    for c in range(1, k+1):
      s += gcd(gcd(a,b),c)
 
print(s)

Submission status: Time Limit Exceeded

Solution code:
"""
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
"""
k = int(input())

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

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
"""
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
"""
k = int(input())

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

cnt=0

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

print(cnt)

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

cnt=0

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

print(cnt)

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

cnt=0

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

print(cnt)

Submission status: Time Limit Exceeded

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

k = int(input())
sum_c = 0

for a in range(1, k + 1):
    for b in range(1, k + 1):
        for c in range(1, k + 1):
            p = gcd(a, b)
            sum_c += gcd(c, p)

print(sum_c)

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_c = 0

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

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:
import math
k = int(input())
sum = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            sum+=math.gcd(a,math.gcd(b,c))
print(sum)

Submission status: Time Limit Exceeded

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

k = int(input())

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

print(sum)

Submission status: 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:
def gcd1(p, q):
  if(p % q == 0):
    return q
  else:
    return gcd1(q, p % q)

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+=gcd1(gcd1(a,b),c)
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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

print(total)

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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


############ ---- Input Functions ---- ############
def in_int():
    return (int(input()))


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


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


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





n  = in_int()



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

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

print(ans)

Submission status: Time Limit Exceeded

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

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


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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

k = int(input())

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

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
import math

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

print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
class sumofgcdoftuples:
    def __init__(self,K):
        self.K = K
        self.sumofgcd(self.K)
        
    def euclid(self,x,y):
        if y > x:
            tmp = x
            x = y
            y = tmp
        while y!=0:
            tmp = y
            y = x%y
            x = tmp
        return x
    
    def gcd_3(self,a,b,c):
        d = self.euclid(a,b)
        if c > d:
            return self.euclid(c,d)
        else:
            return self.euclid(d,c)
    
    def sumofgcd(self,K):
        sum = 0
        for i in range(1,self.K+1):
            for j in range(1,self.K+1):
                for l in range(1,self.K+1):
                    sum += self.gcd_3(i,j,l)
        print(sum)
    
if __name__ == "__main__":
    K = int(input())
    sumofgcdoftuples(K)

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