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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())
num = 0

for a in range(1, k + 1):
  for b in range(1, k + 1):
    c = gcd(a, b)
    num += sum(gcd(c, d) for d in range(1, k + 1))

print(num)

Submission status: Accepted

Solution code:
import math
K =int(input())
list =[]
for a in range(1,K+1) :
    for b in range(1,K+1) :  
        t = math.gcd(a,b)    #全ての場合についての最大公約数を求める
        for c in range(1,K+1) :
           list.append(math.gcd(t,c))  #リストに格納して、合計値を求める
print(sum(list))

Submission status: Accepted

Solution code:
K = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
import math
k=int(input())
list=[]
for a in range(1,k+1):
  for b in range(1,k+1):
    t=math.gcd(a,b)
    for c in range(1,k+1):
      list.append(math.gcd(t,c))
print(sum(list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math

k = int(input())
x = 0
for v in itertools.combinations_with_replacement(list(range(1, k+1)), 3):
    a, b, c = v
    x = x + math.gcd(math.gcd(a,b), c)

y = 0
for v in itertools.combinations(list(range(1, k+1)), 3):
    a, b, c = v
    y = y + math.gcd(math.gcd(a,b), c)

z = k * (1 + k) / 2
print( int(3 * x  + y * 3  -  2 * z))

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

print(res)

Submission status: Accepted

Solution code:
k=int(input())
def gcd(a,b):
    while a%b!=0:
        c=a%b
        a=b
        b=c
    return b
ans=0
for l in range(k):
    for m in range(l,k):
        x=gcd(m+1,l+1)
        for n in range(m,k):
            ai=gcd(n+1,x)
            if l<m and m<n:
                ans+=6*ai
            elif l==m and m==n:
                ans+=ai
            else:
                ans+=ai*3
            
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy
import math

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

Submission status: Accepted

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

def better_gcd(a,b,c):
    tup = set((a,b,c))
    kinds = len(tup)
    if kinds == 3:
        return math.gcd(a, math.gcd(b, c)) * 6
    elif kinds == 2:
        return math.gcd(*tup) * 3
    else:
        return a

ans = 0
cands = list(itertools.combinations_with_replacement(range(1,K+1), 3))
for c in cands:
    ans += better_gcd(*c)
print(ans)

Submission status: Accepted

Solution code:
K = int(input())
# K = 2
import math
import itertools

def better_gcd(a,b,c):
    tup = set((a,b,c))
    kinds = len(tup)
    if kinds == 3:
        return math.gcd(a, math.gcd(b, c)) * 6
    elif kinds == 2:
        return math.gcd(*tup) * 3
    else:
        return a

ans = 0
cands = list(itertools.combinations_with_replacement(range(1,K+1), 3))
for c in cands:
    ans += better_gcd(*c)
print(ans)

Submission status: Accepted

Solution code:
def gcd(x,y):
  if x<y: x,y=y,x
  while True:
    tmp=x%y
    if tmp==0:
      return y
    x,y=y,tmp

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(x,y):
  if x<y: x,y=y,x
  while True:
    tmp=x%y
    if tmp==0:
      return y
    x,y=y,tmp

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

Submission status: Accepted

Solution code:
def gcd(a, b):
    # `a >= b` is sopposed
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


def main():
    K = int(input())
    gcd_memo = [[0 for _ in range(K + 1)] for _ in range(K + 1)]
    for a in range(1, K + 1):
        for b in range(a, K + 1):
            left, right = max([a, b]), min([a, b])
            gcd_ab = gcd(left, right)
            gcd_memo[a][b] = gcd_ab
            gcd_memo[b][a] = gcd_ab
    answer = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            gcd_ab = gcd_memo[a][b]
            answer += sum(gcd_memo[gcd_ab])
    print(answer)


if __name__ == '__main__':
    main()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:



import math

K = int(input())


memo = [[None for _ in range(201)]  for _ in range(201)]
for i in range(1,201):
    for j in range(1,201):
        memo[i][j] = math.gcd(i,j)

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


print(ans)

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

Submission status: Accepted

Solution code:
def main():
    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)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def gcd(a,b):
    while b != 0:
        tmp = a % b
        a = b
        b = tmp
    return a
            


k = int(input())

count =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:
                if b ==c:
                    count += c
                else:
                    count +=3*gcd(b,c)
            else:
                if b == c:
                    count += 3*gcd(a,b)
                else:
                    tmp = gcd(b,c)
                    count += 6*gcd(a,tmp)


print(count)

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

Submission status: Accepted

Solution code:
def gcd(m,n):
    while (m % n != 0):
        n,m = m%n,n
    return(n)


k = int(input())
s = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        t = gcd(a,b)
        for c in range(b,k+1):
            v = gcd(t,c)
            l = len(set([a,b,c]))
            if(l == 1):s += v
            elif(l == 2):s += v * 3
            else:s += v * 6
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

def gcd2(a,b):
    if b==0:
        return a
    return gcd2(b,a%b)
def gcd3(a,b,c):
    return gcd2(gcd2(a,b),c)

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

print(ans)

Submission status: Accepted

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

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):
            result += math.gcd(d, c)

