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

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools

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

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

sum = 0
Y =  int(input())

for v in itertools.combinations_with_replacement(range(1,Y+1), 3):
    if v[0] ==v[1] and v[1] == v[2]:
        sum += (gcd(v[0],v[1],v[2]))
    elif v[0] == v[1] or v[1] == v[2] or v[0] == v[2]:
        sum += (gcd(v[0],v[1],v[2])*3)
    elif v[0] != v[1] and v[1] != v[2]:
        #print("2")
        sum += (gcd(v[0],v[1],v[2])*6)
    else:
        print("nn")
           #print(gcd(i,j,k))
    #print("---------------------")

 

print(sum)

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

Submission status: Accepted

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

l = [[-1 for _ in range(k+1)] for _ in range(k+1)]

for i in range(1,k+1):
  for j in range(1,k+1):
     if l[i][j] == -1:
        ans = fractions.gcd(i,j)
        l[i][j] = ans
        l[i][i] = ans
sum = 0
#1種類
for i in range(1,k+1):
  sum += i
#２種類
for i in range(1,k):
  for j in range(i+1,k+1):
    sum += l[i][j]*6

#3種類
for i in range(1,k-1):
  for j in range(i+1,k):
    for m in range(j+1,k+1):
      sum += l[l[i][j]][m]*6

print(sum)

Submission status: Accepted

Solution code:
import math
 
K = int(input())
summ = 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)
            summ+=math.gcd(tmp,k)
 
print(summ)

Submission status: Accepted

Solution code:
# https://stackoverflow.com/questions/29194588/python-gcd-for-list

from math import gcd
from functools import reduce
from itertools import combinations

K = int(input())

def find_gcd(list):
    x = reduce(gcd, list)
    return x
  
s = 0
for i in range(3):
    if i == 0:
        s += sum(map(find_gcd, combinations(range(1, K + 1), i + 1)))
    else:
        s += sum(map(find_gcd, combinations(range(1, K + 1), i + 1))) * 6
print(s)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
total = 0
ab_gcd = []
for a in range(1,k+1):
    for b in range(1,k+1):
        ab_gcd.append(gcd(a,b))
for ab in ab_gcd:
    for c in range(1,k+1):
        abc = gcd(ab,c)
        total += abc
print(total)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:

k = int(input())

import math
from functools import reduce

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

Sum = 0

# 一種類の数字が出てくるパターン
for i in range(k):
    Sum += gcd(i+1, i+1, i+1)

# 二種類の数字が出てくる場合 -> 各パターンは3通りあるはず
for i in range(k-1):
    for j in range(i+1, k):
        Sum += gcd(i+1, j+1, j+1)*3

# 二種類の数字が出てくる場合 
for i in range(k-1):
    for j in range(i+1, k):
        Sum += gcd(i+1, i+1, j+1)*3
        
# 三種類の数字が出てくる場合
for i in range(k-2):
    for j in range(i+1, k-1):
        for m in range(j+1, k):
            Sum += gcd(i+1, j+1, m+1)*6
            
print(Sum)

Submission status: Accepted

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


print(g)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import fractions, math
from functools import reduce

def solve(K: int):
    counter = 0
    L = [0] * 201
    for i in range(1, K+1):
        for j in range(1, K+1):
            L[math.gcd(i, j)] += 1
    for i in range(1, K+1):
        for j in range(1, K+1):
            counter += math.gcd(i, j) * L[j]
    print(counter)
    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:
def gcd(a,b):
    while b:
        a,b=b,a%b
        
    return a
    
ans=0
    
K=int(input())
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(k,gcd_)
            
print(ans)

Submission status: Accepted

Solution code:
import math
k = int(input())
box = []
for i in range(1,k+1):
  for j in range(1,k+1):
    gap = math.gcd(i,j)
    if gap == 1:
      box.append(1*k)
    else:
      for l in range(1,k+1):
        gap2 = math.gcd(gap,l)
        box.append(gap2)        
print(sum(box))

Submission status: Accepted

Solution code:
class GCD:
    def __init__(self):
        self.numlist = dict()
    def culc(self, K):
        ret = 0
        
        for i in range(1,K):
            for j in range(i + 1,K+1):
                ret += self.gcd_(i,j) * 6

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

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

        return ret

    def gcd(self, a,b,c):
        #l = (a,b,c)
        #if l in self.numlist:
        #    return self.numlist[l]
        #else:
        temp_ = self.gcd_(a,b)
        temp_ = self.gcd_(temp_, c)
        
        return temp_

    def gcd_(self,a,b):
        l = (min(a,b), max(a,b))
        if l in self.numlist:
            return self.numlist[l]
        else:
            mod = max(a,b) % min(a,b)
            if mod == 0:
                ret = min(a,b)
                self.numlist[l] = ret
                return ret
            else:
                ret = self.gcd_(min(a,b), mod)
                self.numlist[l] = ret
                return ret

