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:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,boundary=1,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

## return prime factors of N as dictionary {prime p:power of p}
## within 2 sec for N = 2*10**20+7
def primeFactor(N):
    i,n=2,N
    ret={}
    d,sq=2,99
    while i<=sq:
        k=0
        while n%i==0:
            n,k,ret[i]=n//i,k+1,k+1
        if k>0 or i==97:
            sq=int(n**(1/2)+0.5)
        if i<4:
            i=i*2-1
        else:
            i,d=i+d,d^6
    if n>1:
        ret[n]=1
    return ret


## return the list of prime numbers in [2,N], using eratosthenes sieve
## within 2sec for N = 1.2*10**7
def PrimeNumSet(N):
    Max = int(N**0.5)
    seachList = [i for i in range(2,N+1)]
    primeNum = []
    while seachList:
        if seachList[0] <= Max:
            break
        primeNum.append(seachList[0])
        tmp = seachList[0]
        seachList = [i for i in seachList if i % tmp != 0]
    primeNum.extend(seachList)
    return primeNum

## retrun LCM of numbers in list b
## within 2sec for no of B = 10*5  and  Bi < 10**6
def LCM(b,mo=10**9+7):
    prs=PrimeNumSet(max(b))
    M=dict(zip(prs,[0]*len(prs)))
    for i in b:
        dc=primeFactor(i)
        for j,k in dc.items():
            M[j]=max(M[j],k)
    
    r=1
    for j,k in M.items():
        if k!=0:
            r*=pow(j,k,mo)
            r%=mo
    return r

show_flg=False
show_flg=True


#C
g=[[1]*201 for i in range(201)]
for a in range(1,201):
    for b in range(1,201):
        g[a][b]=math.gcd(a,b)

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

Submission status: Accepted

Solution code:
K = int(input())

from math import gcd


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

print(sum)

Submission status: Accepted

Solution code:
import math
 
K = int(input())
 
lis = tuple([i for i in range(1,K+1)])
a = 0
 
for i in lis:
    for j in lis:
        i_j= math.gcd(i,j)
        for k in lis:
            i_j_k = math.gcd(i_j,k)
            a += i_j_k
print(a)

Submission status: Accepted

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

# 3つとも等しい数字
for i in range(1, k+1):
    for j in range(i, k+1):
        for m in range(j, k+1):
            if i==j==m:
                result += i
            elif i==j or j==m:
                result += gcd(i, m)*3
            else:
                result += gcd(i, gcd(j,m))*6
print(result)

Submission status: Accepted

Solution code:
from fractions import gcd
k = int(input())
check = [[1 for _ in range(k+3)] for i in range(k+3)]
for i in range(1, k+2):
    for j in range(i, k+2):
        check[i][j] = gcd(i, j)

result = 0
for s in range(1, k+1):
    for t in range(1, k+1):
        n, m = sorted([s, t])
        x = check[n][m]
        for u in range(1, k+1): 
            y, z = sorted([x, u])
            result += check[y][z]
print(result)

Submission status: Accepted

Solution code:
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b,a%b)
n = int(input())
ans=0
for i in range(n):
    for j in range(n):
        for k in range(n):
            ans += gcd(gcd(i+1, j+1), k+1)
print(ans)

Submission status: Accepted

Solution code:
import math
 
K = int(input())
 
lis = tuple([i for i in range(1,K+1)])
a = 0
 
for i in lis:
    for j in lis:
        i_j= math.gcd(i,j)
        for k in lis:
            i_k = math.gcd(i,k)
            i_j_k = math.gcd(i_j,i_k)
            a += i_j_k
print(a)

Submission status: Accepted

Solution code:
def main():
    from itertools import product
    from math import gcd

    K = int(input())

    ans = 0
    for prod in product(range(1, K + 1), repeat=3):
        a, b, c = prod
        ans += gcd(gcd(a, b), c)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
import math

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

K = i_i()

res = sum([math.gcd(math.gcd(a, b), c) for a in range(1,K+1) for b in range(1,K+1) for c in range(1,K+1)])
print(res)

Submission status: Accepted

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

total = 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) :
      total += math.gcd(tmp,c)

print(total)

Submission status: Accepted

Solution code:
from math import gcd
 
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 math
n = int(input())
sum = 0
for i in range(1,n+1):
    for j in range(1,n+1):
      gcd = math.gcd(i,j)
      for k in range(1,n+1):
      	sum += math.gcd(gcd, k)
 
