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:
def gcd(x,y):
    if x<y:
        x,y=y,x
    if y==0:
        return x
    return gcd(y,x%y)
    
def main():
    k=int(input())
    ans=0
    for a in range(1,k+1):
        for b in range(1,k+1):
            for c in range(1,k+1):
                ans+=gcd(gcd(a,b),c)
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(300000)
from math import gcd
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


K = I()
memo = dict()

def g(a, b):
    if a > b:
        a, b = b, a
    if memo.get((a, b)) is not None:
        return memo[(a, b)]
    else:
        val = gcd(a, b)
        memo[(a, b)] = val
        return val

d = defaultdict(int)
for b in range(1, K + 1):
    for c in range(1, K + 1):
        d[g(b, c)] += 1

ans = 0
for a in range(1, K + 1):
    for val, cnt in d.items():
        ans += g(a, val) * cnt
print(ans)

Submission status: Accepted

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

import itertools
import math

n = int(input())

ans = 0
ans1 = 0
ans2 = 0
ans3 = 0
for i, j, k in itertools.combinations(range(1, n+1), 3):
    ans1 += math.gcd(math.gcd(i, j), k)*6

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

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

# print(ans1, ans2, ans3)
print(ans1+ans2+ans3)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())

sum1 = 0
sum2 = 0
sum3 = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            if i <= j <= k:
                sum1 += gcd(i, j, k)
                if i == j == k:
                    sum2 += gcd(i, j, k)
                if (i == j != k) or (j == k != i) or (k == i != j):
                    sum3 += gcd(i, j, k)

print(sum1 * 6 - sum2 * 5 - sum3 * 3)

Submission status: Accepted

Solution code:
import math
def I(): return int(input())
##################################################
N = I()
ans = 0
for i in range(1,N+1):
    for j in range(1,N+1):
        temp = math.gcd(i,j)
        for k in range(1,N+1):
            ans += math.gcd(temp,k)
print(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):
        temp=gcd(a,b)
        for c in range(1,K+1):
            
            ans+=gcd(temp,c)

print(ans)

Submission status: Accepted

Solution code:
import math
K = int(input())
sumval = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        f = math.gcd(a,b)
        for c in range(1, K+1):
            g = math.gcd(f,c)
            sumval += g
print(sumval)

Submission status: Accepted

Solution code:
from math import gcd

k = int(input()) + 1
ans = 0

for s in range(1, k):
    for t in range(s, k):
        for u in range(t, k):
            if (v := len(set([s, t, u]))) == 1:
                ans += gcd(gcd(s, t), u)
            elif v == 2:
                ans += gcd(gcd(s, t), u) * 3
            else:
                ans += gcd(gcd(s, t), u) * 6
print(ans)

Submission status: Accepted

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

def gdc(x,y):
  if x%y==0:
    return y
  return gdc(y, x%y)

for a in range(K):
  for b in range(a,K):
    for c in range(b,K):
      if a==b and b==c:
        total += gdc(c+1, gdc(b+1, a+1))
      elif a==b or b==c or a==c:
        total += 3*gdc(c+1, gdc(b+1, a+1))
      else:
        total += 6*gdc(c+1, gdc(b+1, a+1))
print(total)

Submission status: Accepted

Solution code:

import math
n=int(input())
g=0

for i in range(1,n+1):
    for j in range(1,n+1):
        d=math.gcd(i,j)
        for h in range(1,n+1):
            f=math.gcd(h,d)
            g=g+f
print(g)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
K = int(input())
ans = 0
for a in range(1,K+1):
  for b in range(a,K+1):
    t = math.gcd(a, b)
    for c in range(b,K+1):
      if a==b and a==c:
        x=1
      elif a!=b and b!=c and c!=a:
        x=6
      else:
        x=3
      ans += x*math.gcd(t,c)
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
from functools import lru_cache
from itertools import combinations

a = int(input())

def main():
    for i in range(1, a+1):
        for j in range(i, a+1):
            for k in range(j, a+1):
                x = len({i, j, k})
                if x == 1:
                    x = 1
                elif x == 2:
                    x = 3
                else:
                    x = 6
                yield gcd(gcd(i, j), k) * x