K = int(input())
gcd = GCD()
ret = gcd.culc(K)
print(ret)

Submission status: Accepted

Solution code:
import math
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):
            ab = math.gcd(a,b)
            total += math.gcd(ab, c)
print(total)

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
import math

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

import time
from functools import wraps


def stop_watch(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        elapsed_time = time.time() - start
        print(f'{elapsed_time} [ms]')
        return result

    return wrapper


def gcd(a, b):
    """
    Greatest Common Divisor
    Euclidean Algortihm
    """
    if a < b:
        gcd(b, a)
    while b != 0:
        a, b = b, a % b
    return a


def is_prime(n):
    if n < 2:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False

    m = math.floor(math.sqrt(n)) + 1
    for p in range(3, m, 2):
        if n % p == 0:
            return False
    return True


def main():
    K = int(input())
    num_list = range(1, K + 1)

    total = 0

    for a in range(1, K + 1):
        if a == 1:
            total += K ** 2
            continue
        for b in range(1, K + 1):
            if b == 1:
                total += K
                continue
            tmp = gcd(a, b)
            if tmp == 1:
                total += K
                continue
            tmp_b = is_prime(tmp)
            for c in range(1, K + 1):
                if c == 1:
                    total += 1
                    continue
                if tmp_b:
                    if c % tmp == 0:
                        total += tmp
                        continue
                    else:
                        total += 1
                        continue
                else:

                # print(a, b, c)
                # print(gcd(tmp, c))
                    total += gcd(tmp, c)
    print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

def get_gcd(a, b, c):
    min_ab = min(a, b)
    max_ab = max(a, b)
    amari = max_ab % min_ab
    if amari == 0:
        gcd1 = min_ab
    else:
        x = min_ab
        y = amari
        while y != 0:
            amari = x % y
            if amari == 0:
                gcd1 = y
                break
            x = y
            y = amari
    min_2 = min(c, gcd1)
    max_2 = max(c, gcd1)
    amari = max_2 % min_2
    if amari == 0:
        gcd2 = min_2
    else:
        x = min_2
        y = amari
        while y != 0:
            amari = x % y
            if amari == 0:
                gcd2 = y
                break
            x = y
            y = amari

    return gcd2


k = int(input())
ans = 0

for a in range(1, k+1):
    for b in range(a+1, k+1):
        if b == a:
            continue
        for c in range(b+1, k+1):
            if c == a or c == b:
                continue
            ans += get_gcd(a, b, c) * 6

for a in range(1, k+1):
    b = a
    for c in range(1, k+1):
        if c == a:
            continue
        ans += get_gcd(a, b, c) * 3

for a in range(1, k+1):
    b = a
    c = a
    ans += get_gcd(a, b, c)

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
k=int(input())
l=[]
dp=[[0]*(k+1) for i in range(k+1)]

for i in range(1,k+1):
    for j in range(1,k+1):
        dp[i][j]=gcd(i,j)
        l.append(gcd(i,j))
sum=0
for x in range(1,k+1):
    for y in range(len(l)):
        sum += int(dp[l[y]][x])
print(sum)

Submission status: Accepted

Solution code:
import numpy as np
import math
K = int(input())
memo = np.zeros((K,K),int)
s = 0
for i in range(K):
  for j in range(i,K):
    for k in range(j,K):
      if i == j and j == k:
        rate = 1
      elif i == j or j == k or i == k:
        rate = 3
      else:
        rate = 6
      s += math.gcd(math.gcd(i+1,j+1),k+1) * rate
print(s)

Submission status: Accepted

Solution code:
K = int(input())

def gcd(n,m,l):
    if n < m :
        n , m = m , n
    while n % m != 0 :
        n , m = m , n % m
    
    if m < l :
        m , l = l , m
    while m % l != 0 :
        m , l = l , m % l
    
    return l
  
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(a, b, c)

print ( S )

Submission status: Accepted

Solution code:
import math

k =int(input())
ans = 0
temp_list =[]


for i in range(k):
    for j in range(k):
        temp_list.append(math.gcd(i+1,j+1))

temp_set= list(set(temp_list))

memo = [0]*len(temp_set)

for i in temp_list:
    for j in range(len(temp_set)):
        if i == temp_set[j]:
            memo[j] += 1
            break

for i in range(k):
    for j in range(len(temp_set)):
        ans += memo[j] * math.gcd(temp_set[j],i+1)

print(ans)

Submission status: Accepted

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


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

    ans = 0
    for a in range(1,K+1):
        for b in range(a,K+1):
            ab = gcd(b,a)
            for c in range(b,K+1):
                n = 6
                if a==b or b==c: n = 3
                if a==b and b ==c : n=1
                ans += gcd(c,ab)*n

    print(ans)


    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions


num = int(stdin.readline().rstrip())
count = 0
arr = []
for a in range(1, num + 1):
    for b in range(1, num + 1):
        arr.append(math.gcd(a, b))
for c in range(1, num + 1):
    for d in arr:
        count += math.gcd(c, d)
print(count)

Submission status: Accepted

Solution code:
K = list(range(1,int(input())+1))

import itertools

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

result = 0


if len(K)>=3:
    for i in list(itertools.combinations(K,3)):
        result += gcd(gcd(i[0],i[1]),i[2])*6

if len(K)>=2:
    for i in list(itertools.combinations(K,2)):
        result += gcd(i[0],i[1])*6

for i in list(itertools.combinations(K,1)):
    result += i[0]

print(result)

Submission status: Accepted

Solution code:
k=int(input())
list=[0, 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(list[k])

Submission status: Accepted

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


def gcd3(cmb):
    return reduce(gcd, cmb)


k = int(input())
s1 = sum(range(1, k + 1))
s2 = 0
cmb2 = combinations(range(1, k + 1), 2)
for i, j in cmb2:
    s2 += gcd(i, j)
s3 = 0
cmb3 = combinations(range(1, k + 1), 3)
for i in cmb3:
    s3 += gcd3(i)

print(s1 + s2 * 6 + s3 * 6)

Submission status: Accepted

Solution code:
import math

k = int(input())

ans = 0
for i in range(1,k+1):
    for j in range(i,k+1):
        for h in range(j,k+1):
            if i == j and j == h:
                ans += math.gcd(math.gcd(i,j),h)
            elif i == j and j != h:
                ans += math.gcd(math.gcd(i,j),h) * 3
            elif i != j and j == h:
                ans += math.gcd(math.gcd(i,j),h) * 3
            else:
                ans += math.gcd(math.gcd(i,j),h) * 6


print(ans)

Submission status: Accepted

Solution code:
from __future__ import division, print_function

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

	print(ans)

py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange
 
import os, sys
from io import IOBase, BytesIO
 
BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None
 
    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
 
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()
 
    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s:self.buffer.write(s.encode('ascii'))
            self.read = lambda:self.buffer.read().decode('ascii')
            self.readline = lambda:self.buffer.readline().decode('ascii')
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
 
# Cout implemented in Python
import sys
class ostream:
    def __lshift__(self,a):
        sys.stdout.write(str(a))
        return self
cout = ostream()
endl = '\n'
 
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero = 0):
    conv = ord if py2 else lambda x:x
    A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
    try:
        while True:
            if s[i] >= b'0' [0]:
                numb = 10 * numb + conv(s[i]) - 48
            elif s[i] == b'-' [0]: sign = -1
            elif s[i] != b'\r' [0]:
                A.append(sign*numb)
                numb = zero; sign = 1
            i += 1
    except:pass
    if s and s[-1] >= b'0' [0]:
        A.append(sign*numb)
    return A
 
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

k = int(input())
num = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        for c in range(b,k+1):
          x = gcd(a,b,c)
          bai = sum([a==b,b==c,a==c])
          if bai == 0:
            num += x*6
          elif bai == 1:
            num += x*3
          else:
            num += x
            
print(num)

Submission status: Accepted

Solution code:
import numpy as np


n = int(input())
table = [[0 for i in range(n+10)] for j in range(n+10)]

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

ab_gcd = [0 for i in range(n+10)]
for i in range(1, n+1):
    for j in range(1, n+1):
        ret = gcd(i, j)
        table[i][j] = ret
        ab_gcd[ret] += 1

table = np.array(table)
ans = 0
for i in range(n+10):
    if ab_gcd[i] != 0:
        ans += int(np.sum(table[i, :]))*ab_gcd[i]
print(ans)

Submission status: Accepted

Solution code:
def kk(n,m):
    if n<m:
        n,m=m,n
    while  n%m!=0:
        n=n%m
        n,m=m,n
    return m


k= int(input())
count=0
for p in range(1,k+1):
    for q in range(p,k+1):
        pq=  kk(p,q)
        for r in range(q,k+1):
            pqr= kk(pq,r)
            if p==r:
                 pqr=pqr
            elif p==q or q==r:
                pqr=pqr*3
            else:
                pqr=pqr*6
            count= count+ pqr
print(count)

Submission status: Accepted

Solution code:
import math
from functools import reduce


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


n = int(input())

ans = 0

for i in range(1, n + 1):
    for j in range(i, n + 1):
        for k in range(j, n + 1):
            cnt = gcd(i, j, k)
            if i == j and j == k:
                ans += cnt
            elif (i == j and i != k) or (i == k and i != j) or (j == k and j != i):
                ans += cnt * 3
            else:
                ans += cnt * 6

print(ans)

Submission status: Accepted

Solution code:
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)

