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: Accepted

Solution code:
import math
import itertools

k = int(input())
lst = [i for i in range(1,k+1)]
itr = itertools.combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += math.gcd(a,b) * 3
  else:
    ans += math.gcd(math.gcd(i[0],i[1]), i[2]) * 6

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

print(ans)

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

# N = 200 x 200 x 200 = 8000000でO(N)で計算できることから間に合うはず
import math
ans = 0
K = int(input())
for i in range(1,K+1):
    for j in range(1,K+1):
        tmp = math.gcd(i,j)
        for k in range(1,K+1):
            ans += math.gcd(k,tmp)
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
def gcd(lst):
  return math.gcd(math.gcd(lst[0],lst[1]), lst[2])

k = int(input())
lst = [i for i in range(1,k+1)]
itr = itertools.combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += math.gcd(a,b) * 3
  else:
    ans += math.gcd(math.gcd(i[0],i[1]), i[2]) * 6

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

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

Submission status: Accepted

Solution code:
import math as np
k = int(input())
D = {}
for a in range(1, k + 1):
    for b in range(1, k + 1):
        try:
            D[np.gcd(a, b)] += 1
        except:
            D[np.gcd(a, b)] = 1
ans = 0
for c in range(1, k + 1):
    for key, cnt in D.items():
        ans += cnt * np.gcd(c, key)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
k = int(input())
D = {}
for a in range(1, k + 1):
    for b in range(1, k + 1):
        try:
            D[np.gcd(a, b)] += 1
        except:
            D[np.gcd(a, b)] = 1
ans = 0
for c in range(1, k + 1):
    for key, cnt in D.items():
        ans += cnt * np.gcd(c, key)
print(ans)

Submission status: Accepted

Solution code:
import math


k = int(input())

sum_gcd = 0

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

        for c in range(1, k + 1):
            sum_gcd += math.gcd(gcd_ab, c)

print(sum_gcd)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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: Accepted

Solution code:
# coding: utf-8
import itertools
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
    
def main():
    k = int(input())
    array = list(range(1, k+1))
    ans = sum(array)
    for i in range(2, 4):
        for x in itertools.combinations(array, i):
            x = list(x)
            tmp = gcd(*x)
            ans += tmp * 6
    print(ans)
    
main()

Submission status: Accepted

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(a, k+1):
    for c in range(b, k+1):
      t = len({a,b,c})
      if t == 3:
        ans += gcd(a,b,c) * 6
      elif t == 2:
        ans += gcd(a,b,c) * 3
      else:
        ans += gcd(a,b,c)
        
print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())
wa = []

for i in range(1,K+1):
    for j in range(1,K+1):
        double = math.gcd(i,j)
        for k in range(1,K+1):
            triple = math.gcd(double,k)
            wa.append(triple)

print(sum(wa))

Submission status: Accepted

Solution code:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
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()
    return divisors

#maincode-------------------------------------------------
k = ini()
ans = 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):
            ans += gcd(d,c)
print(ans)

Submission status: Accepted

Solution code:
import math
k = int(input())
k += 1

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

print(s)

Submission status: Accepted

Solution code:
import numpy as np
K = int(input())
k = np.arange(1,K+1, dtype=np.int64)
print(np.gcd.outer(np.gcd.outer(k,k),k).sum())

Submission status: Accepted

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

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

ans = 0
for a in range(1,K+1):
  if (a==1):
    ans += K*K
    continue
  for b in range(1,K+1):
    t = gcd(a,b)
    if(t==1):
      ans += K
      continue
    for c in range(1,K+1):
      ans += gcd(t,c)
print(ans)

Submission status: Accepted

Solution code:
import math
a = int(input())
lis =[]
ans = 0
for x in range(1,a+1):
    for y in range(1, a+1):
        lis.append(math.gcd(x,y))
for z in range(1, a+1):
    for l in lis:
        ans += math.gcd(z,l)
print(ans)

Submission status: Accepted

Solution code:
import math
k = int(input())
su = 0
lis = []
for x in range(1, k+1):
  for y in range(1, k+1):
    a = math.gcd(x, y)
    lis.append(a)

for z in range(1, k+1):
  for p in lis:
    ans = math.gcd(p, z)
    su += ans
  
print(su)

Submission status: Accepted

Solution code:
import math
k = int(input())
dec = {}

