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 math import gcd
import itertools
K=int(input())
res=0
li=[]
for x in itertools.product(range(1,K+1),repeat=2):
    li.append(gcd(x[0],x[1]))
for x,y in itertools.product(range(1,K+1),li):
    res+=gcd(x,y)
print(res)

Submission status: Accepted

Solution code:
import math as m

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 += m.gcd(i,m.gcd(j,k))
print(ans)

Submission status: Accepted

Solution code:
K = int(input())
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
ans = 0
a = []
glist = [[0 for i in range(K+1)] for j in range(K+1)]
for i in range(1,K+1):
    for j in range(1,K+1):
        g = gcd(i,j)
        a.append(g)
        glist[i][j] = g

for i in a:
    for j in range(1,K+1):
        ans += glist[i][j]
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 i in range(1,k+1):
    for j in range(1,k+1):
        for n in range(1,k+1):
            ans += gcd(i, j, n)
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):
        a = gcd(i, j)
        if a == 1:
            ans += K
            continue
        for k in range(1, K + 1):
            ans += gcd(a, k)
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

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

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

K = int(input())

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

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:
#!python3.8
# -*- coding: utf-8 -*-
# abc162/abc162_c
import sys
from math import gcd
from itertools import product

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    K = i2n()
    r = sum(gcd(gcd(i, j), k) for (i, j, k) in product(range(1, K+1), range(1, K+1), range(1, K+1)))
    print(r)
    return

main()

Submission status: Accepted

Solution code:
import math
k = int(input())
#gcd_ls = []
sub = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        mn = math.gcd(a, b)
        for c in range(1, k+1):
            sub+=math.gcd(mn, c)
print(sub)

Submission status: Accepted

Solution code:
import math 
S=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):
            G=math.gcd(j,k)
            B=math.gcd(G,i);
            S+=B;
print(S)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

K = int(readline())


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

    return b


ans = 0

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


print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
k = int(input())

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


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

print(s)

Submission status: Accepted

Solution code:
import math
from functools import reduce

K=int(input())
sum=0

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

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

K = int(input())

Gs = []
for a in range(1, K+1):
    for b in range(1, K+1):
        Gs.append(gcd(a, b))

cnt = Counter(Gs)
#print('cnt:', cnt)

ans = 0
for G, num in cnt.items():
    for c in range(1, K+1):
        ans += gcd(G, c) * num

print(ans)

Submission status: Accepted

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

res = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        cand = math.gcd(i, j)
        for l in range(1, k+1):
            res += math.gcd(l, cand)
print(res)

Submission status: Accepted

Solution code:

import math

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

k = int(input())

c = 0

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

            t = gcd(i, j, h)

            c += t

print (c)

Submission status: Accepted

Solution code:
K = int(input())
ans = 0
def gcd(a,b):
    while b > 0:
        a,b = b,a%b
    return a
for a in range(1,K+1):
    for b in range(1,K+1):
        ab = gcd(a,b)
        for c in range(1,K+1):
            ans += gcd(ab,c)
print(ans)

Submission status: Accepted

Solution code:
def main():
    K = int(input())
    from math import gcd
    total = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            ab = gcd(a, b)
            if ab == 1:
                total += K
            else:
                for c in range(1, K+1):
                    total += gcd(ab, c)
    print(total)

main()

Submission status: Accepted

Solution code:
import math
def main():
    k = int(input())
    
    ans = 0
    for i in range(1,k+1):
        for j in range(1,k+1):
            s = math.gcd(i,j)
            for t in range(1,k+1):
                ans+=math.gcd(s,t)
    
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

total = 0

for i in range(1, n+1):
  for j in range(1, n+1):
    for k in range(1, n+1):
      tmp = math.gcd(i, j)
      total += math.gcd(tmp, 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):
        _gcd = gcd(i, j)
        for k in range(1, k+1):
            ans += gcd(_gcd, 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):
        for c in range(1,K+1):
            ans += gcd(gcd(a,b),c)

print(ans)

Submission status: Accepted

Solution code:
import math
total=0
a=int(input())
for x in range(1,a+1):
    for y in range(1,a+1):
        for z in range(1,a+1):
            total+=math.gcd(math.gcd(x,y),z)
print(total)

Submission status: Accepted

Solution code:
import sys

# import bisect
# from collections import Counter, deque, defaultdict

# import copy
# from heapq import heappush, heappop, heapify
# import itertools

# from operator import attrgetter, itemgetter
# import math

# from numba import jit
# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from math import gcd
    k = int(input())
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

NN = 202020
MOD = 10**9+7
# MOD = 998244353
INF = float("inf")

n = int(input())
s = 0
for i in range(1, n+1):
    for j in range(i, n+1):
        for k in range(j, n+1):
            tmp = math.gcd(i, j)
            a = math.gcd(tmp, k)
            if i == j == k:
                s += a
            elif i == j or j == k or k == j:
                s += 3*a
            else:
                s += 6*a

print(s)

Submission status: Accepted

