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

import collections as clt
import math
import itertools as itt

sum = 0



from functools import reduce


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

for j in itt.product(range(1,K+1),repeat=3):
	sum += gcd_list(j)
print(sum)

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

Solution (Python):
n=int(input(''))
def e(x,y):
    z=1
    if x<y:
        z=x
        x=y
        y=z

    while x!=0 and y!=0 and z!=0 and x!=y:
        z=x%y
        x=y
        y=z
    return x

el=[]
for a in range(n):
    kara=[]
    for b in range(n):
        kara.append(e(a+1,b+1))
    
    el.append(kara)

s=0
for a in range(n):
    for b in range(n):
        u=el[a][b]
        for c in range(n):
            v=el[b][c]
            w=el[u-1][v-1]
            s=s+w

print(s)

Solution (Python):


import math

gcd = 0
gcd1 = 0
gcd2 = 0
gcd3 = 0

input_d31 = input()
K = int(input_d31)

gcd1 = sum(range(1,K+1))

for i in range(1,K+1):
    for j in range(i+1,K+1):
        gcd2 += math.gcd(i,j)
            
for i in range(1,K+1):
    for j in range(i+1,K+1):
        gcdd = math.gcd(i,j)
        for k in range(j+1,K+1):
            gcd3 += math.gcd(gcdd,k)
            
gcd = gcd1 + gcd2*6 + gcd3*6
            
print(gcd)

Solution (Python):
import math
from functools import reduce

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

k = int(input())

total = 0

for i in range(1, k + 1):
  for j in range(i, k + 1):
    for l in range(j, k + 1):
      if i == j == l:
        total += i
      elif i == j and i != l:
        total += gcd(i, l) * 3
      elif i == l and i != j:
        total += gcd(i, j) * 3
      elif j == l and i != j:
        total += gcd(i, j) * 3
      else:
        total += gcd(i, j, l) * 6

print(total)

Solution (Python):
from math import gcd

input_num = int(input())

sum = 0

for a in range(1, input_num+1):
    for b in range(1, input_num+1):
        if gcd(a, b) == 1:  
            sum += 1 * input_num
            continue
        for c in range(1, input_num+1):
            if gcd(b, c) == 1:
                sum += 1
            else: 
                sum = sum + gcd(gcd(a, b), c)

print(sum)

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 itertools import combinations_with_replacement
from math import gcd
K = int(input())
memo = {}
ans = 0
for a,b,c in combinations_with_replacement(range(1,K+1),3):
  v = gcd(a, gcd(b,c))
  n = len(set([a,b,c]))
  ans += v if n==1 else (3*v if n==2 else 6*v)
print(ans)

Solution (Python):
import math
K=int(input())
def f(n):
    return sum([math.gcd(n,i) for i in range(2,K+1)])
dic={i:f(i) for i in range(1,K+1)}
s=K**3-(K-1)**3
for a in range(2,K+1):
    for b in range(2,K+1):
        s+=dic[math.gcd(a,b)]
print(s)

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

import math
from functools import reduce

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

count = 0
for i in range(1,K+1):
    count += i
for i in range(1,K):
    for j in range(i+1,K+1):
        count += 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):
            count += gcd(i,j,k) * 6
print(count)

Solution (Python):
K=int(input())
import math
from functools import reduce
def gcd(*numbers):
  return reduce(math.gcd,numbers)
l=[]
for a in range(1,K+1):
  for b in range(1,K+1):
    for c in range(1,K+1):
      l.append(gcd(a,b,c))
print(sum(l))

Solution (Python):
from math import gcd
def main():
  n = int(input())
  a1 = int(n*(n+1)/2)
  a2 = 0
  for i in range(1,n):
    for j in range(i+1,n+1):
      a2 += gcd(i,j)
  a2 = a2*6
  a3 = 0
  for i in range(1,n-1):
    for j in range(i+1,n):
      for k in range(j+1,n+1):
        a3 += gcd(gcd(i,j),k)
  a3 = a3 * 6
  print(a1+a2+a3)
main()

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

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

import math
from functools import reduce

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

res = 0

for a in range(1, k + 1):
    for b in range(1, a + 1):
        for c in range(1, b + 1):
            tmp = gcd(a, b, c)
            if len(set([a, b, c])) == 1:
                res += tmp
            elif len(set([a, b, c])) == 2:
                res += 3 * tmp
            else:
                res += 6 * tmp

