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
k = int(input())
b = 3*k*k - 3*k + 1
for i in range(2,k+1):
    for j in range(2,k+1):
        ij = math.gcd(i,j)
        for m in range(2,k+1):
            b += math.gcd(ij,m)
print(b)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

K = int(input())

total = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        ab = math.gcd(a, b)
        if ab == 1:
            total += K
        else:
            for c in range(1, K + 1):
                total += math.gcd(ab, c)

print(total)

Submission status: Accepted

Solution code:
import math

K = int(input())

total = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        ab = math.gcd(a, b)
        for c in range(1, K + 1):
            total += math.gcd(ab, c)

print(total)

Submission status: Accepted

Solution code:
k = int(input())

from math import gcd
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):
            ans += gcd(ab, c)
print(ans)

Submission status: Accepted

Solution code:
k = int(input())

from math import gcd
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:
import math
K = int(input())
ans = 0
d = [[math.gcd(i, j) for i in range(0, K+1)] for j in range(0, K+1)]

for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            ans += d[i][d[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())
res = 0
for c in range(1,k+1):
    for b in range(1,k+1):
        gcd_cd = math.gcd(c,b)
        for a in range(1,k+1):
            # res += gcd(a,b,c)
            gcd_abc = math.gcd(a,gcd_cd)
            res += gcd_abc
print(res)

Submission status: Accepted

Solution code:
from math import gcd
counter = 0
K = int(input())
for a in range(1,K+1):
    for b in range(1,K+1):
        if gcd(a,b)==1:
            counter += K
        else:
            for c in range(1,K+1):
                d = gcd(a,b)
                counter += gcd(c,d)


print(counter)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
sum = 0
lst = []
for a in range(1,k+1):
  for b in range(1,k+1):
      g = gcd(a,b)
      lst.append(g)
for c in range(1,k+1):
    for i in range(len(lst)):
        sum += gcd(lst[i],c)     
print(sum)

Submission status: Accepted

Solution code:
K=int(input())

Ans=0
from math import gcd
for i in range(K):
    for j in range(K):
        tempA=gcd(i+1,j+1)
        for k in range(K):
            Ans+=gcd(tempA,k+1)
            
print(Ans)

Submission status: Accepted

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

Ans=0
from math import gcd
for i in range(K-1):
    for j in range(K-1):
        tempA=gcd(i+1,j+1)
        for k in range(1,K):
            Ans+=gcd(tempA,k)
            
print(Ans)

Submission status: Accepted

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

Ans=0
from math import gcd
for i in range(1,K):
    for j in range(1,K):
        tempA=gcd(i,j)
        for k in range(K-1):
            Ans+=gcd(tempA,k+1)
            
print(Ans)

Submission status: Accepted

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

Ans=0
from math import gcd
for i in range(1,K):
    for j in range(1,K):
        tempA=gcd(i,j)
        for k in range(1,K):
            Ans+=gcd(tempA,k)
            
print(Ans)

Submission status: Accepted

Solution code:
def gcd(a,b):
    if a < b:
        a, b = b, a
    if mem[a][b]:
        return mem[a][b]
    if b == 0:
        return a
    mem[a][b] = gcd(b, a % b)
    return mem[a][b]

k = int(input())
ans = 0
mem = [[0 for a in range(k+1)]for b in range(k+1)]
n = k+1
for i in range(1,n):
    for j in range(1,n):
        d = gcd(i,j)
        for k in range(1,n):
            ans += gcd(d,k)
print(ans)

Submission status: Accepted

Solution code:
k = int(input())

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

s = 0

for a in range(1, k + 1):
    #print(f'a={a}')
    s += a # 同じ数
    for b in range(a + 1, k + 1):
        if a == b:
            print(a, b)
            continue
        #print(f' b={b}', 6)
        s += 6 * gcd(a, b)
        for c in range(b + 1, k + 1):
            if a == c or b == c:
                print(a, b, c)
                continue
            #print(f'  c={c}')
            s += 6 *  gcd(a, gcd(b, c)) # 全部違う
        
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):
        d = gcd(a, b)
        for c in range(1, k+1):
            ans += gcd(d, c)
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):
    x=math.gcd(i, j)
    for k in range(1, K+1):
      _gcd=math.gcd(x, k)
      count += _gcd
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

