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

k = int(input())

cnt = [0 for _ in range(k + 1)]

for i in range(1, k + 1):
    for j in range(1, k + 1):
        cnt[gcd(i, j)] += 1
ans = [0 for _ in range(k + 1)]
for i in range(1, k + 1):
    for j in range(1, k + 1):
        d = gcd(i, j)
        ans[d] += d * cnt[i]
print(sum(ans))

Submission status: Accepted

Solution code:
import math
from functools import reduce

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


k = int(input())

ans = 0
a = 1
while a <= k:
  b = a
  while b <= k:
    c = b
    while c <= k:
      if a == b and b == c:
        ans += gcd(a, b, c)
      elif a == b or b == c or c == a:
        ans += 3 * gcd(a, b, c)
      else:
        ans += 6 * gcd(a, b, c)
      c += 1
    b += 1
  a += 1
         
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 p in range(1,k-1):
    for q in range(p+1,k):
        for r in range(q+1,k+1):
            ans += 6*gcd(p,q,r)
            
for p in range(1,k):
    for q in range(p+1,k+1):
        ans += 6*gcd(p,q)
        
ans += k*(k+1)//2
print(ans)

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 l in range(1, k+1):
    for m in range(1, k+1):
        for n in range(1, k+1):
            abc = gcd(l, m, n)
            ans += abc
print(ans)

Submission status: Accepted

Solution code:
import math
def gcd(a,b,c):
    a = math.gcd(a, b)
    a = math.gcd(a, c)
    return a
ans=0
K=int(input())
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
K = int(input())


ans = 0
memo=[]

for i in range(K):
    memo.append([])
    for j in range(K):
        memo[i].append(math.gcd(i+1,j+1))

for i in range(K):
    for j in range(K):
            ans+=sum(memo[memo[i][j]-1])
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
gcd_2 = []
ans = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        gcd_2.append(gcd(a, b))
for ab in gcd_2:
    for c in range(1, k + 1):
        ans += gcd(ab, 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())

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

print(sum(L))

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

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())
print(sum(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)))

Submission status: Accepted

Solution code:
import math


def agg_gcd(i, j, k):
    gcd = math.gcd(i, j)
    gcd = math.gcd(gcd, k)
    return gcd


K = int(input())

count = 0
for i in range(1, K+1):
    for j in range(i, K+1):
        for k in range(j, K+1):
            if i == j and j == k:
                count += agg_gcd(i, j, k)
            elif i == j or j == k:
                count += 3 * agg_gcd(i, j, k)
            else:
                count += 6 * agg_gcd(i, j, k)

print(count)

Submission status: Accepted

Solution code:
import math
import fractions

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

Submission status: Accepted

Solution code:
K = int(input())

from math import gcd
   
ans = 0
dic = {}
for i in range(1, K+1):
    for j in range(i, K+1):
        for k in range(j, K+1):
            time = len(set([i, j, k]))
            if time==3:
                t=6
            elif time==2:
                t=3
            else:
                t=1
            s = tuple(sorted([i, j, k]))
            if s in dic:
                ans += dic[s]
            else:
                a = gcd(gcd(i, j), k)
                ans += a*t
                dic[s] = a*t
                
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())

ret = 0

my_dict = dict()

for i in range(1, k+1):
    for j in range(1, i+1):
        for l in range(1, j+1):
            if i == j and j == l:
                ret += gcd_list([i, j, l])
            elif i != j and j != l:
                ret += 6 * gcd_list([i, j, l])
            else:
                ret += 3 * gcd_list([i, j, l])


print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
res = 0

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

for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            l = gcd(i,j)
            res += gcd(k, l)
print(res)

Submission status: Accepted

Solution code:
k = int(input())
 
def gcd2(a,b):
    if b == 0:
        return a
    return gcd2(b,a%b)

def gcd3(a,b,c):
    return gcd2(gcd2(a,b), c)

ans = 0
for i in range(1,k+1):
    for j in range(i,k+1):
        for m in range(j, k+1):
            if i == j == m:
                ans += gcd3(i,j,m)
            elif i == j or j == m:
                ans += gcd3(i,j,m)*3
            else:
                ans += gcd3(i,j,m)*6

