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:
#!/usr/bin/env python3

import numpy as np
from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit("i4(i4, i4)", cache=True)
def gcd(x, y):
    if x < y:
        x, y = y, x  # x >= y
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

@njit("i4(i4)", cache=True)
def getresult(k):
    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)
    return res

if __name__ == "__main__":
    main()

Submission status: Accepted

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

import numpy as np
from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit("i4(i4, i4)", cache=True)
def gcd(x, y):
    if x < y:
        x, y = y, x  # x >= y
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

@njit("i8(i4)", cache=True)
def getresult(k):
    res = np.array([0], dtype=np.int64)
    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)
    return res[0]

if __name__ == "__main__":
    main()

Submission status: Accepted

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

from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit
def getresult(k):
    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)
    return res

@njit("i4(i4, i4)", cache=True)
def gcd(x, y):
    if x < y:
        x, y = y, x  # x >= y
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

if __name__ == "__main__":
    main()

Submission status: Accepted

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

from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit
def getresult(k):
    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)
    return res

@njit("i4(i4, i4)")
def gcd(x, y):
    if x < y:
        x, y = y, x  # x >= y
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

from numba import njit

def main():
    k = int(input())
    print(getresult(k))

@njit
def getresult(k):
    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)
    return res

@njit
def gcd(x, y):
    if x < y:
        x, y = y, x  # x >= y
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

# from math import gcd


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


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

N = int(input())
ans = 0
for i in range(1, N + 1):
    if i == 1:
        ans += N ** 2
        continue
    for j in range(1, N + 1):
        if j == 1:
            ans += N
            continue
        tmp = gcd(i, j)
        if tmp == 1:
            ans += N
            continue
        for k in range(1, N + 1):
            if k == 1:
                ans += 1
                continue
            ans += gcd(tmp, k)

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
from time import time


def main():
    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)

if __name__ == '__main__':
    start = time()
    main()
    # print(time() - start)

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

from math import gcd, floor, sqrt

INF = float('inf')


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

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


N = int(input())
ans = 0

for i in range(1, N + 1):
    if i == 1:
        ans += N ** 2
        continue

    for j in range(1, N + 1):
        if j == 1:
            ans += N
            continue
        tmp = gcd(i, j)
        tmp_prime = is_prime(tmp)
        if tmp == 1:
            ans += N
            continue

        for k in range(1, N + 1):
            if k == 1:
                ans += 1
                continue
            if tmp_prime:
                if k % tmp == 0:
                    ans += tmp
                    continue
                else:
                    ans += 1
                    continue
            else:
                ans += gcd(tmp, k)

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
ar = [0] * 41000
for i in range(1, 201):
    for j in range(1, 201): ar[i*200+j] = gcd(i, j)

ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        d = ar[a*200+b]
        if d == 1:
            ans += k
            continue
        for c in range(1, k+1): ans += ar[d*200+c]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
g=__import__('math').gcd
r=range(1,int(input())+1)
print(sum(g(g(i,j),k)for i in r for j in r for k in r))

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

from math import gcd

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

N = int(input())
ans = 0
for i in range(1, N + 1):
    if i == 1:
        ans += N ** 2
        continue
    for j in range(1, N + 1):
        if j == 1:
            ans += N
            continue
        tmp = gcd(i, j)
        if tmp == 1:
            ans += N 
            continue
        for k in range(1, N + 1):
            if k == 1:
                ans += 1
                continue
            ans += gcd(tmp, k)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

from math import gcd

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

N = int(input())
ans = 0
for i in range(1, N + 1):
    if i == 1:
        ans += N ** 2
        continue
    for j in range(1, N + 1):
        if j == 1:
            ans += N
            continue
        tmp = gcd(i, j)
        for k in range(1, N + 1):
            ans += gcd(tmp, k)
print(ans)

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

from math import gcd
    
INF = float('inf')
NIL = - 1

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

Submission status: Accepted

Solution code:
[print(sum([g(g(i,j),k)for g in[__import__('math').gcd]for n in[int(input())] for r in[range(1,n+1)]for i in r for j in r for k in r]))]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from math import gcd
from math import ceil, floor
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    ans = 0
    for a in range(1, n+1):
        for b in range(1, n+1):
            tmp = gcd(a, b)
            for c in range(1, n+1):
                ans += gcd(tmp, c)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

r = range(1, k + 1)
ans = sum([gcd(gcd(a, b), c) for a in r for b in r for c in r])
print(ans)

Submission status: Accepted

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

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):
      if a == b == c:
        total += gcd(gcd(a, b), c)
      elif a == b or b == c:
        total += 3*gcd(gcd(a, b), c)
      else:
        total += 6*gcd(gcd(a, b), c)
print(total)

Submission status: Accepted

Solution code:
k = int(input())

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

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

Submission status: Accepted

Solution code:
k = int(input())

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

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

Submission status: Accepted

Solution code:
K = int(input())

from fractions import gcd
# return gcd(x, y)

d = {}

