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
k = int(input())
wa = 0
for a in range(k):
    wa += (a + 1)
for a in range(k):
    for b in range(k):
        if a != b:
            wa += 3 * math.gcd(a + 1, b + 1)


for a in range(k):
    for b in range(a):
        for c in range(b):
            wa += 6*math.gcd(math.gcd(a+1, b+1), c+1)
print(wa)

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:
K=int(input())
import math
X=[]
for a in range(1,K+1):
    _=[math.gcd(a,b) for b in range(1,K+1)]
    X.append(_)
ans=0
for c in range(1,K+1):
    #print(c,ans)
    for a in range(1,K+1):
        for _ in X[a-1]:
            ans+=math.gcd(_,c)
    #ans-=math.gcd(X[c-1][0],c)*2
print(ans)

Submission status: Accepted

Solution code:
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
k=int(input())
count=0
count+=k*(k+1)//2
for i in range(1,k+1):
  for j in range(1,k+1):
    if i!=j:
      count+=3*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):
      count+=6*gcd(gcd(a,b),c)
print(count)

Submission status: Accepted

Solution code:
import math
 
k = int(input())
sum = 0
 
for a in range(k - 2):
  for b in range(a + 1, k - 1):
    for c in range(b + 1, k):
      sum += 6 * math.gcd(a + 1, math.gcd(b + 1, c + 1))
      
for a in range(k - 1):
  for b in range(a + 1, k):
    sum += 6 * math.gcd(a + 1, b + 1)
    
sum += k * (k + 1) // 2
      
print(sum)

Submission status: Accepted

Solution code:
K = int(input())

from fractions import gcd
ans = 0


m = [[0] * K for _ in range(K)]
for x in range(1, K + 1):
    for y in range(1, K + 1):
        m[x - 1][y - 1] = gcd(x, y)


for x in range(1, K + 1):
    for y in range(1, K + 1):
        for z in range(1, K + 1):
            if x == y and y == z:
                ans += x
            else:
                temp = m[x - 1][y - 1]
                if temp >= z:
                    temp = m[z - 1][temp - 1]
                else:
                    temp = m[temp - 1][z - 1]

                #ans += (3 * temp)
                ans += temp

print(ans)

Submission status: Accepted

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

if K==1:
  print(1)
  exit()
elif K==2:
  print(9)
  exit()
else:
  t=0
  for i in range(1,K-1):
    for j in range(i+1,K):
      for k in range(j+1,K+1):
        a=math.gcd(i,j)
        b=math.gcd(a,k)
        t+=b
  tw=0
  for i in range(1,K):
    for j in range(i+1,K+1):
      c=math.gcd(i,j)
      tw+=c
  
  on=0
  for i in range(1,K+1):
    on+=i
  print(t*6+tw*6+on)

Submission status: Accepted

Solution code:

from math import gcd
from itertools import combinations
K = int(input())
ans = 0

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

Submission status: Accepted

Solution code:
import math
N = int(input())
S = 0
S += N*(N+1)//2 #3つ同じ
for i in range(1,N+1): #(i,i,j),(j,j,i)なるペア(i != j)
    for j in range(1,N+1):
        if i != j:
            S += 3*math.gcd(i,j)
if N >= 3: #(i,j,k)
    for k in range(3,N+1):
        for j in range(2,k):
            for i in range(1,j):
                S += 6*math.gcd(math.gcd(i,j),k)
print(S)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
gcdl = [[0 for i in range(k + 1)] for i in range(k + 1)]
for i in range(1, k + 1):
    for j in range(1, k + 1):
        gcdl[i][j] = gcd(i, j)
s = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for l in range(1, k + 1):
            s += gcdl[i][gcdl[j][l]]

print(s)

Submission status: Accepted

Solution code:
K = int(input())

ans = 0
ans2 = 0


import math

for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            tmp = math.gcd(i,j)
            ans += math.gcd(tmp,k)
            
