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:
K = int(input())

total = 0
for i in range(1,K+1,1):
  for j in range(1,K+1,1):
    tmp1 = i
    tmp2 = j
    while tmp1 % tmp2 != 0:
      tmp = tmp1 % tmp2
      tmp1 = tmp2
      tmp2 = tmp
    for k in range(1,K+1,1):
      tmp1 = k
      tmp3 = tmp2
      while tmp1 % tmp3 != 0:
        tmp = tmp1 % tmp3
        tmp1 = tmp3
        tmp3 = tmp
      total += tmp3
print(total)

Submission status: Accepted

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

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

K = int(input())

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

print(ans)

Submission status: Accepted

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

K = int(input())

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

print(ans)

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

Submission status: Accepted

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

sm=0
for i in range(1,k+1):
    for l in range(1,k+1):
        gcdab=math.gcd(i,l)
        for m in range(1,k+1):
            sm+=math.gcd(gcdab,m)

print(sm)

Submission status: Accepted

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

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):
    x = gcd(a,b)
  	
    for c in range(1,K+1):
      Y = gcd(x,c)
      ans += Y

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):
        p = gcd(a, b)
        for c in range(1, K+1):
            sum += gcd(p, c)
print(sum)

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

Submission status: Accepted

Solution code:
#162
import math
from functools import reduce

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

K = int(input())
combinations = []
combinations.extend([[i+1]*3 for i in range(K)])
for i in range(K):
    combinations.extend([[i+1,i+1,j+1] for j in range(K) if j>i])
    for j in range(K):
        combinations.extend([[i+1,j+1,k+1] for k in range(K) if (j>i and k>j)])
result=0
for combination in combinations:
    a, b, c = combination
    if(a==b and b==c):
        result += a
    elif((a==b and b!=c) or (b==c and c!=a) or (c==a and a!=b)):
        result += gcd(a,b,c)*6
    else:
        result += gcd(a,b,c)*6
    
print(result)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K = int(input())
ans = 0

A = {}
for a in range(1, K+1):
    A[a] = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        num = math.gcd(a, b)
        A[num] += 1

for c in range(1, K+1):
    for k in A.keys():
        num = math.gcd(c, k) * A[k]
        ans += num

print(ans)

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())
sum = 0

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

Submission status: Accepted

Solution code:
import math
K = int(input())
sum = 0
for i in range(1, K+1):
  for j in range(i, K+1):
    #2i or 3i
    if (i == j):
      for k in range(i, K+1):
        #print("i"+str(i)+"j"+str(j)+"k"+str(k))
        if ((i == j) and (i == k)):
          sum += math.gcd(i, math.gcd(j, k))
        else:
          sum += (3*math.gcd(i, math.gcd(j, k)))
    #1i
    else:
      for k in range(i+1, K+1):
        #print("i"+str(i)+"j"+str(j)+"k"+str(k))
        sum += (3*math.gcd(i, math.gcd(j, k)))
      
print(sum)

Submission status: Accepted

Solution code:
import math, itertools
from functools import reduce
def gcd_list(numbers):
    return(reduce(math.gcd,numbers))
k=int(input())
ans=0
for numbers in itertools.combinations_with_replacement(range(1,k+1),3):
    a,b,c=numbers
    if a==b==c:
        ans+=gcd_list(numbers)
    elif a!=b and a!=c and b!=c:
        ans+=6*gcd_list(numbers)
    else:
        ans+=3*gcd_list(numbers)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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) :
        X = gcd(a, b)
        for c in range(1,K+1) :
           Y = gcd(X, c)
           ans += Y

print(ans)

Submission status: Accepted

Solution code:
k = int(input()) + 1

import math
from functools import reduce

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import math


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

    print(s)


if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import math


def main():
    K = int(input())
    s = 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):
                s += math.gcd(tmp, k)

    print(s)


if __name__=="__main__":
    main()

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

Submission status: Accepted

Solution code:
from math import gcd

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

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

print(num)

Submission status: Accepted

Solution code:
import math
from functools import reduce

k = int(input())

ans = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            #ans += reduce(math.gcd, [i, j, l])
            ans += math.gcd(math.gcd(i, j), l)

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

