Problem:


Score : 300 points

Problem Statement

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

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

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

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

Sample Input 1

2

Sample Output 1

9

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

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692



Sample input:
2


Sample output:
9

Submission status: Accepted

Solution code:
k = int(input())
def gcd1 (a, b):
    while True:
        if (a < b):
            a, b = b, a
        c = a%b
        if (c == 0):
            return (b)
        else:
            a = b
            b = c

def gcd2 (a, b, c):
    tmp = gcd1(a, b)
    ans = gcd1(tmp, c)
    return (ans)

count = 0
for i in range(k):
    for j in range(i, k):
        for l in range(j, k):
            tmp = gcd2(i + 1, j + 1, l + 1)
            if (i == j == l):
                count = count + tmp
            elif (i == j or j == l):
                count = count + tmp*3
            else:
                count = count + tmp*6
print(count)

Submission status: Accepted

Solution code:
k = int(input())
def gcd1 (a, b):
    while True:
        if (a < b):
            a, b = b, a
        c = a%b
        if (c == 0):
            return (b)
        else:
            a = b
            b = c

count = 0
for i in range(k):
    for j in range(k):
        tmp = gcd1(i + 1, j + 1)
        if (tmp == 1):
            count = count + k
        else:
            for l in range(k):
                tmp2 = gcd1(tmp, l + 1)
                count = count + tmp2

print(count)

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

k = int(input())

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

Submission status: Accepted

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

for a in range(1, K+1):
    for b in range(a, K+1):
        for c in range(b, K+1):
            s = math.gcd(a, b)
            t = math.gcd(s, c)
            if a == c:
                ans += t
            elif (a == b or b == c) and a != c:
                ans += 3*t
            else:
                ans += 6*t
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):
    tmp=math.gcd(a,b)
    for c in range(1,k+1):
      ans+=math.gcd(tmp,c)

print(ans)

Submission status: Accepted

Solution code:
import math

K = int(input())
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
    k = int(input())

    k2 = np.arange(1, k+1)
    k2gcd = np.gcd.outer(k2, np.gcd.outer(k2, k2))
    print(k2gcd.sum())

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from math import ceil as C, floor as F, sqrt, gcd as G
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data) 

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
memo = [[0] * (n+1) for x in range(n+1)]

def gcd(x,y):
  if y == 0:
    return x
  if not memo[x][y] == 0:
    return memo[x][y]
  memo[x][y] = gcd(y,x % y)
  return gcd(y,x % y)

res = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    q = gcd(i,j)
    if q == 1:
      res += n
    else:
      for k in range(1,n+1):
        p = gcd(q,k)
        res += p
print(res)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import functools
import math
def gcd_2(*vals):
    return functools.reduce(math.gcd, vals)
K = int(input())
ANS = 0
L = list(range(1, K+1))
M = list(range(1, K+1))
N = list(range(1, K+1))



for i in range(K):
    for j in range(K):
        for k in range(K):
            ANS = ANS + gcd_2(L[i], M[j], N[k])

print(ANS)

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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


total = 0

for x in range(1,k+1):
    for y in range(x,k+1):
        for z in range(y,k+1):
            if x == y == z:
                total += x
            elif x == y or y == z:
                total += 3 * math.gcd(math.gcd(x,y),z)
            else:
                total += 6 * math.gcd(math.gcd(x,y),z)

print(total)

Submission status: Accepted

Solution code:
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")


K = I()
ans = 0

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)

Submission status: Accepted

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

import math

k = int(input())
s = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        gcdab = math.gcd(a,b)
        if  gcdab== 1:
            s += k
        else:
            for c in range(1,k+1):
                s += math.gcd(gcdab,c)

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)
        if tmp == 1:
            ans += tmp*K
        else:
            for k in range(1, K+1):
                ans += gcd(tmp, k)
            

print(ans)

Submission status: Accepted

Solution code:
K = int(input())
def gcd(a, b):
    if a < b:
        return gcd(b, a)
    elif a % b == 0:
        return b
    else:
        r = a % b
        return gcd(b, r)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import gcd


