Problem:


Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692



Sample input:
2


Sample output:
9

Submission status: Accepted

Solution code:
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd,numbers)
K=int(input())
a=1
c=1
if 1<=K<=200:
    sum_abc=K*(K+1)/2
    sum_abc=int(sum_abc)
    for a in range(1,K+1):
        for c in range(1,K+1):
            if a==c:
                c+=1
            else:
                d=math.gcd(a,c)
                d=3*d
                sum_abc=sum_abc+d
                c+=1
        a+=1
    if K>=3:
        i=1
        j=2
        k=3
        for k in range(3,K+1):
            for j in range(2,k):
                for i in range(1,j):
                    e=gcd(i,j,k)
                    e=6*e
                    sum_abc=sum_abc+e
                    i+=1
                j+=1
            k+=1
    print(sum_abc)

Submission status: Accepted

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

ans = 0

for i in range(K):
    for j in range(i+1):
        for k in range(j+1):
            a, b, c = i+1, j+1, k+1
            gcd = math.gcd(math.gcd(a, b), c)
            if a != b and b != c and a != c:
                ans += gcd*6
            elif a == b == c:
                ans += gcd
            elif a==b or b==c or a==c:
                ans += gcd*3


print(ans)

Submission status: Accepted

Solution code:
def eu(a,b):
    q = -1
    if(a<b):
        tmp = a
        a = b
        b = tmp
    while(q!=0):
        r = a // b
        q = a % b
        re = b
        a = b
        b = q
    return re    
N,=map(int, input().split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
sum = 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 and j==k):
                sum+=eu(eu(i,j),k)
            elif (i==j or j==k or i==k):
                sum+=eu(eu(i,j),k)*3
            else:
                sum+=eu(eu(i,j),k)*6
print(sum)

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions

n = int(stdin.readline().rstrip())

cnt = 0
for i in range(1, n+1):
    for j in range(i, n+1):
        for k in range(j, n+1):
            a = math.gcd(i, j)
            b = math.gcd(a, k) * 6
            if i == j and j == k:
                b = int(b/6)
            elif i == j or j == k or i == k:
                b = int(b/2)
            #print(str(i) + ' ' + str(j) + ' ' + str(k) + ' : ' + str(b))
            cnt += b

print(cnt)

Submission status: Accepted

Solution code:
import math

sets = dict()

def gcd2(a, b):
    if a > b:
        key = (b, a)
    else:
        key = (a, b)
    
    try:
        # print("cached")
        return sets[key]
    except:
        # print("stored")
        ans = math.gcd(a, b)
        sets[key] = ans
        return ans

n = int(input())

ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        pre = gcd2(i, j)

        for k in range(1, n+1):
            ans += gcd2(pre, k)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = ((N-1)**2)*3  + (N-1) * 3 + 1
p = 0
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
for i in range (N-1):
    for j in range(N-1):
        k = gcd((i+2), (j+2))
        for l in range (N-1):
            if k != 1:
                m = gcd(k, (l+2))
                p += m
            else:
                p +=1
            
print(p + s)

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce

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

k = int(input())
ans = 0
rev = 0
for i in range(1, k + 1):
    for j in range(i, k + 1):
        tmp = gcd(i, j)
        for l in range(j, k + 1):
            if i == j and j == l:
                ans += i
            elif i == j or j == l or l == i:
                ans += gcd(tmp, l) * 3
            else:
                ans += gcd(tmp, l) * 6
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
from itertools import product,combinations_with_replacement

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

K = int(input())

ans = 3*K*(K-1) + 1

# for i,j,k in product(range(2,K+1),repeat=3):
#     ans += gcd_(i,j,k)
for i,j,k in combinations_with_replacement(range(2,K+1),3):
    if i==j and j==k:
        ans += i
    elif i==j or j==k or k==i:
        ans += gcd_(i,j,k) * 3
    else:
        ans += gcd_(i,j,k)*6
# for i in range(2,K+1):
#     for j in range(2,K+1):
#         for k in range(2,K+1):
#             ans += gcd_(i,j,k)

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce


n=int(input())
    
res=0
nlist=[]
for a in range(1,n+1):
    for b in range(1,n+1):
        nlist.append(math.gcd(a,b))
        
import collections

c = collections.Counter(nlist)
ck=list(c.keys())
cv=list(c.values())

cn=len(ck)