gcd_cnt = defaultdict(int)
ans = 0
k = int(input())
for a in range(1, k+1):
    for b in range(1, k+1):
        gcd_cnt[gcd(a, b)] += 1

for key in gcd_cnt:
    for c in range(1, k+1):
        ans += gcd(c, key) * gcd_cnt[key]
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

comb_count = {1:1, 2:3, 3:6}
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):
            ans += gcd(a, b, c) * comb_count[len(set([a, b, c]))]
print(ans)

Submission status: Accepted

Solution code:
import math
K=int(input())
ans = []
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.append(math.gcd(g,c))
print(sum(ans))

Submission status: Accepted

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

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


K = int(input())
G = [[0]*K for i in range(K)]
x=0
for i in range(K):
    for j in range(K):
        g1=gcd(i+1,j+1)
        G[i][j]=g1

for i in range(K):
    for j in range(K):
        for k in range(K):
            g1=G[i][j]
#            print("{}".format(g1))
            x=x+G[g1-1][k]
#        for k in range(K):
#            g2=gcd(g1,k+1)
#            x=x+g2
            

print("{}".format(x))

Submission status: Accepted

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10**7)
input = lambda: sys.stdin.readline().strip()

def main():
    K = int(input())
    num_list = np.array([i for i in range(1,K+1)])
    print(np.sum(np.gcd.outer(np.gcd.outer(num_list,num_list),num_list)))

main()

Submission status: Accepted

Solution code:
import itertools
import math
import sys
import numpy as np
sys.setrecursionlimit(10**7)
input = lambda: sys.stdin.readline().strip()


def main():
    K = int(input())
    num_list = np.array([i for i in range(1,K+1)])
    print(np.sum(np.gcd.outer(np.gcd.outer(num_list,num_list),num_list)))

main()

Submission status: Accepted

Solution code:
from math import gcd

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

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

    print(t)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def gcd(p, q) :
	if p % q == 0 :
		return q
	else :
		return gcd(q,p%q)
if __name__ == '__main__':
	k = int(input())
	a = []
	ans = 0
	for i in range(1,k+1) :
		for j in range(1,k+1) :
			a.append(gcd(i,j))
	for i in range(1,k+1) :
		for j in a :
			ans += gcd(i,j)
	print(ans)

Submission status: Accepted

Solution code:
#ABC162c
from math import gcd
K = int(input())+1
Sum = 0
for i in range(1,K):
    for j in range(1,K):
        d = gcd(i,j)
        for k in range(1,K):
            Sum+=gcd(d,k)

print(Sum)

Submission status: Accepted

Solution code:
k = int(input())

s = 0
# 3つ違う
for i1 in range(1, k+1):
    s = s + i1
    for i2 in range(1, i1):
        x1, x2 = i1, i2
        while x2 != 0:
            x1, x2 = x2, x1 % x2
        gcd1 = x1
        s = s + gcd1 * 6
        for i3 in range(i1+1, k+1):
            x1, x2 = gcd1, i3
            while x2 != 0:
                x1, x2 = x2, x1 % x2
            gcd2 = x1
            s = s + gcd2 * 6

print(s)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)

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

x = int(input())

ans=0

for a in range(1,x+1):
    for b in range(1,a+1):
        for c in range(1,b+1):
            gcd1=gcd(a,b)
            gcd2=gcd(gcd1,c)
            if a==b and b==c:
                ans += gcd2
            elif a==b or b==c or c==a:
                ans += 3*gcd2
            else:
                ans += 6*gcd2

print(ans)

Submission status: Accepted

Solution code:
k = int(input())

s = 0
for i1 in range(1, k+1):
    for i2 in range(1, i1+1):
        x1, x2 = i1, i2
        while x2 != 0:
            x1, x2 = x2, x1 % x2
        gcd1 = x1
        s2 = i1 == i2
        for i3 in range(i1, k+1):
            x1, x2 = gcd1, i3
            while x2 != 0:
                x1, x2 = x2, x1 % x2
            gcd2 = x1

            s3 = i2 == i3 or i1 == i3
            if s2 and s3:
                s = s + gcd2
            elif s2 or s3:
                s = s + gcd2*3
            else:
                s = s + gcd2*6
print(s)

Submission status: Accepted

Solution code:
from math import gcd #or import math
K = int(input())#or K = int(input())+1
plus = 0

