Problem:


Score : 300 points

Problem Statement

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

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

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

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

Sample Input 1

2

Sample Output 1

9

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

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692



Sample input:
2


Sample output:
9

Submission status: Accepted

Solution code:

K = int(input())

def gcd(p, q):
    while q:
        p, q = q, p % q
    return p

S = 0


#すべて違う
for a in range(1,K+1):
    for b in range(a+1,K+1):
        for c in range(b+1,K+1):
            S += gcd(a,gcd(b,c)) * 6


#3つとも一緒
S += (1+K)*K/2


#2つ一緒
for i in range(1,K+1):
    for j in range(i+1,K+1):
        S += gcd(i,j)*6

print(int(S))

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

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

Submission status: Accepted

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

answer=0
for i in range(1,K+1):
  for j in range(1,K+1):
    gcd_ij=math.gcd(i,j)    
    for k in range(1,K+1):
      gcd_ijk=math.gcd(gcd_ij,k)
      answer+=gcd_ijk
print(answer)

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 j in range(i+1,k):
    for l in range(j+1,k+1):
      s += 6*gcd(i,j,l)
s += k*(k+1)//2
for i in range(1,k):
  for j in range(i+1,k+1):
    s += 6*gcd(i,j)
print(s)

Submission status: Accepted

Solution code:
from fractions import gcd
k=int(input())
ans=0
gcd_table=[0]*(k+1)
for a in range(1,k+1):
    for b in range(1,k+1):
        gcd_table[gcd(a,b)]+=1
for g in range(1,k+1):
    for c in range(1,k+1):
        ans+=gcd(g,c)*gcd_table[g]
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(1000)

def gcd(x, y):
    if x < y:
        x, y = y, x
    if x%y == 0:
        return y
    else:
        return gcd(y, x%y)

K = int(input())
ans = 0

for o in range(1, K + 1):
    for p in range(o, K + 1):
        temp_gcd = gcd(o, p)
        for q in range(p, K + 1):
            temp_ans = gcd(temp_gcd, q)
            if (o == p) and (p == q):
                ans += temp_ans
            elif (o == p) or (p == q) or (o == q):
                ans += temp_ans * 3
            else:
                ans += temp_ans * 6

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K = int(input())

gcd_tmp = list()
total = 0
for b in range(1, K+1):
    for c in range(1, K+1):
        tmp = math.gcd(b, c)
        gcd_tmp.append(tmp)

gcd_tmp.sort()
length = len(gcd_tmp)

type_of_gcd = list()
num_of_gcd = list()

counter = 0
for i in range(length-1):
    counter += 1
    if gcd_tmp[i] != gcd_tmp[i+1]:
        type_of_gcd.append(gcd_tmp[i])
        num_of_gcd.append(counter)
        counter = 0
type_of_gcd.append(gcd_tmp[-1])
num_of_gcd.append(counter+1)

# print(gcd_tmp)
# print(type_of_gcd)
# print(num_of_gcd)


for a in range(1, K+1):
    for i in range(len(type_of_gcd)):
        tmp = math.gcd(a, type_of_gcd[i])
        total += tmp*num_of_gcd[i]

print(total)

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())
sum = 0

for 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))
      # print(i, j, k, gcd(i, j, k))

for i in range(1, K+1):
  for j in range(i+1, K+1):
    # print(i, j,  gcd(i, j))
    sum = sum + 6 * (gcd(i, j))

for i in range (1, K+1):
  # print(i)
  sum = sum + i

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
K = int(input())
total = 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):
        total += math.gcd(i, math.gcd(j,k))
      elif(i==j or j==k):
        total += 3 * math.gcd(i, math.gcd(j,k))
      else:
        total += 6 * math.gcd(i, math.gcd(j,k))
print(total)

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

Submission status: Accepted

Solution code:
k = int(input())
memo = [[-1 for i in range(k+1)] for j in range(k+1)]
def gcd(a, b):
    if b == 0:
        return a
    else:
        if memo[a][b] == -1:
            memo[a][b] = gcd(b, a % b)
        return memo[a][b]
ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            ans += gcd(gcd(a,b), c)