for i in range(1,K+1):
    for j in range(i+1,K+1):
        for k in range(j+1,K+1):
            tmp3 = math.gcd(i,j)
            ans2 += math.gcd(tmp3,k)
      
tmp2 = 0

for i in range(1,K+1):
    tmp2 += i
    
print((ans - tmp2 - ans2)*3 + ans2*6 + tmp2)

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

Submission status: Accepted

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

import math
K = int(input())
ans_list = []
ans = 0
for i in range(1, K + 1):
    for j in range(1,K+1):
        ans_list.append(math.gcd(i,j))
for i in range(1,K+1):
    for j in range(0,len(ans_list)):
        ans += math.gcd(i,ans_list[j])

print(ans)

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')
 
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime
 
K = iin()

ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        ab = 1
        if a == 1 or b == 1:
            ab = 1
        elif a == b:
            ab = a
        else:
            ab = math.gcd(a, b)
        for c in range(1, K+1):
            if ab == 1 or c == 1:
                ans += 1
            elif ab == c:
                ans += c
            else:
                ans += math.gcd(ab, c)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
N = int(input())
# N=3
# M=1
# A=[5,4,2,1]
 
cnt = 0
cnt1 = 0
 
# for i in range(N):
#     cnt = cnt + A[i]
#
# for i in range(N):
#     if A[i] / cnt > 1 / 4 / M:
#         cnt1 = cnt1 + 1
 
sum=0
for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N + 1):
            sum =sum + gcd(i,j,k)
            # print(i,j,k)
 
# a1 = N % 10
# a2 = ((N-a1*10) % 100)/10
# a3 = ((N-a1*100-a2*10))/100
 
print(sum)

Submission status: Accepted

Solution code:
import math
from functools import reduce
from functools import lru_cache

@lru_cache(maxsize=10000)
def gcd(*numbers):
    return reduce(math.gcd, numbers)

# 整数を一つ入力
N = int(input())

res = 0

for a in range(1,N+1):
    for b in range(1,N+1):
        gcdab = gcd(a,b)
        for c in range(1,N+1):
            res += gcd(gcdab,c)

print(res)

Submission status: Accepted

Solution code:
import math
N = int(input())
ans = (1+N)*N/2
for i in range(1,N+1):
  for j in range(1,i):
    ans += math.gcd(i,j)*6
for i in range(1,N+1):
  for j in range(1,i):
    if i== j:
      break
    for k in range(1,j):
      if i == k or j == k:
        break
      else:
        ans += math.gcd(k,math.gcd(i,j))*6
print(int(ans))

Submission status: Accepted

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

sum=K

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
import itertools

def main():
# 整数の入力
    k = int(input())
    
    l = list(itertools.combinations_with_replacement(range(1, k+1), 3))
    l = set(l)
    
    result = []
    for t in l:
        tmp = math.gcd(t[0], t[1])
        ans = math.gcd(tmp, t[2])

        ab = 1 if t[0] == t[1] else 0 
        ac = 1 if t[0] == t[2] else 0
        bc = 1 if t[1] == t[2] else 0

        num = ab + ac + bc

        if num == 0:
            result.append(ans * 6)
        elif num == 1:
            result.append(ans * 3)
        elif num == 2:
            result.append(ans * 3)
        else:
            result.append(ans)
        
                
    print(sum(result))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

K=int(input())

def gcd(a,b,c):
 gcd=gcd2(a,b)
 return gcd2(gcd,c)
      
ans=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 
     
for a in range(1,K+1):
  for b in range(a+1,K+1):
     ans += gcd(a,a,b) * 6 

ans += K*(K+1)/2

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K = int(input())
import math
from functools import reduce
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
tmp_sum = 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:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)*6
      elif i == j and j == k:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)
      else:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)*3
print(tmp_sum)

Submission status: Accepted

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

for i in range(1, k+1):
    for j in range(1, k+1):
        for t in range(1, k+1):
            cnt += gcd(i, gcd(j, t))