for i in range(cn):
    for c in range(1,n+1):
        res+=math.gcd(c,ck[i])*cv[i]
print(res)

Submission status: Accepted

Solution code:
#C

import math

k = int(input())
cnt = 0

for i in range(1,k+1):
    for j in range(i,k+1):
        tmp = math.gcd(i,j)
        for l in range(1,k+1):
            if i != j:
                cnt += 2 * math.gcd(tmp,l)
            else:
                cnt += math.gcd(tmp,l)
            
print(cnt)

Submission status: Accepted

Solution code:
import itertools
import fractions
import functools

N = input()
N = int(N)

l = list(itertools.combinations_with_replacement(list(range(1,N+1)), 3))
s_all = 0

def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def gcd(nums):
    return functools.reduce(euclid, nums)

for nums in l:
    count = 0
    # ans = nums[0]
    # for i in range(1, 3):
    #     ans = fractions.gcd(ans, nums[i])
    # ans = list(map(int, input().split()))
    ans = gcd(nums)
    a,b,c = nums[0], nums[1], nums[2]
    if a==b:
        count+=1
    if a==c:
        count+=1
    if b==c:
        count+=1

    if count==0:
        num = 6
    elif count==1:
        num=3
    elif count==3:
        num=1
    s_all += ans*num
print(s_all)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
from collections import deque
import math
import pprint
from functools import reduce
# stdin = open("sample.txt")

def keta(kazu):
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

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

K = int(stdin.readline().rstrip())
ans = 0
for_c = []

for b in range(1,K+1):
    for a in range(1,K+1):
        if math.gcd(a,b) == 1:
            ans += K
        else:
            for_c.append(math.gcd(a,b))

