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

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




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



if __name__ == "__main__":
	main()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)
    
    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)
                
    return ans

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

Solution (Python):
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_)

Solution (Python):
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)

Solution (Python):
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)))

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):


import sys
from math import gcd

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

K = INT()

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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()


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


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


print(main())

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
K = int(input())

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

gcd_table = [[0] * (K+1) for i in range(K+1)]
for i in range(1, K+1):
    for j in range(i, K+1):
        g = gcd(i, j)
        gcd_table[i][j] = gcd_table[j][i] = g


ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            g1 = gcd_table[a][b]
            g2 = gcd_table[g1][c]
            ans += g2

print(ans)

Solution (Python):
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)

Solution (Python):
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)

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
k = int(input())



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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):

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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
import math

def main():
    A = int(input())
    
    ans = 0
    for i in range(1,A+1):
        for j in range(1,A+1):
            _gcd = math.gcd(i,j)
            for k in range(1,A+1):
                gcd = math.gcd(_gcd,k)
                ans += gcd
    
    print(ans)
    
    
    
if __name__ == "__main__":
    main()

Solution (Python):
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)

Solution (Python):
import math
import itertools
K = int(input())
ans = 0
for x in itertools.product(list(range(1,K+1)), repeat = 3):
    [a,b,c] = x
    ans += math.gcd((math.gcd(a,b)),c)
print(ans)

Solution (Python):
from math import gcd

K = int(input())

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

print (ans)

Solution (Python):
import os

import itertools
import sys
from math import gcd

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


K = int(sys.stdin.buffer.readline())





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

Solution (Python):
from math import gcd

ans = 0
k = int(input().strip())
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)

Solution (Python):
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()

Solution (Python):
from math import gcd
K = int(input())
s = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            s += gcd(a,gcd(b,c))

print(s)

Solution (Python):
k = int(input())


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

ansl = [[gcd(i, j) for i in range(201)] for j in range(201)]

ans = 0
for i in range(1,k+1):
    for j in range(1, k + 1):
        p = ansl[i][j]
        for k in range(1, k + 1):
            ans += ansl[p][k]

print(ans)

Solution (Python):
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)

Solution (Python):
import math
K = int(input())

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

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

Solution (Python):
import math

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

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

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

Solution (Python):
K = int(input())

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

ret = 0

x = [0] * (K + 1)
for a in range(1, K + 1) :
  for b in range(1, K + 1) :
    x[gcd(a, b)] += 1

for c in range(1, K + 1) :
  for d in range(1, K + 1) :
    ret += gcd(c, d) * x[d]
    
print(ret)

Solution (Python):
from math import gcd as g
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 += g(g(a, b), c)
print(ans)

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

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

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

K = int(input())

res = 0

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

print(res)

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

print(ans)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(K: int):
    dp = [[-1] * (K + 1) for _ in range(K + 1)]
    def gcd(a, b):
        if dp[a][b] >= 0:
            return dp[a][b]
        else:
            ret = a if b == 0 else gcd(b, a % b)
        dp[a][b] = ret
        return ret
    ret = 0
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                tmp = gcd(i, j)
                ret += gcd(tmp, k)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    solve(K)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    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(i, gcd(j, l))
    print(ans)


main()

Solution (Python):
def Int():
    return int(input())
def Ints():
    return map(int,input().split())
def IntList():
    return list(Ints())
def IntMat(N):
    return [IntList() for i in range(N)]

import time
s = time.time()
import math
K = Int()
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)
g = time.time()


print(ans)

Solution (Python):

import math
INF = 2**62-1

def read_int():
    return int(input())


def slv(K):
    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)
    return ans


def main():
    K = read_int()
    print(slv(K))


if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd

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

print(res)

Solution (Python):

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


def gcdlist(l):
    res = l[0]
    for i in range(1,len(l)):
        res = gcd(res, l[i])
    return res

n = int(input())
res = 0

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):
            res += gcd(g,k)
print(res)

Solution (Python):

import sys,os
from collections import defaultdict, deque
from math import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

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

if __name__ == '__main__':
    main()

Solution (Python):

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)

Solution (Python):
def gcd(a: int, b: int) -> int:
    """a, bの最大公約数(greatest common divisor: GCD)を求める
    計算量: O(log(min(a, b)))
    """
    if b == 0:
        return a
    return gcd(b, a%b)
  
  
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 += gcd(gcd(a, b), c)
print(ans)

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

Solution (Python):
import math
from functools import reduce

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

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

Solution (Python):
import sys
from math import gcd
input = sys.stdin.readline
N = int(input())
res = 0
for i in range(1, N + 1):
  for j in range(1, N + 1):
    for k in range(1, N + 1):
      res += gcd(gcd(i, j), k)
print(res)

Solution (Python):
import sys

def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0:
        a, b = b, a % b
    return b

def solve():
    K = int(input())
    ans = 0
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            ab = gcd(i, j)
            for c in range(1, K + 1):
                ans += gcd(ab, c)
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
import copy
import itertools
n = int(readline())

lst1 = [i for i in range(1,n+1)]
lst2 = copy.deepcopy(lst1)
lst3 = copy.deepcopy(lst1)

def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
def lcm(a,b):
    return (a//gcd(a,b)*b)
ans = 0
for i,j,k in itertools.product(lst1,lst2,lst3):
    ans += gcd(i,gcd(j,k))
print(ans)

Solution (Python):

import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

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

Solution (Python):
from math import gcd
k = int(input())
ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        x = gcd(a, b)
        if x == 1:
            ans += k
            continue
        for c in range(1, k+1):
            ans += gcd(x, c)
print(ans)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

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


if __name__ == "__main__":
    main()

Solution (Python):
from math import *

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

print(ans)

Solution (Python):
k = int(input())
import math
ans = 0
def GCD(a,b,c):
    return math.gcd(math.gcd(a,b),c)
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)

Solution (Python):
import sys
from collections import *
import heapq
import math
from math import gcd
import bisect
import copy
from itertools import permutations,accumulate,combinations,product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

Solution (Python):
from math import gcd

k = int(input())

cnt = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        x = gcd(i, j)
        for l in range(1, k + 1):
            cnt += gcd(x, l)
print(cnt)

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

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)

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

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

    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read




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

Solution (Python):
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(l,gcd(i,j))

print(ans)

Solution (Python):
import math
k=int(input())
ans=0
for i1 in range(1,k+1):
  for i2 in range(1,k+1):
    for i3 in range(1,k+1):
      x=math.gcd(i1,i2)
      x=math.gcd(x,i3)
      ans+=x
print(ans)

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

Solution (Python):
from math import gcd
K=int(input())
a=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):
            a+=gcd(t,k)
print(a)

Solution (Python):




import math
K=int(input())
ans=0
for x in range(1,K+1):
    for y in range(1,K+1):
        for z in range(1,K+1):
            ans+=math.gcd(x,math.gcd(y,z))
print(ans)

Solution (Python):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
K = int(input())
ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            x = gcd(i,j)
            ans += gcd(x,k)
print(ans)