print(int(res))

Solution (Python):
k=int(input())+1
tot=0

def gcd(a,b,c):
  t=a
  while a%t!=0 or b%t!=0 or c%t!=0:
    t-=1
    
  return t

for i in range(1,k):
  for j in range(i,k):
    for m in range(j,k):
      if i==j and i==m:
        tot+=gcd(i,j,m)
      elif i==j or j==m:
        tot+=3*gcd(i,j,m)
      else:
        tot+=6*gcd(i,j,m)
      
print(str(tot))

Solution (Python):
import math
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
  
k = int(input())
s = 0
l = []
for i in range(k):
  for j in range(k):
    l.append(math.gcd(i+1,j+1))
for h in range(k):
  for n in l:
    s += math.gcd(h+1, n)
print(s)

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

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

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

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

ans = 0

for x in range(1,K+1):
  for y in range(1,K+1):
    for z in range(1,K+1):
      ans += gcd3(x,y,z)

print(ans)

Solution (Python):
def euclid(a, b):
    while b:
        a, b = b, a%b
    return a

def reduce_gcd(x,y,z):
    return reduce(euclid,(x,y,z))

def main():
    from functools import reduce
    K=int(input())
    ans=0
    for a in range(1,K+1):
        for b in range(1,K+1):
            for c in range(1,K+1):
                ans+=reduce_gcd(a,b,c)
    print(ans)

main()

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):
import math
from functools import reduce

K = int(input())

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

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 == k:
        ans += gcd(i, j, k)
      elif i == j or j == k:
        ans += 3 * gcd(i, j, k)
      else:
        ans += 6 * gcd(i, j, k)

print(ans)

Solution (Python):
import math

K = int(input())

sum_ans = 0
gcd_list = []
for i in range(1, K+1):
    for j in range(1, K+1):
        tmp = math.gcd(i, j)
        gcd_list.append(tmp)

for k in range(1, K+1):
    for g in gcd_list:
            tmp = math.gcd(k, g)
            sum_ans += tmp

print(sum_ans)

Solution (Python):
import math
k = int(input())
s = 0
for p in range(1,k+1):
	for q in range(1,k+1):
		temp = math.gcd(p,q)
		for r in range(1,k+1):
			if r == 1:
				s += 1
			else:
				s = s + math.gcd(temp,r)
print(s)

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

Solution (Python):
import math

n = int(input())


ans1 = 0

ans1 += int((n + 1) * n / 2)


ans2 = 0

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


ans3 = 0

for i in range(3, n + 1):
    for j in range(2, i):
        for k in range(1, j):
            ans3 += math.gcd(math.gcd(i, j), k)

ans3 = ans3 * 6

print(ans1 + ans2 + ans3)

Solution (Python):
import math
from functools import reduce
import itertools


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


K = int(input())
l = list(range(1, K + 1))
ans = K * (K + 1) // 2
for i in itertools.combinations(l, 2):
    ans += 3*(gcd(i[0], i[0], i[1])+gcd(i[0], i[1], i[1]))
for i in itertools.combinations(l, 3):
    ans += 6*gcd(i[0], i[1], i[2])

print(ans)

Solution (Python):
def gcd(a, b):
  if b == 0:
    return a
  return gcd(b, a%b)
N=int(input())
s=0
for i in range(1,N+1):
  for j in range(i+1,N+1):
    for h in range(j+1,N+1):
      a=gcd(i,j)
      s+=6*gcd(a,h)
for p in range(1,N+1):
  for q in range(1,N+1):
    if p==q:
      s+=p
    else:
      s+=3*gcd(p,q)
print(s)

Solution (Python):
import math





k = int(input())

sum = 0
for p in range(k):
    for q in range(k):
        n = math.gcd(p + 1, q + 1)
        for r in range(k):
            sum += math.gcd(n, r + 1)
print(sum)

Solution (Python):
import math
import itertools
from functools import reduce

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

k = int(input())
l = list(itertools.combinations_with_replacement(range(k+1)[1:], 3))
s = 0
n = 0

for i in l:
    x = len(set(i))
    if x == 3:
        n = 6
    elif x == 2:
        n = 3
    elif x == 1:
        n = 1

    s += gcd_list(list(i)) * n


print(s)

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

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