print(result)

Submission status: Accepted

Solution code:
import math
import itertools
K=int(input())
l=list(itertools.combinations_with_replacement(range(1,K+1),3))
S=0
for i in l:
    a=i[0]
    b=i[1]
    c=i[2]
    x=math.gcd(a,b)
    if a==b==c:
        S+=math.gcd(x,c)
    elif a==b or b==c or c==a:
        S+=math.gcd(x,c)*3
    else:
        S+=math.gcd(x,c)*6   
print(S)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

n = int(input())
m = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
from math import gcd
k = int(input()) + 1
total = 0

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

print(total)

Submission status: Accepted

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

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

print(total)

Submission status: Accepted

Solution code:

import math
k = int(input())
s = 0
box = [0]*(k+1)
for l in range (1,k+1):
     for m in range (1, k+1):
          box[math.gcd(l,m)] += 1
for l in range (1,k+1):
     for n in range (1, k+1):
          s += math.gcd(l,n)*box[l]
print(s)

Submission status: Accepted

Solution code:
def main():
    k = int(input())
    gcds = []
    for i in range(1, k+1):
        for j in range(1, k+1):
            gcds.append(gcd(i, j))
    ans = {i:0 for i in set(gcds)}
    for i in gcds:
        ans[i] += 1
    out = 0
    for i, v in ans.items():
        for j in range(1, k+1):
            out += gcd(i, j)*v
    print(out)

def gcd(x, y):
    r = x % y
    while r:
        x, y, r = y, r, y%r
    return y

if __name__ == "__main__":
    main()

Submission status: Accepted

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

import math

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

Submission status: Accepted

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

def gcd2(a,b,c):
    d=math.gcd(a,b)
    e=math.gcd(c,d)
    return e
    
s=0
s1=0
s2=0
s3=0

for i in range(1,n+1):
    s1+=i
    for i2 in range(i+1,n+1):
        s2+=math.gcd(i,i2)
        for i3 in range(i2+1,n+1):
            s3+=gcd2(i,i2,i3)

s=s1+6*s2+6*s3

print(s)

Submission status: Accepted

Solution code:
import math

K = int(input())

sum = 0

for a in range(1,K + 1):
    sum += a
    #print(a, a)
    for b in range(a + 1,K + 1):
        gcd2_sum = math.gcd(a, b) * 3 * 2
        sum += gcd2_sum
        #print(a, b, gcd2_sum)
        for c in range(b + 1, K + 1):
            gcd3_sum = math.gcd(math.gcd(a, b), c) *  6
            sum += gcd3_sum
            #print(a, b, c, gcd3_sum)
print(sum)

Submission status: Accepted

Solution code:
import math

K = int(input())

sum = 0

for a in range(K):
    sum += a + 1
    #print(a + 1, a + 1)
    for b in range(K):
        if a < b:
            gcd2_sum = math.gcd(a + 1, b + 1) * 3 * 2
            sum += gcd2_sum
            #print(a + 1, b + 1, gcd2_sum)
            for c in range(K):
                if b < c:
                    gcd3_sum = math.gcd(math.gcd(a + 1, b + 1), c + 1) *  6
                    sum += gcd3_sum
                    #print(a + 1, b + 1, c + 1, gcd3_sum)
print(sum)

Submission status: Accepted

Solution code:
import math
import itertools
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
  
k = int(input())
count = 0
list_k = [i for i in range(1,k+1)]

#case すべて同じ数字の場合
count += k*(k + 1)/2
#case 2つ同じ数字の場合
combi2 = itertools.combinations(list_k,2)
for x in list(combi2):
  count += math.gcd(x[0],x[1]) * 6
#case すべて異なる数字の場合
combi3 = itertools.combinations(list_k,3)
for x in list(combi3):
  count += gcd(x[0],x[1],x[2]) * 6
print(int(count))

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

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

Submission status: Accepted

Solution code:
def gcd(a,b):
    if(a%b==0):
        return b
    else:
        return gcd(b,a%b)
 
k = int(input())
val = [[0]*(k+1) for i in range(k+1)]
sum = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        if(val[i][j] == 0):
            val[i][j] = gcd(i,j)
        for l in range(1,k+1):
            if(val[val[i][j]][l] == 0):
                val[val[i][j]][l] = gcd(val[i][j],l)
            sum = sum + val[val[i][j]][l]
 