print(sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()

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

def gcd(a, b):
	if a > b:
		a, b = b, a

	while a > 0:
		a, b = b % a, a
	return b

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
class Graph(object):
    """
    隣接リストによる有向グラフ
    """

    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra(object):
    """
    ダイクストラ法（二分ヒープ）による最短経路探索
    計算量: O((E+V)logV)
    """

    def __init__(self, graph, start):
        self.g = graph.graph

        # startノードからの最短距離
        # startノードは0, それ以外は無限大で初期化
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0

        # 最短経路での1つ前のノード
        self.prev = defaultdict(lambda: None)

        # startノードをキューに入れる
        self.Q = []
        heappush(self.Q, (self.dist[start], start))

        while self.Q:
            # 優先度（距離）が最小であるキューを取り出す
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self, goal):
        """
        startノードからgoalノードまでの最短距離
        """
        return self.dist[goal]

    def shortest_path(self, goal):
        """
        startノードからgoalノードまでの最短経路
        """
        path = []
        node = goal
        while node is not None:
            path.append(node)
            node = self.prev[node]
        return path[::-1]

def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))

# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
# a,bの最大公約数
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


K = int(input())
ans = 0

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

Submission status: Accepted

Solution code:
from math import gcd

K=int(input())+1
sum=0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
K = int(input())
res = sum([math.gcd(math.gcd(a, b), c) for a in range(1,K+1) for b in range(1,K+1) for c in range(1,K+1)])
print(res)

Submission status: Accepted

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

def main():
    K = int(input())
    table = [[0]*K for i in range(K)]
    for i in range(K):
        for j in range(K):
            table[i][j] = gcd(i + 1, j + 1)
    ans = 0
    for i in range(K):
        for j in range(K):
            for k in range(K):
                ans += gcd(i + 1, table[j][k])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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


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

print(ans)

Submission status: Accepted

Solution code:
K = int(input())

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

gcd_table = [[0] * (K+1) for i in range(K+1)]
for i in range(1, K+1):
    for j in range(i, K+1):
        g = gcd(i, j)
        gcd_table[i][j] = gcd_table[j][i] = g


ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            g1 = gcd_table[a][b]
            g2 = gcd_table[g1][c]
            ans += g2

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce


k = int(input())

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

k += 1
ans = 0

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

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


print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
res = 0
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):
            res += gcd(t,c)
print(res)

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""

# 最大公約数 と 最小公倍数
"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""

# 二分探索
"""import bisect # 二分探索
print(A,bisect.bisect_left(A,4))
print(C,n - bisect.bisect_right(C,4))
print(sum(bisect.bisect_left(A,b) * (n-bisect.bisect_right(C,b)) for b in B))"""

# 円周率 pi
"""import math
pi_ = math.pi"""

# エラトステネスの篩
"""def Erato(n):
    prime = []
    is_prime = [True] * (n+1)
    p = 0
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,n+1):
      if is_prime[i] == True:
        prime.append(i)
        p += 1
        for j in range(2 * i,n+1,i):
          is_prime[j] = False
    return p"""

def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  import math
  k = int(input())
  ans = 0
  BC = []
  for b in range(1,k+1):
    for c in range(1,k+1):
      BC.append(math.gcd(b,c))
  for a in range(1,k+1):
    for bc in BC:
      ans += math.gcd(a,bc)
  print(ans)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:

from math import gcd
k = int(input())
a = 0
b = 0
ans = 0
memo =[[[0 for _1 in range(k+1)] for _ in range(k + 1)] for _2 in range(k + 1)]
for i in range(1, k + 1):
    for j in range(1, k + 1):
        a = gcd(i,j)
        for k in range(1, k + 1):
            ans += gcd(a,k)

print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8
from math import gcd

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:
from math import gcd 
N = int(input())
i = 0
 
for a in range(1, N+1):
    for b in range(1, N+1):
        for c in range(1, N+1):
            i += gcd(gcd(a, b), c)
            
print(i)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

from math import gcd

k = int(readline())
cnt = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        gcd_ans = gcd(i, j)
        for l in range(1, k + 1):
            cnt += gcd(l, gcd_ans)
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop

from math import gcd

INF = float('inf')
NIL = - 1

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log,gcd
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter

from functools import reduce
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
n=ii()
def gcd_list(numbers):
    return reduce(gcd, numbers)
ans=0
for i in range(1,n+1):
    for j in range(i,n+1):
        for k in range(j,n+1):
            temp=0
            if len(list(set([i,j,k])))==1:
                temp=1
            elif len(list(set([i,j,k])))==2:
                temp=3
            else:
                temp=6
            ans+=gcd(gcd(i,j),k)*temp
print(ans)

Submission status: Accepted

Solution code:
import sys
from math import gcd

def solve(K: int):

    ans = 0

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

    print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k=int(input())

from math import gcd

gcd_sum=[0]*201
ans=0

for i in range(1,k+1):
    for j in range(1,k+1):
        gcd_sum[i]+=gcd(i,j)

