Problem:


Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692



Sample input:
2


Sample output:
9

Submission status: Accepted

Solution code:
import math
from functools import reduce
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
k =int(input())
ans1=0
ans2=0
ans3=0
ans=0
p=[0,0,0]
if k==1:
    print(k)
    exit()
for i in range(1,k+1):
    ans1+=i
for a in range(2,k+1):
    for b in range(1,k+1):
        if a<b:
            continue
        for c in range(1,k+1):
            if b<c:
                continue
            if a==b and b==c:
                continue
            p[0]=a
            p[1]=b
            p[2]=c
            if a==b or b==c:
                ans2+=gcd_list(p)
            else:
                ans3+=gcd_list(p)
ans=ans1+ans2*3+ans3*6
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

k = int(input())
list = [0]*201
for b in range(1, k + 1):
    for c in range(1, k + 1):
        list[math.gcd(b, c)] += 1
list_2 = [0]*201
for i in range(k):
    for a in range(1, k + 1):
        list_2[i + 1] += math.gcd(i + 1, a)*list[i + 1]
print(sum(list_2))

Submission status: Accepted

Solution code:
import math
import itertools

k = int(input())

list= []

ans = 0

for i in range(k):
  list.append(i+1)
  
ans += sum(list)

for i in itertools.combinations(list,2):
  x,y= i
  ans += math.gcd(x,y) * 6
  
for i in itertools.combinations(list,3):
  x,y,z = i
  ans+= math.gcd(math.gcd(x,y),z) * 6
  
  
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
n = int(input())

sum = 0
for i in range(1,n+1):
  for j in range(i,n+1):
    for k in range(j,n+1):
#      print(i, j, k)
      if i ==j and i == k:
        sum += gcd(i, j, k)
      elif i == j or j == k or i == k:
        sum += gcd(i, j, k)*3
      else: 
        sum += gcd(i, j, k)*6
#      print(sum)
print(sum)

Submission status: Accepted

Solution code:
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):
            num=math.gcd(x,y)
            ans+=math.gcd(num,z)
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
r = range(1, int(input()) + 1)
print(sum(gcd(gcd(i,j),k) for i in r for j in r for k in r))

Submission status: Accepted

Solution code:
import math
from functools import reduce
def gcd_list(numbers):
  return reduce(math.gcd, numbers)

K = int(input())
gcd = 0

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

print(gcd)

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())
result = 0

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

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

print(result)

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:
import math

k = int(input())

k += 1
ans = 0

ab = []

for a in range(1, k):
    for b in range(1, k):
       ab.append(math.gcd(a, b))

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
 
k=int(input())
cnt=0
tmp=[]
for a in range(k):
  a=a+1
  for b in range(k):
    b=b+1
    tmp.append(math.gcd(a,b))
for i in tmp:
  if i==1:
    cnt=cnt+k
  else:
    for j in range(k):
      j=j+1
      cnt=cnt+math.gcd(i,j)
print(cnt)

Submission status: Accepted

Solution code:
import math

k = int(input())
two = []
total = 0
for x in range(1, k + 1):
	for y in range(1, k + 1):
		two.append(math.gcd(x, y))

for z in range(1, k + 1):
	for i in two:
		total += math.gcd(z, i)
print(total)

Submission status: Accepted

Solution code:
from math import gcd

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

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

print(ans)

Submission status: Accepted

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

def Main(N):
    ans = 0
    memo2 = [[0 for _ in range(N+1)] for _ in range(N+1)]
    for i in range(1, N+1):
        for j in range(1, N+1):
            if memo2[i][j] == 0:
                memo2[i][j] = gcd(i,j)
            for k in range(1, N+1):
                M = memo2[i][j]
                if memo2[M][k] == 0:
                    memo2[M][k] = gcd(M, k)
                ans += memo2[M][k]
    return ans
    

def main():
    N = int(input())
    print(Main(N))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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


sum = 0
for i in range(1,K+1):
  for j in range(i+1,K+1):
    for k in range(j+1,K+1):
      sum = sum + 6*gcd(i,j,k)
      