for i in range(1,K+1):# range(1,K)
    for a in range(1,K+1):# range(1,K)
        x = gcd(i,a) #or math.gcd(i,a)
        for j in range(1,K+1):# range(1,K)
            plus += gcd(x,j) #or math.gcd(x,j)
            
print(plus)

Submission status: Accepted

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

Submission status: Accepted

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


@lru_cache(maxsize=None)
def _gcd(a, b):
    return math.gcd(a, b)


k = int(input())
C = combinations_with_replacement(range(1, k + 1), 3)

r = 0
for a, b, c in C:
    l = len({a, b, c})
    t = 1
    if l is 3:
        t = 6
    if l is 2:
        t = 3
    r += _gcd(_gcd(a, b), c) * t

print(r)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
from math import gcd
K = int(input())+1
Sum = 0
for i in range(1,K):
    for j in range(1,K):
        d = gcd(i,j)
        for k in range(1,K):
            Sum+=gcd(d,k)

print(Sum)

Submission status: Accepted

Solution code:
from numba import njit
from sys import stdin


@njit(cache=True)
def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


@njit(cache=True)
def fact(x):
    if x == 1:
        return x
    return x * fact(x - 1)


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * fact(3) // fact(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin


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


def fact(x):
    if x == 1:
        return x
    return x * fact(x - 1)


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * fact(3) // fact(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from math import factorial

from sys import stdin


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


def main():
    k = int(stdin.readline())

    ans = 0

    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * factorial(3) // factorial(3 - len(set([a, b, c])) + 1)

    print(ans)


if __name__ == "__main__":
    main()

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

print(total)

Submission status: Accepted

Solution code:
import sys
import os
import itertools

def file_input():
    f = open('Beginner_Contest_162/input.txt', 'r')
    sys.stdin = f

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

def main():
    #file_input()
    K=int(input())
    # map(int, input().split())
    sum=0

    combi_l = itertools.combinations_with_replacement(range(1,K+1),3)
    # print(combi_l)

    for combi in combi_l:
        # print(combi)
        if combi[0]==combi[1]==combi[2]:
            sum+=gcd(gcd(combi[0],combi[1]),combi[2])
        elif combi[0]==combi[1] or combi[1]==combi[2] or combi[2]==combi[0]:
            sum+=3*gcd(gcd(combi[0],combi[1]),combi[2])
        else:
            sum+=6*gcd(gcd(combi[0],combi[1]),combi[2])

    # for a in range(1,K+1):
    #     for b in range(1,K+1):
    #         tmp=fractions.gcd(a,b)
    #         for c in range(1,K+1):
    #             sum+=fractions.gcd(tmp,c)

    print(sum)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
num = (int)(input())
total = 0

for i in range(1, num + 1):
  for j in range(1, num + 1):
    sub = math.gcd(i, j)
    for k in range(1, num + 1):
      total += math.gcd(sub, k)
print(total)

Submission status: Accepted

Solution code:
import math

k=int(input())
s=0
for a in range(1,k+1):
  for b in range(1,k+1):
    ab=math.gcd(a,b)
    for c in range(1,k+1):
      abc=math.gcd(ab,c)
      s+=abc
      
print(s)

Submission status: Accepted

Solution code:
import sys
import math
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(a, gcd(b, c))

print(sum)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

print(ans)

Submission status: Accepted

Solution code:
import itertools
K = int(input())
ans = 0
S = list(range(1,K+1))
def gcd(a,b):
  while a != 0 or b != 0:
    if a % b == 0:
      return min(a,b)
    a, b = b, a % b
for v in itertools.combinations_with_replacement(S, 3):
  if v[0] == v[1] == v[2]:
    ans += v[0]
  elif v[0] != v[1] and v[1] != v[2] and v[0] != v[2]:
    ans += gcd(gcd(v[0],v[1]),v[2])*6
  else:
    ans += gcd(gcd(v[0],v[1]),v[2])*3

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
K = int(input())
ans = 0
S = list(range(1,K+1))
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) 
for v in itertools.combinations_with_replacement(S, 3):
  if v[0] == v[1] == v[2]:
    ans += v[0]
  elif v[0] != v[1] and v[1] != v[2] and v[0] != v[2]:
    ans += gcd(gcd(v[0],v[1]),v[2])*6
  else:
    ans += gcd(gcd(v[0],v[1]),v[2])*3

print(ans)