for a in range(1,k+1):
    for b in range(1,k+1):
        g=gcd(a,b)
        ans += gcd_sum[g]

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
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(gcd(a, b), c)
print(sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
K = int(input())
 
# lcm & gcd not math library
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        gcd_ij = gcd(i, j)
        for k in range(1, K + 1):
            ans += gcd(gcd_ij, k)

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

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


print(sum(res))

Submission status: Accepted

Solution code:
from math import*;r=range(1,int(input())+1);print(sum(gcd(a,gcd(b,c))for a in r for b in r for c in r))

Submission status: Accepted

Solution code:
import math
import sys
from io import StringIO
import unittest
from functools import reduce

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

    abc = [0, 0, 0]
    ans = 0

    for i in range(1, k + 1):
        for j in range(k):

            abc[j] = i

            if 0 in abc:
                continue

            work = reduce(math.gcd, abc)
            if not abc[0] == abc[1] == abc[2]:
                work = work * 3
            ans += work
            print(str(abc) + "| " +str(work))

    print(ans)



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

    ans = 0

    for a in range(1, k + 1):
        # print(a)
        for b in range(a, k + 1):
            for c in range(b, k + 1):
                work = reduce(math.gcd, [a, b, c])
                if a == b == c:
                    print("これは×3しない")
                    print(str(a) + "|" + str(b) + "|" + str(c) + "=" + str(work))
                    # ×3しない
                    pass
                else:
                    print("これは×3する")
                    print(str(a) + "|" + str(b) + "|" + str(c) + "=" + str(work * 3))
                    work = work * 3
                ans += work

    print(ans)


# これじゃダメ。タイムアウトする気がする・・。
def not_resolve():
    k = int(input())

    ans = 0

    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                ans += reduce(math.gcd, [a, b, c])

    print(ans)



class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2"""
        output = """9"""
        self.assertIO(input, output)

    def test_入力例(self):
        input = """3"""
        output = """30"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """200"""
        output = """10813692"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    not_resolve()
    #not_resolve()

Submission status: Accepted

Solution code:
import math as f

K = int(input())
total = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        ij = f.gcd(i, j)
        if ij == 1:
            total += K
            continue
        for k in range(1, K + 1):
            total += f.gcd(ij, k)

print(total)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import math



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

    for a in range(1,k+1):
        for b in range(1,k+1):
            for c in range(1,k+1):
              temp = math.gcd(a,b)
              total += math.gcd(temp,c)
    print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 初期入力
K =int(input())

# 最大公約数(mathは3.5以降) 
from functools import reduce
import math #(2020-0405  fractions→math)
def lcm_base(x, y):
    return  math.gcd(x, y) # 「//」はフロートにせずにintにする
def lcm_list(numbers):
    return reduce(lcm_base, numbers)
def lcm_fukusu(*numbers):
    return reduce(lcm_base, numbers)


saisyou_koyakusu =[]

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:
                saisyou_koyakusu.append(lcm_fukusu(i,j,k))
            elif i==j or j==k:
                saisyou_koyakusu.append( (lcm_fukusu(i,j,k))*3 )
            else:
                saisyou_koyakusu.append( (lcm_fukusu(i,j,k)) *6)

            #print(i,j,k)   
print(sum(saisyou_koyakusu))

Submission status: Accepted

Solution code:
def main():
    import math
    import sys

    input = sys.stdin.readline

    n = int(input())
    c_1 = 0

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

    print(c_1)

if __name__ ==  "__main__":
    main()

Submission status: Accepted

Solution code:
import math

K = int(input())

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


print(sum(res))

Submission status: Accepted

Solution code:
from math import gcd

def solve():
  K = int(input())
  ans = 0
  for i in range(1, K+1):
      for j in range(1, K+1):
          a = gcd(i, j)
          for k in range(1, K+1):
            ans += gcd(a, k)
  return ans
                
print(solve())

Submission status: Accepted

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

import numpy as np

K = int(input())

x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
print(nums.sum())

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=a+gcd(i,j,l)
print(a)

Submission status: Accepted

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

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:
#!/usr/bin/env python3
from math import gcd
K = int(input())
K += 1
ans = 0
for i in range(1, K):
    for j in  range(1, K):
        for k in range(1, K):
            ans += gcd(gcd(i, j), k)
print(ans)

Submission status: Accepted

Solution code:
from math import*;from itertools import*;r=range(1,int(input())+1);print(sum(gcd(a,gcd(b,c))for a,b,c in product(r,r,r)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

k = int(input())

tb = [
    [math.gcd(a, b) for a in range(0, 201)]
    for b in range(0, 201)
]

x = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        t = tb[a][b]
        for c in range(1, k + 1):
            x += tb[t][c]
print(x)

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------------------
# Name:        module6
# Purpose:
#
# Author:      user
#
# Created:     12/04/2020
# Copyright:   (c) user 2020
# Licence:     <your licence>
#-------------------------------------------------------------------------------
from math import gcd



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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(s)

Submission status: Accepted

Solution code:
from math import gcd

a = int(input())

def main():
    for i in range(1, a+1):
        for j in range(i, a+1):
            ab = gcd(i, j)
            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(ab, k) * x

print(sum(main()))