for i in range(1,K+1):
  for j in range(i+1,K+1):
     sum = sum + 6*math.gcd(i,j)
      
sum = sum + K*(K+1)//2

print(sum)

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:
import math
from functools import reduce
 
K = int(input())
total = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            total += reduce(math.gcd, [a,b,c])
 
print(total)

Submission status: Accepted

Solution code:
import math
from functools import reduce

k = int(input())
l = []
s = 0


for a in range(1,k+1):
    for b in range(1,k+1):
        l.append(math.gcd(a, b))

for c in range(1,k+1):
  for i in range(len(l)):
    s += math.gcd(l[i],c)
print(s)

Submission status: Accepted

Solution code:
import math

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

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


Y = [0]*(K+1)
for x in range(1, K+1):
    for c in range(1, K+1):
        g = math.gcd(x, c)
        Y[g] += X[x]

print(sum(i*Y[i] for i in range(1, K+1)))

Submission status: Accepted

Solution code:
import fractions

K = int(input())

gcds = []

for i in range(K):
  g = []
  for j in range(K):
    g.append(fractions.gcd(i + 1, j + 1))
  gcds.append(g)

result = 0

for a in range(K):
  for b in range(K):
    gcd_ab = gcds[a][b]
    for c in range(K):
      result += (gcds[gcd_ab-1][c])

print(result)

Submission status: Accepted

Solution code:
# K, N = map(int, input().split())
# a = list(map(int, input().split()))

# N = int(input())

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

K = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
import math
 
 
def gcd(a, b, c):
  d = math.gcd(a, b)
  return math.gcd(c, d)
 
K = int(input())
sum = 0
for i in range(1, K+1):
  sum += gcd(i, i, i)
  
for i in range(1, K+1):
  for j in range(i, K+1):
    for k in range(j, K+1):
      if i == j and j == k:
        sum += 0
      elif i == j or j == k or i == k:
        sum += gcd(i , j , k )*3
      elif i != j and j != k :
        sum += gcd(i, j, k)*6

print(sum)

Submission status: Accepted

Solution code:
import sys
from math import gcd

K = int(sys.stdin.readline().rstrip())
ans = 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 == c:
                ans += a
            elif a == b:
                ans += gcd(a, c) * 3
            elif a == c:
                ans += gcd(a, b) * 3
            elif b == c:
                ans += gcd(a, b) * 3
            else:
                ans += gcd(gcd(a, b), c) * 6

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
count=0
N=int(input())
for i in range(1,N+1):
	for j in range(i,N+1):
		for k in range(j,N+1):
			if i==j and j==k and k==i:
				count+=(gcd_list([i,j,k]))
			elif i==j or j==k or i==k:
				count+=(gcd_list([i,j,k]))*3
			else:
				count+=(gcd_list([i,j,k]))*6

print(count)

Submission status: Accepted

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

a=[]
b=0

for i in range(1,1+K):
  for j in range(1,K+1):
    gcd=math.gcd(i,j)
    a.append(gcd)

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

print(b)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
import math
N = int(input())
def gcdtriple(a,b,c):
    return math.gcd(a, math.gcd(b,c))
ans = 0
for a in range(1, N+1):
    for b in range(1, a+1):
        for c in range(1, b+1):
            if a == b and b == c:
                ans += gcdtriple(a,b,c)
            elif a == b or b == c or c == a:
                ans += gcdtriple(a,b,c) * 3
            else:
                ans += gcdtriple(a,b,c) * 6
print(ans)

Submission status: Accepted

Solution code:
import math
K = int(input())
p = int(K*(K+1)/2)
for a in range(K):
    for b in range(a+1, K):
        g = math.gcd(a+1, b+1)
        p += 6*g
        for c in range(b+1, K):
                p += 6*math.gcd(g, c+1)
print(p)

Submission status: Accepted