print(cnt)

Submission status: Accepted

Solution code:
K = int(input())
import math
c = 0
for i in range(1,K+1):
    for j in range(1,i):
        for k in range(1,j):
            c += 6*math.gcd(math.gcd(i,j),k)
            
for i in range(1,K+1):
    for j in range(1,i):
        c += 6*math.gcd(i,j)
        
c += K*(K+1)//2

print(c)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
memo = {}
res = 0

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

print(res)

Submission status: Accepted

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

n = int(input())
ans = n * (n + 1) // 2
for i in range(1, n):
    for j in range(i + 1, n + 1):
        ans += gcd(i, j) * 6

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


print(ans)

Submission status: Accepted

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

ans = 0

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

ans *= 6
ans += sum([i for i in range(1, k + 1)])
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:
from math import gcd
from functools import reduce
k = int(input())

def f(num):
    return reduce(gcd, num)

ans = 0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            ans += f((x,y,z))
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import math
from functools import reduce

def main():

    input = stdin.readline

    K = int(input())

    ans_sum = 0

    for i in range(1,K+1):
        for j in range(i,K+1):
            for k in range(j,K+1):
                check = [i,j,k]

                sum_type = len(set(check))
                if sum_type == 1:
                    ans_sum += gcd(i,j,k)
                elif sum_type == 2:
                    ans_sum += gcd(i,j,k) * 3
                else:
                    ans_sum += gcd(i,j,k) * 6

    print(ans_sum)


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
r = 0
for i in range(1, k + 1):
    if i > 1:
        for j in range(1, k + 1):
            if j > 1:
                for l in range(1, k + 1):
                    r += gcd(i, gcd(j, l))
            else:
                r += k
    else:
        r += k * k
print(r)

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):
        for l in range(1,K+1):
            ans += math.gcd(math.gcd(i,j),l)
            
print(ans)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
import math
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):
            if i == j and j == k and k == i:
                sum += math.gcd(math.gcd(i,j),k)
            elif i != j and j != k and k != i:
                sum += 6 * math.gcd(math.gcd(i,j),k)
            else:
                sum += 3 * 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)

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

n = int(input())
result = 0
for i in range(1, n+1):
  for j in range(1, n+1):
    for k in range(1, n+1):
      if i == 1 or j == 1 or k == 1:
        result += 1
        continue
      result += math.gcd(math.gcd(i, j), k)

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd
from itertools import combinations_with_replacement
def gcds(a):
    tmp = None
    for i in a:
        if tmp == None:
            tmp = i
        else:
            tmp = gcd(i, tmp)
    return tmp


cnt = 0
n = int(input())
for i in combinations_with_replacement(range(1, n + 1), 3):
    tmp = gcds(i)
    # cnt += 6 * tmp
    if len(set(i)) == 3:
        tmp *= 6
        cnt += tmp
    elif len(set(i)) == 2:
        tmp *= 3
        cnt += tmp
    else:
        cnt += tmp
print(cnt)

Submission status: Accepted

Solution code:


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

def main():
    k=int(input())
    a=0
    b=0
    ans = 0

    w = [[0]*(k+1) for _ in range(k+1)]
    for i in range(1,k+1):
        for j in range(i,k+1):
            x = gcd(i,j)
            w[i][j] = x
            w[j][i] = x

    for i in range(1,k+1):
        for j in range(i,k+1):
            for k in range(j,k+1):
                s =set([i,j,k])
                length = len(s)
                if length == 1:
                    ans += i
                elif length == 2:
                    l = list(s)
                    ans += 3*w[l[0]][l[1]]
                else:
                    a = w[i][j]
                    b = w[a][k]
                    ans += 6*b

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from math import gcd
ans=0
K=int(input())
for i in range(1,K+1):
  for j in range(1,K+1):
    if abs(i-j)==1 or j==1 or i==1:
      a=1
    else:
      a=gcd(i,j)
    for k in range(1,K+1):
        if a==1:
            ans+=K
            break
        elif abs(a-k)==1:
            ans+=1
        else:
            ans+=gcd(a,k)
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):
        tmp = math.gcd(a, b)
        if tmp == 1:
            ans += k
        else:
            for c in range(1, k+1):
                ans += math.gcd(tmp, c)