for a in range(1, k+1):
    for b in range(1, k+1):
        if math.gcd(a, b) not in dec:
            dec[math.gcd(a,b)] = 1
        else:
            dec[math.gcd(a,b)] += 1
ans = 0
for c in range(1, k+1):
    for d in dec.keys():
        ans += math.gcd(d, c) * dec[d]
print(ans)

Submission status: Accepted

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: Accepted

Solution code:
def solve():
    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)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    from itertools import product
    import math

    K = int(input())

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
K = int(input())
k = np.arange(1,K+1)
print(np.sum(np.gcd.outer(np.gcd.outer(k,k),k)))

Submission status: Accepted

Solution code:
K = int(input())

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

dec = {}
for a in range(1,K+1):
    for b in range(1, K+1):
        if gcd(a,b) not in dec:
            dec[gcd(a,b)] = 1
        else:
            dec[gcd(a,b)] += 1
ans = 0
for c in range(1, K+1):
    for key in dec.keys():
        ans += gcd(c, key)*dec[key]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
k = int(input())
ans = 0
for q in range(1,k+1):
    for r in range(1,k+1):
        x = math.gcd(q,r)
        for s in range(1,k+1):
            ans += math.gcd(x,s)

print(ans)

Submission status: Accepted

Solution code:
import math

N=int(input())
sum=0
for i in range(1,N+1):
  for j in range(1,N+1):
    x=math.gcd(i,j)
    for k in range(1,N+1):
      y=math.gcd(x,k)
      sum = sum+y
print(sum)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
from functools import reduce
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

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

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)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import math

sum = 0
K = int(input())

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

print(sum)

Submission status: Accepted

Solution code:
import array

K = int(input())
s = 0

cache = [1] * K


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


for i in range(1, K+1):
    cache[i-1] = [1] * K
    for j in range(i, K+1):
        cache[i-1][j-1] = array.array('i', [1] * K)
        for k in range(j, K+1):
            cache[i-1][j-1][k-1] = gcd(k, gcd(j, i))

for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            a, b, c = sorted([i, j, k])
            s += cache[a-1][b-1][c-1]

print(s)

Submission status: Accepted

Solution code:
K = int(input())
s = 0

cache = {}


def gcd(a, b):
    key = (a, b)
    r = cache.get(key, None)
    if r:
        return r
    while b != 0:
        a, b = b, a % b
    cache[key] = a
    return a


for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            a, b, c = tuple(sorted([i, j, k]))
            s += gcd(a, gcd(b, c))

print(s)

Submission status: Accepted

Solution code:
k = int(input())
from math import gcd

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)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def gcd(p, q):
    if (p%q == 0):
        return q
    return gcd(q, p%q)
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 += gcd(gcd(i, j), k)
print(result)

Submission status: Accepted

Solution code:
import math
K = int(input())
result = 0
# 全て等しい場合はそれ自身が最大公約数
for i in range(1, K+1):
    result += i
# 二つが等しい場合は二つある方を選ぶので2倍、二数の公約数の三倍で6倍
d = {}
for i in range(1, K+1):
    for j in range(i+1, K+1):
        d[(i, j)] = math.gcd(i, j)
        result += 6 * d[(i, j)]
# 三つバラバラなら6倍
for i in range(1, K+1):
    for j in range(i+1, K+1):
        for k in range(j+1, K+1):
            tmp = 0
            if i < j:
                tmp = d[(i, j)]
            else:
                tmp = d[(j, i)]
            if tmp < k:
                result += 6 * d[(tmp, k)]
            else:
                result += 6 * d[(k, tmp)]
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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: Accepted

Solution code:
K = int(input())
g = [[] for i in range(K+1)]
for i in range(1, K+1):
    for j in range(i, K+1, i):
        g[j].append(i)
sigma = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            for d in reversed(range(len(g[i]))):
                if g[i][d] in g[j] and g[i][d] in g[k]:
                    sigma += g[i][d]
                    break
print(sigma)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import gcd
def main():
    k = int(input())
    r = 0
    for i1 in range(1, k + 1):
        for i2 in range(1, k + 1):
            for i3 in range(1, k + 1):
                t1 = gcd(i1, i2)
                r += gcd(t1, i3)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

n = int(input())

