Problem p02550 (python)

Fixed submission:
n,x,m = list(map(int,input().split()))

if x == 0:
    ans = 0
else:
    ans = x % m
dicNum = dict()
dicSum = dict()
dicNum[x] = 1
dicSum[1] = x
dicSum[0] = 0

for i in range(2,n+1):
    b = x
    x = x**2 % m
    if x in dicNum.keys():
        t = i - dicNum[x]
        zan = n - i + 1
        k = zan // t
        ans += (dicSum[i-1] - dicSum[dicNum[x]-1]) * k
        if zan% t != 0:
            zan = zan % t
            ans += dicSum[dicNum[x] - 1 + zan] - dicSum[dicNum[x]-1]
        break
    else:
        ans += x
        dicSum[i] = ans
        dicNum[x] = i

print(ans)

Buggy submission (verdict: Runtime Error):
n,x,m = list(map(int,input().split()))

ans = x % m
dicNum = dict()
dicSum = dict()
dicNum[x] = 1
dicSum[1] = x

for i in range(2,n+1):
    b = x
    x = x**2 % m
    if x in dicNum.keys():
        t = i - dicNum[x]
        zan = n - i + 1
        k = zan // t
        ans += (dicSum[i-1] - dicSum[dicNum[x]-1]) * k
        if zan% t != 0:
            zan = zan % t
            ans += dicSum[dicNum[x] - 1 + zan] - dicSum[dicNum[x]-1]
        break
    else:
        ans += x
        dicSum[i] = ans
        dicNum[x] = i

print(ans)

Buggy submission (verdict: Runtime Error):
n,x,m = list(map(int,input().split()))

ans = x % m
#dic = dict()
dicNum = dict()
dicSum = dict()
dicNum[x] = 1
dicSum[1] = x

if n > 10000:
    for i in range(2,n+1):
        b = x
        x = x**2 % m
        if x in dicNum.keys():
            t = i - dicNum[x]
            zan = n - i + 1
            k = zan // t
            ans += (dicSum[i-1] - dicSum[dicNum[x]-1]) * k
            if zan% t != 0:
                zan = zan % t
                ans += dicSum[dicNum[x] - 1 + zan] - dicSum[dicNum[x]-1]
            break
        else:
            ans += x
            dicSum[i] = ans
            #dic[x] = ans
            dicNum[x] = i

else:
    for i in range(2,n+1):
        x = x**2 % m
        ans += x
        dicSum[i] = ans
        #dic[x] = ans
        dicNum[x] = i

print(ans)

Buggy submission (verdict: Runtime Error):
n,x,m = list(map(int,input().split()))

ans = x % m
dic = dict()
dicNum = dict()
dicSum = dict()

if n > 10000:
    for i in range(2,n+1):
        b = x
        x = x**2 % m
        if x in dicNum.keys():
            t = i - dicNum[x]
            zan = n - i + 1
            k = zan // t
            ans += (dicSum[i-1] - dicSum[dicNum[x]-1]) * k
            if zan% t != 0:
                zan = zan % t
                ans += dicSum[dicNum[x] - 1 + zan] - dicSum[dicNum[x]-1]
            break
        else:
            ans += x
            dicSum[i] = ans
            dic[x] = ans
            dicNum[x] = i

else:
    for i in range(2,n+1):
        x = x**2 % m
        ans += x
        dicSum[i] = ans
        dic[x] = ans
        dicNum[x] = i

print(ans)

Buggy submission (verdict: Runtime Error):
n,x,m = list(map(int,input().split()))

ans = x % m
dic = dict()
dicNum = dict()
dicSum = dict()

if n > 10000:
    for i in range(2,n+1):
        b = x
        x = x**2 % m
        if x in dicNum.keys():
            t = i - dicNum[x]
            zan = n - i + 1
            k = zan // t
            ans += (dicSum[i-1] - dicSum[dicNum[x]-1]) * k
            if zan% t != 0:
                zan = zan % t
                ans += dicSum[dicNum[x] - 1 + zan] - dicSum[dicNum[x]-1]
            break
        else:
            ans += x
            dicSum[i] = ans
            dic[x] = ans
            dicNum[x] = i

else:
    for i in range(2,n+1):
        ans += x
        dicSum[i] = ans
        dic[x] = ans
        dicNum[x] = i

print(ans)

Buggy submission (verdict: Runtime Error):
n,x,m = list(map(int,input().split()))

ans = x % m
dic = dict()
dicNum = dict()
dicSum = dict()

for i in range(2,n+1):
    b = x
    x = x**2 % m
    if x in dicNum.keys():
        t = i - dicNum[x]
        zan = n - i + 1
        k = zan // t
        ans += (dicSum[i-1] - dicSum[dicNum[x]-1]) * k
        if zan% t != 0:
            zan = zan % t
            ans += dicSum[dicNum[x] - 1 + zan] - dicSum[dicNum[x]-1]
        break
    else:
        ans += x
        dicSum[i] = ans
        dic[x] = ans
        dicNum[x] = i

print(ans)

Buggy submission (verdict: Runtime Error):
n,x,m = list(map(int,input().split()))

ans = x % m
dic = dict()
dicNum = dict()
dicSum = dict()

for i in range(2,n+1):
    b = x
    x = x**2 % m
    if x in dicNum.keys():
        t = i - dicNum[x]
        zan = n - i + 1
        k = zan // t
        ans += (dicSum[i-1] - dicSum[dicNum[x]-1]) * k
        zan = zan % t
        ans += dicSum[dicNum[x] - 1 + zan] - dicSum[dicNum[x]-1]
        break
    else:
        ans += x
        dicSum[i] = ans
        dic[x] = ans
        dicNum[x] = i

print(ans)