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:
class LCM():
    #a、bに最小公倍数または最大公約数を求めたい二つの整数の組を入れる
    def __init__(self,a,b):
        if a < b:a,b = b,a
        self.a = a
        self.b = b

    #最大公約数
    def gcd(self):
        a,b = self.a,self.b
        def nono(a,b):
            if a < b:a,b = b,a
            if a % b == 0:return b
            else:return nono(b,a % b)
        return nono(a,b)

    #最小公倍数
    def lcm(self):
        a,b = self.a,self.b
        def nono(a,b):
            if a < b:a,b = b,a
            if a % b == 0:return b
            else:return nono(b,a % b)
        num = nono(a,b)
        return a*b//num

n = int(input())
ans = 0
dp = [[[-1 for i in range(201)] for j in range(201)] for k in range(201)]
for i in range(1,n+1):
    for j in range(1,1+n):
        for k in range(1,1+n):
            ls = [i,j,k]
            ls.sort()
            if dp[ls[0]][ls[1]][ls[2]] != -1:
                ans += dp[ls[0]][ls[1]][ls[2]]
                continue
            c = LCM(i,j)
            l = LCM(c.gcd(),k)
            a = l.gcd()
            ans += a
            dp[ls[0]][ls[1]][ls[2]] = a

print(ans)

Submission status: Accepted

Solution code:
import math
import sys


input = sys.stdin.readline


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


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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K = int(input())

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from math import gcd

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

Submission status: Accepted

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

k = int(input())
a = [[0] * k for _ in range(k)]
ans = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        a[i - 1][j - 1] = gcd(i, j)
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for l in range(1, k + 1):
            ans += gcd(a[i - 1][j - 1], l)
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
import sys,math

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	k = II()
	ans = 0
	for i in range(1,k+1):
		for j in range(i,k+1):
			for k in range(j,k+1):
				if(i==j==k):
					ans += math.gcd(math.gcd(i,j),k)
				elif(i!=j and j!=k and k!=i):
					ans += math.gcd(math.gcd(i,j),k)*6
				else:
					ans += math.gcd(math.gcd(i,j),k)*3
	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):
        t = gcd(i, j)
        for k in range(1, K+1):
            ans += gcd(t, 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):
        for k in range(1, K+1):
            ans += math.gcd(math.gcd(i, j), k)

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

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

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


print(ans)

Submission status: Accepted

Solution code:
from math  import gcd
import sys
sys.setrecursionlimit(10**5)

K = int(input())
ans = 0
a = 1
while a <= K:
  b = 1
  while b <= K:
    c = 1
    while c <= K:
      ans += gcd(a, gcd(b, c))
      c += 1
    b += 1
  a += 1
print(ans)

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

print(ans)

Submission status: Accepted

Solution code:
#
from math import gcd


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def c_sum_of_gcd_of_tuples_easy():
    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)
    return ans

print(c_sum_of_gcd_of_tuples_easy())

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'


def main():
    K = ii()
    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)



main()

Submission status: Accepted

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


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

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd


def main():
    K = int(input())
    answer = 0
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            a = gcd(i, j)
            for k in range(1, K + 1):
                answer += gcd(a, k)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
K=int(input())

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

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

Solution code:
from math import gcd
k = int(input())
s = 0
for x in range(1, k + 1):
    for y in range(1, k + 1):
        t = gcd(x, y)
        for z in range(1, k + 1):
            s += gcd(t, z)
print(s)

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import Counter
from pprint import pprint
from math import gcd
from functools import reduce

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

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

k = int(input())

def gcd3(*numbers):
    return reduce(gcd, numbers)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

MOD = 1000000007

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

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

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

from collections import defaultdict

def main():
    K = int(readline())
    dd = defaultdict(int)

    for i in range(1,K+1):
        for j in range(1,K+1):
            dd[gcd(i,j)]+=1

    ans = 0
    for key in dd:
        for k in range(1,K+1):
            ans += dd[key]*gcd(key,k)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd
def solve():
    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)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

def solve(K: int):
    from math import gcd
    from itertools import product
    from functools import reduce
    def f():
        for abc in product(range(1, K+1), repeat=3):
            yield reduce(gcd, abc)
    print(sum(f()))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    solve(K)

if __name__ == '__main__':
    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(tmp, k)
print(ans)

Submission status: Accepted

Solution code:
def main():
    K = int(input())
    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):
                d = gcd(a, b)
                ans += gcd(c, d)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def gcd(a,b):
    while b:
        a,b=b,a%b
    return a
from functools import reduce
def gcd_list(numbers):
   return reduce(gcd, numbers)
k=int(input())
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for x in range(1,k+1):
            ans+=gcd_list([i,j,x])
print(ans)

Submission status: Accepted

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


K = int(input())
tot = 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):
            y = gcd(x, c)
            tot += y
print(tot)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

k = int(input())
count = 0

for a in range(1,k+1):
    for b in range(a,k+1):
        for c in range(b,k+1):
            if a == b and b == c:
                count += gcd(a,b,c)
            elif a == b:
                count += gcd(a,b,c)*3
            elif b == c:
                count += gcd(a,b,c)*3
            else:
                count += gcd(a,b,c)*6
        
print(count)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
gcdl = [[0 for i in range(k + 1)]for i in range(k+1)]
for i in range(1, k + 1):
    for j in range(1, k + 1):
        gcdl[i][j] = gcd(i, j)
s = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for l in range(1, k + 1):
            s += gcdl[i][gcdl[j][l]]

print(s)

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

Submission status: Accepted

Solution code:
from math import gcd

############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
n = inp()
s = 0
for i in range(1,n + 1):
    for j in range(1,n + 1):
        b = gcd(i,j)
        for k in range(1, n + 1):
            s += gcd(b,k)
print(s)
#print('No')
#print()

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

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

K = int(input())
cnt = [0] * (K ** 2 + 1)

for a in range(1, K + 1):
    for b in range(1, K + 1):
        cnt[gcd(a, b)] += 1
#print(cnt)

answer = 0
for ab in range(1, K ** 2 + 1):
    if cnt[ab]:
        for c in range(1, K + 1):
            answer += cnt[ab] * gcd(ab, c)
print(answer)

Submission status: Accepted

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

    

ans = 0

k = int(input())
for ai in range(1,k+1):
    for bi in range(ai,k+1):
        for ci in range(bi,k+1):
            count = len(set({ai,bi,ci}))
            if count == 1:
                comb = 1
            elif count ==2:
                comb =  3
            else:
                comb = 6
            ans += gcd(gcd(ai,bi),ci) * comb
print(ans)

Submission status: Accepted

Solution code:
# for #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
from math import gcd

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


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
	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 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:
from math import gcd
K = int(input())

res = 0

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

print(res)

Submission status: Accepted

Solution code:
def main():

    K = int(input())
    def gcd(a, b):
        if a > b: a, b = b, a
        if a == 0: return b
        return gcd(b%a, a)
    # print("here")
    ans = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            v = gcd(a, b)
            for c in range(1, K+1):
                ans += gcd(v, c)
                # print(ans)
    return ans

if __name__ == '__main__':
    print(main())

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

from math import gcd

k = int(readline())
cnt = 0
gcd_ans = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        gcd_ans = gcd(i, j)
        for l in range(1, k + 1):
            memo = gcd(l, gcd_ans)
            cnt += memo
print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin
from math import gcd
def main():
    #入力
    readline=stdin.readline
    K=int(readline())
    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(c,gcd(a,b))

    print(res)
if __name__=="__main__":
    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):
        g = math.gcd(i,j)
        for x in range(1,k+1):
            g0 = math.gcd(g,x)
            ans += g0
print(ans)

Submission status: Accepted

Solution code:
K=int(input())
from math import gcd
ans=0
g=[[0]*(K+1) for _ in range(K+1)]
for i in range(1,K+1):
    for j in range(1,K+1):
        g[i][j] = gcd(i,j)

for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            ans+=g[i][g[j][k]]
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

K = int(input())

ans = 0
for x, y, z in itertools.product(range(1, K + 1), repeat=3):
    a = math.gcd(x, y)
    b = math.gcd(a, z)
    ans += b

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def linput():
    return [int(x) for x in input().split()]

k = int(input())

ans = 0

from math import gcd

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

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# GCD -- START --
def gcd(x,y):
  while y:
    x,y=y,x%y
  return x
# GCD --- END ---

def main():
  k=I()

  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)

  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from math import gcd
def main():
    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):
                g = gcd(i, j)
                g = gcd(g, k)
                ans += g
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
total = 0
for l in range(1, k+1):
    for m in range(l, k+1):
        for n in range(m, k+1):
            if l != m and m != n:
                total += gcd(l, gcd(m, n)) * 6
            elif l != m or m != n:
                total += gcd(l, gcd(m, n)) * 3
            else:
                total += gcd(l, gcd(m, n))

print(total)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
k=int(input())
ans=0
import math
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            d=math.gcd(a,b)
            e=math.gcd(c,d)
            ans+=e
print(ans)

Submission status: Accepted

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

K = INT()

many_gcd = lambda numbers: reduce(gcd, numbers)

ans = 0
for i in range(1, K+1):
	for j in range(1, K+1):
		for k in range(1, K+1):
			ans += many_gcd([i, j, k])
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

v = 0

for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1,K+1):
            v += gcd(gcd(i,j), k)

print(v)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k = int(input())

from math import gcd

gcd_sum = [0] * 201
ans = 0


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):
        g = gcd(a,b)
        ans += gcd_sum[g]

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

ans=0
k=int(input())
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            ans+=gcd(gcd(i,j),l)
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(1,K+1):
        a = math.gcd(i,j)
        for k in range(1,K+1):
            s += math.gcd(a,k)

print(s)

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

print(ans)

Submission status: Accepted

Solution code:
k = int(input())
#a,b = map(int,input().split())
#a_L = list(map(int,input().split()))

import math
from functools import reduce

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

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

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

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

Submission status: Accepted

Solution code:
from math import gcd
K = int(input())
dic = dict()
s = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            s += gcd(gcd(i,j), k)
print(s)

Submission status: Accepted

Solution code:
from math import gcd
def main():
    K = int( input())
    ans = 0
    GCD = [0]*201
    for i in range(1,K+1):
        for j in range(1, K+1):
            GCD[gcd(i,j)] += 1
    for i in range(1, K+1):
        for k in range(1, K+1):
            ans += gcd(i, k)*GCD[i]
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
cnt = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        s = gcd(a, b)
        for c in range(1, K + 1):
            t = gcd(s, c)
            cnt += t

print(cnt)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

k = ni()

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

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:
from math import gcd
from functools import reduce
n=int(input())

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