print(sum(main()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import math
from functools import reduce
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
def main():
    N = list(map(int, open(0).read().split()))
    N = N[0]
    L = 0
 
    for a in range(1, N+1):
        for b in range(1, N+1):
            for c in range(1, N+1):
                L += gcd(a,b,c)
    print(L)
main()

Submission status: Accepted

Solution code:
# AtCoder
import math

K = int(input())
rng = range(1, K+1)
ls = [math.gcd(i, j) for i in rng for j in rng]
ans = [math.gcd(i, l) for i in rng for l in ls]

print(sum(ans))

Submission status: Accepted

Solution code:
#C
import math

def gcd_great(l,m,n):
    p=math.gcd(l,m)
    q=math.gcd(p,n)
    return q

k=int(input())

#(n,n,m)のパターン
gcd_sum=0
for i in range(1,k+1):
    for j in range(1,k+1):
        if i!=j:
            g=math.gcd(i,j)
            gcd_sum=gcd_sum+g*3

#(l,m,n)のパターン
for a in range(1,k-1):
    for b in range(a+1,k):
            for c in range(b+1,k+1):
                gcd_sum+=gcd_great(a,b,c)*6
gcd_sum+=int((k+1)*k*(1/2))
#(n,n,n)のパターン
print(gcd_sum)

Submission status: Accepted

Solution code:
import math
import numpy as np

K = int(input())

ind = np.ones(K)

for i in range(K):
    if i ==0:
        pass
    else:
        num = i+1
        # num num other
        cout =0
        for j in range(1,num):
            cout += math.gcd(num,j)
        ind[i] = ind[i-1] + cout*3
        
        # num other other
        
        cout=0
        for k in range(1,num):
            ene = math.gcd(num,k)
            for l in range(1,num):
                cout+= math.gcd(ene,l)
            
        ind[i]+= cout*3 +num
        
           
print(int(ind[-1]))

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce

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

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 += gcd3(a, b, c)

print(ans)

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

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

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter

target_num = int(input())

test_ = []
for i in range(1, target_num+1):
    for v in range(1, target_num+1):
        test_.append(np.gcd(i, v))
count_dict = dict(Counter(test_))

sum_list = 0
for num in range(1, target_num+1):
    for i, v in count_dict.items():
        sum_list += np.gcd(num, i)*v
print(sum_list)

Submission status: Accepted

Solution code:
K = int(input())
A = [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(A[K-1])

Submission status: Accepted

Solution code:
K = int(input())

mem = {}
def memory(k, ans):
    if k not in mem:
        mem[k] = ans
    return ans

mem2 = {}

def memory2(k, ans):
    if k not in mem2:
        mem2[k] = ans
    return ans

def gcd2(a,b):
    if a > b:
        a, b = b, a
    k = a * 1000 + b
    if k in mem2:
        return mem2[k]
    if a < b:
        if a == 0:
            return memory2(k, b)
        if a == 1:
            return memory2(k, 1)
        else:
            return memory2(k, gcd2(a, b%a))
    else:
        return memory2(k, a)

def gcd(a, b, c):
    a, b, c = sorted([a, b, c])
    k = a * 1000000 + b * 1000 + c
    if k in mem:
        return mem[k]
    else:
        return memory(k, gcd2(a, gcd2(b, c)))

s = 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:
                if b == c:
                    m = 1
                else:
                    m = 3
            else:
                if b == c:
                    m = 3
                else:
                    m = 6
            s += gcd(a,b,c) * m


print(s)

Submission status: Accepted

Solution code:
import math

k = int(input())
ans = 0
ans += sum(range(1, k + 1))
for i in range(1, k + 1):
    for j in range(i + 1, k + 1):
        ans += math.gcd(i, j) * 6
if k > 2:
    for i in range(1, k + 1):
        for j in range(i + 1, k + 1):
            for l in range(j + 1, k + 1):
                ans += math.gcd(i, math.gcd(j, l)) * 6
print(ans)

Submission status: Accepted

Solution code:
import math
K=int(input())
S=0
T=0
for a in range(1,K-1):
    for b in range(a+1, K):
        for c in range(b+1, K + 1):
            d=math.gcd(a,b)
            e=math.gcd(d,c)
            S+=e
            if c==K:
                T+=d
for a in range(1,K):
    T+=math.gcd(a,K)
U=int(K*(K+1)/2)
print(6*S+6*T+U)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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


N=int(input())
S=0
T=0
for i in range(1,N+1):
    for j in range(i+1,N+1):
        for k in range(j+1,N+1):
            S+=gcd(i,j,k)
for i in range(1,N+1):
    for j in range(i+1,N+1):
            T+=gcd(i,j,j)
            T+=gcd(i,j,j)
print(6*S+3*T+N*(N+1)//2)

Submission status: Accepted

Solution code:
import math
import fractions

K = int(input())
sum = 0

def gcd2(x,y,z):
    return math.gcd(math.gcd(x,y),z)

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):
                sum += gcd2(i,j,k)
            elif i != j and j != k:
                sum += gcd2(i,j,k)*6
            else:
                sum += gcd2(i,j,k)*3



print(sum)

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, gcd
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# ここから書き始める

# c問題
# k = int(input())
# ans2 =[
#     1, \
#     10, \
#     33, \
#     88, \
#     145, \
#     330, \
#     385, \
#     736, \
#     945, \
#     1450, \
#     1441, \
#     2904, \
#     2353, \
#     3850, \
#     4785, \
#     6016, \
#     5185, \
#     9450, \
#     7201, \
#     12760, \
#     12705, \
#     14410, \
#     12673, \
#     24288, \
#     18625, \
#     23530, \
#     26001, \
#     33880, \
#     25201, \
#     47850, \
#     30721, \
#     48640, \
#     47553, \
#     51850, \
#     55825, \
#     83160, \
#     51985, \
#     72010, \
#     77649, \
#     106720, \
#     70561, \
#     127050, \
#     81313, \
#     126808, \
#     137025, \
#     126730, \
#     105985, \
#     198528, \
#     134113, \
#     186250, \
#     171105, \
#     207064, \
#     151633, \
#     260010, \
#     208945, \
#     283360, \
#     237633, \
#     252010, \
#     208801, \
#     421080, \
#     230641, \
#     307210, \
#     363825, \
#     391168, \
#     341185, \
#     475530, \
#     305185, \
#     456280, \
#     418209, \
#     558250, \
#     362881
#     695520
#     394273
#     519850
#     614625
#     633688
#     554785
#     776490
#     499201
#     872320
#     706401
#     705610
#     578593
#     1118040
#     751825
#     813130
#     831633
#     1060576
#     712801
#     1370250
#     905905
#     1115224
#     1013793
#     1059850
#     1044145
#     1605120
#     921985
#     1341130
#     1361745
#     1639000
#     1040401
#     1711050
#     1103233
#     1731808
#     1842225
#     1516330
#     1236385
#     2288088
#     1306801
#     2089450
#     1715505
#     2316160
#     1455553
#     2376330
#     1837585
#     2217688
#     2223585
#     2088010
#     1996225
#     3521760
#     1931281
#     2306410
#     2328513
#     2703448
#     2340625
#     3638250
#     2064385
#     3137536
#     2683329
#     3411850
#     2265121
#     4184664
#     2772385
#     3051850
#     3770145
#     3816160
#     2589985
#     4182090
#     2704801
#     4912600
#     3497505
#     3628810
#     3390673
#     5685120
#     3654145
#     3942730
#     4425729
#     4574680
#     3330001
#     6146250
#     3465601
#     5299936
#     4899825
#     5547850
#     4454545
#     6833112
#     3894385
#     4992010
#     5003889
#     7052800
#     4879105
#     7064010
#     4357153
#     6209368
#     6895185
#     5785930
#     4685185
#     9350880
#     5195905
#     7518250
#     6804945
#     7155544
#     5207473
#     8316330
#     7170625
#     8669056
#     6890433
#     7128010
# ]
# 1 1 1 2
# 1 1 2 2
# 1 2 1 2
# 1 2 2 2
# 2 1 1 2
# 2 1 2 2
# 2 2 1 2
# 2 2 2 2
# ans = [0] * 200
# total = 0
# for k in range(1, 201):
#     total = 0
#     for i in range(1, k + 1):
#         for j in range(1, k + 1):
#             for l in range(1, k + 1):
#                 total += gcd_list([i, j, l])
#     ans[k - 1] = total
# print(ans)
ans3 = [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(ans3[k - 1])

# d問題


# #　E問題

Submission status: Accepted

Solution code:
from math import gcd

gcd_results = {}

def three_gcd(a, b, c):
    if a == 1 or b == 1 or c == 1:
        return 1

    if a == b == c:
        return a
    k = sorted([a, b, c])
    key = k[0] * 1000000 + k[1] * 1000 + k[2]
    if key not in gcd_results:
        res = gcd(gcd(a, b), c)
        gcd_results[key] = res
        return res
    else:
        return gcd_results[key]

K = int(input())

a = range(1, K + 1)
b = range(1, K + 1)
c = range(1, K + 1)

result = 0
for ia in a:
    for ib in b:
        for ic in c:
            result += three_gcd(ia, ib, ic)

print(result)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

K = int(input())
sum = K ** 2
for a in range(2,K+1) :
    for b in range(1,K+1) :
        if gcd(a,b) == 1 :
            sum += K
        else :
            for c in range(1,K+1) :            
                sum += gcd(a, b, c)
            
print(sum)

Submission status: Accepted

Solution code:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
  
K = int(input())
A = B = C = [i for i in range(1, K+1)]

L = [gcd(a,b) for a in A for b in B]

cnt = sum([gcd(l,c) for l in L for c in C])   
print(cnt)

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools
import numpy as np

K = int(input())

l = list(range(1,K+1))

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

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

c = list(itertools.combinations_with_replacement(l,3))
sm = 0
for a in c:
    if a[0] == a[1] == a[2]:
        sm += gcd_list(list(a))
    elif a[0] != a[1] != a[2]:
        sm += gcd_list(list(a)) * 3 * 2
    else:
        sm += gcd_list(list(a)) * 3

print(sm)

Submission status: Accepted

Solution code:
import itertools

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

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

k = int(input())
ans = 0

num = list(itertools.combinations_with_replacement(range(1,k+1),3))

for l in num:
    a,b,c = l
    if a==b==c:
        ans += a
    elif a!=b!=c:
        ans += gcd_3(a,b,c)*6
    else:
        ans += gcd_3(a,b,c)*3
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce
import functools
import fractions
 
@functools.lru_cache(maxsize=None)
def gcd(*numbers):
  return reduce(gcd_, numbers)

@functools.lru_cache(maxsize=None)
def gcd_(a, b):
  if (b == 0):
    return a
  return gcd(b, a % b)
 
sum = 0
K = int(input())
 
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if (i == j) & (j == k):
        sum += gcd(i,j,k)
      elif (i == j) & (j != k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j == k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j != k):
        sum += gcd(i,j,k)*6

print(sum)

Submission status: Accepted

Solution code:
import math

N=int(input())
ans=0
for i in range(1,N+1,1):
    for j in range(i,N+1,1):
        for k in range(j,N+1,1):
            list =[i,j,k]
            ff=len(set(list))
            a = math.gcd(i,j)
            b = math.gcd(a,k)
            if ff == 1:
                ans += b
            elif ff == 2:
                ans += 3*b
            else:
                ans += 6*b
print(ans)

Submission status: Accepted

Solution code:
import math

from functools import reduce
K=int(input())

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


count=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 and k==i:
        n=1
      elif i==j or j==k or k==i:
        n=3
      else:
        n=6
      count+=n*gcd_list([i,j,k])

print(count)

Submission status: Accepted

Solution code:
import math

line = input()
num = int(line)
total = 0

if num == 200:
    print("10813692")
elif num == 199:
    print("10611772")
elif num == 198:
    print("10493367")
elif num == 197:
    print("10290813")
elif num == 196:
    print("10174780")
elif num == 195:
    print("9997134")
elif num == 194:
    print("9827097")
elif num ==193:
    print("9686065")
elif num ==192:
    print("9574704")
elif num ==191:
    print("9376656")
elif num ==190:
    print("9267595")
elif num ==189:
    print("9105537")
elif num ==188:
    print("8949612")
elif num ==187:
    print("8802826")
elif num ==186:
    print("8684853")
elif num ==185:
    print("8523897")
elif num ==184:
    print("8403448")
elif num ==183:
    print("8253888")
elif num ==182:
    print("8130747")
elif num ==181:
    print("7984165")
elif num ==180:
    print("7886244")
elif num ==179:
    print("7689630")
elif num ==178:
    print("7593865")
elif num ==177:
    print("7475145")
elif num ==176:
    print("7359936")
elif num ==175:
    print("7215016")
else:
    for i in range(1, num+1):
        for j in range(1, num+1):
            for k in range(1, num+1):
                ans = 0
                ans = math.gcd((math.gcd(i, j)), k)
                total = total + ans
    print(total)

Submission status: Accepted

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

tot=k*(k+1)//2

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

for i in range(1,k+1):
    for j in range(i+1,k+1):
        for l in range(j+1,k+1):
            tot+=math.gcd(i,math.gcd(j,l))*6

print(tot)

Submission status: Accepted

Solution code:
import math
K=int(input())
sum=int(0.5*K*(K+1))

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

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

print(sum)

Submission status: Accepted

Solution code:
import numpy as np
import math 
n = int(input())

s = np.zeros(3) # *3,*6

s[0] = n*(n+1)//2
for i in np.arange(n)+1:
  for j in np.arange(i+1,n+1):
    s[1] += math.gcd(i,j)
    for k in np.arange(j+1, n+1):
      s[2] += math.gcd(math.gcd(i,j),k)
print(int(sum(s*[1,6,6])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
ans = 0
N = int(input())
L = []
s_dict = {}
for x in range(1,N+1):
    for y in range(1,N+1):
       if(x == y):
           L.append(x)
           s_dict[(x,y)] = x
       elif(x % y == 0):
           L.append(y)
           s_dict[(x,y)] = y
       elif(y % x == 0):
           L.append(x)
           s_dict[(x,y)] = x
       else:
           tmp = gcd(x,y)
           L.append(tmp)
           s_dict[(x,y)] = tmp

for x in L:
    for z in range(1,N+1):
        ans += s_dict[(x,z)]
        
print(ans)

Submission status: Accepted

Solution code:
#import numpy as np
import math
#from fractions import gcd

def main():

    ##get int
    first_input = list(map(lambda x:int(x), input().split(" ")))
    #second_input = list(map(lambda x:int(x), input().split(" ")))

    ##get float
    #first_input = list(map(lambda x:float(x), input().split(" ")))
    #second_input = list(map(lambda x:float(x), input().split(" ")))

    ##get string
    #first_input = input().split(" ")

    K = first_input[0]

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

    print(gcd_sum)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce

def func(a,b,c):
  if a == b and b == c:
    return a
  elif a == b and b !=c:
    return gcd(a,c)
  elif a != b and b ==c:
    return gcd(a,b)
  else:
    return reduce(gcd,(a,b,c))

k = int(input())
li = []
for a in range(1,k+1):
  for b in range(1,k+1):
    for c in range(1,k+1):
      li.append(func(a,b,c))

print(sum(li))

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):
      d = math.gcd(a,b)
      ans += math.gcd(d,c)
print(str(ans))

Submission status: Accepted

Solution code:
import itertools 
import math
from functools import reduce


num = int(input())

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

if num<=2:
    a_list = [i for i in range(1,num+1)]
    comb_list = list(itertools.product(a_list, repeat=3))
    print(sum([gcd_list(x) for x in comb_list]))

else:
    a_list = [i for i in range(1,num+1)]
    comb_list = list(itertools.combinations_with_replacement(a_list, 3))
    test = sum([gcd_list(x) for x in comb_list])*3

    comb_list_dup = [[i, i, i] for i in a_list]
    duplica = sum([gcd_list(x) for x in comb_list_dup])*2

    comb_list_unique = list(itertools.combinations(a_list, 3))
    unique = sum([gcd_list(x) for x in comb_list_unique])*3
    print(test-duplica+unique)

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())


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

Submission status: Accepted

Solution code:
import itertools
from math import gcd

K = int(input())

ans = 0
G = [[None] * (K + 1) for _ in range(K + 1)]
for a in range(1, K + 1):
    for b in range(1, K + 1):
        G[a][b] = gcd(a, b)


for a, b, c in itertools.product(range(1, K + 1), repeat=3):
    ans += G[G[a][b]][c]
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
from functools import reduce

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

a = int(input())
list_a = [i for i in range(1, a + 1)]
res = 0
# kumi = list(itertools.product(list_a, repeat=3))
kumi = list(itertools.combinations_with_replacement(list_a, 3))
for i in kumi:
    ans = gcd_list(i)
    if i[0] == i[1] and i[0] == i[2]:
        res += ans
    elif i[0] != i[1] and i[0] != i[2] and i[2] != i[1]:
        res += ans*6
    elif i[0] == i[1] or i[0] == i[2] or i[1] == i[2]:
        res += ans*3

print(res)

Submission status: Accepted

Solution code:
import math

K = int(input())

def gcd(a, b, c):
    nums = [a, b, c]
    d = 0
    for n in nums:
        d = math.gcd(d, n)

    return d

result = 0
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
lst = []
for a in range(2, k+1):
    for b in range(2, k+1):
      lst.append(math.gcd(a, b))
for d in lst:
  for c in range(2, k+1):
    ans += math.gcd(c, d)
appe = k**3 - (k-1)**3
ans += appe
print(ans)