alexjercan/bugnet huggingface dataset:
Problem p02710 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
Cs = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    adj[a - 1].append(b - 1)
    adj[b - 1].append(a - 1)

degs = [0] * N
clusters = [[] for _ in range(N)]


def go(curr, fro):
    col = Cs[curr] - 1
    deg = 1
    clust = {}
    for n in adj[curr]:
        if n == fro:
            continue
        ch_deg, ch_clust = yield n, curr
        clusters[col].append(ch_deg - ch_clust.get(col, 0))
        if len(ch_clust) > len(clust):
            clust, ch_clust = ch_clust, clust
        for k, v in ch_clust.items():
            clust[k] = clust.get(k, 0) + v
        deg += ch_deg
    clust[col] = deg
    return deg, clust


def tramp(gen_fn, *args):
    stack = [gen_fn(*args)]
    val = None
    while stack:
        gen = stack[-1]
        try:
            params = gen.send(val)
        except StopIteration as e:
            stack.pop()
            val = e.value
        else:
            stack.append(go(*params))
            val = None
    return val


_, root_clust = tramp(go, 0, -1)
for i in range(N):
    clusters[i].append(N - root_clust.get(i, 0))


for clust in clusters:
    tot = (N * N + N) // 2
    for cnt in clust:
        tot -= (cnt * cnt + cnt) // 2
    print(tot)


Error: TLE

Fixed submission:

N = int(input())
Cs = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    adj[a - 1].append(b - 1)
    adj[b - 1].append(a - 1)

degs = [0] * N
clusters = [[] for _ in range(N)]


def go(curr, fro):
    col = Cs[curr] - 1
    deg = 1
    clust = {}
    for n in adj[curr]:
        if n == fro:
            continue
        ch_deg, ch_clust = yield n, curr
        clusters[col].append(ch_deg - ch_clust.get(col, 0))
        if len(ch_clust) > len(clust):
            clust, ch_clust = ch_clust, clust
        for k, v in ch_clust.items():
            clust[k] = clust.get(k, 0) + v
        deg += ch_deg
    clust[col] = deg
    return deg, clust


stack = [[None, None], [go(0, -1), None]]
while len(stack) > 1:
    gen, val = stack[-1]
    try:
        params = gen.send(val)
    except StopIteration as e:
        stack[-2][1] = e.value
        stack.pop()
    else:
        stack.append([go(*params), None])

_, root_clust = stack[0][1]
for i in range(N):
    clusters[i].append(N - root_clust.get(i, 0))


for clust in clusters:
    tot = (N * N + N) // 2
    for cnt in clust:
        tot -= (cnt * cnt + cnt) // 2
    print(tot)
Problem p02710 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
Cs = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    adj[a - 1].append(b - 1)
    adj[b - 1].append(a - 1)

under_col = [0] * N
ans = [0] * N


def tri(n):
    return n * (n + 1) // 2


def go(curr, fro):
    col = Cs[curr] - 1
    prev_under = under_col[col]
    deg = 1
    for n in adj[curr]:
        if n == fro:
            continue
        curr_under = under_col[col]
        ch_deg = yield go(n, curr)
        diff_under = under_col[col] - curr_under
        ans[col] += tri(ch_deg - diff_under)
        deg += ch_deg
    under_col[col] = prev_under + deg
    return deg


def tramp(gen):
    stack = [gen]
    val = None
    while stack:
        try:
            stack.append(stack[-1].send(val))
            val = None
        except StopIteration as e:
            stack.pop()
            val = e.value
    return val


tramp(go(0, -1))
for i in range(N):
    ans[i] += tri(N - under_col[i])
    print(tri(N) - ans[i])


Error: 0
/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02710/Python/s158459294.py:33: DeprecationWarning: There is no current event loop
  _, root_clust = asyncio.get_event_loop().run_until_complete(go(0, -1))


Fixed submission:

import asyncio

N = int(input())
Cs = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    adj[a - 1].append(b - 1)
    adj[b - 1].append(a - 1)

degs = [0] * N
clusters = [[] for _ in range(N)]


async def go(curr, fro):
    col = Cs[curr] - 1
    deg = 1
    clust = {}
    for n in adj[curr]:
        if n == fro:
            continue
        ch_deg, ch_clust = await go(n, curr)
        clusters[col].append(ch_deg - ch_clust.get(col, 0))
        if len(ch_clust) > len(clust):
            clust, ch_clust = ch_clust, clust
        for k, v in ch_clust.items():
            clust[k] = clust.get(k, 0) + v
        deg += ch_deg
    clust[col] = deg
    return deg, clust