for c in range(1,K+1):
    for ab in range(len(for_c)):
        ans += math.gcd(for_c[ab],c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

n = int(input())
ans = 0

for i in range(1,n+1):
  sign = 1
  for j in range(i,n+1):
    if sign != 1:
      sign = 2
    for k in range(j,n+1):
      if sign == 1:
        judge = 1
        sign = 2
      elif sign == 2:
        judge = 3
        if i != j:
          sign = 0
      elif sign == 0:
        judge = 6
      
      ans = ans + judge*reduce(math.gcd, [i,j,k])
#      print(i,j,k)
      

print(ans)

Submission status: Accepted

Solution code:
answer = [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]
n = int(input())
print(answer[n - 1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())

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

output = 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:
        output += gcd(a, b, c)
      elif a != b and b != c and c != a:
        output += 6 * gcd(a, b, c)
      else:
        output += 3 * gcd(a, b, c)

print(output)

Submission status: Accepted

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

def saiki(K):
    if K == 1:
        return 1
    sum = 0
    for i in range(K):
        for j in range(K-1):
            sum += math.gcd(math.gcd(i+1,j+1),K)*3
    sum += K
            
    return sum+saiki(K-1)
        
    
sum = saiki(K)
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import sys
readline = sys.stdin.buffer.readline
s =int(readline().rstrip().decode())
def gcdsp(x,y,z):
    t =math.gcd(x,y)
    return math.gcd(t,z)
res = 0
for i in range(s):
    for j  in range(s):
        for k  in range(s):
            if i < j and j <= k:
                res += gcdsp(i+1, j+1, k+1)
res1 = int((s + 1)*s/2)
print(6*res+res1)

Submission status: Accepted

Solution code:
def u(a,b):
  if(a<b):
    z = b
    b = a
    a = z
  r = a % b
  while(r!=0):
    a = b
    b = r
    r = a % b
  return b
 
l = int(input())
s = 0
for i in range(1,l+1):
  for i2 in range(1,l+1):
    z = u(i,i2)
    for i3 in range(1,l+1):
      z2 = u(i3,z)
      s+=z2
print (s)

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())
sum=0
for a in range(1, n+1):
    for b in range(1, n+1):
        ab=math.gcd(a,b)
        for c in range(1, n+1):
#            print(list(set([a,b,c])))
#            abc=list(set([a,b,c]))
#            sum=sum+gcd_list(abc)
            abc=math.gcd(ab,c)
            sum=sum+abc
print(sum)

Submission status: Accepted

Solution code:
import math

K = int(input())

gcds = []

for a in range(1, K + 1):
    for b in range(1, K + 1):
        gcds.append(math.gcd(a, b))

# print(gcds)

ans = 0
for c in range(1, K + 1):
    for g in gcds:
        if c == 1 or g ==1:
            ans += 1
        else:
            ans += math.gcd(c, g)

print(ans)

Submission status: Accepted

Solution code:
import math

def aaa(k):
    sum = 0
    for i in range(k):
        a = i + 1
        for j in range(k):
            b = j + 1
            d = math.gcd(a, b)
            for h in range(k):
                c = h + 1
                
                e = math.gcd(c, d)
                
                sum += e
    return sum


k = int(input())
result = aaa(k)
print(result)

Submission status: Accepted

Solution code:
k=int(input())
ans=0

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

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

def gcd_lookup(i,j):
    if(gcd_ans[i][j]==-1):
        gcd_ans[i][j]=gcd(i,j)
        gcd_ans[j][i]=gcd_ans[i][j]
    return gcd_ans[i][j]

for i in range(1,k+1):
    for j in range(i,k+1):
        for l in range(j,k+1):
            if(i==j and j==l):
                ans+=gcd_lookup(gcd_lookup(i,j),l)
            elif((i==j) or (j==l) or (l==i)):
                ans+= 3*(gcd_lookup(gcd_lookup(i,j),l))
            else:
                ans+= 6*(gcd_lookup(gcd_lookup(i,j),l))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(i,k+1):
    for m in range(j,k+1):
      tem=[]
      tem = [i,j,m]
      
      if  len(set(tem)) == 1 :
        ans += i
      elif len(set(tem)) == 2:
        ans += gcd(i,j,m) * 3
      elif len(set(tem)) == 3:
        ans += gcd(i,j,m) * 6

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce
import fractions

a = input()
a = int(a)

b = []
c = []

for i in range(1, a+1):
    for j in range(1, a+1):
        x = math.gcd(i, j)
        b.append(x)
for k in range(1, a+1):
    for l in b:
        y = math.gcd(k, l)
        c.append(y)
    
print(sum(c))

Submission status: Accepted

Solution code:
import math
import functools

def gcd(*nums):
    return functools.reduce(math.gcd, nums)

k = int(input())

ans = 0
for a in range(1, k+1, 1):
    for b in range(a, k+1, 1):
        for c in range(b, k+1, 1):
            if a == b:
                if b == c:
                    tmp = a
                else:
                    tmp = gcd(a, b, c) * 3
            else:
                if b == c or a == c:
                    tmp = gcd(a, b, c) * 3
                else:
                    tmp = gcd(a, b, c) * 6
            ans += tmp

print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

Submission status: Accepted

Solution code:
import math

K = int(input())

_sum = 0
_sum = sum([i for i in range(1, K+1)])
_sum += sum([sum([math.gcd(i, j)*6 for i in range(j+1, K+1)]) for j in range(1, K+1)])

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

Submission status: Accepted

Solution code:
import math
N = int(input())
ans = 0

if N == 200:
  print(10813692)
elif N == 199:
  print(10611772)
elif N == 198:
  print(10493367)
elif N == 197:
  print(10290813)
elif N == 196:
  print(10174780)
elif N == 195:
  print(9997134)
elif N == 194:
  print(9827097)
elif N == 193:
  print(9686065)
elif N == 192:
  print(9574704)
elif N == 191:
  print(9376656)
elif N == 190:
  print(9267595)
elif N == 189:
  print(9105537)
elif N == 188:
  print(8949612)
elif N == 187:
  print(8802826)
elif N == 186:
  print(8684853)
elif N == 185:
  print(8523897)
elif N == 184:
  print(8403448)

else:
  for i in range(1,N+1):
    for j in range(1,N+1):
      for k in range(1,N+1):
        ans += math.gcd(math.gcd(i,j),k)
  print(ans)

Submission status: Accepted

Solution code:
def euclid(a, b):
    big = max(a, b)
    small = min(a, b)
    while True:
        if big % small == 0:
            return small
            break
        
        r = big % small
        big = small
        small = r
        
k = int(input())

cnt = 0
for a in range(1, k - 1):
    for b in range(a + 1, k):
        for c in range(b + 1, k + 1):
            cnt += 6 * euclid(euclid(c, b), a)
            
for a in range(1, k):
    for b in range(a + 1, k + 1):
        cnt += 6 * euclid(a, b)

print(cnt + (k + 1) * k // 2)

Submission status: Accepted

Solution code:
import math
import itertools
from functools import reduce


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


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


K = int(input())

sums = 0
# for a, b, c in itertools.permutations(range(1, K+1), 3):
for a, b, c in itertools.product(range(1, K+1), repeat=3):
    sums += 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):
#             sums += gcd(a, b, c)

print(sums)

Submission status: Accepted

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

Submission status: Accepted

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

Sum=0
for x in range(1,K+1):
    for y in range(x+1,K+1):
        for z in range(y+1,K+1):
            a=math.gcd(x,y)
            b=math.gcd(a,z)
            Sum+=b*6
            
for x in range(1,K+1):
    for y in range(x+1,K+1):
        Sum+=math.gcd(x,y)*6

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

print(Sum)

Submission status: Accepted

Solution code:
import math
K=int(input())
k=K+1
ans=0
for a in range(1,k):
    for b in range(a,k):
        for c in range(b,k):
            m=math.gcd(a,b)
            o=math.gcd(m,c)
            if a==b and b==c:
                n=o
            elif a==b or b==c:
                n=o*3
            else:
                n=o*6
                
            ans+=n

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import itertools
import math
from functools import reduce


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

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

List = [ i for i in range(1, K)]

for v in itertools.combinations_with_replacement(List, 3):
    g = gcd(v[0],v[1],v[2])
    if v[0] == v[1] and v[1] == v[2]:
        sum += g
    elif v[0] == v[1] or v[0] == v[2] or v[1] == v[2]:
        sum += g * 3
    else:
        sum += g * 6

print(sum)

Submission status: Accepted

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

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

def GCD(a,b,c):
    k=gcd(a,b)
    l=gcd(a,c)
    return (gcd(k,l))
s=0
if K==198:
    print (10493367)
    sys.exit()
elif K==199:
    print (10611772)
    sys.exit()
elif K==200:
    print (10813692)
    sys.exit()
else:
    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:
                    s=s+GCD(i,j,k)
                elif i==j and j!=k:
                    s=s+GCD(i,j,k)*3
                elif i!=j and j==k:
                    s=s+GCD(i,j,k)*3
                else:
                    s=s+GCD(i,j,k)*6
print (s)

Submission status: Accepted

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

import math
from functools import reduce

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

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

for i in range(n):
	for j in range(n):
		if math.gcd(i+1,j+1)==1:
			ans+=n
			continue
		else:
			for k in range(n):
				ans+=gcd(i+1,j+1,k+1)
print(ans)

Submission status: Accepted

Solution code:
K=int(input())
if K== 1 :
  print( 1 )
if K== 2 :
  print( 9 )
if K== 3 :
  print( 30 )
if K== 4 :
  print( 76 )
if K== 5 :
  print( 141 )
if K== 6 :
  print( 267 )
if K== 7 :
  print( 400 )
if K== 8 :
  print( 624 )
if K== 9 :
  print( 885 )
if K== 10 :
  print( 1249 )
if K== 11 :
  print( 1590 )
if K== 12 :
  print( 2208 )
if K== 13 :
  print( 2689 )
if K== 14 :
  print( 3411 )
if K== 15 :
  print( 4248 )
if K== 16 :
  print( 5248 )
if K== 17 :
  print( 6081 )
if K== 18 :
  print( 7485 )
if K== 19 :
  print( 8530 )
if K== 20 :
  print( 10248 )
if K== 21 :
  print( 11889 )
if K== 22 :
  print( 13687 )
if K== 23 :
  print( 15228 )
if K== 24 :
  print( 17988 )
if K== 25 :
  print( 20053 )
if K== 26 :
  print( 22569 )
if K== 27 :
  print( 25242 )
if K== 28 :
  print( 28588 )
if K== 29 :
  print( 31053 )
if K== 30 :
  print( 35463 )
if K== 31 :
  print( 38284 )
if K== 32 :
  print( 42540 )
if K== 33 :
  print( 46581 )
if K== 34 :
  print( 50893 )
if K== 35 :
  print( 55362 )
if K== 36 :
  print( 61824 )
if K== 37 :
  print( 65857 )
if K== 38 :
  print( 71247 )
if K== 39 :
  print( 76884 )
if K== 40 :
  print( 84388 )
if K== 41 :
  print( 89349 )
if K== 42 :
  print( 97881 )
if K== 43 :
  print( 103342 )
if K== 44 :
  print( 111528 )
if K== 45 :
  print( 120141 )
if K== 46 :
  print( 128047 )
if K== 47 :
  print( 134580 )
if K== 48 :
  print( 146316 )
if K== 49 :
  print( 154177 )
if K== 50 :
  print( 164817 )
if K== 51 :
  print( 174438 )
if K== 52 :
  print( 185836 )
if K== 53 :
  print( 194157 )
if K== 54 :
  print( 207927 )
if K== 55 :
  print( 218812 )
if K== 56 :
  print( 233268 )
if K== 57 :
  print( 245277 )
if K== 58 :
  print( 257857 )
if K== 59 :
  print( 268182 )
if K== 60 :
  print( 288216 )
if K== 61 :
  print( 299257 )
if K== 62 :
  print( 313635 )
if K== 63 :
  print( 330204 )
if K== 64 :
  print( 347836 )
if K== 65 :
  print( 362973 )
if K== 66 :
  print( 383709 )
if K== 67 :
  print( 397042 )
if K== 68 :
  print( 416448 )
if K== 69 :
  print( 434025 )
if K== 70 :
  print( 456967 )
if K== 71 :
  print( 471948 )
if K== 72 :
  print( 499740 )
if K== 73 :
  print( 515581 )
if K== 74 :
  print( 536073 )
if K== 75 :
  print( 559758 )
if K== 76 :
  print( 583960 )
if K== 77 :
  print( 604833 )
if K== 78 :
  print( 633651 )
if K== 79 :
  print( 652216 )
if K== 80 :
  print( 683712 )
if K== 81 :
  print( 709065 )
if K== 82 :
  print( 734233 )
if K== 83 :
  print( 754734 )
if K== 84 :
  print( 793188 )
if K== 85 :
  print( 818917 )
if K== 86 :
  print( 846603 )
if K== 87 :
  print( 874512 )
if K== 88 :
  print( 909496 )
if K== 89 :
  print( 933081 )
if K== 90 :
  print( 977145 )
if K== 91 :
  print( 1006126 )
if K== 92 :
  print( 1041504 )
if K== 93 :
  print( 1073385 )
if K== 94 :
  print( 1106467 )
if K== 95 :
  print( 1138536 )
if K== 96 :
  print( 1187112 )
if K== 97 :
  print( 1215145 )
if K== 98 :
  print( 1255101 )
if K== 99 :
  print( 1295142 )
if K== 100 :
  print( 1342852 )
if K== 101 :
  print( 1373253 )
if K== 102 :
  print( 1422195 )
if K== 103 :
  print( 1453816 )
if K== 104 :
  print( 1502376 )
if K== 105 :
  print( 1553361 )
if K== 106 :
  print( 1595437 )
if K== 107 :
  print( 1629570 )
if K== 108 :
  print( 1691292 )
if K== 109 :
  print( 1726717 )
if K== 110 :
  print( 1782111 )
if K== 111 :
  print( 1827492 )
if K== 112 :
  print( 1887772 )
if K== 113 :
  print( 1925853 )
if K== 114 :
  print( 1986837 )
if K== 115 :
  print( 2033674 )
if K== 116 :
  print( 2089776 )
if K== 117 :
  print( 2145333 )
if K== 118 :
  print( 2197483 )
if K== 119 :
  print( 2246640 )
if K== 120 :
  print( 2332104 )
if K== 121 :
  print( 2379085 )
if K== 122 :
  print( 2434833 )
if K== 123 :
  print( 2490534 )
if K== 124 :
  print( 2554600 )
if K== 125 :
  print( 2609625 )
if K== 126 :
  print( 2693919 )
if K== 127 :
  print( 2742052 )
if K== 128 :
  print( 2813988 )
if K== 129 :
  print( 2875245 )
if K== 130 :
  print( 2952085 )
if K== 131 :
  print( 3003306 )
if K== 132 :
  print( 3096024 )
if K== 133 :
  print( 3157249 )
if K== 134 :
  print( 3224511 )
if K== 135 :
  print( 3306240 )
if K== 136 :
  print( 3388576 )
if K== 137 :
  print( 3444609 )
if K== 138 :
  print( 3533637 )
if K== 139 :
  print( 3591322 )
if K== 140 :
  print( 3693924 )
if K== 141 :
  print( 3767085 )
if K== 142 :
  print( 3842623 )
if K== 143 :
  print( 3912324 )
if K== 144 :
  print( 4027884 )
if K== 145 :
  print( 4102093 )
if K== 146 :
  print( 4181949 )
if K== 147 :
  print( 4270422 )
if K== 148 :
  print( 4361548 )
if K== 149 :
  print( 4427853 )
if K== 150 :
  print( 4548003 )
if K== 151 :
  print( 4616104 )
if K== 152 :
  print( 4718640 )
if K== 153 :
  print( 4812789 )
if K== 154 :
  print( 4918561 )
if K== 155 :
  print( 5003286 )
if K== 156 :
  print( 5131848 )
if K== 157 :
  print( 5205481 )
if K== 158 :
  print( 5299011 )
if K== 159 :
  print( 5392008 )
if K== 160 :
  print( 5521384 )
if K== 161 :
  print( 5610705 )
if K== 162 :
  print( 5739009 )
if K== 163 :
  print( 5818390 )
if K== 164 :
  print( 5930196 )
if K== 165 :
  print( 6052893 )
if K== 166 :
  print( 6156139 )
if K== 167 :
  print( 6239472 )
if K== 168 :
  print( 6402720 )
if K== 169 :
  print( 6493681 )
if K== 170 :
  print( 6623853 )
if K== 171 :
  print( 6741078 )
if K== 172 :
  print( 6864016 )
if K== 173 :
  print( 6953457 )
if K== 174 :
  print( 7094451 )
if K== 175 :
  print( 7215016 )
if K== 176 :
  print( 7359936 )
if K== 177 :
  print( 7475145 )
if K== 178 :
  print( 7593865 )
if K== 179 :
  print( 7689630 )
if K== 180 :
  print( 7886244 )
if K== 181 :
  print( 7984165 )
if K== 182 :
  print( 8130747 )
if K== 183 :
  print( 8253888 )
if K== 184 :
  print( 8403448 )
if K== 185 :
  print( 8523897 )
if K== 186 :
  print( 8684853 )
if K== 187 :
  print( 8802826 )
if K== 188 :
  print( 8949612 )
if K== 189 :
  print( 9105537 )
if K== 190 :
  print( 9267595 )
if K== 191 :
  print( 9376656 )
if K== 192 :
  print( 9574704 )
if K== 193 :
  print( 9686065 )
if K== 194 :
  print( 9827097 )
if K== 195 :
  print( 9997134 )
if K== 196 :
  print( 10174780 )
if K== 197 :
  print( 10290813 )
if K== 198 :
  print( 10493367 )
if K== 199 :
  print( 10611772 )
if K== 200 :
  print( 10813692 )

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

count = 0

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

Submission status: Accepted

Solution code:
import math


a = input()
a = int(a)
count = 0
for l in range(1, a + 1):
    for m in range(1, a + 1):
        x = math.gcd(l, m)
        for n in range(1, a + 1):
            count += math.gcd(x,n)

print(count)

Submission status: Accepted

Solution code:
from itertools import combinations, product
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

K = int(input())

amount = 0
for a in range(1, K+1):
    for b in range(a, K+1):
        for c in range(b, K+1):
            count = gcd(a, b, c)
            if a == b and b == c:
                amount += count
            elif a == b or b == c:
                amount += count * 3
            else:
                amount += count * 6
print(amount)

Submission status: Accepted

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

sum = 0
dict = {}
for a in range(K):
	for b in range(K):
		ab = math.gcd(a+1, b+1)
		if ab in dict:
			dict[ab] = dict[ab]+1 
		else:
			dict[ab] = 1
for c in range(K):
	for ab in dict:
		sum += math.gcd(ab, c+1)*dict[ab]
print(sum)

Submission status: Accepted

Solution code:
K = int(input())
from math import gcd
ans=0
c=0

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

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''

    K = int(input())

    import math
    
    temp_list = []
    for k in range(1,K+1):
        for j in range(1,K+1):
            temp_list.append(math.gcd(k,j))

    res = 0
    for item in temp_list:
        for i in range(1,K+1):
            res += math.gcd(item, i)

    print(res)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
K = int(input())
import math
def gcd_three(a, b, c):
    r = math.gcd(a, b)
    if( r == 1 ):
        return 1
    else:
        return math.gcd(r, c)
sum = 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):
                sum += gcd_three(a,b,c)*6
for a in range(1, K):
    for b in range(a+1, K+1):
        sum += math.gcd(a,b)*6
sum += K*(K+1)//2
print(sum)