Buggy python submission (verdict: Time Limit Exceeded):
# encoding: utf-8
N, K = map(int, input().split())

ans = 0

# 2 * (a + b + c) = K * (p + q + r)
for pqr in range(3, (6 * N) // K + 1):
    # a + b + c = ?
    if (pqr * K) % 2: continue
    else: abc = (pqr * K) // 2
    for p in range(max((1, -((N - abc) // K))), abc // K + 1):
        qr = pqr - p
        min_r = -((p * K - 2 * abc) // (2 * K))
        max_r = min(((2 * abc) // K - 2 * p, (abc - 1) // K))
        # count w/o r loop
        if min_r > max_r: continue
        # print(abc, (p, qr - min_r, min_r), (p, qr - max_r, max_r))
        ans += (max_r - min_r + 1) * 6
        if p == qr - min_r == min_r: ans -= 5 # p = q = r
        if p < qr - min_r == min_r: ans -= 3 # p < q = r
        if p == qr - max_r < max_r: ans -= 3 # p = q < r
            
    
print(ans)


Fixed submission:
# encoding: utf-8
N, K = map(int, input().split())

ans = 0
for a in range(1, N + 1):
    # find a (a, b, c)
    # maybe TLE
    # for b in range(1, N + 1):
    #     if (a + b) % K == 0: break
    # for c in range(1, K + 1):
    #     # c and c + K returns same result
    #     if (b + c) % K == 0 and (a + c) % K == 0: break
    #     else: continue
    # else:
    #     continue # next a
    if (K - 2 * a) % K: continue
    b = K - (a % K)
    c = K - b            
    # b_min, c_min
    b_min = K - (K - b) % K
    c_min = K - (K - c) % K
    # b_max, c_max
    b_max = N - (N - b_min) % K
    c_max = N - (N - c_min) % K
    # add
    # print(a, b, c, (b_min, b_max), (c_min, c_max))
    ans += ((b_max - b_min) // K + 1) * ((c_max - c_min) // K + 1)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# encoding: utf-8
N, K = map(int, input().split())

ans = 0
pqr = 3
while pqr * K <= 6 * N:
    for r in filter(lambda x: pqr - x > x, range(pqr // 3, pqr)):
        pq = pqr - r
        if r * K > 2 * N: break
        for p in range(pq - r, (pq // 2) + 1):
            q = pq - p
            if p > q: break
            elif q * K > 2 * N: continue
            elif p * K > 2 * N: break
            elif (q + r - p) * K > 2 * N: continue
            elif (r + p - q) * K > 2 * N: break
            elif (q + r - p) * K % 2: continue
            elif (r + p - q) * K % 2: continue
            # print(p, q, r)
            if p == q == r: ans += 1
            elif p < q < r: ans += 6
            else: ans += 3
    pqr += 1
    
print(ans)

Fixed submission:
# encoding: utf-8
N, K = map(int, input().split())

ans = 0
for a in range(1, N + 1):
    # find a (a, b, c)
    # maybe TLE
    # for b in range(1, N + 1):
    #     if (a + b) % K == 0: break
    # for c in range(1, K + 1):
    #     # c and c + K returns same result
    #     if (b + c) % K == 0 and (a + c) % K == 0: break
    #     else: continue
    # else:
    #     continue # next a
    if (K - 2 * a) % K: continue
    b = K - (a % K)
    c = K - b            
    # b_min, c_min
    b_min = K - (K - b) % K
    c_min = K - (K - c) % K
    # b_max, c_max
    b_max = N - (N - b_min) % K
    c_max = N - (N - c_min) % K
    # add
    # print(a, b, c, (b_min, b_max), (c_min, c_max))
    ans += ((b_max - b_min) // K + 1) * ((c_max - c_min) // K + 1)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# encoding: utf-8
N, K = map(int, input().split())

ans = 0
pqr = 3
while pqr * K <= 6 * N:
    for r in filter(lambda x: pqr - x > x, range(1, pqr)):
        pq = pqr - r
        if r * K > 2 * N: break
        for p in range(pq - r, (pq // 2) + 1):
            q = pq - p
            if p > q: break
            elif q * K > 2 * N: continue
            elif p * K > 2 * N: break
            elif (q + r - p) * K > 2 * N: continue
            elif (r + p - q) * K > 2 * N: continue
            elif (q + r - p) * K % 2: continue
            elif (r + p - q) * K % 2: continue
            # print(p, q, r)
            if p == q == r: ans += 1
            elif p < q < r: ans += 6
            else: ans += 3
    pqr += 1
    
print(ans)

Fixed submission:
# encoding: utf-8
N, K = map(int, input().split())

ans = 0
for a in range(1, N + 1):
    # find a (a, b, c)
    # maybe TLE
    # for b in range(1, N + 1):
    #     if (a + b) % K == 0: break
    # for c in range(1, K + 1):
    #     # c and c + K returns same result
    #     if (b + c) % K == 0 and (a + c) % K == 0: break
    #     else: continue
    # else:
    #     continue # next a
    if (K - 2 * a) % K: continue
    b = K - (a % K)
    c = K - b            
    # b_min, c_min
    b_min = K - (K - b) % K
    c_min = K - (K - c) % K
    # b_max, c_max
    b_max = N - (N - b_min) % K
    c_max = N - (N - c_min) % K
    # add
    # print(a, b, c, (b_min, b_max), (c_min, c_max))
    ans += ((b_max - b_min) // K + 1) * ((c_max - c_min) // K + 1)

print(ans)