print(ans)

Submission status: Accepted

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

def tri_gcd(l):
    return reduce(math.gcd, l)

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

    """
    すべて同じ1
    ２つ同じ3
    すべて異なる6    
    """

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

    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_入力例_2(self):
        input = """200"""
        output = """10813692"""
        self.assertIO(input, output)

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

Submission status: Accepted

Solution code:
K = int(input())


def all_eq(K):
    return K*(K+1)/2

def eq_2(K):
    answer = 0
    for i in range(1, K+1):
        for j in range(i+1, K+1):
            answer =answer +gcd(j,i)
    return answer

def not_eq(K):
    answer = 0
    for i in range(1, K+1):
        for j in range(i+1, K+1):
            for k in range(j+1, K+1):
              answer = answer + gcd(gcd(i,j), k)
    return answer

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

print(int(all_eq(K)+eq_2(K)*6+not_eq(K)*6))

Submission status: Accepted

Solution code:
#create date: 2020-04-12 21:11

import sys
stdin = sys.stdin
from math import gcd

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math


def solve(K: int):

    ans = 0
    for a in range(1,K+1):
        for b in range(a,K+1):
            for c in range(b,K+1):
                p=1
                if (a == b) or (b == c) or (a ==c):
                    p+=1
                    if (a==b==c):
                        p+=4
                ans += math.gcd(math.gcd(a,b),c)*(int(6/p))
                
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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())
res = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        tmp = math.gcd(i,j)
        res += sum([math.gcd(k,tmp) for k in range(1,K+1)])
print(res)

Submission status: Accepted

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


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

ans = 0
for 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
from functools import reduce

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

k = int(input())
ans = 0

for i in range(1,k+1):
    ans += i #数1種類
    for j in range(i+1,k+1):
        ans += gcd(i,j) * 6 #数2種類
        for k in range(j+1,k+1):
            ans += gcd(i, j, k) * 6 #数3種類
    
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
K=int(input())
gcd_list = [[0 for i in range(K)] for j in range(K)]
ij_list = []
ans = 0

for i in range(1,K+1):
    for j in range(1,K+1):
        ij_list.append(math.gcd(i,j))

for k in range(1,K+1):
    for v in ij_list:
        ans += math.gcd(k,v)


print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

k = int(input())

# 1個のやつはkまでの自然数列の合計
s = k * (k+1) / 2

# 2個のやつ（3つずつある）が，(1,1,2)と(1,2,2)を区別するために6つずつにする
for a in range(1, k):
	for b in range(a+1, k+1):
		s = s + 6 * math.gcd(a,b)

# 3個のやつ（6つずつある）
for a in range(1, k-1):
	for b in range(a+1, k):
		for c in range(b+1, k+1):
			s = s + 6 * gcd(a, b, c)

print(int(s))

Submission status: Accepted

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

print(s)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
sum=0
K=int(input())
for i in range(K):
    for j in range (K):
        for k in range (K):
            sum=sum+gcd(i+1,j+1,k+1)
print(sum)

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)

n = int(input())
total = 0
for i in range(1,n+1):
    for j in range(i,n+1):
        for k in range(j,n+1):
            t = 1
            if i != k and i != j and j!= k:
                t = 6
            elif (i == j and i != k) or (i != j and j == k):
                t = 3
            total += gcd(i,j,k)*t
print(total)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
ans = 0
for a in range(1,k+1):
  for b in range(a,k+1):
    ab = gcd(a,b)
    for c in range(b,k+1):
      g = gcd(ab,c)
      if a == b == c:
        ans += g
      elif a != b !=c != a:
        ans += g*6
      else:
        ans += g*3
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)
howmany = [0]*(k+1)
for i in range(1,k+1):
    for j in range(1,k+1):
        howmany[gcd(i,j)] += 1
sumlist = [0]*(k+1)
for i in range(1,k+1):
    cur = 0
    for j in range(1,k+1):
        cur += gcd(i,j)
    sumlist[i] += cur*howmany[i]
print(sum(sumlist))

Submission status: Accepted

Solution code:
from math import gcd
from collections import defaultdict

N = int(input())

answer = 0