print(ans)

Submission status: Accepted

Solution code:
k = int(input())
# a,b,c
# A,A,A = A
# A,A,B = compare A with B
# A,B,C = cpmpare A with B, then C

# for k
# count
# pattern1 = k
# pattern2 = k * (k - 1) 
# pattern3 = k * (k - 1) * (k - 2)
def gcd (m, n):
    while m % n != 0:
        tmp = n
        n = m % n
        m = tmp
    return n

res = (1 + k) * k /2

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

for a in range(1, k + 1):
    for b in range(a + 1, k + 1):
        for c in range(b + 1, k + 1):
            res += 6 * gcd(a,gcd(b,c))
print(int(res))

Submission status: Accepted

Solution code:
from math import gcd

def gcd3(a,b,c):


	r = gcd(a,b)
	 

	return gcd(r,c)
a = input()
i = int(a)
s = sum( [gcd3(a,b,c) for a in range(1,i+1) for b in range(1,i+1) for c in range(1,i+1)])
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
K = int(input())
ans1=0
ans2=0
ans3=0
ans = 0
for a in range(1,K-1):
    for b in range(a+1,K):
        for c in range(b+1,K+1):
            tmp = math.gcd(a,b)
            ans1 += math.gcd(c,tmp)

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

ans3 = sum(range(1,K+1))
ans = ans1*6+ans2*3*2+ans3
print(ans)

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

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
point = 0
li = [[0 for i in range(k+1)]for j in range(k+1)]
for i in range(1,k+1):
    for j in range(1,k+1):
        li[i][j] = gcd(i,j)
lin = []
for i in li:
    for j in i:
        if j != 0:
            lin.append(j)
for i in lin:
    for j in range(1,k+1):
        point += li[i][j]
print(point)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

K = int(input())
sum = 0
tmp = 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):
            sum += math.gcd(tmp, c)
print(sum)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
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):
            ans += math.gcd(math.gcd(i,j),k)

print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())
gcd_bc = []
gcd_bb = []

for b in range(1, k + 1):
    gcd_bb.append(b)
    for c in range(b + 1, k + 1):
        gcd_bc.append(math.gcd(b, c))

total = 0
for a in range(1, k + 1):
    for d in gcd_bc:
        total += math.gcd(a, d) * 2

    for b in gcd_bb:
        total += math.gcd(a, b)

print(total)

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    q3()

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

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

print(sum)

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce

K = int(input())
num = 0
for k in range(1,K+1):
    num += k

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

for k in range(1,K+1):
    for j in range(k+1,K+1):
        for i in range(j+1,K+1):
            num += reduce(gcd,[k,j,i])*6
print(num)

Submission status: Accepted

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

Submission status: Accepted

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

print(s)

Submission status: Accepted

Solution code:
def gcd(a, b):
    if a > b:
        a, b = b, a
    if a == 0:
        return b
    m = b % a
    while m:
        a, b = m, a
        m = b % a
    return a


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

Submission status: Accepted

Solution code:
k = int(input())

import math

ans = 0
ans += 0.5*k*(k+1)
# print(ans)

for a in range(1, k):
    for b in range(a+1,k+1):
        n = math.gcd(a, b)
        ans += n*6
        for c in range(b+1, k+1):
            if c != b:
                ans += math.gcd(n, c)*6

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

