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:
k = int(input())
#gcd, lcm
from math import gcd
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(c,gcd(a,b))
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(i, k+1):
        g_2 = math.gcd(i, j)
        for k in range(j, k+1):
            g_3 = math.gcd(g_2, k)
            if i==j and j==k:
                ans += g_3
            elif i==j or j==k or k==i:
                ans += g_3 * 3
            else:
                ans += g_3 * 6

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

Submission status: Accepted

Solution code:
import math

k = int(input())
rng = range(1, k+1)

gcd_ls = [math.gcd(i, j) for i in rng for j in rng]
ans = [math.gcd(i, l) for i in rng for l in gcd_ls]

print(sum(ans))

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


print(ans)

Submission status: Accepted

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

#すべての要素が等しい場合
for i in range(1,K+1):
    ans += i

#2要素が等しい場合(3*2通り)
for i in range(1,K):
    for j in range(i+1,K+1):
        ans += 6*gcd(i,j)
        
#すべての要素が異なる場合(6通り)
for i in range(1,K-1):
    for j in range(i+1,K):
        for k in range(j+1,K+1):
            ans += 6*gcd(gcd(i,j),k)


print(ans)

Submission status: Accepted

Solution code:
print([0, 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][int(input())])

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
out = 0
Count = [0]*K
gcds = [[0]*K for i in range(K)]

for i in range (1,K+1):
    for j in range(1,+K+1):
        value = gcd(i,j)
        gcds[i-1][j-1]=value
        Count[value-1]+=1

for i in range(1,K+1):
    for j in range(1,K+1):
        out +=gcds[i-1][j-1]*Count[j-1] #値*回数
        
print(out)

Submission status: Accepted

Solution code:
from math import gcd


k = int(input())

ans = 0

gcd_sum = [0] * 201

for i in range(1, k+1):
    for j in range(1, k+1):
        gcd_sum[i] += gcd(i, j)

for a in range(1, k+1):
    for b in range(1, k+1):
        q = gcd(a, b)
        ans += gcd_sum[q]

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce

n = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

def resolve():
    k = int(input())
    l = [None] * k ** 2
    for i in range(k):
        for j in range(k):
            l[i * k + j] = gcd(i + 1, j + 1)
    ans = 0
    for i in l:
        for j in range(k):
            ans += gcd(i, j + 1)
    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:

import math

n = int(input())
count = 0

for i in range(1,n+1):
    for j in range(1,n+1):
        t = math.gcd(i,j)
        for k in range(1,n+1):
            m = math.gcd(t,k)
            count += m
print(count)

Submission status: Accepted

Solution code:
K=int(input())
import math
def f(x):
  if x==1:
    return 1
  elif x==2:
    return 9
  else:
    m=0
    for j in range(1,x):
      m+=math.gcd(j,x)*6
    for i in range(1,x-1):
      for k in range(i+1,x):
        m+=math.gcd(x,math.gcd(i,k))*6
    return m+x+f(x-1)
print(f(K))

Submission status: Accepted

Solution code:
import sys
from math import gcd

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    k = int(rl())
    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(c, d)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
from math import gcd

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    k = int(rl())
    ans = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                d = gcd(a, b)
                ans += gcd(c, d)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k = int(input())

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

res = 0
for i in range(1, k+1):
    res += i
for i in range(1, k+1):
    for j in range(i+1, k+1):
        tmp = gcd(i, j)
        res += 6*tmp
for i in range(1, k+1):
    for j in range(i+1, k+1):
        for h in range(j+1, k+1):
            tmp = gcd(i, gcd(j, h))
            res += 6*tmp
print(res)

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

Submission status: Accepted

Solution code:
import math

num = int(input())
sum = 0

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

print(sum)

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
k = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
"""
    Author : thekushalghosh
    Team   : CodeDiggers
"""
import sys,math,cmath,time
start_time = time.time()
################# ---- USER DEFINED INPUT FUNCTIONS ---- #################
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().split()))
##########################################################################
################# ---- THE ACTUAL CODE STARTS BELOW ---- #################