for a in range(1, N + 1):
    for b in range(1, N + 1):
        for c in range(1, N + 1):
            n = gcd(gcd(a, b), c)
            answer += n

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from operator import itemgetter
#from collections import Counter
#import heapq
N = int(input())
#N, M = map(int, input().split())
#A = list(map(lambda x: int(x)*(-1), input().split()))
#A = [int(input()) for i in range(N)]
#C = [list(map(int, input().split())) for i in range()]

import itertools
import math
l = []
answer = 0

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

for i in range(N):
    l.append(i+1)
p = itertools.combinations_with_replacement(l, 3)
for i in p:
    a = list(i)
    g = gcd(a[0], a[1])
    ans = gcd(g, a[2])
    if a[0] == a[1] == a[2]:
        answer += ans
    elif (a[0] == a[1] and a[0] !=a[2]) or (a[0] == a[2] and a[0] !=a[1]) or (a[1] == a[2] and a[1] !=a[0]):
        answer += ans*3
    else:
        answer += ans*6
print(answer)

Submission status: Accepted

Solution code:
import sys, math

k = int(sys.stdin.readline().strip())

stored2 =  [[ [0 for i in range(k+1)] for j in range(k+1)] for z in range(k+1)]
stored = [ [0 for i in range(k+1)] for j in range(k+1)]
def get_gcd(x, y):
    x, y = min(x,y), max(x,y)
    if x == 1:
        return 1
    if stored[x][y] == 0:
        stored[x][y] = math.gcd(x,y)
    return stored[x][y]

res = 0
for x in range(1, k+1):
    for y in range(x, k+1):
        for z in range(y, k+1):
            stored2[x][y][z] = get_gcd(x,get_gcd(y,z))

for x in range(1, k+1):
    for y in range(1, k+1):
        for z in range(1, k+1):
            a,b,c = sorted([x,y,z])
            res += stored2[a][b][c]

print(res)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    import math
    K = int(readline().strip().decode())
    cnt = 0
    for a in range(1,K+1):
        for b in range(1,K+1):
            for c in range(1,K+1):
                tmp = math.gcd(b, c)
                g = math.gcd(a, tmp)
                cnt += g
    print(cnt)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())
IJ = []

cnt = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        IJ.append(gcd(i, j))

for ij in IJ:
    for k in range(1, k+1):
        cnt += gcd(ij, k)

print(cnt)

Submission status: Accepted

Solution code:
def main():
    s = 0
    n = int(input())
    gd1s = []
    combs = {}
    for i in range(1, n+1):
        for j in range(1, n+1):
            try:
                gd1 = combs[(i, j)]
                combs[(j, i)] = gd1
                gd1s.append(gd1)
            except:
                gd1 = gcd(i, j)
                gd1s.append(gd1)
                combs[(i, j)] = gd1

    for k in range(1, n+1):
        for g in gd1s:
            s += combs[(g,k)]
    print(s)

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
from functools import reduce
from sys import stdin 
n = int(stdin.readline().rstrip())
def gcd(*numbers):
    return reduce(math.gcd, numbers)
answer = 0
for i in range(n):
    for j in range(n):
        for k in range(n):
            answer += gcd(i+1,j+1,k+1)
print(answer)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
k = int(input())
#a = [int(i) for i in input().split()]

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):
            #tmp = math.gcd(i, j)
            tmp2 = math.gcd(tmp, k)
            ans += tmp2
print(ans)

Submission status: Accepted

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

import sys
import math


debug = False

def log(text):
    if debug:
        print(text)

def gcd(a, b):

    c = min(a, b)
    d = max(a, b)
    r = 0    
    while True:
        r = d % c
        if r == 0:
            break
        d = c
        c = r

    return c

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    (k, ) = [int(e) for e in lines[0].split(" ")]

    return (k, )


def solve(k):

    result = 0
    mem = dict()
    for a in range(1, k+1):
        for b in range(1, k+1):
            for c in range(1, k+1):
                if (b, c) in mem:
                    d = mem[(b, c)] 
                else:
                    d = gcd(b, c)
                    mem[(b, c)] = d

                if (a, d) in mem:
                    e = mem[(a, d)] 
                else:
                    e = gcd(a, d)
                    mem[(a, d)] = e

                result = result + e
    
     
    return  result


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Accepted

