Problem: 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.

Submission attempts (in order):
#
# Written by NoKnowledgeGG @YlePhan

#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7











"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""


"""import bisect 
print(A,bisect.bisect_left(A,4))
print(C,n - bisect.bisect_right(C,4))
print(sum(bisect.bisect_left(A,b) * (n-bisect.bisect_right(C,b)) for b in B))"""


"""import math
pi_ = math.pi"""




def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  from fractions import gcd
  from functools import reduce
  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(gcd,(a,b,c))
  print(ans)
if __name__ == '__main__':
  main()

#
# Written by NoKnowledgeGG @YlePhan

#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7

 

 

 



 

"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""
 

"""import bisect 
print(A,bisect.bisect_left(A,4))
print(C,n - bisect.bisect_right(C,4))
print(sum(bisect.bisect_left(A,b) * (n-bisect.bisect_right(C,b)) for b in B))"""
 

"""import math
pi_ = math.pi"""
 


 
def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def main():
  import fractions
  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 += fractions.gcd(a,fractions.gcd(b,c))
  print(ans)
if __name__ == '__main__':
  main()

#
# Written by NoKnowledgeGG @YlePhan

#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7











"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""


"""import bisect 
print(A,bisect.bisect_left(A,4))
print(C,n - bisect.bisect_right(C,4))
print(sum(bisect.bisect_left(A,b) * (n-bisect.bisect_right(C,b)) for b in B))"""


"""import math
pi_ = math.pi"""




def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  import fractions
  k = int(input())
  L = []
  ans = 0
  for a in range(1,k+1):
    for b in range(1,k+1):
      L.append(fractions.gcd(a,b))
  Llen = len(L)
  for c in range(1,k+1):
    for i in range(0,Llen):
      ans += fractions.gcd(c,L[i])
  print(ans)
if __name__ == '__main__':
  main()

#
# Written by NoKnowledgeGG @YlePhan

#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7











"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""


"""import bisect 
print(A,bisect.bisect_left(A,4))
print(C,n - bisect.bisect_right(C,4))
print(sum(bisect.bisect_left(A,b) * (n-bisect.bisect_right(C,b)) for b in B))"""


"""import math
pi_ = math.pi"""




def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  import math
  k = int(input())
  ans = 0
  BC = []
  for b in range(1,k+1):
    for c in range(1,k+1):
      BC.append(math.gcd(b,c))
  for a in range(1,k+1):
    for bc in BC:
      ans += math.gcd(a,bc)
  print(ans)
if __name__ == '__main__':
  main()

Verdicts: Time Limit Exceeded

Time Limit Exceeded

Time Limit Exceeded

Accepted