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 sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

K = INT()

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

K = INT()

@lru_cache(maxsize=None)
def memo_gcd(a, b):
	return gcd(a, b)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

k = int(input())
sum = 0

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

Submission status: Accepted

Solution code:
from math import gcd
n=int(input())
a=0
for i in range(1,n+1):
    for j in range(1,n+1):
        c=gcd(i,j)
        for h in range(1,n+1):
            a+=gcd(c,h)
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

A=[0]*K

for i in range(K,0,-1):
    b=pow(K//i,3)
    A[i-1]=b

for i in range(K,0,-1):
    for j in range(K//i-1):
        c=i*(j+2)
        A[i-1]-=A[c-1]

s=0
for i in range(K):
    s+=A[i]*(i+1)

print(s)

Submission status: Accepted

Solution code:
import collections

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

k=int(input())
cnt=collections.defaultdict(int)
for i in range(1,k+1):
  for j in range(1,k+1):
    cnt[gcd(i,j)]+=1
ans=0
for i in range(1,k+1):
  for key in cnt.keys():
    ans+=gcd(i,key)*cnt[key]
print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
うんち = 0
for でびさま in range(1,k+1):
    for でびでび in range(1,k+1):
        for でびる in range(1,k+1):
            うんち += gcd(でびさま,gcd(でびでび,でびる))
print(うんち)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input())
うんち = 0
for デビ in range(1,k+1):
    for デビデ in range(1,k+1):
        for ビル in range(1,k+1):
            うんち += gcd(デビ,gcd(デビデ,ビル))
print(うんち)

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys
from math import gcd

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

K = INT()

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:

count=0
import math
n=int(input())
a=1
b=1
c=1
while c<=n:
    while b<=n:
        while a<=n:
            d=math.gcd(a,b)
            e=math.gcd(d,c)
            count+=e
            a+=1
        a=1
        b+=1
    b=1
    c+=1
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())
ans = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys
from math import gcd
from functools import reduce

input = sys.stdin.readline


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


def main():
    k = int(input().strip())
    ans=0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for k in range(1,k+1):
                ans += find_gcd([i,j,k])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
K = int(input())

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

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

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

sum3 = 0
for a in range(1, K+1):
  sum3 += a

sum = sum1*6 + sum2*6 + sum3
print(sum)

Submission status: Accepted

Solution code:
import sys

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

k = int(sys.stdin.readline().rstrip())

def main():
  res = 0
  cnt = [0] * (k + 1)
  for i in range(1, k + 1):
    for j in range(1, k + 1):
      cnt[gcd(i, j)] += 1
  for i in range(1, k + 1):
    for j in range(1, k + 1):
      res += gcd(i, j) * cnt[i]
  print(res)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
import scipy as sp
import numpy as np
import math


N = int(input())

ans = 0
for a in range(1, N+1):
    for b in range(1, N+1):
        d = math.gcd(a, b)
        for c in range(1, N+1):

            ans += math.gcd(c, d)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
K = int(input())

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

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

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

sum3 = 0
for a in range(1, K+1):
  sum3 += a

sum = sum1*6 + sum2*6 + sum3
print(sum)

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce
# import itertools
from collections import defaultdict

a=int(input())

sum=0


for x in range(1,a+1):
    for y in range(1,a+1):
        for z in range(1,a+1):
            sum+=gcd(gcd(x,y),z)


print(sum)

Submission status: Accepted

Solution code:
from math import gcd

line = int(input())

ans = 0

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

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction
from math import gcd
# fractions.gcd() を用いること
# math.log2()はatcoderでは対応していない．留意せよ．

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


def lcm(n, m):
    return int(n * m / gcd(n, m))