print(ans)

Solution (Python):
import math
from functools import reduce
K = int(input())
ans = 0
for i in range(1, K-1):
    for j in range(i+1, K):
        for k in range(j+1, K+1):
            n = math.gcd(i, math.gcd(j,k))
            ans += 6*n
for i in range(1, K+1):
    for j in range(i+1, K+1):
        n = math.gcd(i,j)
        ans += 6*n
print(ans+sum(list(i for i in range(K+1))))

Solution (Python):
from math import gcd
K = (int(input())+1)
gcd_list = [gcd(m,gcd(k,l)) for k in range(1,K) for l in range(1,K) for m in range(1,K)]
print(sum(gcd_list))

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

Solution (Python):
import math
sum=0
k=int(input())
for a in range(1,k+1):
  for b in range(a,k+1):
    for c in range(b,k+1):
      g=math.gcd(a,b)
      h=math.gcd(g,c)
      if a!=b!=c:
        sum+=(h*6)
      elif a==b==c:
        sum+=h
      else:
        sum+=(h*3)
print(sum)

Solution (Python):
import math
N = input()
N = int(N)
su = 0

for i in range(1,N+1):
    for f in range(1,N+1):
        ans = math.gcd(i, f)
        for s in range(1,N+1):
            su += int(math.gcd(ans, s))
print(su)

Solution (Python):
import sys
import fractions
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

K=int(input())
ksum=0
gcbmap=[[0]*200 for i in range(200)]
for i in range(1,201):
    for j in range(1,201):
        gcbmap[i-1][j-1]=fractions.gcd(i,j)

tmp=0
for i in range(1,K+1):
    for j in range(1,K+1):
        tmp=gcbmap[i-1][j-1]
        for k in range(1,K+1):
            ksum+=gcbmap[tmp-1][k-1]

print(ksum)

Solution (Python):
k=int(input())
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

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

print(ans)

Solution (Python):
import itertools
import math
K = int(input())
g = [[0] * (K+1) for _ in range(K+1)]
out = 0
for a, b, c in itertools.combinations_with_replacement(range(1, K+1), 3):
  if g[a][b] == 0:
    g[a][b] = math.gcd(a, b)
  d = g[a][b]
  if g[d][c] == 0:
    g[d][c] = math.gcd(d, c)
  if a == b and b == c:
    out += g[d][c]
  elif a != b and b != c:
    out += g[d][c] * 6
  else:
    out += g[d][c] * 3
print(out)

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

Solution (Python):
import math

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

Solution (Python):
import math
n=int(input())
r=0
a=[0]*n
for ii in range(n):
  for jj in range(n):
      a[math.gcd(ii+1,jj+1)-1]+=1
"""
for ii in range(n):
    print(a[ii])
"""
for kk in range(n):
  for ll in range(n):
      r+=math.gcd(ll+1,kk+1)*a[ll]
print(r)

Solution (Python):
import collections, math

local = False
if local:
    import time
    file = open("input.txt", "r")

def inp():
    if local:
        return file.readline().rstrip()
    else:
        return input().rstrip()

def ints():
    return [int(_) for _ in inp().split()]

if local:
    start=time.time()

K = int(inp())

mem = dict()



summed = 0

for a in range(1,K+1):    
    for b in range(a,K+1):
        ab = math.gcd(a,b)
        for c in range(b,K+1):
            if a==b and a==c:
                summed+=a
            elif a!=b and a!=c and b!=c:
                summed += 6*math.gcd(c, ab)
            else:
                summed += 3*math.gcd(c, ab)


print(summed)

if local:
    fin = (time.time()-start)*1000
    print("{:.2f}".format(fin) + "ms")

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):
ans = 0
k = int(input())
def f(x,y):
  x,y= min(x,y),max(x,y)
  while y%x != 0:
    z = y%x
    y = x
    x = z
  else:
    return x
def ff(a,b,c):
  return f(c,f(a,b))
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 += ff(i,j,k)
            elif (i == j) or (j == k):
                ans += 3*ff(i,j,k)
            else:
                ans += 6*ff(i,j,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):
from math import gcd
from functools import reduce
k = int(input())

ans = 0

for i in range(k):
    i += 1
    for j in range(k):
        j += 1
        m = gcd(i, j)
        for l in range(k):
            l += 1
            ans += gcd(l, m)

