Buggy python submission (verdict: Time Limit Exceeded):
gcd_d = {}
import math

K = int(input())

ans_sum = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    t_ab = tuple([ai, bi] if ai <= bi else [bi, ai])
    if t_ab not in gcd_d:
      gcd_d[t_ab] = math.gcd(ai, bi)
    gcd_ab = gcd_d[t_ab]
    for ci in range(1, K + 1):
      t_abc = tuple([gcd_ab, ci] if gcd_ab <= ci else [ci, gcd_ab])
      if t_abc not in gcd_d:
        gcd_d[t_abc] = math.gcd(gcd_ab, ci)
      ans_sum += gcd_d[t_abc]

print(ans_sum)

Fixed submission:

from math import gcd

K = int(input())

a = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    gcd_ab = gcd(ai, bi)
    for ci in range(1, K + 1):
      a += gcd(gcd_ab, ci)

print(a)

Buggy python submission (verdict: Time Limit Exceeded):

def q3():
    import math
    k = int(input())
    sum_n = 0
    gcd_2_dict = {}

    def gcd(n1, n2):
        if n1 == n2:
            return n2
        if n1 == 1:
            return 1
        if n2 % n1 == 0:
            return n1
        key = tuple([n1, n2])
        if key in gcd_2_dict:
            return gcd_2_dict[key]
        gcd_num = math.gcd(n1, n2)
        gcd_2_dict[key] = gcd_num
        return gcd_num

    import time
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                l = [a, b, c]
                l.sort()
                sum_n += gcd(gcd(l[0], l[1]), l[2])

    print(sum_n)

q3()

Fixed submission:

from math import gcd

K = int(input())

a = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    gcd_ab = gcd(ai, bi)
    for ci in range(1, K + 1):
      a += gcd(gcd_ab, ci)

print(a)

Buggy python submission (verdict: Time Limit Exceeded):

def q3():
    import math
    k = int(input())
    sum_n = 0
    gcd_2_dict = {}

    def gcd(n1, n2):
        key = tuple([n1, n2])
        if key in gcd_2_dict:
            return gcd_2_dict[key]
        gcd_num = math.gcd(n1, n2)
        gcd_2_dict[key] = gcd_num
        return gcd_num

    import time
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                l = [a, b, c]
                l.sort()
                sum_n += gcd(gcd(l[0], l[1]), l[2])

    print(sum_n)

q3()

Fixed submission:

from math import gcd

K = int(input())

a = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    gcd_ab = gcd(ai, bi)
    for ci in range(1, K + 1):
      a += gcd(gcd_ab, ci)

print(a)

Buggy python submission (verdict: Time Limit Exceeded):

def q3():
    import math
    k = int(input())
    sum_n = 0
    gcd_2_dict = {}

    def gcd(n1, n2):
        key = tuple([n1, n2])
        if key in gcd_2_dict:
            return gcd_2_dict[key]
        gcd_num = math.gcd(n1, n2)
        gcd_2_dict[key] = gcd_num
        return gcd_num

    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                sorted_nums = sorted([a, b, c])
                sum_n += gcd(gcd(sorted_nums[0], sorted_nums[1]), sorted_nums[2])

    print(sum_n)

q3()

Fixed submission:

from math import gcd

K = int(input())

a = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    gcd_ab = gcd(ai, bi)
    for ci in range(1, K + 1):
      a += gcd(gcd_ab, ci)

print(a)

Buggy python submission (verdict: Time Limit Exceeded):

def q3():
    import math
    k = int(input())
    sum_n = 0
    gcd_2_dict = {}

    def gcd(n1, n2):
        if n1 < n2:
            sorted_nums = tuple([n1, n2])
        else:
            sorted_nums = tuple([n2, n1])
        if sorted_nums in gcd_2_dict:
            return gcd_2_dict[sorted_nums]
        gcd_num = math.gcd(n1, n2)
        gcd_2_dict[sorted_nums] = gcd_num
        return gcd_num

    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                sorted_nums = sorted([a, b, c])
                sum_n += gcd(gcd(sorted_nums[0], sorted_nums[1]), sorted_nums[2])

    print(sum_n)

q3()

Fixed submission:

from math import gcd

K = int(input())

a = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    gcd_ab = gcd(ai, bi)
    for ci in range(1, K + 1):
      a += gcd(gcd_ab, ci)

print(a)

Buggy python submission (verdict: Runtime Error):

def q3():
import math
    k = int(input())
    sum_n = 0
    gcd_2_dict = {}
    def gcd(n1, n2):
        if n1 < n2:
            sorted_nums = tuple([n1, n2])
        else:
            sorted_nums = tuple([n2, n1])
        if sorted_nums in gcd_2_dict:
            return gcd_2_dict[sorted_nums]
        gcd_num = math.gcd(n1, n2)
        gcd_2_dict[sorted_nums] = gcd_num
        return gcd_num

    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                sorted_nums = sorted([a, b, c])
                sum_n += gcd(gcd(sorted_nums[0], sorted_nums[1]), sorted_nums[2])

    print(sum_n)

q3()

Fixed submission:

from math import gcd

K = int(input())

a = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    gcd_ab = gcd(ai, bi)
    for ci in range(1, K + 1):
      a += gcd(gcd_ab, ci)

print(a)

Buggy python submission (verdict: Time Limit Exceeded):

def q3():
    import math
    k = int(input())
    sum_n = 0
    gcd_2_dict = {}
    gcd_3_dict = {}

    def gcd(n1, n2):
        if n1 < n2:
            sorted_nums = tuple([n1, n2])
        else:
            sorted_nums = tuple([n2, n1])
        if sorted_nums in gcd_2_dict:
            return gcd_2_dict[sorted_nums]
        gcd_num = math.gcd(n1, n2)
        gcd_2_dict[sorted_nums] = gcd_num
        return gcd_num

    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                sorted_nums = tuple(sorted([a, b, c]))
                if sorted_nums in gcd_3_dict:
                    gcd_num = gcd_3_dict[sorted_nums]
                else:
                    gcd_num = gcd(gcd(sorted_nums[0], sorted_nums[1]), sorted_nums[2])
                    gcd_3_dict[sorted_nums] = gcd_num
                sum_n += gcd_num

    print(sum_n)

q3()

Fixed submission:

from math import gcd

K = int(input())

a = 0
for ai in range(1, K + 1):
  for bi in range(1, K + 1):
    gcd_ab = gcd(ai, bi)
    for ci in range(1, K + 1):
      a += gcd(gcd_ab, ci)

print(a)