def coprimize(p, q):
    common = fractions.gcd(p, q)
    return (p // common, q // common)


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


def main():
    k = int(input().strip())
    ans=0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for k in range(1,k+1):
                ans += find_gcd([i,j,k])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for a in range(1,K + 1):
    for b in range(a + 1,K + 1):
        for c in range(b + 1,K + 1):
            j = gcd3(a,b,c)
            i = i + j

for a in range(1,K + 1):
    for b in range(a + 1,K + 1):
        o = gcd(a,b)
        p = p + o

d = int((1 + K)* K /2)

print(6*i + d + 6*p)

Submission status: Accepted

Solution code:
from math import gcd

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

Submission status: Accepted

Solution code:
import math
# from itertools import product
from functools import reduce

def main():
#     def gcd(*numbers): return reduce(math.gcd, numbers)
    K = int(input())
    ans = 0
#     valiation = list(itertools.product(range(1, K+1), repeat=3))
#     for v in valiation:
#         ans += gcd(v)
    for i in range(1, K+1):
        for j in range(i, K+1):
            for k in range(j, K+1):
                x = math.gcd(math.gcd(i, j), k)
                if i == j == k:
                    ans += x
                elif i != j and j != k and k != i:
                    ans += 6 * x
                else:
                    ans += 3 * x
    print(ans)
        
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
k = int(input())
n = 3
c = {i: pow(k//i, n) for i in range(1, k+1)}
for i in range(k, 0, -1):
    for j in range(2*i, k+1, i):
        c[i] -= c[j]
_sum = 0
for x in range(1, k+1):
    _sum += c[x] * x
print(_sum)

Submission status: Accepted

Solution code:
K = int(input())
def gcd(x,y):
    if x<y:x,y = y,x
    while y!=0:
        r = x%y
        x = y
        y = r
    return x
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:
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:
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(gcd(a, b),c)

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
from functools import reduce
from itertools import product

def resolve():
  K = int(input())
  # result = sum([ gcd(gcd(x[0], x[1]), x[2]) for x in itertools.product(range(1, K+1), range(1, K+1), range(1, K+1)) ])
  step1 = [ gcd(x[0], x[1]) for x in product(range(1, K+1), range(1, K+1)) ]
  result = sum([ gcd(x[0], x[1]) for x in product(step1, range(1, K+1)) ])
  print(result)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import math
from functools import lru_cache

K = int(input())
ans = 0

@lru_cache(maxsize=1000)
def main(K):
    g=0
    for i in range(1,K+1):
        for j in range(1,K+1):
            for k in range(1,K+1):
                g += math.gcd(math.gcd(i, j), k)
    return (g)
 
print(main(K))

Submission status: Accepted

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

K = INT()
dic = []
res = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        dic.append(math.gcd(i, j))

for i in range(1, K+1):
    for temp in dic:
        res += math.gcd(temp,i)

print(res)

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())
ans = 0

def main(K):
    g=0
    for i in range(1,K+1):
        for j in range(1,K+1):
            for k in range(1,K+1):
                g += math.gcd(math.gcd(i, j), k)
    return (g)
 
print(main(K))

Submission status: Accepted

Solution code:
K = int(input())
total = 0

def gcd(a, b):
    if(a > b):
        a, b = b, a

    while(b > 0):
        r = a % b
        a = b
        b = r

    return a

dp = [[0 for i in range(K+1)] for j in range(K+1)]

for a in range(1, K+1):
    for b in range(1, K+1):
        if(dp[a][b] == 0):
            dp[a][b] = gcd(a, b)

        res = dp[a][b]

        for c in range(1, K+1):
            if(dp[res][c] == 0):
                dp[res][c] = gcd(res, c)

            total += dp[res][c]


print(total)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

#from math import gcd
from functools import reduce

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

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

K = ii()
ans = 0

memo = dp3(-1, K+1, K+1, K+1)

for i in range(1, K+1):
    for j in range(i+1, K+1):
        for k in range(j+1, K+1):
            ans += gcd_list([i, j, k])*6

for i in range(1, K+1):
    for j in range(1, K+1):
        if i!=j:
            ans += gcd_list([i, i, j])*3

for i in range(1, K+1):
    ans += gcd_list([i, i, i])


print(ans)

Submission status: Accepted

Solution code:
from math import gcd
from numba import jit

@jit
def calc(): 
  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)
calc()

Submission status: Accepted

Solution code:
from math import gcd
from collections import defaultdict

k = int(input())

memo = defaultdict(defaultdict)

rg = range(1, k + 1)
for i in rg:
    for j in rg:
        memo[i][j] = memo[j][i] = gcd(i, j)

ret = 0
for i in rg:
    for j in rg:
        tmp = memo[i][j]
        for k in rg:
            res = memo[k][tmp]
            ret += res

print(ret)

Submission status: Accepted

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

result=[]
count=0
cnt=0


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


print(cnt)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
from math import gcd
from functools import reduce

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

K = ii()
ans = 0

memo = dp3(-1, K+1, K+1, K+1)

for i in range(1, K+1):
    for j in range(1, K+1):
        for k in range(1, K+1):
            #l = sorted([i, j, k])
            #l = [i, j, k]
            #a, b, c = l[0], l[1], l[2]
            #a, b, c = i, j, k
            #if memo[a][b][c] != -1:
                #ans += memo[a][b][c]
            #else:
            #memo[i][j][k] = gcd_list([i, j, k])
                #memo[i][k][j] = memo[k][i][j] = memo[k][j][i] = memo[j][i][k] = memo[j][k][i] = memo[i][j][k]
                #memo[i][k][j] = memo[i][j][k]
                #memo[k][i][j] = memo[i][j][k]
                #memo[k][j][i] = memo[i][j][k]
                #memo[j][j][i] = memo[i][j][k]
                #memo[j][i][k] = memo[i][j][k]
            ans += gcd_list([i, j, k])
            #ans += gcd_list([i, j, k])


print(ans)

Submission status: Accepted

Solution code:
import math
N=int(input())
C=0
 
for l in range(N):
    for n in range(N):
        for m in range(N):
            if l==0 or m==0 or n==0:
                C+=1
            else:
                C+=math.gcd(math.gcd(l+1,m+1),n+1)
 
print(C)

Submission status: Accepted

Solution code:
import math
k=int(input())
l=[]
s=0
for x in range(1,k+1):
  for y in range(1,k+1):
    l.append(math.gcd(x,y))

for x in range(len(l)):
  for y in range(1,k+1):
    s+=math.gcd(l[x],y)
print(s)

Submission status: Accepted

Solution code:
def gcd(x, y): 
    while(y): 
        x, y = y, x % y 
  
    return x
n = int(input())
sums = 0
for i in range(1,n+1):
    for k in range(1,n+1):
        for j in range(1,n+1):
            num = gcd(i,j)
            nums = gcd(num,k)
            sums += nums
print(sums)

Submission status: Accepted

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


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


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

Submission status: Accepted

Solution code:
import sys
from math import gcd

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    K = int(rl())
    ans = 0
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                tmp = gcd(i, j)
                tmp = gcd(tmp, k)
                ans += tmp
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import math
# from itertools import product
from functools import reduce

def main():
#     def gcd(*numbers): return reduce(math.gcd, numbers)
    K = int(input())
    ans = 0
#     valiation = list(itertools.product(range(1, K+1), repeat=3))
#     for v in valiation:
#         ans += gcd(v)
    for i in range(1, K+1):
        for j in range(1, K+1):
            for k in range(1, K+1):
                ans += math.gcd(math.gcd(i, j), k)
    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):
    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 s in range(1,k+1):
  for t in range(1,k+1):
    for u in range(1, k+1):
      ans += gcd(gcd(s,t),u)
