Problem:


Score : 300 points

Problem Statement

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

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

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

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

Sample Input 1

2

Sample Output 1

9

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

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692



Sample input:
2


Sample output:
9

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


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


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True

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


def gcd_n(a, n):
    ans = a[0]
    for i in range(1, n):
        ans = gcd(ans, a[i])
    return ans


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


def main():
    K = I()
    ans = 0

    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                # print(a, b, c, gcd_n([a, b, c], 3))
                ans += gcd_n([a, b, c], 3)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
def gcd(a, b) : 
    while a :
        a, b = b%a, a 
    return b 

def c(K) :
    dp = defaultdict(int)
    ans = 0
    for i in range(1, K+1) :
        for j in range(1, K+1) :
            if i > j and (j,i) not in dp :
                temp = dp[(j,i)]
            else : 
                dp[(i,j)] = gcd(i,j)
                temp = dp[(i,j)]  
            for k in range(1, K+1) : 
                if k > temp :
                    q = (temp, k)
                else :
                    q = (k, temp)
                if q not in dp :
                    dp[q] = gcd(temp,k )
                ans += dp[q]
    return ans 

if __name__ == "__main__" :
    k = int(input())
    print(c(k))

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
#!/bin/bash
''':'
docker run --rm -it -v $(cd $(dirname $0) && pwd):/vo:ro pypy:3-2.4.0 \
   /bin/bash -c "pypy3 /vo/$(basename $0) < /vo/input.txt"
exit $?
'''
import sys


def _i(): return int(sys.stdin.readline().strip())


def _gcd(a, b):
    a, b = max(a, b), min(a, b)
    if a % b == 0:
        return b
    return _gcd(b, a % b)


def main():
    gcd = [[1]*201 for _ in range(201)]
    for i in range(1, 201):
        for j in range(i, 201):
            g = _gcd(i, j)
            gcd[i][j] = gcd[j][i] = g

    k = _i() + 1
    ans = 0
    for l in range(1, k):
        for m in range(1, k):
            glm = gcd[l][m]
            for n in range(1, k):
                ans += gcd[glm][n]
    return ans


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

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())

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

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

Submission status: Accepted

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

ans = 0

lst = [[0 for _ in range(1, k+1)] for __ in range(k+1)]
cnt = [0 for _ in range(1, k+1)]


for i in range(1, k+1):
    for j in range(1, k+1):
        lst[i-1][j-1] = [gcd(i, j)]
        cnt[gcd(i, j)-1] += 1

ans = 0

for i in range(k):
    for j in range(1, k+1):
        ans += cnt[i]*gcd((i+1),j)

print(ans)

Submission status: Accepted

Solution code:
import math
from itertools import product

K = int(input())
s = 0
for a, b, c in product(range(1, K + 1), repeat=3):
    s += math.gcd(math.gcd(a, 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):
        a_b_gcd = gcd(a, b)
        for c in range(1, K+1):
            ans += gcd(a_b_gcd, c)
print(ans)

Submission status: Accepted

Solution code:
K=int(input())

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

Submission status: Accepted

Solution code:
import math

k=int(input())
tochu={}
for i in range(1,k+1):
    for q in range(1,k+1):
        res=math.gcd(i,q)
        if res in tochu:
            tochu[res]+=1
        else:
            tochu[res]=1

ans=0
for i in range(1,k+1):
    for ele in tochu:
        ans+=math.gcd(i,ele)*tochu[ele]
print(ans)

Submission status: Accepted

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

def lcm(x, y):
    return x//gcd(x, y)*y

K = int(input())

GCD = [[0 for _ in range(201)] for _ in range(201)]
for i in range(1, 201):
    for j in range(1, 201):
        GCD[i][j] = gcd(i, j)

ans = 0
for a in range(1, K+1):
    for b in range(a, K+1):
        for c in range(b, K+1):
            # t = gcd(gcd(a, b), c)
            t = GCD[GCD[a][b]][c]
            if (a == b and b < c ) or (a < b and b == c):
                ans += 3*t
            elif a < b and b < c:
                ans += 6*t
            else:
                ans += t

print(ans)

Submission status: Accepted

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

ans=n*(n+1)//2

for i in range(1,n+1):
    for j in range(i+1,n+1):
        ans+=gcd(i,j)*6
        
for i in range(1,n+1):
    for j in range(i+1,n+1):
        for r in range(j+1,n+1):
            ans+=gcd(gcd(i,j),r)*6
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):
      tmp = gcd(a, b)
      if tmp == 1:
         ans += 1 * k
      else:
         for c in range(1, k+1):
            ans += gcd(tmp, c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
from functools import reduce
#import math
K = int(input())

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

def gcd2(number):
    return reduce(gcd, number)

ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K+1):
            ans += gcd2([i,j,k])