if __name__ == "__main__":
    k = int(input())
    
    ans = 0

    for a in range(1, k + 1):
        for b in range(1, a + 1):
            for c in range(1, b + 1):
                if a == b and b == c and c == a:
                    ans += gcd3(a, b, c)
                elif a == b or b == c or c == a:
                    ans += gcd3(a, b, c) * 3
                else:
                    ans += gcd3(a, b, c) * 6
    
    print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

k = int(input())
total = 0

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import itertools as it
import numpy as np
from collections import Counter
d = {}
def _gcd(a,b):
    a, b = max(a,b), min(a,b)
    while b:
        if d.get((a, b), False):
            return d[(a, b)]
        else:
            a, b = b, a%b

    d[(a, b)] = a
    return a

# def _gcd(a,b):
#     a, b = max(a,b), min(a,b)
#     if b == 0:
#         d[(a, b)] = a
#         return a
#     elif d.get((a, b), False):
#         return d[(a, b)]
#     else:
#         ans = _gcd(b, a%b)
#         d[(a, b)] = ans
#         return ans

def gcd(a,b,c):
    return _gcd(a, _gcd(b,c))

def solve(K: int):
    # val_arr = np.zeros(201,201)
    # cnt_arr = np.zeros(201,201)
    r = range(1, K+1)

    arr = []
    for (a,b) in it.product(r,r):
        arr.append(_gcd(a,b))
        # val_arr[a,b] += _gcd(a,b)
        # cnt_arr[a,b] += 1

    ans = 0
    cnt_dict = Counter(arr)
    for i in r:
        for key, cnt in cnt_dict.items():
            ans += _gcd(i,key)*cnt

    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())