Solution code:
import fractions as fr
k = int(input())
ans = (k+1)*k//2
tttt = [[0]*(k+1) for _ in range(k+1)]
for i in range(1, k+1):
  for j in range(1, k+1):
    if i == j:
      continue
    else:
      if tttt[i][j] != 0:
        g = tttt[i][j]
        ans += 3*g
      else:
        g = fr.gcd(i,j)
        tttt[i][j] = g
        tttt[j][i] = g
        ans += 3*g
for i in range(1, k-1):
  for j in range(i+1, k):
    for l in range(j+1, k+1):
      temp = tttt[i][j]
      temp2 = tttt[temp][l]
      ans+= 6*temp2
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
import math
from functools import reduce
import numpy as np

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()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]

k = int(input())
def gcd(*numbers):
    return reduce(math.gcd, numbers)

count = 0
a = []
for i in range(1,k+1):
    for j in range(1,k+1):
        a = math.gcd(i,j)
        for l in range(1,k+1):
            count += math.gcd(a,l)
print(count)

Submission status: Accepted

Solution code:
import math
k = int(input())
t = 0
# a = b = c
for x in range(1, k+1):
    t += x
# a = b != c
for a in range(1, k+1):
    for b in range(a+1, k+1):
        t += 6*math.gcd(a, b)
# a != b != c
for a in range(1, k+1):
    for b in range(a+1, k+1):
        for c in range(b+1, k+1):
            d = math.gcd(a, b)
            t += 6*math.gcd(d, c)
print(t)

Submission status: Accepted

Solution code:
import math

n = int(input()) 
total = 0
for a in range(1, n + 1):
    for b in range(1, n + 1):
        g = math.gcd(a ,b)
        if g == 1:
            total += n
        else:
            for c in range(1, n + 1):
                total += math.gcd(g, c)

print(total)

Submission status: Accepted

Solution code:
import math

def mgcd(a, b, c):
    g = math.gcd(a, b)
    g = math.gcd(g, c)
    return g
    
memo={}
cnt = 0

K = int(input())

for a in range(1, K+1):
    for b in range(1, K+1):
        p = [a, b]
        #p.sort()
        key = str(p)
        if memo.get(key):
            #cnt += memo[key]
            continue
        else:
            g = math.gcd(a, b)
            memo[key] = g
            #cnt += g

#print(memo)
for c in range(1, K+1):
    for v in memo.values():
        #print(v, c)
        cnt += math.gcd(v, c)
        
print(cnt)

Submission status: Accepted

Solution code:
#test.py
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())
ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            if i==1 or j==1 or k ==1:
                ans += 1
            else:
                ans += gcd(i,j,k)

print(ans)

Submission status: Accepted

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

print(c)

Submission status: Accepted

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

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

for a in range(1,K+1):
    for b in range(1,K+1):
        sum += gcd(gcd(a,b),b) * 3

for i in range(1,K+1):
    sum -=2*i
    
print(sum)

Submission status: Accepted

Solution code:
import math
from functools import reduce

k = int(input())

ans = 0

ab = [math.gcd(a,b) for a in range(1, k+1) for b in range(1, k+1)]

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

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools

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

k=int(input())

l=[]
for i in range(1,k+1):
    l.append(i)

c_list = list(itertools.combinations_with_replacement(l,3))

cnt=0
for l in c_list:
    a = l[0]
    b = l[1]
    c = l[2]
    if a == b == c:
        d = 1
    elif a == b or b == c or c == a:
        d = 3
    else:
        d = 6
    cnt += gcd(a,b,c)*d
print(cnt)

Submission status: Accepted

Solution code:
import math

def main():
    N = input()
    N = int(N)
    N = N+1
    
    sum = 0

    for a in range(1, N):
        for b in range(1, N):
            for c in range(1, N):

                if a == b or b == c or a == c:
                    tmp = math.gcd(a,b)
                    gcd = math.gcd(tmp,c)
                    sum = sum + gcd

                elif a < b and b < c:
                    tmp = math.gcd(a,b)
                    gcd = math.gcd(tmp,c)
                    sum = sum + 6*gcd

                else:
                    continue

    print(sum)




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
import math