print(ans)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math


def read():
    K = int(input())
    return K,

def solve(K):
    ans = 0
    cache = [[math.gcd(i, j) for j in range(K+1)] for i in range(K+1)]
    for a in range(1, K+1):
        for b in range(1, K+1):
            for c in range(1, K+1):
                bc = cache[b][c]
                abc = cache[a][bc]  
                ans += abc
    return ans


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq

# 最大公約数
def gcd(m,n):
  x = max(m,n)
  y = min(m,n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z

#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]


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

Submission status: Accepted

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

from math import gcd

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

a = 0
for i in range(1, N+1):
    for j in range(1, N+1):
        b = math.gcd(i, j)
        for k in range(1, N+1):
            a += math.gcd(b, k)
print(a)

Submission status: Accepted

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

n = int(input())
res = 0

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
K = int(input())
b = []
ans = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        b.append(math.gcd(i, j))
n = len(b)
for i in range(n):
    for k in range(1, K+1):
        ans += math.gcd(b[i], k)
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):
		ab = math.gcd(a,b)
		for c in range(1, K+1):
			sum+=math.gcd(ab,c)

print(sum)

Submission status: Accepted

Solution code:
from math import gcd
ans = 0
k = int(input())
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())

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

print(total_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(a, K + 1):
        for c in range(b, K + 1):
            if a == b == c:
                n = 1
            elif a == b or b == c or c == a:
                n = 3
            else:
                n = 6
            ans += n * gcd(gcd(a, 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 l in range(1,k+1):
      ans+=math.gcd(a,l)
print(ans)

Submission status: Accepted

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

import sys
import math

sys.setrecursionlimit(10 ** 6)

input_line = input()
inputs = input_line.split(' ')
K, = [int(s) for s in inputs]

ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        g1 = math.gcd(a, b)
        for c in range(1,K+1):
            g2 = math.gcd(g1, c)
            ans += g2
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(a,k+1):
    for c in range(b,k+1):
      ans+=math.gcd(math.gcd(a,b),c)*(1 if a==b==c else (3 if a==b or b==c else 6))
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
from math import gcd
K = int(input())
ans = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        a1 = gcd(i, j)
        for k in range(1, K+1):
            ans += gcd(a1, k)
print(ans)
# print(gcd(1, gcd(2, 200)))

Submission status: Accepted

Solution code:
# coding: utf-8
import math
N = int(input())
ans = 0
for i in range(1,N+1):
    for j in range(i,N+1):
        for k in range(j,N+1):
            if len(set([i,j,k])) == 3:
                cnt = 6
            elif len(set([i,j,k])) == 2:
                cnt = 3
            else:
                cnt = 1
            ans += math.gcd(math.gcd(i,j),k) * cnt
            
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):
    for l in range(1,k+1):
      g=math.gcd(i,j)
      ans+=math.gcd(g,l)
print(ans)

Submission status: Accepted

Solution code:
from math import *
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:
K = int(input())

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

#cache = {}

def gcd(a, b, c):
#	nums = [a, b, c]
#	nums.sort()
#	nums = tuple(nums)
	nums = (a, b, c)
#	if nums in cache:
#		return cache[nums]
	
	d = calc_gcd(nums[0], nums[1])
	d = calc_gcd(d, nums[2])
	
#	cache[nums] = d
	return d

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

print(total)

Submission status: Accepted

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

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

import math
from functools import reduce

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

sum=0

for i in range(1,K+1):
    for j in range(1,K+1):
        for n in range(1,K+1):
            sum+=gcd(i,j,n)

print(sum)

Submission status: Accepted

Solution code:
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

def gcd(a, b):  # a >= b
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
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 sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict
from fractions import gcd

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	K = int(input())
	ans = 0
	D = defaultdict(int)
	for i in range(1, K + 1):
		for j in range(1, K + 1):
			s = gcd(i, j)
			D[s] += 1

	for i in range(1, K + 1):
		for j in range(1, K + 1):
			ans += gcd(i, j) * D[j]

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import product
from collections import Counter
K = int(input())

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

def list_gcd(l):
    return functools.reduce(gcd,l)

ans = 0
l = []
for i,j in product(range(1,K+1),repeat=2):
    l.append(gcd(i,j))
dic = Counter(l)

for k,v in dic.items():
    for i in range(1,K+1):
        ans += gcd(k,i)*v

print(ans)

Submission status: Accepted

Solution code:
K = int(input())

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

dp = [[0] * (K+1) for _ in range(K+1)]
for i in range(1,K+1):
	for j in range(1,K+1):
		dp[i][j] = gcd(i,j)

ans = 0
for a in range(1,K+1):
	for b in range(1,K+1):
		for c in range(1,K+1):
			tmp = dp[a][b]
			ans += dp[tmp][c]
print(ans)

Submission status: Accepted

Solution code:
def gcd(x, y):
  b = max(x, y)
  s = min(x, y)
  r = b % s
  while r != 0:
    b = s
    s = r
    r = b % s
  return s

def lcm(x, y):
  return (x * y) // gcd(x,y)

K = int(input()) + 1
gcDP = [[0] * K for _ in range(K)]

for i in range(1, K):
  for j in range(1, K):
    gcDP[i][j] = gcd(i, j)

ans = 0
for i in range(1, K):
  for j in range(1, K):
    for k in range(1, K):
      ans += gcd(gcDP[i][j], 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):
        tmp = math.gcd(i, j)
        for la in range(1, k + 1):
            ans += math.gcd(la, tmp)
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

def main():
    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_list([i, j, k])
    print(ans)

main()

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce

K = int(input())

total = 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):
            total += gcd(x, c)
print(total)

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

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

total = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        if memo[a][b] != -1:
            ab = memo[a][b]
        else:
            ab = math.gcd(a, b)
            memo[a][b] = ab
            memo[b][a] = ab
        for c in range(1, k+1):
            if memo[ab][c] != -1:
                total += memo[ab][c]
            else:
                abc = math.gcd(ab, c)
                memo[ab][c] = abc
                memo[c][ab] = abc
                total += abc
print(total)

Submission status: Accepted

Solution code:
K = int(input())

memo = [[None] * (K + 1) for _ in range(K + 1)]

def mygcd(a, b):
    if memo[a][b] != None: return memo[a][b]
    else:
        if a < b:
            ret = mygcd(b, a)
        elif b == 0:
            ret = a
        else:
            ret = mygcd(b, a % b)
        #
        memo[a][b] = ret
        memo[b][a] = ret
        return ret

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

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 sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools
import heapq
#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    n=int(input())
    total=0
    for a,b,c in itertools.product(range(1,n+1),repeat=3):
        temp1=math.gcd(a,b)
        temp2=math.gcd(temp1,c)
        total+=temp2
    print(total)



if __name__ == "__main__":

    main()

Submission status: Accepted

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

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

for i in range(1, k + 1):
    # print(i)
    ans += i
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(a, k + 1):
        x = math.gcd(a, b)
        for c in range(b, k + 1):
            y = math.gcd(x, c)
            if a == b and b == c:
                ans += y
            elif a == b or b == c:
                ans += y * 3
            else:
                ans += y * 6
print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

n = int(input())
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd


G = [[gcd(l, r) for l in range(201)] for r in range(201)]
            
K = int(input())

S = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            S += G[G[a][b]][c]

print(S)

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

print(res)

Submission status: Accepted

Solution code:
import math
N, = (int(x) for x in input().split())

sol = 0
for i in range(1, N+1):
    for j in range(1, N + 1):
        G = math.gcd(i, j)
        if G == 1:
            sol += N
            continue
        for k in range(1, N + 1):
            g = math.gcd(G, k)
            sol += g
print(sol)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K = int(input())
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

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

print(s)

Submission status: Accepted

Solution code:
K=int(input())
ans=0
def gcd(a,b):#最大公約数を返す。Euclid
    if a<b:
        a,b=b,a
    c=a%b
    if c==0:
        return b
    return gcd(b,c)
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            d=gcd(i,j)
            ans+=gcd(d,k)
print(ans)

Submission status: Accepted

Solution code:
k = int(input())

cache = {}
def tgcd(a, b, c):
    def gcd(a, b):
        if a < b:
            a, b = b, a
        if (a, b) in cache:
            return cache[a, b]
        elif b == 0:
            cache[a, b] = a
            return a
        else:
            res = gcd(b, a % b)
            cache[a, b] = res
            return res
    ab = gcd(a, b)
    return gcd(c, ab)

total = 0
for a in range(1, k + 1):
    for b in range(a, k + 1):
        for c in range(b, k + 1):
            local = tgcd(c, b, a)
            if a == b and b == c:
                total += local
            elif a != b and b != c and c != a:
                total += 6 * local
            else:
                total += 3 * local


print(total)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
try:
    from math import gcd
except:
    from fractions import gcd

k = int(input())

memo = [[0] * 201 for _ in range(201)]
for i in range(201):
    for j in range(201):
        memo[i][j] = gcd(i, j)
ret = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            val = memo[a][b]
            ret += memo[val][c]
print(ret)

Submission status: Accepted

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

import math
from functools import reduce

k = int(input())


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


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

print(res)

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools
def gcd(*numbers):
    return reduce(math.gcd, numbers)
k = int(input())
a = [i for i in range(1, k+1)]
ans = 0
for v in itertools.combinations_with_replacement(a, 3):
    if len(set(v)) == 1:
        ans += gcd(v[0], v[1], v[2])
    elif len(set(v)) == 2:
        ans += gcd(v[0], v[1], v[2]) * 3
    else:
        ans += gcd(v[0], v[1], v[2]) * 6
print(ans)

Submission status: Accepted

Solution code:
k=int(input())

import math
from functools import reduce

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

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:
import math
import fractions
import collections
import itertools
def ggcd(a,b):
    if(b==0):
        return a
    return ggcd(b,a%b)
K=int(input())
num=0
a=0
for i in range(1,K+1,1):
    for j in range(1,K+1,1):
        for k in range(1,K+1,1):
            a=ggcd(i,j)
            num=num+ggcd(a,k)

print(num)

Submission status: Accepted

Solution code:
import sys
import math
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

def LII_split():
    return list(map(int, input().split()))

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

K = input_int()

count = 0
for a in range(K):
    ap = a + 1

    for b in range(K):
        bp = b + 1
        gcd_ab = math.gcd(ap, bp)
        for c in range(K):
            count += math.gcd(gcd_ab, c + 1)

print(count)

Submission status: Accepted

Solution code:


"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)

def iin(): return int(input())


def lin(): return list(map(int, input().split()))

# out = []
# range = xrange
# input = raw_input

def main():
    from math import gcd
    n = iin()
    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)

main()
# threading.Thread(target=main).start()

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)