Buggy python submission (verdict: Time Limit Exceeded):
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)


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

Buggy python submission (verdict: Time Limit Exceeded):
import math
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(i, math.gcd(j, k))

print(ans)


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

Buggy python submission (verdict: Time Limit Exceeded):
import math
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(i, math.gcd(j, k))

print(ans)


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

Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())

GCD_table = [[-1] * (K + 1) for i in [1] * (K + 1)]
#print(GCD_table)

def GCD(a: int, b: int) -> int:
    if a <= b:
        return GCD_Recursive(b, a)
    else:
        return GCD_Recursive(a, b)

def GCD_Recursive(a: int, b: int) -> int:
    if GCD_table[a][b] != -1:
        return GCD_table[a][b]

    remain = a % b
    if remain == 0:
        # print("before:", GCD_table)
        # print("a", a)
        # print("b", b)
        GCD_table[a][b] = b
        GCD_table[b][a] = b
        # print("rewrite!")
        # print("after:", GCD_table)
        return b
    else:
        # GCD_table[a][b] = GCD_Recursive(b, remain)
        # GCD_table[b][a] = GCD_Recursive(b, remain)
        if GCD_table[b][remain] != -1:
            GCD_table[a][b] = GCD_table[b][remain]
            GCD_table[b][a] = GCD_table[b][remain]
            return GCD_table[b][remain]
        else:
            return GCD(b, remain)
ans = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            # print("i, j, k", i, j, k)
            # print("GCD", GCD(i, GCD(j, k)))
            ans += GCD(i, GCD(j, k))

# print(GCD_table)
print(ans)


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

Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())

GCD_table = [[-1] * (K + 1) for i in [1] * (K + 1)]
#print(GCD_table)

def GCD(a: int, b: int) -> int:
    if a <= b:
        return GCD_Recursive(b, a)
    else:
        return GCD_Recursive(a, b)

def GCD_Recursive(a: int, b: int) -> int:
    if GCD_table[a][b] != -1:
        return GCD_table[a][b]

    remain = a % b
    if remain == 0:
        # print("before:", GCD_table)
        # print("a", a)
        # print("b", b)
        GCD_table[a][b] = b
        GCD_table[b][a] = b
        # print("rewrite!")
        # print("after:", GCD_table)
        return b
    else:
        GCD_table[a][b] = GCD_Recursive(b, remain)
        GCD_table[b][a] = GCD_Recursive(b, remain)
        return GCD_Recursive(b, remain)
ans = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            # print("i, j, k", i, j, k)
            # print("GCD", GCD(i, GCD(j, k)))
            ans += GCD(i, GCD(j, k))

# print(GCD_table)
print(ans)


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

Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())

GCD_table = [[-1] * (K + 1) for i in [1] * (K + 1)]
#print(GCD_table)

def GCD(a: int, b: int) -> int:
    if a <= b:
        return GCD_Recursive(b, a)
    else:
        return GCD_Recursive(a, b)

def GCD_Recursive(a: int, b: int) -> int:
    if GCD_table[a][b] != -1:
        return GCD_table[a][b]

    remain = a % b
    if remain == 0:
        # print("before:", GCD_table)
        # print("a", a)
        # print("b", b)
        GCD_table[a][b] = b
        # print("rewrite!")
        # print("after:", GCD_table)
        return b
    else:
        return GCD_Recursive(b, remain)
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, GCD(j, k))

# print(GCD_table)
print(ans)


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