def main():
    K = int(input())
    ans = 0

    for i in range(1, K+1):
        for j in range(1, K+1):
            temp = gcd(i, j)
            for l in range(1, K+1):
                ans += gcd(temp, l)
                
    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:
import math
from functools import reduce

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

n = int(input())
sum = 0

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

print(sum)

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())
result = 0

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

print(result)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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:
from math import gcd
k=int(input())
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        ans_=gcd(i,j)
        for l in range(1,k+1):
            ans+=gcd(ans_,l)
print(ans)

Submission status: Accepted

Solution code:
k=int(input())
import math
s=0
result=0
for a in range(1,k+1,1):
    for b in range(a,k+1,1):
        for c in range(b,k+1,1):
            gcd=math.gcd(math.gcd(a,b),math.gcd(b,c))
            if a==b==c:
                pass
            elif (a==b and b!=c) or (b==c and b!=a) or (a==c and a!=c):
                gcd=3*gcd
            else:
                gcd=6*gcd
            
            result+=gcd
print(result)

Submission status: Accepted

Solution code:


from math import gcd as g


k = int(input())
ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        temp = g(a, b)
        for c in range(1, k+1):
            ans += g(temp, c)


print(ans)

Submission status: Accepted

Solution code:


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

Submission status: Accepted

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

Submission status: Accepted

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

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):
                x =math.gcd(a,b)

                for c in range(1,K+1):
                        sum +=math.gcd(x,c)

                        
                        

print(sum)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
from functools import reduce
def input():
    return sys.stdin.readline()[:-1]
def gcd(*numbers):
    return reduce(math.gcd, numbers)

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
input = sys.stdin.readline

def main():
    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 == c:
                    ans += y
                elif a==b or b==c or c==a:
                    ans += 3 * y
                else:
                    ans += 6 * y

    print(ans)



main()

Submission status: Accepted

Solution code:
from math import gcd 

k=int(input())

cnt=0

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


print(cnt)

Submission status: Accepted

Solution code:
import math

n = int(input())

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

print(s)

Submission status: Accepted

Solution code:
import math
K = int(input())
 
# gcd(a, b, c)=gcd(gcd(a,b), c) が成り立つ
s = 0
for a in range(1, K+1):
  for b in range(1, K+1):
    d = math.gcd(a, b) # gcd(a, b)は先に計算しておく
    for c in range(1, K+1):
      s += math.gcd(d, c)
      
print(s)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  import math
  N = int(input())
  ans=0
  for i in range(1,N+1):
    for j in range(1, N+1):
      for k in range(1, N+1):
          t=math.gcd(i,j)
          t=math.gcd(t,k)
          ans+=t
        
  print(ans)
 
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
def main():
  import math
  N = int(input())
  gcd={}
  ans=0
  for i in range(1,N+1):
    for j in range(1, N+1):
      key=i*N+j
      gcd[key]=math.gcd(i,j)
  
  for k, i in gcd.items():
    for j in range(1, N+1):
      key=j*N+i
      ans+=gcd[key]
        
  print(ans)
 
if __name__=='__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

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


def main():
    k = int(input())
    ans = 0

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

K = int(input())
ans = 0
l = []
for i in range(K+1):
    l.append([0]*(K+1))

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

print(ans)

Submission status: Accepted

Solution code:
def euclid(a, b):
    global l
    if b == 0:
        return a
    else:
        ch = l[a][b]
        if ch == 0:
            return euclid(b, a%b)
        else:
            return ch

def gcd(a, b, c):
    global l
    d = l[c][b]
    if d == 0:
        d = euclid(c, b)
        l[c][b] = d

    e = l[max(d, a)][min(d, a)]
    if e == 0:
        e = euclid(max(d, a), min(d, a))
        l[max(d, a)][min(d, a)] = e

    return e

K = int(input())
ans = 0
l = []
for i in range(K+1):
    l.append([0]*(K+1))

for i in range(1, K+1):
    for j in range(i, K+1):
        for k in range(j, K+1):
            num = gcd(i, j, k)
            if i == j:
                if j == k:
                    ans += num
                else:
                    ans += 3*num
            else:
                if j == k:
                    ans += 3*num
                else:
                    ans += 6*num

print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())