print(ans)

Submission status: Accepted

Solution code:
def gcb(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        gcb_table[a][b] = a
    else:
        if gcb_table[a][b] == -1:
            gcb_table[a][b] = gcb(b, a%b)
    return gcb_table[a][b]

k = int(input())
gcb_table = [[-1 for _ in range(k+1)] for _ in range(k+1)]

for i in range(1, k+1):
    for j in range(1, k+1):
        temp = gcb(i, j)
        gcb_table[i][j] = temp
        gcb_table[j][i] = temp
        
res = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        gcb_first = gcb_table[a][b]
        for c in range(1, k+1):
            res += gcb_table[gcb_first][c]
print(res)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
K = INT()
sum = 0
 
for n in range(K):
  for m in range(K):
    for l in range(K):
      sum += gcd(n+1, gcd(m+1, l+1))
 
print(sum)

Submission status: Accepted

Solution code:
import math
from functools import reduce
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
K = int(input())
 
 
ans = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
           
 
                ans += gcd(i+1,j+1,k+1)
            #1-Kまで
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def main():
  n = int(input())
  l = 0
  for a in range(1, n+1):
     for b in range(1, n+1):
        d = simple_gcd(a,b)
        for c in range(1, n+1):
          l += simple_gcd(d,c)
  print(l)
main()

Submission status: Accepted

Solution code:
import functools
from math import gcd
n = int(input())
@functools.lru_cache(None)
def main(n):
  g=0
  for i in range (1, n+1):
    for j in range (1, n+1):
      for k in range (1, n+1):
        g  += gcd(gcd(i,j),k)
  return (g)
print(main(n))

Submission status: Accepted

Solution code:
from collections import *
from heapq import *
from itertools import *
#from fractions import gcd

from decimal import *
import copy
from bisect import *
import time
import sys
import math

input=lambda :sys.stdin.readline().rstrip()
K=int(input())
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))