k = int(input())

l = list(itertools.combinations_with_replacement(list(range(1, k+1)), 3))
ans = 0

for i in l:
    a, b, c = i
    if a == b == c:
        ans += math.gcd(math.gcd(a, b), c)
    elif (a == b) or (b == c):
        ans += 3 * (math.gcd(math.gcd(a, b), c))
    else:
        ans += 6 * (math.gcd(math.gcd(a, b), c))

print(ans)

Submission status: Accepted

Solution code:
K=int(input())

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

ans=0
re=[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]

if K < 102:
    for a in range(1,K+1):
        for b in range(1,K+1):
            hoge=gcd(a,b)
            for c in range(1,K+1):
                g=gcd(hoge,c)
                ans += g
else:
    ans=re[K-101]



print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())
ans = 0
m = 0
for a in range(1,k+1):
    for b in range(a+1,k+1):
        for c in range(b+1, k+1):
            m = math.gcd(a,b)
            ans += 6*math.gcd(m,c)
for a in range(1,k+1):
    for c in range(a+1,k+1):
        ans += 3*math.gcd(a,c)
for c in range(1,k+1):
    for a in range(c+1,k+1):
        ans += 3*math.gcd(a,c)
for a in range(1,k+1):
    ans += a
print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
m = 0

for i in range(1,n+1):
  for j in range(i,n+1):
    for k in range(j,n+1):
#      print("{}{}{}".format(i,j,k))
      if i != j and j != k and k != i:  
        m += 6*math.gcd(i,math.gcd(j,k))
      elif i == j and j == k:  
        m += math.gcd(i,math.gcd(j,k))
      else:  
        m += 3*math.gcd(i,math.gcd(j,k))
#
print(m)

Submission status: Accepted

Solution code:
from fractions import gcd
import math
K = (int)(input())
sum = 0        
for a in range(3,K+1):
    for b in range(2,a):
        for c in range(1,b):
            sum += 6*math.gcd(math.gcd(a,b),c)
for a in range(1,K+1):
    sum += a
for a in range(2,K+1):
    for b in range(1,a):
        sum += 6*math.gcd(a,b)
print(sum)

Submission status: Accepted

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

N = int(input())

sum1 = 0
sum2 = 0
sum3 = 0

for a in range(1,N+1):
  for b in range(a+1, N+1):
    for c in range(b+1, N+1):
      sum1 += gcd(a,b,c)
      
for a in range(1,N+1):
  for b in range(a+1, N+1):
    sum2 += gcd(a,b)
    
sum3 = N*(N+1)/2

print(int(sum1*6+sum2*6+sum3))

Submission status: Accepted

Solution code:
import itertools

k = int(input())

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

answer = 0
nums = list(range(1, k+1))
for balls in itertools.combinations_with_replacement(nums, 3):
    i, j, l = balls[0], balls[1], balls[2]
    ans = gcd(i, j)
    ans = gcd(ans, l)
    if len(set(balls)) == 1:
        answer += ans
    elif len(set(balls)) == 2:
        answer += ans*3
    elif len(set(balls)) == 3:
        answer += ans*6
            
print(answer)

Submission status: Accepted

Solution code:
import sys
import math as m
input = sys.stdin.buffer.readline

def main():
    n=int(input())
    d=[[[None]*n for i in range(n)]for j in range(n)]
    ans=0

    for i in range(n):
        for j in range(i,n):
            ij=m.gcd(i+1,j+1)
            for k in range(j,n):
                d[i][j][k]=m.gcd(ij,k+1)
    #print(d)

    for i in range(n):
        for j in range(n):
            for k in range(n):
                #print(ans,i,j,k)
                if i<=j and j<=k:
                    ans+=d[i][j][k]
                elif i<=k and k<=j:
                    ans+=d[i][k][j]
                elif j<=i and i<=k:
                    ans+=d[j][i][k]
                elif j<=k and k<=i:
                    ans+=d[j][k][i]
                elif k<=i and i<=j:
                    ans+=d[k][i][j]
                elif k<=j and j<=i:
                    ans+=d[k][j][i]
    print(ans)