print(int(ans))

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)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
import math
k = int(input())
cnt = (k*(k+1))//2
b = [int(i) for i in range(1,k+1)]
for i in itertools.combinations(b,3):
    n = list(i)
    cnt += (math.gcd(math.gcd(n[0],n[1]),n[2]))*6
for t in itertools.combinations(b,2):
    m = list(t)
    cnt += (math.gcd(m[0],m[1]))*6

print(cnt)

Submission status: Accepted

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

K = int(input())

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

Submission status: Accepted

Solution code:
K = int(input())

import fractions

arr = [[0 for i in range(200)] for j in range(200)]

def gcd(a, b):
    if arr[a-1][b-1] != 0:
        return arr[a-1][b-1]
    else:
        ans = fractions.gcd(a, b)
        arr[b-1][a-1] = ans
        arr[a-1][b-1] = ans
        return ans

lis = []
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            lis.append(gcd(gcd(i, j), gcd(j, k)))


print(sum(lis))

Submission status: Accepted

Solution code:
#import sys
#input→sys.stdin.readline半分の時間に
#input=sys.stdin.readline
"""
n=int(input())
s=0
for i in range(n+1):
  if i%5!=0 and i%3!=0:
    s+=i
print(s)
"""
k=int(input())
s=0
#関数を定義
def gcd(p,q):
 
  if p<q:
    p, q = q, p
#①のとき
  if q == 0:
    return p
#②のとき
  else:
    return gcd(q,p%q)
#結果を出力

for c in range (1,k+1):
  for b in range (1,c+1):
    for a in range (1,b+1):
      d=gcd(a,c)
      g=gcd(d,b)
      if a==b and b==c:
        g=g
      elif a!=b and b!=c:
        g=g*6
      else:
        g=g*3
      s+=g
print(s)

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

print(ans)

Submission status: Accepted

Solution code:

import math

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

def get_now_ans(a, b, c):
    times = None
    if a == b == c:
        times = 1
    elif (a == b) or (b == c):
        times = 3
    else:
        times = 6
    return multi_gcd(a,b,c) * times

def calc_num(now_n):
    now_ans = 0
    a = now_n
    for b in range(1, now_n + 1):
        for c in range(1, now_n + 1):
            if c > b:
                continue
            else:
                now_ans += get_now_ans(a,b,c)
    return now_ans

K = int(input())

ans = 0
for i in range(1, K + 1):
    ans += calc_num(i)
print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
f = 0
for a in range(1, N+1):
    for b in range(1, N+1):
        for c in range(1, N+1):
            if b<a or c<a or b>c:
                continue
            elif a==b and b==c:
                d=math.gcd(a, b)
                e=math.gcd(d, c)
                f+=e
                continue
            elif a==b and b!=c:
                d=math.gcd(a, b)
                e=math.gcd(d, c)
                f+=e*3
                continue
            elif b==c and b!=a:
                d=math.gcd(a, b)
                e=math.gcd(d, c)
                f+=e*3
                continue   
            elif a==c and b!=c:
                d=math.gcd(a, b)
                e=math.gcd(d, c)
                f+=e*3
                continue
            else:
                d=math.gcd(a, b)
                e=math.gcd(d, c)
                f+=e*6
                continue
print(f)

Submission status: Accepted

Solution code:
from collections import deque, Counter
from itertools import chain, combinations
import json
import bisect
import sys
import math

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7

mem = {}


def gcd(a, b, c):
    if (a, b, c) not in mem:
        mem[(a, b, c)] = math.gcd(math.gcd(a, b), c)
    return mem[(a, b, c)]