Solution code:
from collections import *
import sys
try: inp = raw_input
except: inp = input
def err(s):
    sys.stderr.write('{}\n'.format(s))

def ni():
    return int(inp())

def nl():
    return [int(_) for _ in inp().split()]

K = ni()

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


DP = {}
def gcd_SUM(K, a):
    if (K, a) in DP: return DP[K, a]
    sm = 0
    for i in range(1, K+1):
        sm += gcd(i, a)
    DP[K, a] = sm
    return sm


S = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        S += gcd_SUM(K, gcd(a, b))
print(S)

Submission status: Accepted

Solution code:

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def gcd(a, b):
    """
    return gcd of a,b
    ユークリッド
    """
    a,b =max(a,b),min(a,b)
    while b:
        a, b = b, a % b
    return a



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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def gcd(a, b):
    # greatest common divisor
    while b > 0:
        a, b = b, a % b

    return a


K = int(input())


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

dif3 *= 6

dif2 = 0
for a in range(1, K + 1):
    for c in range(1, K + 1):
        if a == c:
            continue
        dif2 += gcd(a, c)
dif2 *= 3

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

ans = dif1 + dif2 + dif3
print(ans)

Submission status: Accepted

Solution code:
import sys
import math

input = sys.stdin.readline

K = int(input())
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        n = math.gcd(i, j)
        for l in range(1, K + 1):
            ans += math.gcd(n, l)

print(ans)

Submission status: Accepted

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

from itertools import product
from functools import reduce
ans = [reduce(gcd, tmp) for tmp in product(range(1,n+1), repeat=3)]
print(sum(ans))

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

gcds = [[None] * (K+1) for _ in range(K+1)]
for i in range(1, K+1):
    for j in range(i, K+1):
        gcds[i][j] = gcds[j][i] = gcd(i, j)

ans = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        g = gcds[i][j]
        for k in range(1, K+1):
            ans += gcds[g][k]

print(ans)

Submission status: Accepted

Solution code:
K = int(input())
from fractions import gcd
from math import ceil
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations

sum_gcd = [0]*K
for i in range(1,K+1):
    c = 0
    for j in range(1,K+1):
        c += gcd(i,j)
    sum_gcd[i-1] = c
ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        ans += sum_gcd[gcd(a,b)-1]

print(ans)

Submission status: Accepted

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

import sys
import pprint
from math import gcd


sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(1)
	k = int(sys.stdin.readline())
	s = 0
	for a in range(1, k+1):
		for b in range(a, k+1):
			for c in range(b, k+1):
				if len(set([a, b, c])) == 3:
					s += gcd(gcd(a, b), c) * 6
				elif len(set([a, b, c])) == 2:
					s += gcd(gcd(a, b), c) * 3
				elif len(set([a, b, c])) == 1:
					s += gcd(gcd(a, b), c)
	print(s)
	


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

sum_ = 0

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

print(sum_)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

K = int(input())

g = [[0] * 201 for i in range(201)]

for i in range(201):

    for j in range(201):

        g[i][j] = math.gcd(i,j)

ans = 0

for i in range(1,K+1):

    for j in range(1,K+1):

        for x in range(1,K+1):

            ans += g[g[i][j]][x]

            #print (i,j,x,g[g[i][j]][x])

print (ans)

Submission status: Accepted

Solution code:
from math import gcd
from itertools import product

def main():
    K = int(input())
    T = [[gcd(a, b) for b in range(K+1)] for a in range(K + 1)]

    return sum(T[T[a][b]][c] for a, b, c in product(range(1, K + 1), repeat=3))

print(main())

Submission status: Accepted

Solution code:
k = int(input())
# s = input()
# n,m = map(int, input().split())
# x = list(map(int, input().split))
import math 
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
count = 0
for i in range(1,k+1):
    for j in range(1, k+1):
        for l in range(1,k+1):
            count += gcd(i,j,l)

print(count)

Submission status: Accepted

Solution code:
import math

K = int(input())
count = {}
for a in range(1, K + 1):
    for b in range(1, K + 1):
        d = math.gcd(a, b)
        if d in count:
            count[d] += 1
        else:
            count[d] = 1
res = 0
for c in range(1, K + 1):
    for d, cnt in count.items():
        res += math.gcd(c, d) * cnt
print(res)

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:
def gcd(a,b):
	while(b):
		a,b = b,a%b
	return a
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,gcd(b,c))

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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


K = int(input())
cnt = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            L = [i,j,k]
            cnt += gcd(*L)
print(cnt)

Submission status: Accepted

Solution code:
import sys
from math import gcd
def input(): return sys.stdin.readline().rstrip()

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

if __name__ == '__main__':
    main()

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):
                total += gcd(gcd(a,b),c)
    print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
import math
from functools import reduce

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

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

N = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
#!python3

# input
K = int(input())


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


