Problem p02840 (python)

Fixed submission:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,x,d = list(map(int, input().split()))
if d==0:
    if x==0:
        ans = 1
    else:
        ans = n+1
else:
    if d<0:
        x *= -1
        d *= -1
#         x = x + d*(n-1)
#         d *= -1
    D = {} # (要素数, 最大値)
    for i in range(n+1):
        # iX+sD
        if x!=0:
            ii = i*x % d
            jj = (i*x-ii)//d
        else:
            ii = 0
            jj = 0
#         print(ii,jj)
        if ii not in D:
            D[ii] = []
        D[ii].append((i*(i-1)//2 + jj, (2*n-1-i)*i//2 + jj))
    ans = 0
    for k,v in D.items():
        v.sort()
        m = -float("inf")
        val = 0
        for l,r in v:
#             val += (r - (max(l-1, m)))
            val += max(r - (max(l-1, m)), 0)
            m = max(m, r)
        ans += val
                    
#         num = ((2*n-1-i)*i//2 + jj) - max(i*(i-1)//2 + jj - 1, m if m is not None else -float("inf"))
#         D[ii] = [D[ii][0]+num, (2*n-1-i)*i//2 + jj]
#         print(D)
#         if ii==0 and (2*n-1-i)*i//2 + jj >= 0 >= i*(i-1)//2 + jj:
#             zz = True
#     ans = sum(v[0] for k,v in D.items() if v[1] is not None) + (not zz)
# print(ans)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,x,d = list(map(int, input().split()))
if d<0:
    x = x + d*(n-1)
    d *= -1
    
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
if d==0:
    ans = n
else:
    if x!=0:
        g = gcd(x,d)
        l = x*d//g
        lx = l//x
        ld = l//d
    d = {} # (要素数, 最大値)
    for i in range(n):
        # iX+sD
        if x!=0:
            ii = i - (i//lx)*lx
        else:
            ii = 0
        jj = (i//lx)*ld
    #     print(ii,jj)
        if ii not in d:
            d[ii] = [0,None]
        nn, m = d[ii]
        num = ((2*n-1-i)*i//2 + jj) - max(i*(i-1)//2 + jj - 1, m if m is not None else -float("inf"))
        d[ii] = [d[ii][0]+num, (2*n-1-i)*i//2 + jj]
    ans = sum(v[0] for k,v in d.items() if v[1] is not None) + 1
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,x,d = list(map(int, input().split()))
if d<0:
    x = x + d*(n-1)
    d *= -1
    
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
if x!=0:
    g = gcd(x,d)
    l = x*d//g
    lx = l//x
    ld = l//d
d = {} # (要素数, 最大値)
for i in range(n):
    # iX+sD
    if x!=0:
        ii = i - (i//lx)*lx
    else:
        ii = 0
    jj = (i//lx)*ld
#     print(ii,jj)
    if ii not in d:
        d[ii] = [0,None]
    nn, m = d[ii]
    num = ((2*n-1-i)*i//2 + jj) - max(i*(i-1)//2 + jj - 1, m if m is not None else -float("inf"))
    d[ii] = [d[ii][0]+num, (2*n-1-i)*i//2 + jj]
ans = sum(v[0] for k,v in d.items() if v[1] is not None)
print(ans+1)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,x,d = list(map(int, input().split()))
if d<0:
    x = x + d*(n-1)
    d *= -1
    
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
if x!=0:
    g = gcd(x,d)
    l = x*d//g
    lx = l//x
    ld = l//d
d = {} # (要素数, 最大値)
for i in range(n):
    # iX+sD
    if x!=0:
        ii = i - (i//lx)*lx
    else:
        ii = 0
    jj = (i//lx)*ld
    print(ii,jj)
    if ii not in d:
        d[ii] = [0,None]
    nn, m = d[ii]
    num = ((2*n-1-i)*i//2 + jj) - max(i*(i-1)//2 + jj - 1, m if m is not None else -float("inf"))
    d[ii] = [d[ii][0]+num, (2*n-1-i)*i//2 + jj]
ans = sum(v[0] for k,v in d.items() if v[1] is not None)
print(ans+1)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,x,d = list(map(int, input().split()))
if d<0:
    x = x + d*(n-1)
    d *= -1
    
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
if x!=0:
    g = gcd(x,d)
    l = x*d//g
    lx = l//x
    ld = l//d
d = {i:[0,None] for i in range(n)} # (要素数, 最大値)
for i in range(n):
    # iX+sD
    if x!=0:
        ii = i - (i//lx)*lx
    else:
        ii = 0
    jj = (i//lx)*ld
    nn, m = d[ii]
    num = ((2*n-1-i)*i//2 + jj) - max(i*(i-1)//2 + jj - 1, m if m is not None else -float("inf"))
    d[ii] = [d[ii][0]+num, (2*n-1-i)*i//2 + jj]
ans = sum(v[0] for k,v in d.items() if v[1] is not None)
print(ans+1)