def f(x, y, z):
    # print(d)
    if d.get((x,y)):
        s = d[(x,y)]
    else:
        s = gcd(x, y)
        d[(x,y)] = s

    if d.get((s,z)):
        t = d[(s,z)]
    else:
        t = gcd(s,z)
        d[(s,z)] = t

    return t

ans = 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 == k:
                ans += f(i, j, k)
            elif i == j or j == k:
                ans += 3 * f(i, j, k)
            else:
                ans += 6 * f(i, j, k)

print(ans)

Submission status: Accepted

Solution code:
k = int(input())

"""
memo = [[-1 for i in range(k+1)] for j in range(k+1)]
def gcd(a, b):
    if b == 0:
        return a
    else:
        if memo[a][b] == -1:
            memo[a][b] = gcd(b, a % b)
        return memo[a][b]
"""

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

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

Submission status: Accepted

Solution code:
import math

K = int(input())
l = 0
ans = 0

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

Submission status: Accepted

Solution code:
from math import gcd


def solve():

    K = int(input())

    rangeK = range(1, K+1)
    ans = 0

    for a in rangeK:
        for b in rangeK:
            for c in rangeK:
                ans += gcd(gcd(a, b), c)

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from fractions import gcd

memo = {}
ans = 0

k = int(input())

my_gcd = {}
for i in range(1, k+1):
    for j in range(i, k+1):
        my_gcd[(i, j)] = gcd(i, j)
        my_gcd[(j, i)] = my_gcd[(i, j)]

for a in range(1, k+1):
    for b in range(a, k+1):
        g = my_gcd[(a, b)]
        for c in range(b, k+1):
            g2 = my_gcd[(g, c)]

            if a == b and b == c:
                ans += g2
            elif a == b or b == c or c == a:
                ans += g2*3
            else:
                ans += g2*6

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

K=int(read())
l=[[0]*(K+1) for _ in range(K+1)]
for i in range(1,K+1):
    for j in range(i,K+1):
        x=gcd(i,j)
        l[i][j],l[j][i]=x,x

ans=0
for a in range(1,K+1):
    for b in range(1,K+1):
        x=l[a][b]
        for c in range(1,K+1):
            ans+=l[x][c]

print(ans)

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

Submission status: Accepted

Solution code:
import math 
import sys
read_=sys.stdin.buffer.readline

K=int(read_())
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:
from math import gcd

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+=gcd(gcd(i,j), l)
            #print(gcd(i,j,l))
print(ans)

Submission status: Accepted

Solution code:
from math import gcd


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


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

print(ans)

Submission status: Accepted

Solution code:
def ABC162C(a,b):#ユークリッドの互除法
    while b != 0:
        a %= b
        if a == 0:break
        b %= a
    return max(a,b)

k = int(input())
p=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for m in range(1,k+1):
            p+=ABC162C(i,ABC162C(j,m))
print(p)

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

k = int(input())

ans = 0
for a in range(1,k+1):
    for b in range(1,k+1): #k+1なのに注意
        t = gcd(a,b)
        for c in range(1,k+1):
            ans += gcd(t,c)

print(ans)

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

print(res)

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:
import math
n = int(input())
ans = 0
for i in range(1, n + 1):
  for j in range(1, n + 1):
    g = math.gcd(i, j)
    for k in range(1, n + 1):
      ans += math.gcd(g, k) 
print(ans)

Submission status: Accepted

Solution code:
# 3重ループでテスト

from functools import reduce

k = int(input())

def gcd(a, b):
    # 最大公約数
    while b:
        a, b = b, a % b
    return a

def gcds(*nums):
	return reduce(gcd, nums)

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

print(res)

Submission status: Accepted

Solution code:
k = int(input())

ans = []

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

ans = [
        gcd(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)
      ]
print(sum(ans))

Submission status: Accepted

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

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

def combs(k):
    res = []
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                res.append(gcd(a, b, c))
    print(sum(res))

combs(int(input()))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

total = 0
for i in range(1,k+1):
    for j in range(i+1,k+1):
        tmp = gcd(i,j)
        total += tmp * 6
        for m in range(j+1,k+1):
            total += gcd(tmp,m) * 6
total += k*(k+1)//2

print(total)

Submission status: Accepted

Solution code:
import numpy as np

K = int(input())
x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
ans = nums.sum()
print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
GCD = [[math.gcd(i, j) for i in range(1, N+1)] for j in range(1, N+1)]
#print(GCD)
all_gcd = [GCD[GCD[i][j]-1][m] for i in range(N) for j in range(N) for m in range(N)]
print(sum(all_gcd))

Submission status: Accepted

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

def computeGCD(x, y):
    while (y):
        x, y = y, x % y

    return x

result = list()
sum = 0
for a in range(1, K+1):
    intermediate_result = list()
    for b in range(1, K+1):
        intermediate_result.append(computeGCD(a, b))
    result.append(intermediate_result)

for a in range(1, K+1):
    for b in range(1, K+1):
        intermediate = result[a-1][b-1]
        for c in range(1, K+1):
            sum += result[intermediate-1][c-1]

print(sum)

Submission status: Accepted