main()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
K = int(input())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
    
ans = 0
for a in range(K):
    a += 1
    if a == 1:
        ans += K*K
        continue
    for b in range(K):
        b += 1
        if b == 1:
            ans += K
            continue
        tmp = gcd(a, b)
        if tmp == 1:
            ans += K
            continue
        for c in range(K):
            c += 1
            if c == 1:
                ans += 1
                continue
            dada = gcd(tmp, c)
            ans += dada
            # print(dada)

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

result = 0
for a in range(1, k + 1):
    if a == 1:
        result += k ** 2
        continue
    for b in range(1, k + 1):
        if b == 1:
            result += k
            continue
        for c in range(1, k + 1):
            result += gcd(gcd(a, b), c)

print(result)

Submission status: Accepted

Solution code:
import math

K = int(input())
sum = 0

for i in range(1, K+1):
    for j in range(1, K+1):
        for l in range(1, K+1):
            sum += math.gcd(i, math.gcd(j, l))

print(sum)

Submission status: Accepted

Solution code:
import math
from functools import reduce
answer=0
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
K=int(input())
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            #print(i,j,k)
            answer+=gcd(i,j,k)
                
                
print(answer)

Submission status: Accepted

Solution code:
import math
ans=0
k=int(input())
gcdlist=[[math.inf]*(k+1) for i in range(k+1)]
for i in range(1,k+1):
    for j in range(1,k+1):
        gcdlist[i][j]=math.gcd(i,j)
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans+=gcdlist[gcdlist[a][b]][c]
print(ans)

Submission status: Accepted

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

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

for a in range(K):
    a += 1
    for b in range(K):
        b += 1
        for c in range(K):
            c += 1

            S = S + gcd(a,b,c)

print(S)

Submission status: Accepted

Solution code:
from math import gcd
from itertools import permutations

k = int(input())
ans = 0

for i in range(1, k+1):
    for j in range(i, k+1):
        for l in range(j, k+1):
            dup = len(set([i, j, l]))
            if dup == 1:
                num = 1
            elif dup == 2:
                num = len(list(permutations([i, j, l]))) / 2
            else:
                num = len(list(permutations([i, j, l])))
            ans += gcd(i, gcd(j, l)) * num

print(int(ans))

Submission status: Accepted

Solution code:
K = int(input())

import itertools
import math
from functools import reduce

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

lis = list(range(1,K+1))
S = [gcd_list(team) for team in itertools.product(lis, repeat = 3)]

print(sum(S))

Submission status: Accepted

Solution code:
def dcg(a, b):
    if (b == 0):
        return a
    else:
        return dcg(b, a % b)


ans = 0
n = int(input())
for k in range(1, n+1):
    for b in range(k + 1, n + 1):
        for c in range(b + 1, n + 1):
            gcd1 = dcg(k, b)
            gcd2 = dcg(gcd1, c)
            ans += gcd2

ans *= 6

tmp = 0

for k in range(1, n+1):
    for b in range(k+1, n+1):
        gcd = dcg(k,b)
        tmp += gcd
        
tmp *= 6

tmp2 = sum(range(1,n+1))

print(ans+tmp+tmp2)

Submission status: Accepted

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

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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import bisect

d = dict()

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

def build_table():
    global d
    for i in range(1, 201):
        for j in range(1, 201):
            d[(i, j)] = gcd(i, j)

def query_gcd_3(a, b, c):
    g = d[(a, b)]
    return d[(g, c)]

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
from functools import lru_cache

gcd = [[0] * 201 for _ in range(201)]

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

K = int(input())

total = 0

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

print(total)

Submission status: Accepted

Solution code:
import sys

K=int(sys.stdin.readline().rstrip())
 
def gcd(a,b):

    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b