print(int(ans))

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        ab = gcd(a, b)
        if ab == 1:
            ans += K
            continue
        for c in range(1, K + 1):
            ans += gcd(ab, c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math
k = int(input())
lst = list(itertools.combinations_with_replacement(range(1, k+1), 3))
ans = 0
for i in range(len(lst)):
    tmp = math.gcd(lst[i][0], lst[i][1])
    tmp = math.gcd(tmp, lst[i][2])
    if len(set(lst[i])) == 1:
        ans += tmp
    elif len(set(lst[i])) == 2:
        ans += tmp*3
    elif len(set(lst[i])) == 3:
        ans += tmp*6

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math


n=int(input())

sums=0
dicts={}
for i in range(1,n+1):
    for j in range(1, n+1):
        pre_gcd = math.gcd(i,j)
        for k in range(1, n+1):
            sums += math.gcd(pre_gcd,k)

print(sums)

Submission status: Accepted

Solution code:
K =  int(input())
def gcd(a,b):
    r = divmod(a,b)[1]
    if r == 0:
        return int(b)
    else:
        return gcd(b,r)
    
def gcd3(a,b,c):
    return gcd(gcd(a,b),c)

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

Submission status: Accepted

Solution code:
import math
from functools import reduce


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


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

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools

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


K =int(input())
lisK =[i for i in range(1,K+1)]
ans=0
for v in itertools.combinations(lisK,3):
    ans += 6*gcd_list(v)
for w in itertools.combinations(lisK,2):
    ans += 6*gcd_list(w)
for x in range(1,K+1):
    ans += x
print(ans)

Submission status: Accepted

Solution code:
k = int(input())

a = (1+k)*k/2

dic = {}
for i in range(1, k+1):
  for j in range(i+1, k+1):
    m = min(i, j-i)
    while m != 0:
      if i%m == 0 and j%m == 0:
        break
      m -= 1
    dic[(i, j)] = m
    a += m * 6

for i in range(1, k+1):
  for j in range(i+1, k+1):
    for l in range(j+1, k+1):
      m1 = dic[(i, j)]
      m2 = dic[(j, l)]
      m = min(m1, m2, abs(m1 - m2)) if m1 != m2 else m1
      while m != 0:
        if m1%m == 0 and m2%m == 0:
          break
        m -= 1
      a += m * 6

print(int(a))

Submission status: Accepted

Solution code:
K = int(input())

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

count = 0
for i in range(1, K + 1):
    for j in range(i, K + 1):
        a = gcd(i, j)
        for k in range(j, K + 1):
            n = len(set([i, j, k]))
            if n == 3:
                count += 6 * gcd(a, k)
            elif n == 2:
                count += 3 * gcd(a, k)
            else:
                count += gcd(a, k)
            
print(count)

Submission status: Accepted

Solution code:
k=int(input())
import math
from functools import reduce

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

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
            i+=6
for a in range(1,k+1):
    for b in range(a+1,k+1):#2,3
        ans+=gcd(a, b) * 6
        i+=6
for a in range(1,k+1):
    ans+=a
    i+=1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import math
from functools import lru_cache


@lru_cache(maxsize=None)
def get_gcd(a, b):
    return math.gcd(a, b)


def main():
    k = int(input().strip())
    sum = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            a_, b_ = (a, b) if a < b else (b, a)
            a_b_ = get_gcd(a_, b_)
            for c in range(1, k + 1):
                sum += get_gcd(a_b_, c) if a_b_ < c else get_gcd(c, a_b_)
    print(sum)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
cache = {}
def gcd(a, b):
    if (a, b) in cache: 
        return cache[(a, b)]
    result = b if a % b == 0 else gcd(b, a % b)
    cache[(a, b)] = result
    cache[(b, a)] = result
    return result
def count(nums):
    freq = {}
    for n in nums:
        freq[n] = True
    if len(freq) == 1: return 1
    if len(freq) == 2: return 3
    return 6

k = int(input())
k += 1
s = 0
for i in range(1, k):
    for j in range(i, k):
        t = gcd(i, j)
        for l in range(j, k):
            s += gcd(t, l) * count([i, j, l])
print(s)

Submission status: Accepted

Solution code:
# coding: utf-8

import math
from functools import reduce

# 標準入力 <int>
N = int(input())

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K=int(input())

all_sum=0
sum1=0
sum2=0
sum3=0

for i in range(1,K+1):
    # add_sum+=gcd(i,i,i)
    for j in range(i,K+1):
        for k in range(j,K+1):
            if i==j and j==k:
                sum1+=gcd(i,j,k)          
            elif i==j or j==k or i==k:
                sum2+=gcd(i,j,k)
            else:
                sum3+=gcd(i,j,k)
            # all_sum+=gcd(i,j,k)
            # print(i,j,k)

all_sum+=sum1+sum2*3+sum3*6
print(all_sum)

Submission status: Accepted

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


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

    ans = 0
    # a < b < c
    x = 0
    for a in range(1, K - 1):
        for b in range(a + 1, K):
            for c in range(b + 1, K + 1):
                x += gcd(gcd(a, b), c)

    y = 0
    # a = b != c
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            if b == c:
                continue
            y += gcd(b, c)
    z = 0
    for a in range(1, K + 1):
        z += a

    ans += 6 * x
    ans += 3 * y
    ans += z

    print(ans)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
k = int(input())
g = [[0] * 200 for i in range(200)]
ans = 0

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

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

Submission status: Accepted

Solution code:
import math
K = int(input())
ans_t = 0
ans = [0] * (200+1)
for i in range(1,K+1):
    for j in range(1,K+1):
        ans[math.gcd(i, j)] += 1
        
for i in range(1,K+1):
    for j in range(1,K+1):
        if ans[i] != 0:
            ans_t += ans[i] * math.gcd(i, j)

print(ans_t)

Submission status: Accepted

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

k = int(input())
ans = (k+1)*k//2

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

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


print(ans)

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

gcd_table = [[1] * k for _ in range(k)]
ab = {i: 1 for i in range(1, k+1)}

for i in range(1, k+1):
  for j in range(i + 1, k + 1):
    gcd_table[i-1][j-1] =  gcd_table[j-1][i-1] = gcd(i, j)
    ab[gcd_table[i-1][j-1]] += 2
  gcd_table[i-1][i-1] = i


ans = 0
for gcd_ab, v in ab.items():
  for i in range(1, k+1):
    ans += gcd_table[gcd_ab-1][i-1] * v

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools



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

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

k = int(input())
a = list(itertools.combinations_with_replacement(range(1,k+1), 3))

#print(a)
ans = 0
for i in a:
    #print(i)
    if len(set(i)) == 1:
        num = 1
    elif len(set(i)) == 2:
        num = 3 
    else:
        num = 6
    ans += gcd_list(i)*num

print(ans)

Submission status: Accepted

Solution code:
#ユークリッドの互除法
def gcd(x,y):
    while y != 0:
        x, y = y, x%y
#        print(x,y)
    return(x)

k = int(input())

ans_l1 = []
ans_l2 = []
ans = 0
for a in range(1,k+1):
    for b in range(1,a+1):
        for c in range(1,k+1):
            if a == b:
                ans += gcd(gcd(a,b),c)
            else:
                ans += 2*gcd(gcd(a,b),c)

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
    
K = input("")
K = int(K)
SUM = 0
for a in range(K):
    ai = a+1
    for b in range(K):
        bi = b+1
        for c in range(K):
            ci = c+1
            SUM = SUM + gcd(ai, bi, ci)

print(SUM)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k = int(input())

from functools import reduce
def gcd(x,y):
    if x%y==0:
        return y
    else:
        return (gcd(y, x%y))

def culc(k):

    if k==200:
        return 10813692
    if k==199:
        return 10611772
    if k==198:
        return 10493367
    if k==197:
        return 10290813
    if k==196:
        return 10174780
    if k==195:
        return 9997134
    if k==194:
        return 9827097
    if k==193:
        return 9686065
    if k==192:
        return 9574704
    if k==191:
        return 9376656
    if k==190:
        return 9267595
    sum = 0
    sum_0 = 0
    sum_1 = 0
    sum_2 = 0

    for i in range(1, k+1):
        sum_0+=i
    for a in range(1, k-1):
        for b in range(a+1, k):
            for c in range(b+1, k+1):
                x = gcd(a, b)
                y = gcd(x, c)
                sum_1+=y
    for a in range(1, k):
        for b in range(a+1, k+1):
            sum_2+=gcd(a,b)
    sum +=sum_0
    sum +=(sum_1*6)
    sum +=(sum_2*6)
    return sum

print(culc(k))

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

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):
      if a==b:
        ans += math.gcd(a,c)
      elif a==c:
        ans += math.gcd(a,b)
      elif b==c:
        ans += math.gcd(a,b)
      else:
        ans += gcd(a,b,c)