ab = []
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        ab.append(math.gcd(a, b))
for i in ab:
    for c in range(1,k+1):
        ans += math.gcd(i, c)

print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())

ab = []
ans = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        if a % b == 0:
            ab.append(b)
        elif b % a == 0:
            ab.append(a)
        else:
            ab.append(math.gcd(a, b))
for i in ab:
    for c in range(1,k+1):
        if i % c == 0:
            ans += c
        elif c % i == 0:
            ans += i
        else:
            ans += math.gcd(i, c)

print(ans)

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

Submission status: Accepted

Solution code:
k = int(input())

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

def gcf3(a,b,c):
    f_ab = gcf(a,b)
    return gcf(f_ab,c)

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:
                ans += gcf3(a,b,c)
            elif a != b and b != c and a != c:
                ans += gcf3(a,b,c)*6
            else:
                ans += gcf3(a,b,c)*3
print(ans)

Submission status: Accepted

Solution code:
k = int(input())

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

def gcf3(a,b,c):
    f_ab = gcf(a,b)
    return gcf(f_ab,c)

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:
                ans += gcf3(a,b,c)
            elif a != b and b != c and a != c:
                ans += gcf3(a,b,c)*6
            else:
                ans += gcf3(a,b,c)*3
print(ans)

Submission status: Accepted

Solution code:
def gcd(a, b, c):
  x = gcd2(a, b)
  y = gcd2(b, c)
  return gcd2(x, y)

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

def main():
  K = int(input())
  ans = 0
  for p in range(1, K+1):
    for s in range(p, K+1):
      for k in range(s, K+1):
        if p == s == k:
          ans += p
        elif p == s or s == k or p == k:
          ans += 3 * gcd(p, s, k)
        else:
          ans += 6 * gcd(p, s, k)
  print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcds(*numbers):
    from math import gcd
    from functools import reduce
    return reduce(gcd, numbers)

def resolve():
    K = int(input())
    ans = 0
    import itertools
    for i, j, k in itertools.combinations_with_replacement(range(1, K+1), 3):
        l = len(set([i, j, k]))
        mu = 1
        if l == 3:
            mu = 6
        elif l == 2:
            mu = 3
        ans += gcds(i, j, k) * mu
    print(ans)

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import math
#from functools import reduce

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

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

    return ans

print(main())

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

def main():
    K = int(input())
    ans = 0
    #全てが同じ数字
    for i in range(1,K+1,1):
        ans += gcd(i,i,i)
    #2つ同じ数字
    tmp = 0
    for i in range(1,K+1,1):
        for j in range(1,K+1,1):
            if j != i:
                tmp += gcd(i,i,j)
    ans += tmp*3
    #3つの異なる数字
    tmp2 = 0
    for i in range(1,K+1,1):
        for j in range(i+1,K+1,1):
            for h in range(j+1,K+1,1):
                tmp2 += gcd(i,j,h)

    ans += tmp2*6

    return ans

print(main())

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

K = int(input())

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

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 main():
    K = int(input())
    ans = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            for c in range(1, K + 1):
                ans += reduce(math.gcd, [a, b, c])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math


def abc162c_sum_of_gcd_of_tuples():
    k = int(input())
    total = 0
    memo = [[-1] * k for _ in range(k)]
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            if memo[a - 1][b - 1] == -1:
                memo[a - 1][b - 1] = math.gcd(a, b)
            g = memo[a - 1][b - 1]
            for c in range(1, k + 1):
                if memo[g - 1][c - 1] == -1:
                    memo[g - 1][c - 1] = math.gcd(g, c)
                total += memo[g - 1][c - 1]
    print(total)

abc162c_sum_of_gcd_of_tuples()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
K = int(input()) + 1
s = 0
for i in range(1,K):
    for j in range(1,K):
        ij = math.gcd(i, j)
        for k in range(1,K):
            s += math.gcd(ij, k)
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):
        x = math.gcd(a, b)
        for c in range(1, K + 1):
            ans += math.gcd(x, c)
print(ans)

Submission status: Accepted

Solution code:
k=int(input())
n=3
mod=10**9+7
lst=[0]*(k+1)
ans=0