print(sum)

Submission status: Accepted

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


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

print(sum)

Submission status: Accepted

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

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

            if a != b and b != c and a != c:
                tmp *= 2
            if a!=b or b!=c:
                tmp *= 3
            sum += tmp

print(sum)

Submission status: Accepted

Solution code:
import math
k = int(input())
s = 0
box = [0]*(k+1)
for l in range (1,k+1):
     for m in range (1, k+1):
          box[math.gcd(l,m)] += 1
for l in range (1,k+1):
     for n in range (1, k+1):
          s += math.gcd(l,n)*box[l]
print(s)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
from functools import reduce

K=int(input())

ans=K*(1+K)/2
for i in range(1, K+1):
    for j in range(i+1, K+1):
        ans+=math.gcd(i,j)*6
        for k in range(j+1,K+1):
            ans+=reduce(math.gcd, [i,j,k])*6
print(str(int(ans)))

Submission status: Accepted

Solution code:
import math

K = int(input())

dp = [[0] * 201 for _ in range(201)]

def dp_gcds(x,y):
    if dp[x][y] != 0:
        n = dp[x][y]
    else:
        n = math.gcd(x,y)
        dp[x][y] = n
        dp[y][x] = n
    return n
    
def gcds(x,y,z):
    n1 = dp_gcds(x,y)
    n2 = dp_gcds(n1,z)
    return n2

total = 0
for i in range(1,K+1):
    for j in range(1, K+1):
        for t in range(1, K+1):
            total += gcds(i,j,t)
print(total)

Submission status: Accepted

Solution code:
import math
from functools import reduce
k_ = int(input())
ans = 0


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


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

print(ans)

Submission status: Accepted

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

def gcds(x,y,z):
    n1 = math.gcd(x,y)
    n2= math.gcd(n1,z)
    return n2
total =0
for i in range(1,k+1):
    for j in range(1,k+1):
        if math.gcd(i,j) == 1:
            total += k
        else:
            for t in range(1,k+1):
                total += gcds(i,j,t)
print(total)

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): #すべての(a,b)の組についてgcd(a,b)を求め、その値の個数を数える
 for j in range(1,k+1):
   cnt[gcd(i,j)]+=1
ans=0
for i in range(1,k+1): #すべてのcについて、上で求めたgcd(a,b)のあり得る値とのgcdを求め、答えに足し合わせる
 for key in cnt.keys():
   ans+=gcd(i,key)*cnt[key]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from itertools import product


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def sums(a, N):
    ans = ((N//a)*((N//a)+1)//2)*a
    return ans


def solve():
    K = read_int()
    return sum(math.gcd(math.gcd(a, b), c)
               for a, b, c in product(range(1, K+1), range(1, K+1), range(1, K+1)))


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

K = int(input())
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
 
K = int(input())
ans_list = []
ans = 0
for a in range(1, K+1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            ans_list = [a, b, c]
            gcd_ans = ans_list[0]
            for item in ans_list:
                gcd_ans = math.gcd(gcd_ans, item)
            ans += gcd_ans
print(ans)

Submission status: Accepted

Solution code:
k = int(input())

import math

def max_min(a,b):
    f = math.gcd(a,b)
    
    return f

ans = 0

for a in range(1, k + 1):
    for b in range(1, k + 1):
        
        n = max_min(a, b)
        
        if n == 1:
            ans += k
            
        else:
            for c in range(1, k + 1):
                
                ans += max_min(n, c)

print(ans)

Submission status: Accepted

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

  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)
  return ans

def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('csolve', 'i8(i8)')(solve)
    cc.compile()

if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        exit(0)
    from my_module import csolve
    print(csolve(int(input())))

Submission status: Accepted

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

  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)
  return ans

def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', 'i8(i8)')(solve)
    cc.compile()

if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        exit(0)
    from my_module import solve
    print(solve(int(input())))

Submission status: Accepted

Solution code:
import numba

@numba.njit(numba.i8(numba.i8), cache=True)
def solve(k):
  def gcd(a, b):
    if a < b:
      a,b = b,a
    while b:
      a, b = b, a%b
    return a

  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)
  return ans

print(solve(int(input())))

Submission status: Accepted

Solution code:
import math
n = int(input())
sum = 0

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

print(sum)

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:
import sys
import math
import itertools
K = int(sys.stdin.readline())
ans = 0
for a, b, c in itertools.product(range(1, K+1),range(1, K+1),range(1, K+1)):
    ans += math.gcd(a, math.gcd(b, c))
print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())

box = [0]*(k+1)
ans = 0

for a in range(1,k+1):
    for b in range(1,k+1):
        box[math.gcd(a,b)] += 1