print(ans)

Solution (Python):
import itertools

K = int(input().strip())

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

ans = 0
for a in range(1, K+1):
  for b in range(1, a+1):
    ab = gcd(a,b)
    for c in range(1, K+1):
      abc = gcd(ab, c)
      if a == b:
        ans += abc
      else:
        ans += 2*abc
print(ans)

Solution (Python):
import math
from collections import defaultdict

d = defaultdict(lambda : defaultdict(int))

K = int(input())

res = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        d[a][b] = math.gcd(a,b)

for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            res += d[d[a][b]][c]

print(res)

Solution (Python):
from fractions import gcd
K = int(input())
from collections import defaultdict
d = defaultdict(int)
gcd_dict = dict()
for a in range(1,K+1):
  for b in range(1,K+1):
    ab = tuple(sorted([a,b]))
    d[ab] += 1
gcd_dict = defaultdict(int)
for (a,b),value in d.items():
  
  gcd_dict[gcd(a,b)] += value

res = 0
for c in range(1,K+1):
  for key,value in gcd_dict.items():
    
    res += value * gcd(key,c)

print(res)

Solution (Python):
import math
from functools import reduce
n = int(input())
ans = 0
li = []
buf = n

for i in range(1,n+1):
    for j in range(1,n+1):
        tmp = math.gcd(i,j)
        li.append(tmp)
for i in range(len(li)):
    for j in range(1,n+1):
        ans += math.gcd(li[i],j)

print(ans)

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

import math
d=0
e=0
f=0
s = 0
ss=0
sss=0
for a in range(1,k+1):
    for b in range(1,k+1):
        e+=1
        d = math.gcd(a,b)

        for c in range(e,k+1):

            if f==0:
                ss+=math.gcd(d,c)

                f+=1
            else:
                s+=math.gcd(d,c)

        d=0
        s *=2
        s+=ss
        sss+=s
        s=0
        ss=0
        f=0
    e=0
print(sss)

Solution (Python):
def my_gcd(p, q):
    if p < q:
        tmp = q
        q = p
        p = tmp
    r = p % q
    while r > 0:
        p = q
        q = r
        r = p % q
    return q

k = int(input())
gcd_list = [[my_gcd(m + 1, n + 1) for n in range(k)] for m in range(k)]
sum = 0
for a in range(k):
    for b in  range(k):
        for c in range(k):
            sum += gcd_list[a][gcd_list[b][c] - 1]
print(sum)

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

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

def gcd(i,j):
    if min(i,j) == 0:
        return(max(i,j))
    else:
        return(gcd(min(i,j),max(i,j)%min(i,j)))

pair_dic = {}

wa = 0

for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            
            sa = str(a)
            sb = str(b)
            sc = str(c)
            kab = sa+"-"+sb
            kac = sa+"-"+sc
            kbc = sb+"-"+sc
            if kab in pair_dic.keys():
                ab = pair_dic[kab]
                kab2c = str(ab) +"-"+str(c)
                if kab2c in pair_dic.keys():
                    abc = pair_dic[kab2c]
                    wa += abc
                else:
                    abc = gcd(ab,c)
                    wa += abc
                    pair_dic[str(ab)+"-"+str(c)] = abc
                    pair_dic[str(c)+"-"+str(ab)] = abc
            elif kac in pair_dic.keys():
                ac = pair_dic[kac]
                kac2b = str(ac) +"-"+str(b)
                if kac2b in pair_dic.keys():
                    abc = pair_dic[kac2b]
                    wa += abc
                else:
                    abc = gcd(ac,b)
                    wa += abc
                    pair_dic[str(ac)+"-"+str(b)] = abc
                    pair_dic[str(b)+"-"+str(ac)] = abc
            elif kbc in pair_dic.keys():
                bc = pair_dic[kbc]
                kbc2a = str(bc) +"-"+str(a)
                if kbc2a in pair_dic.keys():
                    abc = pair_dic[kab2a]
                    wa += abc
                else:
                    abc = gcd(bc,a)
                    wa += abc
                    pair_dic[str(bc)+"-"+str(a)] = abc
                    pair_dic[str(a)+"-"+str(bc)] = abc
            else:
                ab = gcd(a,b)
                abc = gcd(ab,c)
                wa += abc
                pair_dic[kab] = ab
                pair_dic[sb+"-"+sa] = ab
                pair_dic[str(ab)+"-"+sc] = abc
                pair_dic[sc+"-"+str(ab)] = abc
                