cnt1 = 0
cnt2 = 0
cnt3 = 0
for n1 in range(0,K):
    cnt1 += n1+1
    for n2 in range(n1+1, K):
        temp = gcd(n1+1, n2+1)
        cnt2 += temp*6
        for n3 in range(n2+1, K):
            cnt3 += gcd(temp, n3+1)*6
        

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())
S=0
for i in range(1,K+1):
	for h in range(i,K+1):
		for g in range(h,K+1):

			if i==h and h==g:
				S+=gcd(g,h,i)
			elif i==h or h==g or g==i:
				S+=3*gcd(g,h,i)
			else:
				S+=6*gcd(g,h,i)
			
print(S)

Submission status: Accepted

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

for i in range(1, K + 1):
  for j in range(1, K + 1):
    gcd1 = math.gcd(i, j)
    for k in range(1, K + 1):
      gcd2 = math.gcd(k, gcd1)
      sum += gcd2

print(sum)

Submission status: Accepted

Solution code:
import math
k = int(input())
ans = []
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)
            ans.append(math.gcd(c, d))

print(sum(ans))

Submission status: Accepted

Solution code:
import math
from functools import reduce
import sys
input = sys.stdin.readline  # 改行コード「\n」を呼んでしまうため、文字数など、注意！！


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

# print(gcd(100,20,10))

k = int(input().replace("\n",""))
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:
import itertools
import math
from functools import reduce

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

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

def main():
    k = int(input())

    ans = 0
    c = list(itertools.combinations_with_replacement(range(1,k+1), 3))
    for elem in c:
        n_gcd = gcd_list(list(elem))
        length = len(set(elem))
        if length == 1:
            k = 1
        elif length == 2:
            k = 3
        elif length == 3:
            k = 6
        ans += k * n_gcd 
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
from fractions import gcd
import math
import functools
t = []
ans = 0
for i in range(1,n+1):
    for j in range(i,n+1):
        for k in range(j,n+1):
            #t.append([i,j,k])
            t = [i,j,k]
            if i==j==k:
                gcd = functools.reduce(math.gcd, t)
                ans += gcd
            elif i!=k and j!=k and i!=j:
                gcd = functools.reduce(math.gcd, t)
                ans += gcd*6
            else:

                gcd = functools.reduce(math.gcd, t)
             
                ans += gcd*3
            #print(t,ans)

print(ans)

Submission status: Accepted

Solution code:
K = int(input())

g2 = [[0]*(K+1) for _ in range(K+1)]
def gcd(a, b):
    if g2[a][b] > 0:
        return g2[a][b]
    tmpa = a
    tmpb = b
    while b > 0:
        a,b = b, a%b
    g2[tmpa][tmpb] = a
    g2[tmpb][tmpa] = a
    return a

def gcd_3(a,b,c):
    g = gcd(a,b)
    g = gcd(g,c)
    return g

sm = 0
for a in range(1, K+1):
    sm += a
    for b in range(a+1, K+1):
        for c in range(b+1, K+1):
            sm += gcd_3(a,b,c) * 6
    for b in range(1, K+1):
        if a != b:
            sm += gcd(a,b) * 3

print(sm)

Submission status: Accepted

Solution code:
N=int(input())

import math
ans=0

all=0
for i in range(1,N+1):
    all+=i

for i in range(1,N+1):
    for j in range(1,N+1):
        ans+=3*math.gcd(i,j)
        
ans=ans-2*all

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

Submission status: Accepted

Solution code:
n = int(input())
import math
l = []
for i in range(1,n +1):
    for j in range(1,n+1):
        l.append(math.gcd(i,j))
ans = 0
for i in range(1, n+1):
    for j in l:
        ans += math.gcd(j,i)
print(ans)

Submission status: Accepted

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

li = [[0]*(k+1) for i in range(k+1)]
for i in range(k+1):
    for j in range(k+1):
        li[i][j] = fractions.gcd(i,j)

for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            x = li[i][j]
            y = li[x][l]
            ans += y
print(ans)