for i in range(k,0,-1):
  lst[i]+=pow(k//i,n,mod)
  if k//i==1:
    continue
  else:
    for j in range(2,k//i+1):
      lst[i]-=lst[i*j]
      
for i in range(1,k+1):
  ans+=(i*lst[i])%mod
  ans%=mod
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import itertools
def gcd(x,y,z):
    xy = math.gcd(x,y)
    xyz = math.gcd(xy,z)
    return xyz
k1 = int(input())
k = list(range(1,k1+1))
l = []
for i in itertools.combinations_with_replacement(k,3):
    l.append(i)
num = 0
for h in l:
    t = gcd(h[0],h[1],h[2])
    if h[0] == h[1] == h[2]:
        num += t
    elif h[0] == h[1] or h[1] == h[2] or h[2] == h[0]:
        num += 3*t
    else:
        num += 6*t
print(num)

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(i,K+1):
        for k in range(j,K+1):
            if i == j and j == k:
                ans += gcd(i,j,k)
            elif i != j and j != k and k!= i:
                ans += gcd(i,j,k)*6
            else:
                ans += gcd(i,j,k)*3
print(ans)

Submission status: Accepted

Solution code:
def main():
  import math
  N = int(input())
  gcd={}
  ans=0
  for i in range(1,N+1):
    for j in range(1, N+1):
      key=i*N+j
      gcd[key]=math.gcd(i,j)
  
  for k, i in gcd.items():
    for j in range(1, N+1):
      key=j*N+i
      ans+=gcd[key]
        
  print(ans)

if __name__=='__main__':
  main()

Submission status: Accepted

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

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
k=int(input())

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

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
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(a,b,c)
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
ans = [math.gcd(i+1, math.gcd(j+1, k+1)) for i in range(n) for j in range(n) for k in range(n)]
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from numba import jit

k=int(input())

@jit
def f():
  ans=0
  for a in range(1,k+1):
    for b in range(1,k+1):
      for c in range(1,k+1):
        g=math.gcd(math.gcd(a,b),c)
        ans+=g
  return ans

print(f())

Submission status: Accepted

Solution code:
import fractions
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

class Counter:
    def __init__(self):
        self.dict = {}

    def add(self,x):
        if x in self.dict: self.dict[x] += 1
        else: self.dict[x] = 1

    def decrement(self,x):
        self.dict[x] -= 1
        if self.dict[x] <= 0:
            del self.dict[x]

    def get_dict(self):
        return self.dict


INF=10**20
def main():
    K=ii()

    ans = 0
    
    counter = Counter()

    for a in range(1,K+1):
        for b in range(1,K+1):
                x = fractions.gcd(a,b)
                counter.add(x)

    count = counter.get_dict()
    for c in range(1,K+1):
        for key in count.keys():
            val = count[key]
            x = fractions.gcd(key,c)
            ans += val * x



    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
# 自分の得意な言語で
# Let's チャレンジ！！
import math
N=int(input())
count=0
for i in range(1,N+1):
    for j in range(1,N+1):
        s=math.gcd(i,j)
        for k in range(1,N+1):
            count+=math.gcd(s,k)
print(count)

Submission status: Accepted

Solution code:
import math
k=int(input())
ans=0
gcd1=[0]*200
for i in range(1,k+1):
    for j in range(1,k+1):
        gcd1[math.gcd(i,j)-1]+=1


for i in range(1,k+1):
    for j in range(1,k+1):
        if gcd1[j-1]>=1:
            ans+=math.gcd(j,i)*gcd1[j-1]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ARC162 C:Sum of gcd of Tuples(Easy)
import math
import numpy as np
import itertools
k = int(input())
ans = 0

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

Submission status: Accepted

Solution code:
from math import gcd
from itertools import combinations_with_replacement

k = int(input())
lst = [i for i in range(1,k+1)]
itr = combinations_with_replacement(lst, 3)
ans = 0

for i in itr:
  st = set(i)
  num = len(st)
  if num == 1:
    ans += i[0]
  elif num == 2:
    a,b = st
    ans += gcd(a,b) * 3
  else:
    ans += gcd(gcd(i[0],i[1]), i[2]) * 6

print(ans)