k = int(input())

ans = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            ans += reduce(math.gcd, [i, j, l])

print(ans)

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):
        ab = math.gcd(a,b)
        if ab == 1:
            ans += k

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

Submission status: Accepted

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


K = int(input())
ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        g1 = gcd(a, b)
        if g1 == 1:
            ans += K
        else:
            for c in range(1, K+1):
                ans += gcd(g1, c)
print(ans)

Submission status: Accepted

Solution code:
K   = int(input())
Sum = 0
def mygcd(x,y):
    a = min(x,y)  # 最小値
    b = max(x,y)  # 最大値
    r = 2  # 余り
    while r != 0:
        r = b % a
        b = a
        a = r
    return b

for A in range(1,K+1):
    for B in range(1,K+1):
        AB = mygcd(A,B)
        if AB==1:
            Sum = Sum+K
        else:
            for C in range(1,K+1):
                Sum = Sum+mygcd(AB,C)
print(Sum)

Submission status: Accepted

Solution code:
import math

k = int(input())
s = 0

def gcd3(a, b, c):
    d = math.gcd(a, b)
    e = math.gcd(d, c)
    return e


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

print(s)

Submission status: Accepted

Solution code:
import math
K   = int(input())
Sum = 0
for A in range(1,K+1):
    for B in range(1,K+1):
        AB = math.gcd(A,B)
        if AB==1:
            Sum = Sum+K
        else:
            for C in range(1,K+1):
                Sum = Sum+math.gcd(AB,C)
print(Sum)

Submission status: Accepted

Solution code:
import math
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 = Sum+math.gcd(math.gcd(A,B),C)
print(Sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

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

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

    print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
ans = 0
for x in range(1, k + 1):
  for y in range(1, k + 1):
    for z in range(1, k + 1):
      ans += gcd(gcd(x, y), z)
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    import math
    K = int(input())
    ans = []
    for i in range(1, K+1):
        for j in range(1, K+1):
            for p in range(1, K+1):
                q = math.gcd(i, j)
                ans.append(math.gcd(q, p))
    print(sum(ans))
resolve()

Submission status: Accepted

Solution code:
import math
import numpy as np
k = int(input())

Map = [[0 for j in range(k)] for i in range(k)]
for i  in range(1,k+1):
    for j in range(1,k+1):
        tmp = math.gcd(i,j)
        Map[i-1][j-1] = tmp
        Map[j-1][i-1] = tmp

ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        tmp_ans = Map[i-1][j-1]
        for x in range(1,k+1):
            ans += Map[tmp_ans-1][x-1]
            
            
print(ans)

Submission status: Accepted

Solution code:
import math


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

            
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    k=INT()
    ans=0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for h in range(1,k+1):
                ans+=math.gcd(math.gcd(i,j),h)
                #print(ans,i,j,h)
    print(ans)
if __name__ == '__main__':
    do()

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

n = int(input())
ans = 0

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

for x in range(n):
    for k in range(n):
        for l in range(n):
            ans += gcd(x+1,k+1,l+1)

print(ans)

Submission status: Accepted

Solution code:
import collections

def U1(a,b):
    if b > a:
        b, a = a, b
    while a%b != 0:
        r = a%b
        a = b
        b = r
    return b

K = int(input())
c = 0
arr = []

for i in range(1,K+1):
    for j in range(1,K+1):
        arr.append(U1(i,j))

arr=collections.Counter(arr)

for key,value in arr.items():
    for k in range(1,K+1):
        c += U1(k,key)*value

print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
k=int(input())
res=0
#すべてが同じ値の場合:1-kの合計
res += k*(k+1)//2
#2値が同じ場合:*3
for i in range(1,k+1):
  for j in range(1,k+1):
    if i==j:
      continue
    res += math.gcd(i,j)*3
#全部違う場合:*6
for i in range(1,k+1):
  for j in range(i+1,k+1):
    for m in range(j+1,k+1):
      if len(list(set([i,j,k]))) == 3:
        res += math.gcd(math.gcd(i,j),m)*6
print(res)

Submission status: Accepted

Solution code:
import math

math.gcd(2,10)
N =int(input())
total = 0
a=1
b=1
c=1
while a <=N:
	b=1
	while b <=N:
		c=1
		while c <=N:
			if math.gcd(a,b)==1:
				total += N
				break
			total += math.gcd(a,math.gcd(b,c))
			c+=1
		b+=1 
	a+=1
print(total)

Submission status: Accepted

Solution code:
import math
import collections
k=int(input())
ans=0
a=[]
for i in range(1,k+1):
    for j in range(1,k+1):
        a.append(math.gcd(i,j))
a=collections.Counter(a)
for key,value in a.items():
    for i in range(1,k+1):
        ans+=math.gcd(i,key)*value
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
K = int(input())
l = [0] *(K+1)
res = 0
for i in range(1,K+1):
    for j in range(1, K+1):
        gcd_ab = np.gcd(i,j)
        l[gcd_ab] +=1

for i in range(1, K+1):
    for j in range(1,K+1):
        gcd_c = np.gcd(i,j)
        res += l[j]* gcd_c


print(res)

Submission status: Accepted

Solution code:
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a

k = int(input())
temp = [[xgcd(i, j) for i in range(1, k+1)] for j in range(1, k+1)]
ans = 0
for a in range(k):
    for b in range(k):
        t = temp[a][b]
        for c in range(k):
            ans += temp[t-1][c]
print(ans)

Submission status: Accepted

Solution code:
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a

k = int(input())
temp = [[xgcd(i, j) for i in range(1, k+1)] for j in range(1, k+1)]
ans = 0
for a in range(k):
    for b in range(k):
        t = temp[a][b]
        ans += sum([temp[t-1][c] for c in range(k)])
print(ans)

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):
        g = math.gcd(a, b)
        if g == 1:
            ans += K
            continue      
        for c in range(1, K + 1):
            ans += math.gcd(c, g)