def solve():
    n = inp()
    c = 0
    for i in range(1,n + 1):
        for j in range(1,n + 1):
            for k in range(1,n + 1):
                c = c + math.gcd(math.gcd(i,j),k)
    print(c)
################## ---- THE ACTUAL CODE ENDS ABOVE ---- ##################
##########################################################################
ONLINE_JUDGE = __debug__
if not ONLINE_JUDGE:
    sys.stdin = open('input.txt','r')
    sys.stdout = open('output.txt','w')
else:
    input = sys.stdin.readline
t = 1
for tt in range(t):
    solve()
if not ONLINE_JUDGE:
    print("Time Elapsed:",time.time() - start_time,"seconds")
sys.stdout.close()

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

Submission status: Accepted

Solution code:
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(tmp,k)
print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
rg = range(1, n + 1)
k = 0
for a in rg:
    for b in rg:
        g1 = math.gcd(a,b)
        for c in rg:
            g2 = math.gcd(g1,c)
            k += g2
print(k)

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

Submission status: Accepted

Solution code:
def main():
	s = int(input())
	import math
	from functools import reduce
	import itertools
	def gcd(*numbers):
		return reduce(math.gcd, numbers)

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

if __name__ == '__main__':
	main()

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

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

Submission status: Accepted

Solution code:
import math
k = int(input())
a = 0
for m in range(1,k+1):
    for n in range(1,k+1):
        b = math.gcd(m,n)
        for l in range(1,k+1):
            a += math.gcd(l,b)
print(a)

Submission status: Accepted

Solution code:
import sys
import math
input=sys.stdin.readline
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: Accepted

Solution code:
#https://atcoder.jp/contests/abc162/tasks/abc162_c
# -*- coding: utf-8 -*-
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(tmp,k)
print(ans)

Submission status: Accepted

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

def main():
    K=int(input())
    sum=0
    for a in range(1,K+1):
        sum+=a
    for a in range(1,K):
        for b in range(a+1,K+1):
            sum+=6*f(a,b)
    for a in range(1,K-1):
        for b in range(a+1,K):
            for c in range(b+1,K+1):
                sum+=f(a,f(b,c))*6
    return sum

print(main())

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

print(ans)

Submission status: Accepted

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

ans = 0
k = int(input())

for i in range(1,k+1):
    for j in range(1,k+1):
        for k in range(1,k+1):
            ans += gcd3(i,j,k)
print(ans)

Submission status: Accepted

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,a+1):
        for c in range(1,b+1):
            if a == b == c:
                S += a
            if a == b != c or a != b == c:
                S += 3*math.gcd(a,c)
            if a != b != c:
                S += 6*gcd(a, b, c)
print(S)

Submission status: Accepted

Solution code:
import math
n=int(input())
ans=0
d={}
for a in range(1,n+1):
    for b in range(1,n+1):
        p=math.gcd(a,b)
        if d.get(p)!=None:
            d[p]+=1
        else:
            d[p]=1
for c in range(1,n+1):
    for e in range(1,n+1):
        p=math.gcd(c,e)
        ans+=d[e]*p
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
k=0
def f(a,b):
  if a < b:
    a,b=b,a
  while True:
    if a%b==0:
      return b
    else:
      a,b=b,a%b
for a in range(1,n+1):
  for b in range(1,a+1):
    for c in range(1,b+1):
      m=f(f(a,b),c)
      if a==b and b==c:
        k+=m
      elif a==b or b==c:
        k+=3*m
      else:
        k+=6*m
print(k)

Submission status: Accepted

Solution code:
import math

k = int( input() )
out_sum = 0

# a==b and b==c
for a in range(1, k+1):
    out_sum += a

# a==b and b!=c
for a in range(2, k+1):
    for b in range(1, a):
        out_sum += 6 * math.gcd(a,b)