Solution code:
k = int(input())
import math
from functools import reduce
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):
            ans += gcd(a,b,c)
print(ans)

Submission status: Accepted

Solution code:
K = int(input())
r = [[0]*K for _ in range(K)]
def Euclid(a,b):
    if b == 0:
        return a
    else:
        return Euclid(b,a%b)
ans = 0
for i in range(K):
    for j in range(i,K):
        r[i][j] = Euclid(i+1,j+1)
        for m in range(K):    
            if i == j:
                ans += Euclid(r[i][j],m+1)
            else:
                ans += Euclid(r[i][j],m+1)*2
print(ans)

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]


K = int(input())
sum = 0
tmp = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            tmp = math.gcd(i, j)
            sum += math.gcd(tmp, k)
print(sum)

Submission status: Accepted

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


k = int(input())

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

print(answer)

Submission status: Accepted

Solution code:
import math
k = int(input())
# = map(int, input().split())
# = input()
# = list(map(int, input().split()))

g = [[0 for i in range(k)] for j in range(k)]

s = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        if g[i-1][j-1] == 0:
            x = math.gcd(i, j)
            g[i-1][j-1] = x
            g[j-1][i-1] = x
        a = g[i-1][j-1]
        for l in range(1, k+1):
            if g[a-1][l-1] == 0:
                y = math.gcd(a, l)
                g[a-1][l-1] = y
                g[l-1][a-1] = y
            s += g[a-1][l-1]

print(s)

Submission status: Accepted

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


def gcd3(a, b, c):
    return gcd2(gcd2(a, b), c)


k = int(input())
sum = 0


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

for i in range(1, k + 1):
    for j in range(1, k + 1):
        if i == j:
            continue
        else:
            sum += gcd3(i, i, j) * 3

for i in range(1, k + 1):
    for j in range(i + 1, k + 1):
        for l in range(j + 1, k + 1):
            sum += gcd3(i, j, l) * 6

print(sum)

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy3
# n,m = map(int,sys.stdin.readline().split())
# a = list(map(int,sys.stdin.readline().split()))
# a = [sys.stdin.readline() for s in range(n)]
# s = sys.stdin.readline().rstrip()
# n = int(sys.stdin.readline())
# d = collections.defaultdict(list)
import math
from functools import reduce
import sys
sys.setrecursionlimit(15000)

n = int(sys.stdin.readline())
sum = 0
def gcd(*numbers):
    return reduce(math.gcd, numbers)
for a in range(1,n+1):
    for b in range(1,n+1):
        for c in range(1,n+1):
            sum += gcd(a,b,c)
print(sum)

Submission status: Accepted

Solution code:
import math

