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

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):

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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):

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)

Solution (Python):
from math import gcd
from functools import reduce

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)

Solution (Python):
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)

Solution (Python):
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



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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")

N = I()

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

print(ans)

Solution (Python):
import math

from functools import reduce

def main():

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
from math import gcd
from functools import reduce
from itertools import product

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

Solution (Python):
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))

Solution (Python):
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(input())
ans = 0
for a in range(1, K+1):
  for b in range(1, K+1):
    for c in range(1, K+1):
      ans += gcd(a,b,c)
print(ans)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)]




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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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)

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

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

Solution (Python):
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)]


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):
            
            
            
            
            
                
            
            
                
                
                
                
                
                
            ans += gcd_list([i, j, k])
            


print(ans)

Solution (Python):
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)]


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):
            
            
            
            a, b, c = i, j, k
            if memo[a][b][c] != -1:
                ans += memo[a][b][c]
            else:
                memo[a][b][c] = gcd_list([a, b, c])
                memo[i][k][j] = memo[k][i][j] = memo[k][j][i] = memo[j][i][k] = memo[j][k][i] = memo[i][j][k]
                
                
                
                
                
                ans += memo[a][b][c]
            


print(ans)

Solution (Python):
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(input())
a = list(range(1,K+1))
b = list(range(1,K+1))
c = list(range(1,K+1))
Sum = 0
for i in a :
    for j in b:
        for k in c:
            S = gcd(i,j,k)
            Sum = Sum + S
print(Sum)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

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

Solution (Python):
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()

Solution (Python):
import math

from functools import reduce

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 += math.gcd(math.gcd(i, j), k)
    print(ans)
        
    
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())

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

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

print(ans)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)
            
print(ans)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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))

Solution (Python):
from collections import *
from heapq import *
from itertools import *


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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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_)

Solution (Python):
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)

Solution (Python):

"""
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)

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

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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
import math
k = int(input())
x = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            x += math.gcd(math.gcd(a, b), c)
print(x)

Solution (Python):
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)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
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):
            d = gcd(a,b)
            e = gcd(d,c)
            ans += e
print(ans)

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


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)

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

Solution (Python):
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()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
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 
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




K = iin()
 
ans = 0
for a in range(1, K+1):
    for b in range(1, K+1):
        
        
        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)

Solution (Python):
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_)

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

Solution (Python):
def gcd(a,b):
  while b:a,b=b,a%b
  return a
k=int(input())
ans=0
for i in range(1,k+1):
  for j in range(1,k+1):
    x=gcd(i,j)
    for k in range(1,k+1):
      ans+=gcd(k,x)
print(ans)

Solution (Python):
def c_sum_of_gcd_of_tuples_easy():
    import numpy as np
    K = int(input())

    r = np.arange(1, K + 1)
    return np.gcd.outer(np.gcd.outer(r, r), r).sum()

print(c_sum_of_gcd_of_tuples_easy())

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

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

print(ans)

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

Solution (Python):
import math
K = int(input())
s = 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):
      s += math.gcd(x,c)
print(s)

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

Solution (Python):
import math
from functools import reduce
import itertools

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

if __name__ == "__main__":
    resolve()

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

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

Solution (Python):
import math

K= int(input())

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

for i in range(K+1):
    for j in range(K+1):
        gcd[i][j] = math.gcd(i,j) 

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

Solution (Python):
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)

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

print(ans)

Solution (Python):
import sys
from pprint import pprint

import math
from functools import reduce

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

K = int(sys.stdin.readline().strip())

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

Solution (Python):

import math 
gcd = math.gcd
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):
            d = gcd(gcd(i,j),k)
            if i==j==k:
                ans += d
            elif i==j or j== k or k==i:
                ans += 3*d
            else:
                ans += 6*d
print(ans)

Solution (Python):
import math
ans = 0
k = int(input())

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):
            ans += math.gcd(t, c)
print(ans)

Solution (Python):

import sys
import math

def solve(K: int):
    answer = 0
    

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



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    solve(K)

if __name__ == '__main__':
    main()

Solution (Python):


from math import gcd
from functools import reduce
from itertools import product 
k=int(input())
z=range(1,k+1)
print(sum(gcd(i,gcd(j,k)) for i,j,k in product(z, repeat=3)))

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

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

print(ans)

Solution (Python):

import math 
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):
            d = math.gcd(math.gcd(i,j),k)
            if i==j==k:
                ans += d
            elif i==j or j== k or k==i:
                ans += 3*d
            else:
                ans += 6*d
print(ans)

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

import math




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