for i in range(1,k+1):
    for c in range(1,k+1):
        ans += math.gcd(i,c)*box[i]

print(ans)

Submission status: Accepted

Solution code:
import math

k = int(input())

box = [0]*(k+1)
ans = 0

for a in range(1,k+1):
    for b in range(1,k+1):
        box[math.gcd(a,b)] += 1

for a in range(1,k+1):
    for c in range(1,k+1):
        ans += math.gcd(a,c)*box[a]

print(ans)

Submission status: Accepted

Solution code:
import math
from functools import reduce


def gcd(*numbers):
    return reduce(my_gcd, numbers)


def my_gcd(i, j):
    if i > j:
        a = i
        b = j
    elif i == j:
        return i
    else:
        a = j
        b = i
    r = a % b

    if r == 0:
        return b
    return my_gcd(b, r)


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

print(sum)

Submission status: Accepted

Solution code:
from math import gcd

n=int(input())

s = 0

for i in range(1,n+1):
  if i == 1:
    s += n**2
  else:
    for j in range(1,n+1):
      g = gcd(i, j)
      if g == 1:
        s += n
      else:
        for k in range(1,n+1):
          s += gcd(g, k)
print(s)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop
from math import gcd

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


K = int(input())

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

print(total)

Submission status: Accepted

Solution code:
import math
from functools import reduce

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

def myfunc(K):
    if K == 1:
        return 1
    elif K == 2:
        return 9

    temp = K
    for a in range(2,K):
        temp += sum([gcd(K,a,b) for b in range(1,a)])*6
    temp += sum([gcd(K,a,a) for a in range(1,K)])*6

    return myfunc(K-1) + temp

K = int(input())

print(myfunc(K))

Submission status: Accepted

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

for i in range(1,k+1):
    for ii in range(1,k+1):
        value = math.gcd(i,ii)
        for iii in range(1,k+1):
            sum += math.gcd(value,iii)
print(sum)

Submission status: Accepted

Solution code:
K = int(input())

import functools

def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def gcd(nums):
    return functools.reduce(euclid, nums)

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

solution(K)

Submission status: Accepted

Solution code:
def main():
    k = int(input())
    res = 0
    for x in range(1, k + 1):
        for y in range(1, x + 1):
            for z in range(1, y + 1):
                d = gcd(y, z)
                ins = gcd(x, d)
                if x != y and y != z:
                    res += 6 * ins
                elif x == z:
                    res += ins
                else:
                    res += 3 * ins
    print(res)

def gcd(s, t):
    u = s % t
    if u == 0:
        return t
    return gcd(t, u)


    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

print(sum)
# print(gcd(2,6))

Submission status: Accepted

Solution code:
from math import gcd

k = int(input())

sum = 0
for i in range(k):
    for j in range(k):
        tmp = gcd(i+1, j+1)
        if tmp == 1:
            sum += k
        else :
            for l in range(k):
                sum += gcd(l+1, tmp)

print(sum)

Submission status: Accepted

Solution code:
import fractions

k = int(input())

ans = 0
gcd_table = [[0 for _ in range(k)] for _ in range(k)]
for i in range(k):
    for j in range(k):
        gcd_table[i][j] = fractions.gcd(i+1, j+1)
for i in range(k):
    for j in range(k):
        for l in range(k):
            ans += gcd_table[gcd_table[i][j]-1][l]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from functools import reduce
a=1;b=1;c=1;d=0
n=int(input());
for i1 in range(n):
	for i2 in range(n):
		for i3 in range(n):
			d+=reduce(math.gcd,(a,b,c))
			c+=1
		b+=1;c=1;i3=0 #毎回b,cをリセットさせないと増え続ける
	a+=1;b=1;i2=0 #i2,i3もリセットしないとどうなるのかはわからんけど変になる
print(d)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(x,y):
    if x==y:
        return x
    elif x>y:
        r=-1
        while r!=0:
            r=x%y
            x=y
            y=r
        return x
    else:
        r=-1
        while r!=0:
            r=y%x
            y=x
            x=r
        return y

k=int(input())
ans=0
d=[[0]*(1+k) for i in range(1+k)]
for i in range(1,1+k):
    for j in range(1,1+k):
        d[i][j]=gcd(i,j)
for i in range(1,1+k):
    for j in range(1,1+k):
        for l in range(1,1+k):
            ans+=d[d[i][j]][l]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def resolve():
    k=int(input())
    ans=0
    a=0
    b=0
    for h in range(1,k+1):
        for i in range(1,k+1):
            for j in range(1,k+1):
                 a=math.gcd(h,i)
                 b=math.gcd(i,j)
                 ans+=math.gcd(a,b)
    print(ans)
if __name__ == "__main__":
    resolve()