_, root_clust = asyncio.get_event_loop().run_until_complete(go(0, -1))
for i in range(N):
    clusters[i].append(N - root_clust.get(i, 0))


for clust in clusters:
    tot = (N * N + N) // 2
    for cnt in clust:
        tot -= (cnt * cnt + cnt) // 2
    print(tot)
Problem p02711 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
listN = sorted(str(N))
if listN.count("7") >= 1:
    print("Yes")
else:
    print("No")


Error: TypeError: 'int' object is not iterable
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02711/Python/s383379366.py", line 3, in <module>
    listN = sorted(N)
TypeError: 'int' object is not iterable


Fixed submission:

N = int(input())
str(N)
listN = sorted(N)
if listN.count("7") >= 1:
    print("Yes")
else:
    print("No")
Problem p02712 (Python) — buggy submission, verdict: Runtime Error

N = int(input())

print(sum([n + 1 for n in range(N) if (n + 1) % 3 != 0 and (n + 1) % 5 != 0]))


Error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02712/Python/s624885598.py", line 1, in <module>
    N = int(input().split())
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'


Fixed submission:

N = int(input().split())

print(sum([n + 1 for n in range(N) if (n + 1) % 3 != 0 and (n + 1) % 5 != 0]))
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
from functools import reduce
from itertools import combinations
from math import gcd

K = int(input())

sum = K * (K + 1) // 2

for i in combinations(range(1, K + 1), 2):
    sum += 6 * reduce(gcd, i)

for i in combinations(range(1, K + 1), 3):
    sum += 6 * reduce(gcd, i)

print(sum)


Error: TLE

Fixed submission:

#!/usr/bin/env python3
from functools import reduce
from itertools import product
from math import gcd

k = int(input())
p = list(product(range(1, k + 1), repeat=3))
print(sum(reduce(gcd, i) for i in p))
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math


k = int(input())
answer = 0
for w_a in range(1, k + 1):
    for w_b in range(1, k + 1):
        w = math.gcd(w_a, w_b)
        for w_c in range(1, k + 1):
            answer += math.gcd(w, w_c)

print(answer)


Error: TLE

Fixed submission:

import math


def gcd(a, b, c):

    return math.gcd(math.gcd(a, b), c)


k = int(input())
answer = 0
for w_a in range(1, k + 1):
    for w_b in range(1, k + 1):
        for w_c in range(1, k + 1):
            answer += gcd(w_a, w_b, w_c)

print(answer)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys
import math
from functools import lru_cache

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

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

K = int(input())

total = 0

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

print(total)


Error: TLE

Fixed submission:

import math
from functools import lru_cache


@lru_cache(maxsize=None)
def gcd(a, b):
    return math.gcd(a, b)


K = int(input())

total = 0

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

print(total)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

from functools import reduce

# from collections import defaultdict


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


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


def reducef(x, y):
    return gcdd[(x, y)]


def gcd2(*numbers):
    return reduce(reducef, numbers)


K = int(input())
ans = 0
# gcdd=defaultdict()
gcdd = {}
for a in range(1, K + 1):
    for b in range(1, K + 1):
        if (a, b) not in gcdd:
            gcdd[(a, b)] = gcddd(a, b)

for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            ans += gcd2(a, b, c)
print(ans)


Error: TLE

Fixed submission:

import math
from functools import reduce

# from collections import defaultdict


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


def reducef(x, y):
    return gcdd[(x, y)]


def gcd2(*numbers):
    return reduce(reducef, numbers)


K = int(input())
ans = 0
# gcdd=defaultdict()
gcdd = {}
for a in range(1, K + 1):
    for b in range(1, K + 1):
        if (a, b) not in gcdd:
            gcdd[(a, b)] = gcd(a, b)

for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            ans += gcd2(a, b, c)
print(ans)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math as f

K = int(input())
total = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        ij = f.gcd(i, j)
        if ij == 1:
            total += K
            continue
        for k in range(1, K + 1):
            total += f.gcd(ij, k)

print(total)


Error: TLE

Fixed submission:

import math as f

K = int(input())
total = 0

for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            total += f.gcd(f.gcd(i, j), k)

print(total)
Problem p02713 (Python) — buggy submission, verdict: Runtime Error

from math import gcd


def gcd_3(a, b, c):
    return gcd(gcd(a, b), c)


sum_gcd = 0
K = int(input())
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for l in range(1, K + 1):
            sum_gcd += gcd_3(i, j, l)

print(sum_gcd)


Error: EOFError: EOF when reading a line
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02713/Python/s110715782.py", line 2, in <module>
    A = list(map(int, input().split()))
EOFError: EOF when reading a line


Fixed submission:

N = int(input())
A = list(map(int, input().split()))
worker_list = [0] * N

for i in range(0, N - 1):
    worker_list[A[i] - 1] += 1

for i in range(N):
    print(worker_list[i])
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

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)


Error: TLE

Fixed submission:

import math

K = int(input())

sum = 0

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

print(sum)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
n = int(input())
ans = 0
# gcd_grid = [[0] * 201 for _ in range(201)]


def gcd(n, m):
    """if n <= 200 and m <= 200:
    if gcd_grid[n][m] != 0:
        return gcd_grid[n][m]"""
    a, b = n, m
    if a > b:
        a, b = b, a
    while a != 0:
        a, b = b % a, a
    # gcd_grid[n][m] = b
    # gcd_grid[m][n] = b
    return b


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


Error: TLE

Fixed submission:

#!/usr/bin/env python3
n = int(input())
ans = 0


def gcd(n, m):
    a, b = n, m
    if a > b:
        a, b = b, a
    while a != 0:
        a, b = b % a, a
    return b


for i in range(1, n + 1):
    for j in range(1, n + 1):
        for k in range(1, n + 1):
            ans += gcd(k, gcd(i, j))
print(ans)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import itertools
import math


def gcd(a: int, b: int, c: int) -> int:
    return math.gcd(math.gcd(a, b), c)


def main() -> None:
    k = int(input())

    one_to_k = range(1, k + 1)
    s1 = sum(one_to_k)
    s2 = sum(math.gcd(a, b) for a, b in itertools.combinations(one_to_k, 2))
    s3 = sum(gcd(a, b, c) for a, b, c in itertools.combinations(one_to_k, 3))
    print(s1 + 6 * s2 + 6 * s3)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import itertools
import math


def gcd(a: int, b: int, c: int) -> int:
    return math.gcd(math.gcd(a, b), c)


def main() -> None:
    k = int(input())

    s = sum(gcd(a, b, c) for a, b, c in itertools.product(range(1, k + 1), repeat=3))
    print(s)


if __name__ == "__main__":
    main()
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math

K = int(input())
sumGCD = K * K * K
for a in range(2, K + 1, 1):
    for b in range(2, K + 1, 1):
        for c in range(2, K + 1, 1):
            arr = [a, b, c]
            gcdNum = math.gcd(math.gcd(a, b), c)
            # if gcdNum != 1:
            # print(arr)
            # print(gcdNum)
            sumGCD += gcdNum - 1
print(sumGCD)


Error: TLE

Fixed submission:

import math
from functools import reduce


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


K = int(input())
sumGCD = K * K * K
for a in range(2, K + 1, 1):
    for b in range(2, K + 1, 1):
        for c in range(2, K + 1, 1):
            arr = [a, b, c]
            gcdNum = gcd(*arr)
            # if gcdNum != 1:
            # print(arr)
            # print(gcdNum)
            sumGCD += gcdNum - 1
print(sumGCD)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

from math import gcd
from functools import reduce


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


K = int(input())
ans = 1
if K == 1:
    next
else:
    ans += (K - 1) * K * 3
    ans_sub = 0
    for i in range(2, K):
        for j in range(i + 1, K + 1):
            for k in range(i, K + 1):
                ans_sub += gcd_kai(i, j, k)
    ans += ans_sub * 3
    for i in range(2, K + 1):
        ans += i
print(ans)


Error: TLE

Fixed submission:

from math import gcd
from functools import reduce


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


K = int(input())
ans = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            ans += gcd_kai(i + 1, j + 1, k + 1)
print(ans)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

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)


Error: TLE

Fixed submission:

import math

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 += math.gcd(c, math.gcd(a, b))
print(sum)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math
from functools import reduce

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 += reduce(math.gcd, (i, j, k))

print(sum)


Error: TLE

Fixed submission:

import math
from functools import reduce


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


K = int(input())

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

print(sum(hoge))
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math

K = int(input())

gcds = []

for a in range(1, K + 1):
    for b in range(1, K + 1):
        gcds.append(math.gcd(a, b))

# print(gcds)

ans = 0
for c in range(1, K + 1):
    for g in gcds:
        if c == 1 or g == 1:
            ans += 1
        else:
            ans += math.gcd(c, g)

print(ans)


Error: TLE

Fixed submission:

import math

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):
            a = math.gcd(math.gcd(i, j), k)
            # print(math.gcd(math.gcd(i, j), k))
            sum += a

print(sum)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math
from functools import reduce


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


n = int(input())

ans = 0

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

print(ans)


Error: TLE

Fixed submission:

import math
from functools import reduce


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


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):
            cnt = gcd(i, j, k)
            ans += cnt

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