Submission status: Accepted

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

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)
        for c in range(1, K + 1):
            abc = math.gcd(ab, c)
            ans += abc

print(ans)

Submission status: Accepted

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


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


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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  6 01:11:37 2020

@author: shinba
"""
import math
from functools import reduce

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
k = int(input())+1
def gcd(a,b):
  if a < b:
    a, b = b, a
  while b!=0:
    a, b = b ,a%b
  return a
res = 0
for a in range(1,k):
  for b in range(1,k):
    for c in range(1,k):
      res += gcd(a,gcd(b,c))

print(res)

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:
import math
sum=0
K=int(input())+1
for a in range(1,K):
  for b in range(1,K):
    ab=math.gcd(a,b)
    for c in range(1,K):
      sum+=math.gcd(ab,c)
print(sum)

Submission status: Accepted

Solution code:
K=int(input())

import math
total=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)
            e=math.gcd(d,c)
            total=total+e
print(total)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
A = int(input())
# 最小公倍数
ans = 0
from math import gcd


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

Submission status: Accepted

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

k = int(input())
ab = 0
abc = 0
ans = 0

for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ab = calc(b,a)
            abc = calc(c,ab)
            ans += abc

print(str(ans))

Submission status: Accepted

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

ll = []
for i in range(1, k + 1):
    l = []
    for j in range(1, i + 1):
        if i % j == 0:
            l.append(j)
            l.append(i // j)
        if (i // j) < j:
            break
    ll.append(set(l))


sum_k = 0

for i in range(k):
    i_s = ll[i]
    for j in range(k):
        j_s = ll[j]
        m = max(ll[i] & ll[j])
        if m == 1:
            sum_k += k
            continue
        for l in range(k):
            sum_k += max(ll[i] & ll[j] & ll[l])

print(sum_k)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    k = I()
    r = 0
    for a,b,c in itertools.product(range(1,k+1), repeat=3):
        r += math.gcd(math.gcd(a,b), c)

    return r


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')
#from fractions import gcd
from math import gcd

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


if __name__ == '__main__':
    solve()

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())
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
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
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(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())
ans=[]
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            ans.append(gcd(i,j,k))
print(sum(ans))

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
k = int(input())
a = 0
for i in range(1,k+1):
  for j in range(1,k+1):
    d = np.math.gcd(i,j)
    for l in range(1,k+1):
      e = np.math.gcd(d,l)
      a += e
print (a)

Submission status: Accepted

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

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

ans = 0
for i in range(1, k+1):
    count = count_lst[i-1]

    for j in range(1,k+1):
        g = math.gcd(i,j)
        ans += count * g

print(ans)

Submission status: Accepted

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

K = readInt()
d = defaultdict(int)
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())

sum = 0

for a in range(1, k+1):
  for b in range(1, k+1):
    for c in range(1, k+1):
      tmp = math.gcd(a, b)
      sum += math.gcd(tmp, c)

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
gcd(a,b,c) = gcd(a, gcd(b,c))
"""

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
K = int(readline())

from math import gcd

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(i, gcd(j,k))
            
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools

k = int(input().strip())
gcdsum = 0

for a, b, c in itertools.combinations_with_replacement(range(1, k + 1), 3):
    if a == b and b == c:
        gcdsum += math.gcd(math.gcd(a, b), c) * 1
    elif a != b and b == c:
        gcdsum += math.gcd(math.gcd(a, b), c) * 3
    elif a == b and b != c:
        gcdsum += math.gcd(math.gcd(a, b), c) * 3
    elif a != b and b != c:
        gcdsum += math.gcd(math.gcd(a, b), c) * 6
print(gcdsum)

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):
        ij=gcd(i,j)
        for k in range(1,k+1):
            ans+=gcd(ij,k)
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):
        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())

total=0
for a in range(1,k+1):
  for b in range(1,k+1):
    ab=math.gcd(a,b)
    for c in range(1,k+1):
      total+=math.gcd(ab,c)
print(total)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, gcd
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
# from fractions import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

K = I()

ans = 0
for a, b, c in combinations_with_replacement(range(1, K+1), 3):
    # print(a, b, c)
    score = gcd(a,gcd(b,c))
    # print(score)
    l = len({a, b, c})
    # print(l)
    if l == 3:
        ans += score*6
    elif l == 2:
        ans += score*3
    else:
        ans += score

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

print(ans)