# a!=b and b!=c
for a in range(3, k+1):
    for b in range(2, a):
        for c in range(1, b):
            out_sum += 6 * math.gcd( a, math.gcd(b,c) )

print( out_sum )

Submission status: Accepted

Solution code:
class Gcd:

    def __init__(self, K):
        self.table = [[None] * K for i in range(K)]
        return

    def gcd(self, x, y):
        if self.table[x - 1][y - 1] is not None:
            return self.table[x - 1][y - 1]

        if x == y:
            self.table[x - 1][y - 1] = x
            return x
        elif x > y:
            z = x % y
            if z == 0:
                self.table[x - 1][y - 1] = y
                return y
            else:
                z = self.gcd(z, y)
                self.table[x - 1][y - 1] = z
                return z
        else:
            return self.gcd(y, x)

def quest_c():
    K = int(input())

    gcd = Gcd(K)
    sum = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            d = gcd.gcd(a, b)
            if d == 1:
                sum += K
            else:
                for c in range(1, K + 1):
                    sum += gcd.gcd(d, c)
    print(sum)

if __name__ == '__main__':
    quest_c()

Submission status: Accepted

Solution code:
import math
from functools import reduce
def main():
    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 += math.gcd(math.gcd(i, j), k)
    print(s)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# bcd[a][b] : aとbの最大公約数の値
K = int(input())
bcd=[[int(-1) for x in range(K+1)]for j in range(K+1)]


def retBcd(a,b):
    if bcd[a][b] != -1 :
        return bcd[a][b]
    if b == 0:
        bcd[a][b] = a
        bcd[b][a] = a
        return a
    else:
        ret = retBcd(b,a%b)
        bcd[a][b] = ret
        bcd[b][a] = ret
        return ret

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

Submission status: Accepted

Solution code:
def gcd(m, n):
	if m % n == 0:
		return n
	return gcd(n, m % n)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd
import sys
input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
import math
k = int(input())
sum =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):
             sum += math.gcd(x, c)
print(sum)

Submission status: Accepted

Solution code:
from math 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(gcd(a, b), c)
print(sum)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
sum =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):
             sum += gcd(x, c)
print(sum)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
N = N+1
sum = 0

from math import gcd

for i in range(1, N):
    for j in range(1, N):
        for k in range(1, N):
            sum += gcd(gcd(i, j), k)

print( sum )

Submission status: Accepted