print(ans)


Error: TLE

Fixed submission:

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(math.gcd(i, j), k)

print(ans)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math

k = int(input())

k += 1
ans = 0

ab = []

for a in range(1, k):
    for b in range(1, k):
        ab.append(math.gcd(a, b))

for a in ab:
    for c in range(1, k):
        ans += math.gcd(a, c)

print(ans)


Error: TLE

Fixed submission:

import math

k = int(input())

k += 1
ans = 0

for a in range(1, k):
    for b in range(1, k):
        for c in range(1, k):
            ab = math.gcd(a, b)
            bc = math.gcd(b, c)
            abc = math.gcd(ab, bc)

            ans += abc

print(ans)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math
from functools import reduce
import itertools

K = int(input())

# K = 2


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


ans = 0
for i in range(1, K + 1):
    # i = 1
    ans += gcd(i, i, i)

    for j in range(i + 1, K + 1):
        ans += gcd(i, i, j) * 3

    for j, k in itertools.product(range(i + 1, K + 1), range(i + 1, K + 1)):
        ans += gcd(i, j, k) * 3

print(ans)


Error: TLE

Fixed submission:

import math
from functools import reduce

K = int(input())

# K = 2


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


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)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import unittest
from io import StringIO
import sys
import itertools
import math


def gcd(a, b, c):
    n = 1
    if a == b:
        if b != c:
            n = 3
    else:
        if b != c:
            n = 6
        else:
            n = 3
    return math.gcd(math.gcd(a, b), c) * n


def resolve():
    K = int(input())
    T = [i for i in range(1, K + 1)]
    Tr = itertools.combinations_with_replacement(T, 3)
    sumT = 0
    for a, b, c in Tr:
        sumT += gcd(a, b, c)
    print(sumT)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2"""
        output = """9"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """200"""
        output = """10813692"""
        self.assertIO(input, output)

    def test_add(self):
        input = """195"""
        output = """9997134"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()


Error: TLE

Fixed submission:

def gcd(a, b, c):
    minV = min([a, b, c])
    maxV = max([a, b, c])
    start = minV
    diff = maxV - minV
    if diff < start and diff != 0:
        start = diff
    for gcdV in range(start, 1, -1):
        if (a % gcdV == 0) and (b % gcdV == 0) and (c % gcdV == 0):
            return gcdV
    return 1


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(a, b, c)

print(sum)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np

K = int(input())
x = np.arange(1, K + 1)
nums = np.gcd.outer(np.gcd.outer(x, x), x)
ans = nums.sum()
print(ans)


Error: TLE

Fixed submission:

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


K = int(input())

G = [[0] * (K + 1) for _ in range(K + 1)]

for a in range(1, K + 1):
    for b in range(1, K + 1):
        G[a][b] = gcd(a, b)

ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            if G[a][b] == 1:
                ans += 1
                continue
            ans += gcd(G[a][b], c)
print(ans)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

from math import gcd

K = int(input())
ans = 0
for i in range(1, K + 1):
    ans += i
for i in range(1, K + 1):
    for j in range(i + 1, K + 1):
        ans += gcd(i, j) * 6

for i in range(1, K - 1):
    for j in range(i + 1, K):
        for k in range(j + 1, K + 1):
            ans += gcd(gcd(i, j), k) * 6

print(ans)


Error: TLE

Fixed submission:

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):
            ans1 = math.gcd(a, b)
            ans2 = math.gcd(ans1, c)
            ans += ans2

print(ans)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

def GCD(a, b):
    return a if b == 0 else GCD(b, a % b)


k = int(input())

ans = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        ab = GCD(a, b)
        if ab == 1:
            ans += k
            continue
        for c in range(1, k + 1):
            ans += GCD(ab, c)

print(ans)


Error: TLE

Fixed submission:

from math import gcd

k = int(input())
d = {}

ans = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        for c in range(1, k + 1):
            if (a, b, c) in d:
                ans += d[(a, b, c)]
            else:
                d[(a, b, c)] = gcd(a, gcd(b, c))
                ans += d[(a, b, c)]

print(ans)
Problem p02713 (Python) — buggy submission, verdict: Time Limit Exceeded

import math

k = int(input())
result = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        p = math.gcd(a, b)
        for c in range(1, k + 1):
            q = math.gcd(p, c)
            result += q
print(result)


Error: TLE

Fixed submission:

import math

k = int(input())
result = 0
for a in range(1, k + 1):
    for b in range(1, k + 1):
        for c in range(1, k + 1):
            p = math.gcd(a, b)
            q = math.gcd(p, c)
            result += q
print(result)