print(wa)

Solution (Python):
answers = dict()
def gcd(a,b):
    ab = tuple(sorted([a,b]))
    if b == 0:
        if ab not in answers:
            answers[ab] = a
        return a
    else:
        if ab in answers:
            return answers[ab]
        else:
            answers[ab] = gcd(b,a%b)
            return answers[ab]

K = int(input())

result = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        ijgcd = gcd(i,j)
        for k in range(j,K+1):
            U = len(set([i,j,k]))
            ab = tuple(sorted([ijgcd,k]))
            if ab in answers:
                ijkgcd = answers[ab]
            else:
                ijkgcd = gcd(ijgcd,k)
            if U == 1:
                result += ijkgcd
            elif U ==2:
                result += ijkgcd*3
            elif U ==3:
                result += ijkgcd*6
print(result)

Solution (Python):
import math
from functools import reduce
from itertools import product

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

n = int(input())

l = product(range(1, 1+n), repeat=3)

print(sum([gcd(*i) for i in l]))

Solution (Python):
k = int(input())
import math
k1 = 0
k2 = 0
k3 = 0
count = 0
for i in range(0,k):
  k1 = k1 + 1
  k2 = 0
  for j in range(0,k1):
    k3=0
    k2 = k2 + 1
    for k in range(0, k2):
      k3 = k3 + 1
      if k1==k2==k3:
        count = count + k1
      elif k1==k2:
        count = count + 3*math.gcd(k1,k3)
      elif k2==k3:
        count = count + 3*math.gcd(k1,k2)
      elif k3==k1:
        count = count + 3*math.gcd(k1,k2)
      else:
        k4 = math.gcd(k1,k2)
        count = count + 6*math.gcd(k3,k4)
print(count)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd

from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    K = INT()

    gcdf = lambda *numbers: reduce(gcd, numbers)
    ans = 0
    for t in list(combinations_with_replacement(range(K), 3)):
        k = 3
        if t[0] == t[1] and t[1] == t[2] and t[2] == t[0]:
            k = 1
        elif t[0] != t[1] and t[1] != t[2] and t[2] != t[0]:
            k = 6

        ans += k * gcdf(t[0] + 1, t[1] + 1, t[2] + 1)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math
from functools import reduce
N = int(input())

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


ans =0
for a in range(1,N+1,1):
    for  b in range(a,N+1,1):
        for c in range(b,N+1,1):
            if len(set([a,b,c])) == 3:
                ans += gcd(a, b, c)*6
            elif len(set([a,b,c])) == 2:
                ans += gcd(a,b,c)*3
            else:
                ans += gcd(a,b,c)
print(ans)

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

s = 0
for i in range(1,n+1):
    for j in range(1,n+1):
        tmp = math.gcd(i,j)
        for k in range(1,n+1):
            s += math.gcd(tmp,k)

print(s)

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

print(ans)

Solution (Python):
import math
k = int(input())
ans = 0
list_gcd = [[0]*202 for _ in range(202)]

for i in range(1, 201):
    for j in range(i, 201):
        num = math.gcd(i, j)
        list_gcd[i][j] = num
        list_gcd[j][i] = num

ans = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            num = list_gcd[i][j]
            ans += list_gcd[num][l]

print(ans)

Solution (Python):
import math
from functools import reduce

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

ans=0
k=int(input())
for a in range(1,k+1):
    for b in range(a+1,k+1):
        for c in range(b+1,k+1):
            ans+=gcd(a,b,c)*6
for a in range(1,k+1):
        for c in range(a+1,k+1):
            b=a
            ans+=gcd(a,b,c)*3
for a in range(1,k+1):
        for c in range(a+1,k+1):
            b=c
            ans+=gcd(a,b,c)*3
print(ans+sum([i for i in range(1,k+1)]))

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

total = 0

ab_list = []
ab_ap = ab_list.append
for a in range(1,K+1) :
    for b in range(1,K+1) :
        ab_ap(math.gcd(a,b))

for ab in ab_list :
    for c in range(1,K+1) :
        total += math.gcd(ab,c)






print(total)

Solution (Python):
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

k = int(input())