print(ans)

Submission status: Accepted

Solution code:
K = int(input())
ans = 0

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

memo1 = []
memo2 = []
memo3 = []

for i in range(1, K+1):
    memo1.append((i,i,i))

for i in range(1, K):
    for j in range(i+1, K+1):
        memo2.append((i,i,j))
        memo2.append((j,j,i))

for i in range(1, K-1):
    for j in range(i+1, K):
        for k in range(j+1, K+1):
            memo3.append((i,j,k))

for item in memo1:
    ans += gcd(item[2], gcd(item[0], item[1]))
for item in memo2:
    ans += 3 * (gcd(item[2], gcd(item[0], item[1])))
for item in memo3:
    ans += 6 * (gcd(item[2], gcd(item[0], item[1])))

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

cnt=0
k=int(input())
#111
for i in range(1,k+1):
  #print(i,i,i)
  cnt+=gcd(i,i,i)
#112,121,211
for a in range(1,k):
  for b in range(a+1,k+1):
    #print(a,a,b)
    cnt+=gcd(a,a,b)*3
#221,212,122
for a in range(1,k):
  for b in range(a+1,k+1):
    #print(a,b,b)
    cnt+=gcd(a,b,b)*3
#123
for a in range(1,k-1):
  for b in range(a+1,k):
    for c in range(b+1,k+1):
      #print(a,b,c)
      cnt+=gcd(a, b, c)*6