Solution code:
answers = [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]
k = int(input())
print(answers[k-1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import combinations
import math
n = int(input())
ans = 0
for r in range(1,4): 
    if r>n:
        break
    for x  in combinations([x+1 for x in range(n)],r):
        if len(x)==1:
            ans += x[0]
        elif len(x)==2:
            ans += math.gcd(x[0],x[1])*6
        else:
            tmp = math.gcd(x[0],x[1])
            ans += math.gcd(x[2],tmp)*6
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

from sys import stdin

import time
import numpy as np
import math

############################################

# read data for n sequences.
temp = stdin.readline().split()
N = int(temp[0])+1

out = 0
for i in range(1,N):
    for j in range(1,N):
        part = np.gcd(j,i)
        #for k in range(1,N):       
        out += np.sum(np.gcd(part,np.arange(1,N)))
print(out)

Submission status: Accepted

Solution code:
dic={}
def gcd(x,y):
  if x<y:
    a=x
    x=y
    y=a
  t=(x,y)
  if t in dic:
    return dic[t]
  else:
    while y!=0:
      a=x%y
      x=y
      y=a
    dic[t]=x
    return x

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

print(s)

Submission status: Accepted

Solution code:
import math
def gcd_arg3(a,b,c):
    return math.gcd(a,math.gcd(b,c))

k = int(input())

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

print(sum)

Submission status: Accepted

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(a, K+1):
    for c in range(b, K+1):
      l = len(set([a, b, c]))
      if l==1:
        num = 1
      elif l==2:
        num = 3
      elif l==3:
        num = 6
      sum += gcd(a, b, c)*num
print(sum)

Submission status: Accepted

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

n = int(input());

res = 0;

for a in range(1, n + 1):
    for b in range(1, n + 1):
        g = gcd(a, b);
        for c in range(1, n + 1):
            res += gcd(g, c);

print(res);

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

# reduce(function, iterable, initializer)
def gcd(*number):
    return reduce(math.gcd, number)

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

def main():
    sum = 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):
                sum += math.gcd(tmp, k)
    print(sum)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

K = int(input())

ans = 0
for x in range(1, K + 1, 1):
    for y in range(1, K + 1, 1):
        ans = ans + sum(list(map(lambda z: math.gcd(math.gcd(x, y), z), range(1, K+1, 1))))

print(ans)

Submission status: Accepted

Solution code:
import math

t = int (input())
sum = 0

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

print (sum)

Submission status: Accepted

Solution code:
import math
from itertools import product

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

Submission status: Accepted

Solution code:
import math


K=input()
K=int(K)
sum=0

for num_a in range(1,K+1):
	for num_b in range(1,K+1):
		num_d=math.gcd(num_a,num_b)
		for num_c in range(1,K+1):
			num_e=math.gcd(num_c,num_d)
			sum=sum+num_e
			
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
from math import gcd


def main():
        k = int(input())
        result = 0
        for i in range(1,k+1):
                for j in range(1,k+1):
                        t = gcd(i,j)
                        for l in range(1,k+1):
                                result += gcd(t,l)
        print(result)
    

if __name__ == "__main__":
    main()

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

Submission status: Accepted

Solution code:
import math

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

for i in range(1,K+1):
    temp = math.gcd(i,i)
    for k in range(1,K+1):
        s -= math.gcd(temp,k)

print(s)

Submission status: Accepted

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

for i in range(1,K+1):
    for j in range(i,K+1):
        temp = math.gcd(i,j)
        for k in range(1,K+1):
            s += math.gcd(temp,k)
s = s*2

for i in range(1,K+1):
    temp = math.gcd(i,i)
    for k in range(1,K+1):
        s -= math.gcd(temp,k)

print(s)

Submission status: Accepted

Solution code:
import math
import itertools
n = range(1,int(input())+1)
ppp = list(itertools.combinations(n, 3))
pp = list(itertools.combinations(n, 2))
asum,bsum,csum = 0,0,0
for p in ppp:
  asum+=math.gcd(math.gcd(p[0],p[1]),p[2])

for p in pp:
  bsum+=math.gcd(p[0],p[1])

for p in n:
  csum+=p

print(asum*6+bsum*6+csum)

Submission status: Accepted

Solution code:
import sys
import numpy as np

from math import gcd

N = int(sys.stdin.readline())
ans = 0
"""
dp = np.zeros((N+1, N+1))
def gcd(a, b):
    if a < b:
        a, b = b, a
    
    if dp[a][b] != 0:
        return dp[a][b]

    while b>0:
        a, b = b, a % b
    
    dp[a][b] = a
    return a
"""
for i in range(1,N+1):
    for j in range(1,N+1):
        temp = gcd(i, j)
        for k in range(1,N+1):
            ans += gcd(temp, 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):
        for c in range(1,K+1):
            ans += math.gcd(math.gcd(a,b),c)
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd


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

    print(ans)


resolve()

Submission status: Accepted

Solution code:
k = int(input())

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

total = 0

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

print(total)

Submission status: Accepted

Solution code:
import math

number = int(input()) + 1
ans = 0

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

print(ans)

Submission status: Accepted

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(k):
    for b in range(a+1):
        for c in range(b+1):
            if a == b and b == c:
                sum+=gcd(a+1, b+1, c+1)
            elif a!=b and b!=c and c!=a:
                sum += 6*gcd(a+1, b+1, c+1)
            else:
                sum += 3*gcd(a+1, b+1, c+1)
print(sum)

Submission status: Accepted

Solution code:
from math import gcd
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 += gcd(gcd(i,j),l)

print(sum)