Solution code:
import math
N = int(input())
all_gcd = [math.gcd(math.gcd(i, j), m) for i in range(1, N+1) for j in range(1, N+1) for m in range(1, N+1)]
print(sum(all_gcd))

Submission status: Accepted

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

def computeGCD(x, y):
    while (y):
        x, y = y, x % y

    return x

sum = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        intermediate = computeGCD(a, b)
        for c in range(1, K+1):
            sum += computeGCD(c, intermediate)

print(sum)

Submission status: Accepted

Solution code:
import math
N = int(input())
all_gcd = []
for i in range(1, N+1):
  for j in range(1, N+1):
    x = math.gcd(i, j) if i != j else i
    for m in range(1, N+1):
      y = math.gcd(x, m) if x != m else x
      all_gcd.append(y)

print(sum(all_gcd))

Submission status: Accepted

Solution code:
from math import gcd

K = int(input())

def gcd_calc(x:int, y:int):
  while(y != 0):
    z = x % y;
    x = y;
    y = z;
  return x

output = 0

for a in range(1, K+1):
  for b in range(1, K+1):
    for c in range(1, K+1):
      output += gcd_calc(a, gcd_calc(b,c))

print(output)

Submission status: Accepted

Solution code:
import math
from numba import jit
from numba import prange

K = int(input())


@jit(parallel=True)
def apt(N):
    cnt = 0
    for i in prange(1, K+1):
        for j in prange(1, K + 1):
            for k in prange(1, K + 1):
                cnt += math.gcd(k, math.gcd(i, j))
    return cnt


print(apt(K))

Submission status: Accepted

Solution code:
import math

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


print(cnt)

Submission status: Accepted

Solution code:
import math
from numba import jit

K = int(input())


@jit
def apt(N):
    cnt = 0
    for i in range(1, K+1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                cnt += math.gcd(k, math.gcd(i, j))
    return cnt


print(apt(K))

Submission status: Accepted

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

result = 0
for k in range(1, K + 1):
  for l in range(1, K + 1):
    for m in range(1, K + 1):
      gcd_res = gcd(gcd(k, l), m)
      result += gcd_res

print(result)

Submission status: Accepted

Solution code:
def euclid(a, b):
  division = a if (a > b) else b
  divider = b if (a > b) else a
  reminder = 0
  while divider != 0:
    reminder = division % divider
    division = divider
    divider = reminder
  
  return division

K = int(input())

result = 0
for k in range(1, K + 1):
  for l in range(1, K + 1):
    for m in range(1, K + 1):
      gcd = euclid(euclid(k, l), m)
      result += gcd

print(result)

Submission status: Accepted

Solution code:
import sys 
# try:
# 	sys.stdin = open('input.txt', 'r') 
# 	sys.stdout = open('output.txt', 'w')
# except:
# 	pass

def GCD(p,q):
	if (p%q==0):
		return q
	return GCD(q,p%q)
s = 0
k = int(sys.stdin.readline())
for i in range(1,k+1):
	for j in range(1,k+1):
		for m in range(1,k+1):
			s += GCD(GCD(i,j),m)
sys.stdout.write(str(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):
        for c in range(1, K + 1):
            ans += gcd(gcd(a, b), c)
print(ans)

Submission status: Accepted

Solution code:
def getN():
    return int(input())

# def gcd(p, q):
#     if p % q == 0: return q
#     return gcd(q, p % q)

def gcd(p, q):
    while q != 0:
        r = p % q
        p = q
        q = r
    return p

def main():
    
    K = getN()

    result = 0

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

    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

from math import gcd

def gcd2(p, q):  # p > qのこと！
    if p % q == 0:
        # print('result: ' + str(q))
        return q
    else:
        return gcd(q, p % q)


K = int(input())

ans = 0

for i in range(1, K+1):
    for j in range(1, K+1):
        # gcd_ij = gcd2(max(i, j), min(i, j))
        gcd_ij = gcd(i, j)
        for k in range(1, K+1):
            # gcd_ijk = gcd2(max(gcd_ij, k), min(gcd_ij, k))
            gcd_ijk = gcd(gcd_ij, k)
            # print(i, j, k, gcd_ij, gcd_ijk)
            ans += gcd_ijk

print(ans)

# print(max(2, 3))
# print(min(2, 3))
#
# print(gcd(max(2, 3), min(2, 3)))

Submission status: Accepted

Solution code:
ans = [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(ans[n - 1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Sum of gcd of Tuples (Easy)
from math import gcd


def main():
    K = int(input())
    gcd_three = lambda x, y, z: gcd(gcd(x, y), z)
    ans = sum(
        gcd_three(i, j, k)
        for i in range(1, K + 1)
        for j in range(1, K + 1)
        for k in range(1, K + 1)
    )
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# C - Sum of gcd of Tuples (Easy)
from math import gcd


def main():
    K = int(input())
    gcd_three = lambda x, y, z: gcd(gcd(x, y), z)
    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_three(i, j, k)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

K = int(readline())

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:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        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")

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:
def gcd(x, y): 
    while(y): 
        x, y = y, x % y 
    return x 

sum = 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):
            sum += gcd(gcd(i,j),k)

print(sum)