def main():
    K = int(input())
    t1 = 0
    for a in range(1, K + 1):
        for b in range(a + 1, K + 1):
            for c in range(b + 1, K + 1):
                t1 += gcd(a, b, c) * 6
    t2 = 0
    for ab in range(1, K + 1):
        for c in range(ab + 1, K + 1):
            t2 += math.gcd(ab, c) * 6
    t3 = 0
    for abc in range(1, K + 1):
        t3 += abc
    print(t1 + t2 + t3)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
ans = 0
n = int(input())
K = [0 for _ in range(n + 1)]
for i in range(1,n + 1):
  for i2 in range(1,n + 1):
    K[math.gcd(i,i2)] += 1
for i in range(1,n + 1):
  for i2 in range(1,n + 1):
    ans += K[i] * math.gcd(i,i2)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
from functools import lru_cache
import itertools

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

def solve(K: int):
    sum = 0

    # all same
    sum += K * (K+1) // 2

    # 2 same
    l = [ i for i in range(1, K+1)]

    for a, b in itertools.combinations(l, 2):
        sum += 6 * gcd(a,b)

    # all diff
    for a, b, c in itertools.combinations(l, 3):
        t = gcd(a,b)
        sum += 6 * gcd(t,c)


    # for a in range(1, K+1):
    #     for b in range(1, K+1):
    #         t = gcd(a,b)
    #         for c in range(1, K+1):
    #             sum += gcd(t,c)
    print(sum)
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

n = int(input())

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

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

total3 = 0
for i in range(1, n + 1):
    total3 += i

print(total1 + total2 + total3)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

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

Submission status: Accepted

Solution code:
import math

k = int(input())

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

def gcd3(x, y, z):
    return gcd(gcd(x, y), z)

sm = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            sm += gcd3(i, j, l)
print(sm)

Submission status: Accepted

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

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

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

ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            q = [a, b, c]
            ans += gcd_list(q)

print(ans)

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

import math

def solve():
    k = II()
    ans = 0
    for a in range(1, k+1):
        for b in range(1, k+1):
            g1 = math.gcd(a, b)
            for c in range(1, k+1):
                g2 = math.gcd(g1, c)
                ans = ans + g2

    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import math

ans = 0
K = int(input())
dp = [[1] * 220 for i in range(220)] 
for i in range(1,K+1):
    for j in range(1,K+1):
        dp[i][j] = math.gcd(i,j)
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            a = dp[i][j]
            ans += dp[a][k]
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from functools import reduce
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def main():
    n = int(input())
    ans = 0
    for i in range(1, n+1):
        for j in range(1, n+1):
            for k in range(1, n+1):
                ans += reduce(math.gcd, [i, j, k])
    print(ans)


main()

Submission status: Accepted

Solution code:
import itertools
import math
from functools import reduce

k=int(input())
l=list(range(1,k+1))
ans=0

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

for i in itertools.product(l,repeat=3):
    ans+=(gcd(i[0],i[1],i[2]))
print(ans)

Submission status: Accepted

Solution code:
def main():
    k = int(input())
    gcds = list(sorted([gcd(i, j) for i in range(1, k+1) for j in range(1, k+1)]))
    j = 0
    s = list(sorted(list(set(gcds))))
    m = k**2-1
    gcdss = [0]*len(s)
    flag = False
    for i, l in enumerate(s):
        while gcds[j] == l:
            gcdss[i] += 1
            if j == m:
                flag = True
                break
            j += 1
        if flag:
            break
    ans = sum([gcd(i, p)*n for i in range(1, k+1) for p, n in zip(s, gcdss)])
    print(ans)
            
    
def gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return gcd(b, a%b)


if __name__ == "__main__":
    main()

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

print(ans)

Submission status: Accepted

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

def gcd(*numbers):
    return reduce(math.gcd, numbers)
    
ans=0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans+=gcd(a,b,c)
print(ans)