ansA, ansB, ansC = 0, 0, 0
for a in range(k):
    ansC += a+1
    for b in range(k-a-1):
        GCDab = math.gcd(a+1, a+b+2)
        ansB += GCDab
        for c in range(k-a-b-2):
            GCDabc = math.gcd(GCDab, a+b+c+3)
            ansA += GCDabc
ans = ansA * 6 + ansB * 6 + ansC
print(ans)

Submission status: Accepted

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

	return b

N = int(input())

ans_sum = 0

for x in range(1, N+1):
	for y in range(x, N+1):
		for z in range(y, N+1):
			if x == y == z:
				ans_sum += z
			else:
				ans = gcd(x, y)
				ans = gcd(ans, z)
				if x == y or x == z or y == z:
					ans_sum += ans * 3
				else:
					ans_sum += ans * 6

print(ans_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k=int(input())
import math
if k==1:
    print(1)
else:
    s1=k*(k+1)//2
    s2=k-1
    for i in range(2,k):
        for j in range(i+1,k+1):
                s2+=math.gcd(i,j)
    s2=s2*6
    s3=0
    if k>2:
        for a in range(1,k-1):
            for b in range(a+1,k):
                for c in range(b+1,k+1):
                    s3+=math.gcd(math.gcd(a,b),c)
        s3=s3*6
    print(s1+s2+s3)

Submission status: Accepted

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

def tri_gcd(a,b,c):
  return gcd(gcd(a,b),c)

def main():
  K = int(input())
  S = 0
  # a = b = c
  S_0 = 0
  for a in range(1,K+1):
    S_0 = S_0 + tri_gcd(a,a,a)
  S = S_0
  # a = b > c   
  S_1 = 0
  for a in range(1,K+1):
      for c in range(a+1,K+1):
          S_1 = S_1 + tri_gcd(a,a,c)
  S += 6*S_1

  # a > b > c   
  S_3 = 0
  for a in range(1,K+1):
      for b in range(a+1,K+1):
        for c in range(b+1,K+1):
            S_3 = S_3 + tri_gcd(a,b,c)
  S += S_3 * 6
  
  print(S)

  return 0
 
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
from collections import defaultdict

K = int(input())


def calcGcd(a, b):
    large = max(a, b)
    small = min(a, b)
    while (True):
        num = large % small
        large = small
        small = num
        if small == 0:
            return large


ans = 0
dd = defaultdict(int)
for i in range(1, K + 1):
    for j in range(1, K + 1):
        dd[calcGcd(i, j)] += 1

for num in dd.keys():
    for k in range(1, K + 1):
        ans += calcGcd(num, k) * dd[num]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import math
import itertools
k = int(input())
ans = 0

for a in range(1,k+1):
    for b in range(1,k+1):
        d = math.gcd(a,b)
        for c in range(1,k+1):
            e = math.gcd(d,c)
            ans += e
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
from functools import reduce


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


K = int(input())
ans = 0
for comb in itertools.combinations_with_replacement(list(range(1, K + 1)), 3):
    dup = len(set(comb))
    if dup == 1:
        ans += comb[0]
    elif dup == 2:
        ans += gcd(*comb) * 3
    elif dup == 3:
        ans += gcd(*comb) * 6
print(int(ans))

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

k = int(input())
total = 0

for a in range(1, k+1):
    for b in range(a, k+1):
        for c in range(b, k+1):
            n = gcd(a, b, c)
            uni = len(set([a, b, c]))
            if uni == 1:
                total += n
            elif uni == 2:
                total += n * 3
            else:
                total += n * 6
print(total)

Submission status: Accepted

Solution code:
import sys
import math

N = int(input())

ans = 0
for i in range (1,N+1):
    for j in range (1,i+1):
        for k in range (1,j+1):
            if i == j == k:
                ans += math.gcd(math.gcd(i,j),k)
            elif i == j or j== k:
                ans += 3*math.gcd(math.gcd(i,j),k)
            else:
                ans += 6*math.gcd(math.gcd(i,j),k)
            #print(i,j,k)
         
            
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
k=int(input())
count=0
for i in range(1,k+1):
    for j in range(i,k+1):
        for h in range(j,k+1):
            if i==j==h:
                x=gcd(i,j,h)
            elif i==j:
                x=3*gcd(i,j,h)
            elif j==h:
                x=3*gcd(i,j,h)
            else:
                x=6*gcd(i,j,h)
            count+=x
print(count)

Submission status: Accepted

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

	print(ans)
if __name__ == '__main__':
	main()

Submission status: Accepted

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

Sum1=0
for i in range(1,K+1):
    for j in range(1,i+1):
        Ans=0
        Ans=gcd(i,j)
        for k in range(1,j+1):
            if i==j==k:
                Sum1+=gcd(Ans,k)
            elif i!=j!=k:
                Sum1+=6*gcd(Ans,k)
            else:
                Sum1+=3*gcd(Ans,k)

print(Sum1)

Submission status: Accepted

Solution code:
import math

k = int(input())
kl = [i for i in range(1, k+1)]


total = 0
dp = {}
for i in kl:
    for j in kl:
        tmp = math.gcd(i, j)
        for k in kl:
            total += math.gcd(tmp, k)


print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce
import math


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


K = int(input())
gcdsum = K*K*K
gcd_ab = []
ab_list = []
for a in range(1, K + 1):
    for b in range(1, K + 1):
        #print(str(gcd(a, b))+', a:'+str(a)+', b:'+str(b))
        if gcd(a, b) != 1:
            ab_list.append(a)
            ab_list.append(b)
            gcd_ab.append(ab_list)
            ab_list = []
for ab in gcd_ab:
    ab = gcd(ab[0], ab[1])
    for c in range(1, K+1):
        if gcd(ab, c) != 1:
            #print(gcd(ab, c))
            gcdsum += gcd(ab, c)-1

print(gcdsum)

Submission status: Accepted

Solution code:
import itertools
import math


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


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


K = int(input())
data = [k+1 for k in range(K)]
result = list(itertools.combinations_with_replacement(data, 3))

s = 0

for r in result:
    r0, r1, r2 = r
    g0 = gcd(r0, r1)
    g = gcd(g0, r2)
    if r0 == r1 == r2:
        s += g
    elif r0 != r1 and r1 != r2:
        s += g * 6
    else:
        s += g * 3

print(s)