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:
# abc162_c.py
# https://atcoder.jp/contests/abc162/tasks/abc162_c

# C - Sum of gcd of Tuples (Easy) /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# K∑a=1K∑b=1K∑c=1gcd(a,b,c)を求めてください。
# ただし、gcd(a,b,c)は a,b,cの最大公約数を表します。

# 制約
#     1≤K≤200
#     Kは整数

# 入力
# 入力は以下の形式で標準入力から与えられる。
# K

# 出力
# K∑a=1K∑b=1K∑c=1gcd(a,b,c)の値を出力せよ。

# 入力例 1
# 2

# 出力例 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
# となるため、答えは 9です。

# 入力例 2
# 200

# 出力例 2
# 10813692


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    K = int(lines[0])
    # N, M = list(map(int, lines[0].split()))
    # values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(N):
    #     valueses.append(list(map(int, lines[i+1].split())))

    import math

    dic = dict()
    for x in range(1, K):
        for y in range(x+1, K+1):
            dic[(x, y)] = math.gcd(x, y)

    su = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                # ☆これだと5秒かかる
                # su += get_gcd(a, b, c)
                tmp = [a, b, c]
                tmp.sort()
                x, y, z = tmp
                # けど...速くしきらんかった...
                su += get_gcd_by_dic(dic, x, y, z)

    return [su]


# def get_gcd(a, b, c):
#     import math
#     return math.gcd(math.gcd(a, b), math.gcd(a, c))


def get_gcd_by_dic(dic, a, b, c):
    if a == b:
        x = a
    else:
        x = dic[(a, b)]
    if a == c:
        y = a
    else:
        y = dic[(a, c)]
    if x == y:
        ret = x
    elif x > y:
        ret = dic[(y, x)]
    else:
        ret = dic[(x, y)]
    return ret


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['2']
        lines_export = [9]
    if pattern == 2:
        lines_input = ['200']
        lines_export = [10813692]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
import math
res=0
for a in range(1,n+1):
    for b in range(1, n + 1):
        for c in range(1, n + 1):
           gcd1=math.gcd(a,b)
           gcd2=math.gcd(gcd1,c)
           res+=gcd2
print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
import math
res=0
for a in range(1,n+1):
    for b in range(1, n + 1):
        for c in range(1, n + 1):
            li=[]
            for i in range(1,max(a,b,c)+1):
                if a%i==0 and b%i ==0 and c%i==0:
                    li+=[i]
            res+=max(li)
print(res)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
from functools import reduce

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

k = int(stdin.readline().rstrip())

d = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            d = d + reduce(gcd, [i,j,l])

print (d)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import math

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

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

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

K = int(input())
summ = 0

# 組み合わせの数
# a, b, cが同じは1pattern
# a, b, cのうち、２つが同じパターンは３
# a, b, cのうち、全て異なるパターンは6
# つまり、３つの数の組み合わせの数を求め、各パターン倍すればよい

# タプルで集合を作成する
# st = set()
# # # for a in range(1, K+1):
# # #     for b in range(1, K+1):
# # #         for c in range(1, K+1):
# # #             sort_abc = sorted([a, b, c])
# # #             st.add(tuple(sort_abc))
# # # # print(st)
# # # for s in st:
# # #     if s[0] == s[1] == s[2]:
# # #         summ += reduce(math.gcd, s)
# # #         # print(0, s)
# # #     elif s[0] == s[1] or s[0] == s[2] or s[1] == s[2]:
# # #         summ += 3*reduce(math.gcd, s)
# # #         # print(1, s)
# # #     else:
# # #         summ += 6*reduce(math.gcd, s)
# # #         # print(2, s)
# # # print(summ)

for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            summ += reduce(math.gcd, [a, b, c])
print(summ)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

K = int(input())
summ = 0

# 組み合わせの数
# a, b, cが同じは1pattern
# a, b, cのうち、２つが同じパターンは３
# a, b, cのうち、全て異なるパターンは6
# つまり、３つの数の組み合わせの数を求め、各パターン倍すればよい

# タプルで集合を作成する
st = set()
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            sort_abc = sorted([a, b, c])
            st.add(tuple(sort_abc))