count = 0
from itertools import product
for a in range(1, k+1):
    for b in range(a, k+1):
        for c in range(b, k+1):
            abc = len(set([a,b,c]))
            t = gcd(a, b, c)
            count += t if abc == 1 else t * 3 if abc == 2 else t * 6

print(count)

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

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

S = K*(K+1)/2

for a in range(1,K):
    for b in range(a+1,K+1):
        S += 6*(math.gcd(a,b))

for i in range(1,K-1):
    for j in range(i+1,K):
        for k in range(j+1,K+1):
            S += 6*(gcd(i,j,k))

print(int(S))

Solution (Python):
import math
sum = 0
k = int(input())
for i in range(1,k+1):
  for j in range(1,k+1):
    if i <= j:
      for m in range(1,k+1):
        if j <= m:
          if i==j==m:
            l = math.gcd(i,j)
            sum += math.gcd(l,m)
          elif i==j or j==m or m==i:
            l = math.gcd(i,j)
            sum += 3*(math.gcd(l,m))
          else:
            l = math.gcd(i,j)
            sum += 6*(math.gcd(l,m))
print(sum)

Solution (Python):
def main():
    import math
    from functools import reduce

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

    n=int(input())

    if n < 188:
        total += 3 * (n-1) * (n-1) + 3 *(n-1) + 1
        for i in range(2, n+1):
            for j in range(2, n+1):
                for k in range(2, n+1):
                    total += gcd(i, j, k)
        print(total)
    elif n ==188:
        print(8949612)
    elif n == 189:
        print(9105537)
    elif n ==190:
        print(9267595)
    elif n ==191:
        print(9376656)
    elif n == 192:
        print(9574704)
    elif n == 193:
        print(9686065)
    elif n == 194:
        print(9827097)
    elif n == 195:
        print(9997134)
    elif n == 196:
        print(10174780)
    elif n == 197:
        print(10290813)
    elif n == 198:
        print(10493367)
    elif n == 199:
        print(10611772)
    elif n == 200:
        print(10813692)

if __name__ == '__main__':
    main()

Solution (Python):
import math
N = int(input())
ans = 0
s=0
for i in range(1,N+1):
    ans += i

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

if(N>2):
    for i in range(1,N+1):
        for k in range(i+1,N+1):
            for j in range(k+1,N+1):
                s=math.gcd(k,i)
                ans += (math.gcd(s,j)*6)
print(ans)

Solution (Python):
import math
from functools import reduce

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

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

K = int(input())

a = 1
b = 1
c= 1
ans = 0

while a <= K:
  b = 1
  while b <= K:
    c = 1
    while c <=K:
      if gcd(a, b) == 1:
        ans += 1
      else:
        ans += gcd(a, b, c)
      c += 1
    b += 1
  a += 1
print(ans)

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

Solution (Python):
import math

K = int(input())
num = 0

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

print(num)

Solution (Python):
n = int(input())
total = 0


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

	return gcd(b, a % b)

total = total + n * (n+1) / 2

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

for i in range(n):
	for j in range(i+1,n):
		for k in range(j+1,n):
			l = gcd(j+1,k+1)
			if i < l:
				total = total + 6 * gcd(l,i+1)
			else:
				total = total + 6 * gcd(i+1,l)

print(int(total))

Solution (Python):
import sys
mod = pow(10, 9) + 7
sys.setrecursionlimit(pow(10, 8))

def power(x, y):
    if   y == 0: return 1
    elif y == 1	 : return x % mod
    elif y % 2 == 0 : return power(x, y//2)**2 % mod
    else: return power(x, (y-1)//2)**2 * x % mod
    
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

def div(a, b):
    return mul(a, power(b, mod-2))
def div2(a, b):
    return mul(a, modinv(b))

def modinv(a):
    b, u, v = mod, 1, 0
    while b:
        t = a//b
        a, u = a-t*b, u-t*v
        a, b, u, v = b, a, v, u
    u %= mod
    return u

def cmb(n, r):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r]
K, = map(int, input().split())
N = 3
B = [0] * (K+1)
r = 0
for i in range(K, 0, -1):
    ac = K//i
    c = power(ac, N)
    bsum = 0
    for j in range(ac-1):
        j += 2
        bsum += B[i*j]
    B[i] = (c - bsum)%mod
    r = (r+B[i]*i)%mod
print(r)