c=0
for i in range(1,n+1):
    if i==1:
        c += (n*n)
        continue
    for j in range(1,n+1):
        t = math.gcd(i,j)
        if t==1:
            c += n
            continue
        for k in range(1,n+1):
            c += math.gcd(t,k)

print(c)

Submission status: Accepted

Solution code:
import math
K = int(input())
ans = 0
for i in range(1, K+1):
    for j in range(1, i+1):
        for k in range(1, j+1):
            gcd = math.gcd(i, math.gcd(j, k))
            if i == j == k:
                ans += gcd
            elif i != j and j != k:
                ans += 6 * gcd
            else:
                ans += 3 * gcd
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

ans = 0

for h in range(1, K+1):
  for i in range(1, K+1):
    for j in range(1, K+1):
      ans += gcd(h, gcd(i, j))

print(ans)

Submission status: Accepted

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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
l=[1,9,30,76,141,267,400,624,885,1249,1590,2208,2689,3411,4248,5248,6081,7485,8530,10248,11889,13687,15228,17988,20053,22569,25242,28588,31053,35463,38284,42540,46581,50893,55362,61824,65857,71247,76884,84388,89349,97881,103342,111528,120141,128047,134580,146316,154177,164817,174438,185836,194157,207927,218812,233268,245277,257857,268182,288216,299257,313635,330204,347836,362973,383709,397042,416448,434025,456967,471948,499740,515581,536073,559758,583960,604833,633651,652216,683712,709065,734233,754734,793188,818917,846603,874512,909496,933081,977145,1006126,1041504,1073385,1106467,1138536,1187112,1215145,1255101,1295142,1342852,1373253,1422195,1453816,1502376,1553361,1595437,1629570,1691292,1726717,1782111,1827492,1887772,1925853,1986837,2033674,2089776,2145333,2197483,2246640,2332104,2379085,2434833,2490534,2554600,2609625,2693919,2742052,2813988,2875245,2952085,3003306,3096024,3157249,3224511,3306240,3388576,3444609,3533637,3591322,3693924,3767085,3842623,3912324,4027884,4102093,4181949,4270422,4361548,4427853,4548003,4616104,4718640,4812789,4918561,5003286,5131848,5205481,5299011,5392008,5521384,5610705,5739009,5818390,5930196,6052893,6156139,6239472,6402720,6493681,6623853,6741078,6864016,6953457,7094451,7215016,7359936,7475145,7593865,7689630,7886244,7984165,8130747,8253888,8403448,8523897,8684853,8802826,8949612,9105537,9267595,9376656,9574704,9686065,9827097,9997134,10174780,10290813,10493367,10611772,10813692]
print(l[int(input())-1])

Submission status: Accepted

Solution code:
K = int(input())

from math import gcd

ans = 0

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

Submission status: Accepted

Solution code:
K = int(input())

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

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: Accepted

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

Submission status: Accepted

Solution code:
# import itertools
import math
# import sys
# import numpy as np

K = int(input())
# S = input()
# n, *a = map(int, open(0))
# K, N = map(int, input().split())
# A = list(map(int, input().split()))
# Q = list(map(int, input().split()))
# S = input()

# d = sorted(d.items(), key=lambda x:x[0]) # keyでsort

tot = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        gcd_ab = math.gcd(a, b)
        for c in range(1, K + 1):
            tot += math.gcd(gcd_ab, c)

print(tot)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import*
r = range(1, int(input()) + 1)
print(sum(gcd(gcd(a, b), c) for a in r for b in r for c in r))

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

n = int(input()) + 1
ans = 0
for i in range(1,n):
    for j in range(1,i+1):
        for k in range(1,j+1):
            if i == j == k:
                m = 1
            elif i == j or j == k or k == i:
                m = 3
            else:
                m = 6
            a = [i,j,k]
            ans += gcd_list(a) * m
print(ans)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
from math import gcd
import itertools

k = int(input())

lst = [int(i) for i in range(1, k+1)]
all_array = list(itertools.combinations_with_replacement(lst, 3))
num = 0
for element in all_array:
  a = element[0]
  b = element[1]
  c = element[2]
  d = gcd(gcd(a, b), c)
  if a != b and b != c:
    num += d * 6
  elif a == b and b != c:
    num += d * 3
  elif a != b and b == c:
    num += d * 3
  elif a == b and b == c:
    num += d
print(num)