def main():
    ans = 0
    d = {}
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            l = (i, j) if i > j else (j, i)
            if l in d:
                x = d[l]
            else:
                x = gcd(l[0], l[1])
                d[l] = x
            for k in range(1, K + 1):
                l = (x, k) if x > k else (k, x)
                if l in d:
                    y = d[l]
                else:
                    y = gcd(l[0], l[1])
                    d[l] = y
                ans += y
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
k = int(input())
ans = 0
import math
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:


from math import sqrt,ceil,gcd
        
        
        
# t=int(input())

# n,m=map(int,input().split())

# a=list(map(int,input().split()))

arr=[[0 for i in range(201)] for j in range(201)]
for i in range(1,201):
    for j in range(1,201):
        arr[i][j]=gcd(i,j)



a=int(input())
b=a
c=a
ans=0
for i in range(1,a+1):
    for j in range(1,b+1):
        for k in range(1,c+1):
            x=arr[i][j]
            y=arr[x][k]
            ans+=y
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import math
sys.setrecursionlimit(1000000)

cache = {}
def solve(K):
    s = 0
    for a in range(1, K+1):
        cache[a] = 0
        for b in range(1, K+1):
            cache[a] += math.gcd(a, b)

    for a in range(1, K+1):
        for b in range(1, K+1):
            n = math.gcd(a, b)
            s += cache[n]
    return s

if __name__ == "__main__":
    N = int(input())
    print(solve(N))

Submission status: Accepted

Solution code:
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def fb(i):
    if i % 15 == 0 or i % 3 == 0 or i % 5 == 0:
        return True
    else:
        return False


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    ret = 0
    for n in range(1, N+1):
        for m in range(1, N + 1):
            for l in range(1, N + 1):
                ret += math.gcd(math.gcd(n,m),l)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def gcd(a,b):
	if(a==0 or b==0):
		return a+b
	return gcd(b,a%b)
yo = input()
if(yo[-1]=='\n'):
	yo=yo[:-1]
k=int(yo)
ans=0
for i in range(1,k+1):
	for j in range(1,k+1):
		hcf = gcd(i,j)
		for m in range(1,k+1):
			ans+=(gcd(hcf,m))
print(ans)

Submission status: Accepted

Solution code:
import math

def main():
    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 += math.gcd(math.gcd(a, b), c)
    print(sum)
    
main()

Submission status: Accepted

Solution code:
K = int(input())

memo = dict()
def gcd(x,y):
    if (x,y) in memo:
        return memo[(x,y)]
    if y==0: return x
    memo[(x,y)]=gcd(y,x%y)
    return memo[(x,y)]

ans = 0
gs = dict()
for a in range(1,K+1):
    for b in range(1,K+1):
        g = gcd(a,b)
        gs[g] = gs.get(g,0)+1
for g,n in gs.items():
    for c in range(1,K+1):
        ans += gcd(g,c)*n
print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())

ret = list()
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):
            ret.append(math.gcd(ab, c))

print(sum(ret))

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


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


def GCDs(*a):
    # リストで入力しないよう注意。
    if len(a) == 0:
        return -1  # エラー

    if len(a) == 1:
        return a[0]

    res = a[0]
    for i in range(1, len(a)):
        res = GCD(res, a[i])
    return res


def solve(K: int):
    tot = 0
    for i in range(1, K+1):
        for j in range(i, K+1):
            b = GCD(i, j)
            for k in range(j, K+1):
                c = GCD(b, k)
                if i == j == k:
                    tot += c
                elif i == j or j == k or i == k:
                    tot += 3*c
                else:
                    tot += 6*c
    print(tot)
    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:
from math import gcd




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

    sm =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):
                sm += gcd(tmp,k)
    print(sm)



if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

N = int(input())

ans = 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):
            ans += gcd(tmp, k)

print(ans)

Submission status: Accepted

Solution code:
def gcd(x,y):
    if x<y:
        x,y=y,x
    if y==0:
        return x
    else:
        return gcd(y,x%y)
    
K=int(input())
num=0
for i in range(1,K-1):
    for j in range(i+1,K):
        for k in range(j+1,K+1):
            num+=6*gcd(i,gcd(j,k))
for i in range(1,K):
    for j in range(i+1,K+1):
        num+=6*gcd(i, j)
for i in range(1,K+1):
    num+=i
print(num)

Submission status: Accepted

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

N = int(input())

s = 0
for i in range(1, N+1):
  for j in range(1, N+1):
    for k in range(1, N+1):
    	g = math.gcd(i, j)
    	g = math.gcd(g, k)
    	s += g
print(s)

Submission status: Accepted

Solution code:
# %%time
def gcd(a, b):
    if a < b :
        a, b = b, a
    while True:
        a, b = b, a % b
        if b == 0:
            return a
        
k =  int(input())
s = 0
dic = {}
for i in range(1, k+1):
    for j in range(i + 1, k+1):
        a = gcd(i, j)
        for l in range(1, k+1):
            s += gcd(a, l)
            
s = s* 2
for i in range(1, k+1):
    j = i
    a = gcd(i, j)
    for l in range(1, k+1):
        s += gcd(a, l)
print(s)