print(ans)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
from math import gcd
k=int(input())
ans=0
# rangeはk回なのでk+1
for i in range(1,k+1):
    for j in range(1,k+1):
        # シンプルにiとjの最大公約数
        ans_=gcd(i,j)
        for l in range(1,k+1):
            # で一番内側のループ内でgcd(i,j)を計算すると余計に計算することになる
            ans+=gcd(ans_,l)
print(ans)

Submission status: Accepted

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

                ans+=math.gcd(math.gcd(i+1,j+1),k+1)
                
print(ans)

Submission status: Accepted

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

def solve(K: int):
    result = 0
    for a in range(1, K+1):
        for b in range(1, K+1):
            if math.gcd(a, b) == 1:
                result += K
                continue
            for c in range(1, K+1):
                result += math.gcd(a, math.gcd(b, c))
    print(result)

# 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:
K=int(input())
from math import gcd
S=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            S+=gcd(gcd(i,j),k)
print(S)

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
"""
import math as f


K = int(input())
result = 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 == k:
                result += f.gcd(f.gcd(i, j), k)
            elif i == j or j == k or k == i:
                result += 3 * f.gcd(f.gcd(i, j), k)
            else:
                result += 6 * f.gcd(f.gcd(i, j), k)

print(result)

Submission status: Accepted

Solution code:
import math

K = int(input())
check = {}
ans = 0

for i in range(201):
    check[i] = 0

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

for c in range(1, K+1):
    for k in check.keys():
        num = math.gcd(c, k) * check[k]
        ans += num

print(ans)

Submission status: Accepted

Solution code:
import math
k = int(input())
ans = 0
ab = {}

for a in range(1,k+1):
  for b in range(1,k+1):
    tmp = math.gcd(a, b)
    if tmp not in ab:
      ab[tmp] = 1
    else:
      ab[tmp] += 1

for c in range(1,k+1):
  for d in ab.keys():
    ans += math.gcd(c, d) * ab[d]

print(ans)

Submission status: Accepted

Solution code:
# C - Sum of gcd of Tuples (Easy)
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:
from sys import stdin
import numpy as np
def main():
    #入力
    readline=stdin.readline
    k=int(readline())

    a=np.arange(1,k+1,dtype=np.int64)
    b=np.gcd.outer(np.gcd.outer(a,a),a)
    print(b.sum())

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
import math
from functools import reduce
def gcd(numbers):
    return reduce(math.gcd, numbers)
N = int(input())
l = [i for i in range(1,N+1)]
h = itertools.combinations_with_replacement(l, 3)

result=0
for v in h:
    ans=gcd(v)
    i,j,k=zip(v)
    if (i==j)&(i==k):
        result += ans
    elif ((i==j)&(i!=k))|((i==k)&(i!=j))|((j==k)&(j!=i)):
        result += (ans*3)
    else:
        result += (ans*6)
    
print(result)

Submission status: Accepted

Solution code:
#!/usr/bin/python3

from math import gcd

k = int(input())
res = 0

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k = int(input())

def gcd(a, b, c):
    mini = a
    if a > b:
        mini = b
    if b > c:
        mini = c
    if (a % mini == 0) & (b % mini == 0) & (c % mini == 0):
        return mini
    if mini % 2 == 0:
        h = mini / 2
        if (a % h == 0) & (b % h == 0) & (c % h == 0):
            return h
    for i in range((mini + 1)//3 + 1, 1, -1):
        if (a % i == 0) & (b % i == 0) & (c % i == 0):
            return i
    return 1

s = 0
for i in range(1, k + 1):
    for j in range(1, k + 1):
        for l in range(1, k + 1):
            if i <= j <= l:
                if i < j < l:
                    a = gcd(i, j, l) * 6
                    s += int(a)
                elif ((i == j) & (j != l)) | ((i != j) & (j == l)):
                    a = gcd(i, j, l) * 3
                    s += int(a)
                else:
                    a = gcd(i, j, l) 
                    s += int(a)
            else:
                continue

print(s)

Submission status: Accepted

Solution code:
import math

K = int(input())
ans = 0
tmp = {}

for i in range(201):
    tmp[i] = 0

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

for k in range(1, K+1):
    for key in tmp.keys():
        ans += math.gcd(key, k) * tmp[key]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())
total = 0


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


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

print(total)

Submission status: Accepted

Solution code:
import sys
import math

def input():
    return sys.stdin.readline().strip()

def main():
    k = int(input())
    sum = 0
    for a in range(k):
        for b in range(k):
            x = math.gcd(a+1,b+1)
            for c in range(k):
                y = math.gcd(x, c+1)
                sum += y
    print(sum)



main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# from functools import reduce
# from math import gcd
import math

N = int(input())
result = 0
# print(sum([reduce(gcd, [i, j, k]) for i in range(1, N+1) for j in range(1, N+1) for k in range(1, N+1)]))
for i in range(1, N+1):
    for j in range(1, N+1):
        tmp = math.gcd(i, j)
        for k in range(1, N+1):
            result += math.gcd(tmp, k)

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

K = ir()
A = np.arange(1, K+1, dtype=np.int32)
B = np.gcd.outer(np.gcd.outer(A, A), A)
answer = B.sum()
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

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

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K = int(input())

ans = K * (K + 1) / 2

for i in range(K):
    for j in range(i):
        temp = math.gcd(i + 1, j + 1)
        ans += temp * 6
        for k in range(j):
            ans += math.gcd(temp, k + 1) * 6

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
k = int(input())
a = k*(k+1)//2
for i in range(k):
  for j in range(i):
    b = math.gcd(i+1,j+1)
    a += b*6
    for k in range(j):
      a += math.gcd(b,k+1)*6
print(a)

Submission status: Accepted

Solution code:
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
a=0
N=int(input())
for i in range(1,N+1):
  for j in range(i,N+1):
    for k in range(j,N+1):
      if i==j==k:
        a+=i
      elif i==j or j==k or i==k:
        c=euclid(i, j)
        c=euclid(c, k)
        a+=3*c
      else:
        c=euclid(i, j)
        c=euclid(c, k)
        a+=6*c
print(a)

Submission status: Accepted

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

for i in range(1, k+1):
    for j in range(1, k+1):
        x = math.gcd(i, j)
        for l in range(1, k+1):
            sum_ += math.gcd(x, l)
            
print(sum_)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(tot_sum)