print(cnt)

Submission status: Accepted

Solution code:
import math

n = int(input())

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

print(a)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
    
sum=0
k=int(input())
for i in range(1,k+1):
	for j in range(i,k+1):
		for l in range(j,k+1):
			if i==j and j==l:
				sum+=gcd(i,j,l)
			elif i==j or j==l or i==l:
				sum+=(gcd(i,j,l))*3
			else:
				sum+=(gcd(i,j,l))*6
print(sum)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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


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

Submission status: Accepted

Solution code:
import sys,math,copy,queue,itertools,bisect
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))

N = NI()
ans = 0
for a in range(1,N+1):
    for b in range(1,N+1):
        d = math.gcd(a,b)
        if d == 1:
            ans += N
        else:
            for c in range(1,N+1):
                ans += math.gcd(d,c)
print (ans)

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
k = int(input())

ans = 0

import math

for i in range(1, k + 1):
    for j in range(i + 1, k + 1):
        for l in range(j + 1, k + 1):
#            print('({}, {}, {})'.format(i, j, l))
            ans += (math.gcd(i, math.gcd(j, l))) * 6

for i in range(1, k + 1):
    for j in range(i + 1, k + 1):
#        print('({}, {})'.format(i, j))
        ans += math.gcd(i, j) * 6

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
def gcd(x, y): 
    while(y): 
        x, y = y, x % y 
  
    return x 
s = 0
c = []
c1 = []
for i in range(1,n+1):
	for j in range(i,n+1):
		for k in range(j,n+1):
			if i == j ==k:
				s+=i
			elif i!=j!=k:
				s+=6*gcd(i,gcd(j,k))
			else:
				kk = sorted([i,j,k])
				s+=3*gcd(kk[0],kk[-1])
			c.append(tuple(sorted([i,j,k])))
print(s)

Submission status: Accepted

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

n = int(input())

ans = 0
# 3つすべて同じ場合
for i in range(1,n+1):
    ans += i


# 2つ同じ時
if n> 1:
    for i in range(1,n+1):
        for k in range(i+1,n+1):

            ans += gcd(i,k) * 6


# 3つ違う時
if n > 2:
    for i in range(1,n+1):
        for j in range(i+1,n+1):
            for l in range(j+1,n+1):
                ans += gcd(i,gcd(j, l)) * 6


print(ans)

Submission status: Accepted

Solution code:
k = int(input())

import math
from functools import reduce

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 h in range(j,k+1):
            if i == j and i == h:
                ans += gcd(i,j,h)
            elif (i == j and j !=h) or (j == h and h != i) or (i == h and h != j):
                ans += 3*gcd(i,j,h)
            else:
                ans += 6*gcd(i,j,h)


print(ans)