# print(st)
for s in st:
    if s[0] == s[1] == s[2]:
        summ += reduce(math.gcd, s)
        # print(0, s)
    elif s[0] == s[1] or s[0] == s[2] or s[1] == s[2]:
        summ += 3*reduce(math.gcd, s)
        # print(1, s)
    else:
        summ += 6*reduce(math.gcd, s)
        # print(2, s)
print(summ)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from itertools import product
K = int(input())
cc = [i for i in range(1,K+1)]
count = 0
for i in product(cc,repeat = 3):
  aa = i[0]
  for j in [1,2]:
    aa = math.gcd(aa,i[j])
  count += aa
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math

k = int(input())

wa = 0

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


print(wa)

Submission status: Time Limit Exceeded

Solution code:
import math

k = int(input())

wa = 0

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


print(wa)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

k = int(input())

wa = 0

for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            li = [a,b,c]
            wa += reduce(math.gcd,li)

print(wa)

Submission status: Time Limit Exceeded

Solution code:
# -*- conding: utf-8 -*-
import itertools
import math
K = int(input())

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

ans = 0
for x in list(itertools.product(range(1, K+1), repeat=3)):
  # xに対して最大公約数を求める
  ans += gcd(gcd(x[0], x[1]), x[2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
k=int(input())

ans=0
import itertools
import math
for seq in itertools.product(range(1,k+1),range(1,k+1),range(1,k+1)):
    ans+=math.gcd(math.gcd(seq[0],seq[1]),seq[2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

k = int(input())
l = list(range(1,k+1))
y = list(itertools.combinations_with_replacement(l,3))
n = len(y)
p = 0

for i in range(n):
    a = y[i][0]
    b = y[i][1]
    while b != 0:
        c = a % b
        a = b
        b = c
    b = y[i][2]
    while b != 0:
        c = a % b
        a = b
        b = c
    if y[i][0] == y[i][1] == y[i][2]:
        p += a
    elif y[i][0] != y[i][1] != y[i][2]:
        p += a * 6
    else:
        p += a * 3

print(p)

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

print(ans)

Submission status: Time Limit Exceeded

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

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

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import itertools
k = int(input())
x = list(range(1,k+1))
y = list(itertools.product(x,repeat = 3))
n = len(y)
p = 0

for i in range(n):
    a = y[i][0]
    b = y[i][1]
    while b != 0:
        c = a % b
        a = b
        b = c
    b = y[i][2]
    while b != 0:
        c = a % b
        a = b
        b = c
    p += a

print(p)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

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(a,b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
    
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)

Submission status: Time Limit Exceeded

Solution code:
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):
            for d in range(1,k+1):
                if a % d == 0 and b % d == 0 and c % d == 0:
                    e = d
            ans = ans + e
print(ans)

Submission status: Time Limit Exceeded

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

for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            t = 0
            for i in range(1,min(x,y,z)+1):
                if x % i == 0 and y % i == 0 and z % i == 0:
                    t = i
            p += t

print(p)

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
import functools
i=0
N=int(input())
li=[]
li= list(range(1,N+1))
    
hello=list(itertools.product(li, repeat=3))
for a in range(len(hello)):
    
    i+=functools.reduce(math.gcd, hello[a])
print(i)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
from functools import reduce
import functools
i=0
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
N=int(input())
li=[]
li= list(range(1,N+1))
    
hello=list(itertools.product(li, repeat=3))
for a in range(len(hello)):
    
    i+=functools.reduce(math.gcd, hello[a])
print(i)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
from functools import reduce
i=0
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
N=int(input())
li=[]
li= list(range(1,N+1))
    
hello=list(itertools.product(li, repeat=3))
for a in range(len(hello)):
    i+=gcd_list(hello[a])
print(i)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
from functools import reduce
i=0
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
N=int(input())
li=[]
for a in range(1,N+1):
    li.append(a)
hello=list(itertools.product(li, repeat=3))
for a in range(len(hello)):
    i+=gcd_list(hello[a])
print(i)

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):
      num=math.gcd(i,j)
      ans+=math.gcd(num,k)
print(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):
      if i==1 or j==1 or k==1:
        ans+=1
      else:
        ans+=math.gcd(i,(math.gcd(j,k)))
print(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:
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):
            gcd = []
            #print(i,j,k)
            for l in range(1,min(i,j,k)+1):
                #print(i,j,k,l)
                if  i%l==0 and j%l==0 and k%l==0:
                    gcd.append(l)
                    #print(i,j,k,l)
                #print(gcd)
            ans += max(gcd)
print(ans)

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 l in range(1,k+1):
      gcd = 0
      for m in range(1,max(i,j,l)+1):
      	if i % m == 0 and j % m == 0 and l % m == 0:
        	gcd = max(gcd,m)
      ans += gcd
      #print(i,j,l,gcd)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
from itertools import *

class PrimeFactor():

    def __init__(self, n):                  # エラトステネス　O(N loglog N)
        self.n = n
        self.table = list(range(n+1))
        self.table[2::2] = [2]*(n//2)
        for p in range(3, int(n**0.5) + 2, 2):
            if self.table[p] == p:
                for q in range(p * p, n + 1, 2 * p):
                    if self.table[q] == q:
                        self.table[q] = p

    def is_prime(self, x):  # 素数判定　O(1)
        if x < 2:
            return False
        return self.table[x] == x

    def prime_factors(self, x):             # 素因数分解 O(logN)
        res = []
        if x < 2:
            return res
        while self.table[x] != 1:
            res.append(self.table[x])
            x //= self.table[x]
        return res

    def prime_counter(self, x):             # 素因数分解（個数のリスト）
        res = defaultdict(int)
        if x < 2:
            return res
        while self.table[x] != 1:
            res[self.table[x]] += 1
            x //= self.table[x]
        return res

    def prime_gcd(self, X):                 # n個の最大公約数　X:n個のリスト
        exponents = self.prime_counter(X[0])
        for x in X[1:]:
            Y = self.prime_counter(x)
            for prime, exp in exponents.items():
                if Y[prime] < exp:
                    exponents[prime] = Y[prime]
        res = 1
        for prime, exp in exponents.items():
            res *= pow(prime, exp)
        return res

    def prime_lcm(self, X, mod=None):       # n個の最小公倍数　X:n個のリスト
        exponents = defaultdict(int)
        for x in X:
            for prime, exp in self.prime_counter(x).items():
                if exp > exponents[prime]:
                    exponents[prime] = exp
        res = 1
        for prime, exp in exponents.items():
            res *= pow(prime, exp, mod)
        if mod == None:
            return res
        else:
            return res % mod


N = int(input())
table = PrimeFactor(N)


print(sum(table.prime_gcd([a,b,c]) for a, b, c in product(range(1,N+1), repeat=3))
      )

Submission status: Time Limit Exceeded

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

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

print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
from fractions import gcd
ans = 0
for a in range(1,N+1):
  for b in range(1,N+1):
    for c in range(1,N+1):
      ans += gcd(gcd(a,b),c)
print(ans)

Submission status: Time Limit Exceeded

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

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

print(sum)

Submission status: Time Limit Exceeded

Solution code:
gcd_d = {}
import math

K = int(input())

ans_sum = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    t_ab = tuple([ai, bi] if ai <= bi else [bi, ai])
    if t_ab not in gcd_d:
      gcd_d[t_ab] = math.gcd(ai, bi)
    gcd_ab = gcd_d[t_ab]
    for ci in range(1, K + 1):
      t_abc = tuple([gcd_ab, ci] if gcd_ab <= ci else [ci, gcd_ab])
      if t_abc not in gcd_d:
        gcd_d[t_abc] = math.gcd(gcd_ab, ci)
      ans_sum += gcd_d[t_abc]

print(ans_sum)

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def gcd(x, y):
  if y==0: return x
  return gcd(y, x%y)
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 += gcd(gcd(a, b), c)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

n = int(input())

total = 0

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

print(total)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

k = int(input())
gcd_sum = 0

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

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

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from math import gcd
from itertools import product

K = int(input())
b = 0

for a in product(range(1,K+1), repeat=3):
    b += reduce(gcd, a)
print(b)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

K = int(input())
ans = 0
def gcd(a, b):
    while b != 0:
        r = a % b
        a = b
        b = r
    return a
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)

Submission status: Time Limit Exceeded

Solution code:

k = int(input())

gcd_dict = {}

ans = 0
for n in range(k, 0, -1):
   for a in range(n, k + 1, n):
        for b in range(n, k + 1, n):
            for c in range(n, k + 1, n):
                if (a, b, c) not in gcd_dict.keys():
                    gcd_dict[(a, b, c)] = n
                    ans += n

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
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(a,math.gcd(b,c))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

n = int(input())
ans = 0

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#forループが多重ループになっているせいで時間がかかりすぎる
'''
import itertools
import numpy as np

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):
            for a, b, c in itertools.combinations([i, j, k], 3):
                result += np.gcd.reduce([a, b, c])
print(result)
'''

import numpy as np

K = int(input())
result = 0

for i in range(1, K+1):
    result += i

for i in range(1, K):
    for j in range(i+1, K+1):
        result += np.gcd(i,j) * 6

if K >= 3:
    for i in range(1, K-1):
        for j in range(i+1, K):
            for k in range(j+1, K+1):
                result += np.gcd.reduce([i,j,k]) * 6

print(result)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np

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):
            for a, b, c in itertools.combinations([i, j, k], 3):
                result += np.gcd.reduce([a, b, c])
print(result)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

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

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 n in range(1,K+1):
			tmp = math.gcd(i,j)
			ans += math.gcd(tmp,n)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

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

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 += gcd_list([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)

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

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools

k = int(input())
k_list = []
for i in range(1, k+1):
  k_list.append(i)

total = 0
for i in list(itertools.product(k_list,repeat=3)):
  total += math.gcd(i[0],math.gcd(i[1],i[2]))
  
print(total)

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
K = int(input())

def gcd(a, b):
    if a == 0 or b == 0:
        return max(a,b)
    x, y = min(a,b), max(a,b)
    return gcd(x,y%x )

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans=0
for a in range(1,n+1):
  for b in range(1,n+1):
    d=np.gcd(a,b)
    if d==1:
      ans+=n
      continue
    for c in range(1,n+1):
      ans += np.gcd(c,d)

print(ans)

Submission status: Time Limit Exceeded

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

ans=0
for a in range(1,n+1):
  for b in range(1,n+1):
    d=np.gcd(a,b)
    for c in range(1,n+1):
      ans += np.gcd(c,d)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#162C
#Sum of gcd of Tuples (Easy)

import math
from functools import reduce

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

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from functools import reduce
from itertools import product
from math import gcd

k = int(input())
p = list(product(range(1, k + 1), repeat=3))
print(sum(reduce(gcd, i) for i in p))

Submission status: Time Limit Exceeded

Solution code:
import itertools
k = int(input())

d = [[] for _ in range(k)]
for i in range(1, k+1):
    for ii in range(1, k+1):
        if i % ii == 0:
            d[i-1].append(ii)

seq = [i for i in range(1, k+1)]
ans = 0
A = list(itertools.combinations_with_replacement(seq, 3))
for a in A:
    a1 = a[0]
    a2 = a[1]
    a3 = a[2]
    if a1 == a2 == a3:
        m = 1
    elif a1 == a2 or a1 == a3 or a2 == a3:
        m = 3
    else:
        m = 6
    ans += max(set(d[a1-1]) & set(d[a2-1]) & set(d[a3-1])) * m

print(ans)

Submission status: Time Limit Exceeded

Solution code:
k = int(input())

d = [[] for _ in range(k)]
for i in range(1, k+1):
    for ii in range(1, k+1):
        if i % ii == 0:
            d[i-1].append(ii)

ans = 0
for a1 in range(k):
    for a2 in range(k):
        for a3 in range(k):
            ans += max(set(d[a1]) & set(d[a2]) & set(d[a3]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math


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

print(result)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

k = int(input())
ans = 0


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


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

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce


def gcd(a, b, c):
    ans = math.gcd(a, b)
    ans = math.gcd(ans, c)

    # abc = [a, b, c]
    # for _ in [[0, 1], [0, 2], [1, 2]]:
    #     a0 = max(abc[_[0]], abc[_[1]])
    #     a1 = min(abc[_[0]], abc[_[1]])

    #     i = 0
    #     while True:
    #         tmp = a0 % a1
    #         if tmp == 0:
    #             gcds.append(a1)
    #             break
    #         a0 = a1
    #         a1 = tmp

    return ans

# 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:
K = int(input())

ans = 0

import math

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

print(ans)