sys.setrecursionlimit(10**7)
z=0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            z+=gcd(a,gcd(b,c))
print(z)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import math

K = int(input())
n = 0
A = []
for i in range(1, K+1):
    for j in range(1, K+1):
        A.append(math.gcd(i,j))
for a in A:
    for k in range(1, K+1):
        n += math.gcd(a, k)
print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

nums = int(input())
result = 0

gcd_dict = {}

for i in  range(1, nums+1):
    for j in range(1, nums+1):
        for l in range(1, nums+1):
            result += gcd(gcd(i, j), l)

print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 21:45:38 2020
2020-04-13 00:14:06にPython3で提出したのと同じコードを、PyPy3で再提出した。
@author: H_Hoshigi
"""
 
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):
            answer += math.gcd(math.gcd(a, b), c)
print(answer)

Submission status: Accepted

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

def resolve():
  K = int(input())
  gcds = sum([ gcd(gcd(x[0], x[1]), x[2]) for x in itertools.product(range(1, K+1), range(1, K+1), range(1, K+1)) ])
  print(gcds)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
from math import gcd

N = int(input())

ans = 0

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

def gcd_list(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_list([i, j, k])

print(ans)

Submission status: Accepted

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

#i<j<k
for i in range(1, K-1):
    for j in range(i+1, K):
        for k in range(j+1, K+1):
            gcd_ij_1 = math.gcd(i, j)
            gcd_ijk_1 = math.gcd(gcd_ij_1, k)
            ans += gcd_ijk_1 * 6

for l in range(1, K):
    for m in range(l+1, K+1):
        gcd_lm_1 = math.gcd(l,m)
        ans += gcd_lm_1 * 6

for n in range(1, K+1):
    ans += n

print(ans)

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime
# ABC162-C
# 3つ以上の数の最大公約数
# 制約 1 <= N <= 200
# 三つの最大公約数はgcd(gcd(a,b), c)
K = iin()
 
ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        # 三重ループをする前に、ここでa,bの最大公約数を求めておく
        # 三重ループでa,b,cの最大公約数を求めるとTLE
        ab = 1
        if a == 1 or b == 1:
            ab = 1
        elif a == b:
            ab = a
        else:
            ab = math.gcd(a, b)
        for c in range(1, K+1):
            if ab == 1 or c == 1:
                ans += 1
            elif ab == c:
                ans += c
            else:
                ans += math.gcd(ab, c)
print(ans)

Submission status: Accepted

Solution code:
k = int(input())

count = [pow(k // max(i, 1), 3) for i in range(k + 1)]

for i in range(k, 0, -1):
  for j in range(2 * i, k + 1, i):
    count[i] -= count[j]

sum_ = 0
for i in range(k + 1):
  sum_ += i * count[i]

print(sum_)