Submission status: Accepted

Solution code:
from math import gcd
K = int(input())
ans = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        L = gcd(i,j)
        for k in range(j,K+1):
            M = gcd(L,k)
            if i == j == k:
                ans += M
            elif i == j or j == k:
                ans += M*3
            else:
                ans += M*6
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
import functools

K = int(input())

ans = 0

for i in itertools.product(range(1,K+1),repeat=3):
    ans += functools.reduce(math.gcd,i)
print(ans)

Submission status: Accepted

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)

Submission status: Accepted

Solution code:
from math import gcd
def readinput():
    k=int(input())
    return k

def main(k):
    memo=[]
    for _j in range(k+1):
        memo.append([0 for _i in range(k+1)])
    sum=0
    for a in range(1,k+1):
        for b in range(1,k+1):
            if memo[a][b]!=0:
                temp=memo[a][b]
            else:
                temp=gcd(a,b)
                memo[a][b]=temp
                memo[b][a]=temp
            for c in range(1,k+1):
                if memo[temp][c]!=0:
                    sum+=memo[temp][c]
                else:
                    temp2=gcd(temp,c)
                    memo[temp][c]=temp2
                    memo[c][temp]=temp2
                    sum+=temp2
    return sum

if __name__=='__main__':
    k=readinput()
    ans=main(k)
    print(ans)

Submission status: Accepted

Solution code:
K = int(input())

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

sum = 0

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

print(sum)

Submission status: Accepted

Solution code:
import sys
import math
import time
readline = sys.stdin.readline


def main():
    listSum = [1, 3, 6]
    k = int(input())
    sumGCD = 0
    for a in range(1, k + 1):
        for b in range(a, k + 1):
            for c in range(b, k + 1):
                sumGCD += gcd(a, b, c) * listSum[len(set([a, b, c])) - 1]
    print(sumGCD)


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


if __name__ == '__main__':
    s = time.time()
    main()
    # print(time.time() - s)

Submission status: Accepted

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


def calculation2(lines):
    K = int(lines[0])

    gcdlist = [[0 for i in range(K)] for j in range(K)]
    # print(gcdlist)

    for i in range(K):
        for j in range(i, K):
            tmp = gcd(i+1, j+1)
            gcdlist[i][j] = tmp
            gcdlist[j][i] = tmp
    # print(gcdlist)

    su = 0
    for a in range(K):
        for b in range(K):
            for c in range(K):
                if a == b == c:
                    tmp = a+1
                elif a == b:
                    tmp = gcdlist[a][c]
                elif a == c:
                    tmp = gcdlist[a][b]
                elif b == c:
                    tmp = gcdlist[a][b]
                else:
                    tmp = gcdlist[a][gcdlist[b][c]-1]
                # print(f'a=[{a}], b=[{b}], c=[{c}], tmp=[{tmp}]')
                su += tmp

    return [su]


def calculation3(lines):
    K = int(lines[0])
    su = 0
    for a in range(K):
        for b in range(K):
            for c in range(K):
                su += gcd(gcd(a+1, b+1), c+1)

    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 = calculation2(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: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import *
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: Accepted

Solution code:
k = int(input())

sum = 0

def gcd(x, y): # ユークリッドの互除法
    if y > x:
        y,x = x,y
    while y > 0:
        r = x%y
        x = y
        y = r
    return x

import math

for i in range(1,k+1):
    for m in range(1,k+1):
        sub = gcd(i,m)
        for l in range(1,k+1):
            sum += gcd(sub,l)
print(sum)

Submission status: Accepted

Solution code:
import math

k=int(input())
n=0
for a in range(1,k+1):
    for b in range(1,k+1):
        d = math.gcd(a,b)
        for c in range(1,k+1):
            n+=math.gcd(d,c)
print(n)

Submission status: Accepted

Solution code:
import math

N = int(input())
n = 0
m = 0
res =0 
List1 = ["INF"]*(N+1)
for i in range(1,N+1):
  for j in range(1,N+1):
    n = math.gcd(i,j)
    if List1[n] == "INF":
      for k in range(1,N+1):
        m += math.gcd(n,k)
      res += m
      List1[n] = m
      m = 0
    else:
      res += List1[n]
print(res)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

K = int(input())
from math import gcd
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: Accepted

Solution code:
K = int(input())


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


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: Accepted

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