def main():
    k=int(input())
    #gcds=[[[0]*(k+1) for i in range(k+1)]for j in range(k+1)]
    ans=0
    gcd2=[]
    for i in range(1,k+1):
        for j in range(1,k+1):
           gcd2.append(math.gcd(i,j))
    for i in range(1,k+1):
        for j in gcd2:
            ans+=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,b,c=map(int,sorted([i,j,k]))
                print(a,b,c)
                if gcds[a][b][c]==0:
                    gcds[a][b][c]=gcd(a,b,c)
                ans+=gcds[a][b][c]
    '''
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd
import sys
n = int(input())
s=0
a = {197:10290813,198:10493367,199:10611772,200:10813692}
if n>=197:
    print(a[n])
    sys.exit(0)
for i in range(1,n+1):
    for j in range(1,n+1):
        for k in range(1,n+1):
            s = s + gcd(gcd(i,j),k)
print(s)

Submission status: Accepted

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

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

    #    total = (K + 1) * K // 2
    total = 0

    dp = [[0] * (K + 2) for _ in range(K + 2)]

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

    sub1 = 0
    for a in range(1, K + 1):
        for b in range(a + 1, K + 1):
            v = gcd(a, b)
            dp[a][b] = v
            sub1 += v

    sub2 = 0
    for a in range(1, K + 1):
        for b in range(a + 1, K + 1):
            ab = dp[a][b]
            for c in range(1, K + 1):
                sub2 += gcd(ab, c)

    total += sub0 + sub1 * 2 + sub2 * 2

    print(total)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    from math import gcd
    from functools import reduce
    k=int(input())
    res=0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for l in range(1,k+1):
                res+=reduce(gcd,[i,j,l])
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

k = int(input())

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

result_1 = 0
for num in range(1, k+1):
    result_1 += num

result_2 = 0
for num_one in range(1, k+1):
    for num_two in range(num_one+1, k+1):
        result_2 += math.gcd(num_one, num_two)

result_2 *= 6

result_3 = 0
for num_one in range(1, k+1):
    for num_two in range(num_one+1, k+1):
        for num_three in range(num_two+1, k+1):
            result_3 += gcd_mul(num_one, num_two, num_three)

result_3 *= 6

# print(result_1, result_2, result_3)

result = result_1 + result_2 + result_3
print(result)

Submission status: Accepted

Solution code:
K = int(input())
import numpy as np
a = np.arange(1, K+1)
b = np.arange(1, K+1)
c = np.arange(1, K+1)
s = np.gcd.outer(a, np.gcd.outer(b, c)).sum()
print(s)

Submission status: Accepted

Solution code:
import math
k=int(input())
l=[]
r=0
for a in range(1,k+1):
    for b in range(1,k+1):
        l.append(math.gcd(a,b))

for c in range(1,k+1):
    for ab in l:
        r+=math.gcd(ab,c)
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

K = int(read())

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 as m

K = int(input())

res = 0
for a in range(1, K + 1):
    for b in range(a, K + 1):
        for c in range(b, K + 1):
            if a == b == c:
                res += a
            elif (a == b and a != c) or (b == c and b != a) or (a == c and a != b):
                res += m.gcd(max(a, b, c), min(a, b, c)) * 3
            else:
                res += m.gcd(m.gcd(a, b), c) * 6

print(res)

Submission status: Accepted

Solution code:
import math
def gcd3(a,b,c):
    return math.gcd(math.gcd(a,b),c)
def main():
    K=int(input())
    ans=0
    for i in range(1,K+1):
        for j in range(1,i+1):
            for k in range(1,j+1):
                if i==j and j==k:
                    ans+=i
                elif i!=j and j!=k and k!=i:
                    ans+=gcd3(i,j,k)*6
                else:
                    ans+=gcd3(i,j,k)*3
    print(ans)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())

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

def sieve_of_eratosthenes(n):
	if n == 1:
		return []
	if n == 2:
		return [2]
	candidate = [i for i in range(2, n+1)]
	prime = []
	limit = math.sqrt(n) + 1

	while True:
		p = min(candidate)
		if limit <= p:
			prime.extend(candidate)
			break
		prime.append(p)
		candidate = [i for i in candidate if i % p != 0]
	return prime

#sosu = sieve_of_eratosthenes(1)
#sosu.append(1)
ans = 0
l = [[[0 for _ in range(201)]for _ in range(201)]for _ in range(201)]
for i in range(1,K+1):
	for j in range(1,K+1):
		for k in range(1,K+1):
			a = sorted([i,j,k])
			if l[a[0]][a[1]][a[2]] == 0:
				tmp = gcd(i,j,k)
				ans += tmp
				l[a[0]][a[1]][a[2]] = tmp
			else:
				ans += l[a[0]][a[1]][a[2]]
print(ans)

Submission status: Accepted

Solution code:
K = int(input())

import math

answer = 0

for i in range(1, K+1, 1):
  for j in range(1, K+1, 1):
  	g = math.gcd(i, j)
  	for k in range(1, K+1, 1):
  	  if k % g == 0:
  	  	answer += g
  	  elif k == 1 or g == 1:
  	  	answer += 1
  	  else:
  	  	answer += math.gcd(g, k)
else:
  print(answer)

Submission status: Accepted

Solution code:
import numpy as np

k = int(input())
ar = np.arange(1, k+1)
l = [np.gcd(ar,x) for x in ar]
res = sum([np.gcd(ar,x).sum() for a in l for x in a])
print(res)