ZhenlongDai/ACPR huggingface dataset:
Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
k=input()
if int(k)%2==0:
    print(-1)
else:
    s='7'
    flag=0
    for _ in range(12):
        if int(s)%int(k)==0:
            flag=1
            print(len(s))
            break
        s=s+'7'
    if flag==0 and k[-1]=='3':
        print(int(k)-1)
    elif flag==0:
        print(-1)

Failing test case input:
21831

Expected output:
3438


Actual output:
-1


Fixed submission (verdict: Accepted):
k=input()
if int(k)%2==0:
    print(-1)
else:
    s=7
    count=1
    flag=0
    while 1:
        if s%int(k)==0:
            print(count)
            break
        if count>int(k):
            print(-1)
            break
        s=(s%int(k))*10+7
        count+=1

Problem: p02596
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect

INF = 10**9+7

if __name__ == "__main__":
    k = int(input())
    if k%2 == 0:
        print(-1)
        sys.exit()
    a = 7
    p = len(str(k)) + 1
    for i in range(p+100):
        if a%k == 0:
            print(i+1)
            sys.exit()
        a*=10
        a+=7
    print(k-1)

Failing test case input:
958033

Expected output:
25740


Actual output:
958032


Fixed submission (verdict: Accepted):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect

INF = 10**9+7

if __name__ == "__main__":
    k = int(input())
    if k%2 == 0:
        print(-1)
        sys.exit()
    cnt = 1
    a = 7
    d = [a]
    for i in range(10**6+1):
        if d[i]%k == 0:
            print(i+1)
            sys.exit()
        b = 10*d[i]
        b%=k
        b+=7
        b%=k
        d.append(b)
    print(-1)

Problem: p02596
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
 
def I(): return int(input())

def main():
    
    if K%2 == 0:
        print('-1')
        sys.exit()
    
    ans = 1
    n = 7
    for i in range(K):
        
        if n%K ==0:
            print(ans)
            break
        
        n = (10*n + 7)%K  
        print(f"ai{i+1}: ",n)
        ans += 1
        

Failing test case input:
7083

Expected output:
1179


Actual output:

Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
 
def I(): return int(input())

def main():
    
    K = I()
    if K%2 == 0:
        print('-1')
        sys.exit()
    
    ans = 1
    n = 7
    for i in range(K):
        
        if n%K ==0:
            print(ans)
            break
        
        n = (10*n + 7)%K  
        ans += 1
    
    else:
        print('-1')

main()
Problem: p02596
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
k = int(input())
x = 1
s = 0
for i in range(1, 2000005):
    s += x
    s %= k
    if s == 0:
        print(i)
        exit()
    x *= 10
    x %= k
print(-1)

Failing test case input:
5163823

Expected output:
73450


Actual output:
220350


Fixed submission (verdict: Accepted):
k = int(input())
x = 1
s = 0
for i in range(1, 2000005):
    s += x
    s %= k
    if s == 0 or (7 * s) % k == 0:
        print(i)
        exit()
    x *= 10
    x %= k
print(-1)

Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
K = int(input())
if K%2 == 0:
  print(-1)
  exit()
num = 0
for i in range(int(K**0.5)+1):
  num *= 100
  num += 77
  if num%K == 0:
    print(len(str(num)))
    exit()
print(K-1)

Failing test case input:
6451

Expected output:
2150


Actual output:
6450


Fixed submission (verdict: Accepted):
K = int(input())
if K%2 == 0:
  print(-1)
  exit()
num = 0
for i in range(10**6+1):
  num *= 10
  num += 7
  if num%K == 0:
    print(i+1)
    exit()
  num %= K
print(-1)

Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

import sys

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


k = I()

m = k % 10
if m % 2 == 0 or m == 5:
    print(-1)
    exit()
elif k == 1:
    print(1)
    exit()
else:
    s = "1"
    rk = 1
    for i in range(1, 10 ** 6 + 1):
        rk1 = ((10 % k) * rk + 1) % k
        if rk1 == 0:
            print(i + 1)
            exit()
        rk = rk1


Failing test case input:
139909

Expected output:
286


Actual output:
858


Fixed submission (verdict: Accepted):

import sys

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


k = I()

m = k % 10
if m % 2 == 0 or m == 5:
    print(-1)
    exit()
if k == 1 or k == 7:
    print(1)
    exit()
else:
    rk = 7
    for i in range(2, 10 ** 6 + 1):
        rk1 = ((10 % k) * rk % k + 7 % k) % k
        if rk1 == 0:
            print(i)
            exit()
        rk = rk1

Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
k=input()
if int(k)%2==0:
    print(-1)
else:
    s='7'
    flag=0
    for _ in range(20):
        if int(s)%int(k)==0:
            flag=1
            print(len(s))
            break
        s=s+'7'
    if flag==0 and k[-1]=='3':
        print(int(k)-1)
    elif flag==0:
        print(-1)

Failing test case input:
1841409

Expected output:
6138


Actual output:
-1


Fixed submission (verdict: Accepted):
k=input()
if int(k)%2==0:
    print(-1)
else:
    s=7
    count=1
    flag=0
    while 1:
        if s%int(k)==0:
            print(count)
            break
        if count>int(k):
            print(-1)
            break
        s=(s%int(k))*10+7
        count+=1
Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
k = int(input().strip())
f=-1

for i in range(1,500):
    if ((10**i)-1)*7%(9*k)==0:
        
        f=i
        break
print(f)

Failing test case input:
4869

Expected output:
540


Actual output:
-1


Fixed submission (verdict: Accepted):
k=int (input())
f=-1
a=7
for i in range(1000000):
    a=(a*10+7)%k
    if a==0:
        f=i+2
        break
if k==7 or k==1:
    f=1
print(f)

Problem: p02596
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import re, math


if __name__ == '__main__':
    k = int(input())

    if k % 2 == 0 or k % 2 == 5:
        print('-1')
    else:

        num = 7
        count = 1
        while not num % k == 0:
            count += 1
            num = num * 10 + 7
            if count > math.sqrt(k):
                break
        if count > math.sqrt(k):
            print(k - 1)
        else:
            print(num, len(str(num)))



Failing test case input:
5181

Expected output:
78


Actual output:
5180


Fixed submission (verdict: Accepted):
if __name__ == '__main__':
    k = int(input())

    if k % 2 == 0 or k % 5 == 0:
        print('-1')
    else:
        mod_list = [7 % k]
        if mod_list[-1] == 0:
            print(1)
        else:
            for i in range(1000000):
                mod_list.append((mod_list[0] + mod_list[-1] * 10) % k)
                if mod_list[-1] == 0:
                    break
            print(len(mod_list))

Problem: p02596
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N = int(input())
a=0
flag=0
for i in range(1,10**3):
    a+=7*10**(i-1)
    if a%N==0:
        print(i)
        flag=1
        break
if flag==0:
    print(-1)
    

Failing test case input:
1841409

Expected output:
6138


Actual output:
-1


Fixed submission (verdict: Accepted):
N = int(input())
a=0
flag=0
for i in range(1,N+1):
    a=(a*10+7)%N   
    if a==0:
        print(i)
        flag=1
        break

if flag==0:
    print(-1)

Problem: p02596
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
k = int(input())
mod = 7 % k
print(mod)
ans = 1
for i in range(k):
    if k % 2 == 0:
        ans = -1
        break
    if mod == 0:
        break
    mod = (mod*10+7)%k
    ans += 1
if ans == i:
    ans = -1
print(ans)

Failing test case input:
9187

Expected output:
4593


Actual output:
7
4593


Fixed submission (verdict: Accepted):
k = int(input())
mod = 7 % k
ans = 1
for i in range(k):
    if k % 2 == 0:
        ans = -1
        break
    if mod == 0:
        break
    mod = (mod*10+7)%k
    ans += 1
if mod != 0:
    ans = -1
print(ans)

Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
k = int(input())

ans = 1
mod = 7

for i in range(k):
    if mod % 7 == 0:
        break
    ans += 1
    mod = (mod * 10 + 7) % k

if mod % k == 0:
    print(ans)
else:
    print(-1)

Failing test case input:
3711

Expected output:
618


Actual output:
-1


Fixed submission (verdict: Accepted):
k = int(input())

ans = 1
mod = 7

for i in range(k):
    if mod % k == 0:
        break
    ans += 1
    mod = (mod * 10 + 7) % k

if mod % k == 0:
    print(ans)
else:
    print(-1)
Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
k=int(input())
mod=0
ans=0
for i in range(1000000):
    ans+=1
    mod=(mod*10+7)%k

    if mod==0:
        break
print(ans if ans!=1000001 else -1)

Failing test case input:
1803846

Expected output:
-1


Actual output:
1000000


Fixed submission (verdict: Accepted):
k=int(input())
mod=0
ans=0
for i in range(1000000):
    ans+=1
    mod=(mod*10+7)%k

    if mod==0:
        break
print(ans if ans!=1000000 else -1)

Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
k=int(input())


def get(k):
    if k%7==0:
        k=k
    ans=1
    for i in range(1,k):
        if ans%k==0:
            return i
        ans=(ans*10+1)%k
    else:
        return 0





if k%2==0:
    print(-1)

else:
    print(get(k))



Failing test case input:
52171

Expected output:
1792


Actual output:
5376


Fixed submission (verdict: Accepted):
k=int(input())


def get(k):
    ans=7
    i=1
    while True:
        if ans%k==0:
            return i
        ans=(ans*10+7)%k
        i+=1




if k%2==0 or k%5==0:
    print(-1)

else:
    print(get(k))

Problem: p02596
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
k = int(input())

if k % 2 == 0:
    print(-1)
    exit()

tmp = 7
for i in range(1, k):
    tmp = (10 * tmp) % k
    if tmp == 63:
        print(i)
        break

Failing test case input:
191049

Expected output:
15540


Actual output:

Fixed submission (verdict: Accepted):
k = int(input())

if k % 2 == 0 or k % 5 == 0:
    print(-1)
    exit()

if k == 1:
    print(1)
    exit()

tmp = 7
for i in range(1, 10**7):
    tmp = (10 * tmp) % (9*k)
    if tmp == 7:
        print(i)
        exit()

print(-1)

Problem: p02596
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
k=int(input())
mod7 = 7%k
mod10= 10%k

mod = mod7
mod10times = mod7

if mod%k==0:
    print(1)

for i in range(2,2000000):
    mod10times = mod10times*mod10%k
    mod = mod10times+mod%k
    
    if  mod%k==0:
        print(i)
        break
        
print(-1)

Failing test case input:
1676603

Expected output:
67860


Actual output:
67860
-1


Fixed submission (verdict: Accepted):
k=int(input())
mod7 = 7%k
mod10= 10%k

mod = mod7
mod10times = mod7

if mod%k==0:
    print(1)
else:

    for i in range(2,2000000):
        mod10times = mod10times*mod10%k
        mod = mod10times+mod%k

        if  mod%k==0:
            break

    if i==1999999:
        print(-1)
    else:
        print(i)
Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x % m
a = x % m
note = []
for i in range(n-1):
    note.append(a)
    a = a**2 % m
    if a in set(note):
        ind = note.index(a)
        l = len(note[ind:])
        n -= len(note[:ind-1])
        syo = n 
        a = n % l
        s = s + sum(note[ind:]) * (syo-1)
        if a != 0:
            for j in range(ind, ind+a-1):
                s += note[j]
        print(s)
        exit()
    s += a
    if a == 0:
        print(s)
        exit()

print(s)

Failing test case input:
10111011000 2 324

Expected output:
1557095693734


Actual output:
28027722492658


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind-1])
        syo = n 
        r = n % l
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r-1):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)




Failing test case input:
11110000100 2 99959

Expected output:
556111910251094


Actual output:
874207922707065783


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)
Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(n):
    aa = pow(aa,2,m)
    if aa in aset:
        ind = alist.index(aa)
        kosu = n
        ans = kosu*ans
        ans += sum(alist[0:n-kosu*(ind+1)])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

    if aa == 0:
        break

print(ans)


Failing test case input:
10010001000 2 99959

Expected output:
501051370879075


Actual output:
787652814556587000


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(1,n):
    aa = pow(aa,2,m)
    if aa in aset:
        offset = alist.index(aa)
        loop = alist[offset:i]
        nloop, tail = divmod(n-offset, len(loop))
        ans += sum(loop)*(nloop-1)
        ans += sum(loop[0:tail])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

print(ans)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, X, M = map(int, input().split())

A_change_list = [0] * (M + 1)
check_list = [0] * (M + 1)

for i in range(1, M + 1):
    A_change_list[i] = i**2 % M

A = X
count = 0
sum_value = 0
ans = 0
for i in range(1, N + 1):
    if check_list[A] != 2:
        if check_list[A] == 1:
            count += 1
            sum_value += A
        ans += A
        check_list[A] += 1
        A = A_change_list[A]
    else:
        if A == 0:
            break
        elif A == 1:
            ans += N - i
            break
        else:
            div, mod = divmod((N - i), count)
            ans += sum_value * div
            for _ in range(mod + 1):
                ans += A
                A = A_change_list[A]
            break

print(ans)

Failing test case input:
0101 1 16

Expected output:
101


Actual output:
100


Fixed submission (verdict: Accepted):
N, X, M = map(int, input().split())

A_change_list = [0] * (M + 1)
check_list = [0] * (M + 1)

for i in range(1, M + 1):
    A_change_list[i] = i**2 % M

A = X
count = 0
sum_value = 0
ans = 0
for i in range(1, N + 1):
    if check_list[A] != 2:
        if check_list[A] == 1:
            count += 1
            sum_value += A
        ans += A
        check_list[A] += 1
        A = A_change_list[A]
    else:
        if A == 0:
            break
        elif A == 1:
            ans += N - i + 1
            break
        else:
            div, mod = divmod((N - i), count)
            ans += sum_value * div
            for _ in range(mod + 1):
                ans += A
                A = A_change_list[A]
            break

print(ans)
Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(1,n):
    aa = pow(aa,2,m)
    if aa in aset:
        ind = alist.index(aa)
        loop = alist[ind:i]
        nloop = (n-i+1)
        ans += sum(loop)*(nloop-1)
        ans += sum(loop[0:(n-i+1)%len(loop)-1])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

    if aa == 0:
        break

print(ans)




Failing test case input:
10000000101 4 952

Expected output:
5280000052020


Actual output:
10560000103764


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(1,n):
    aa = pow(aa,2,m)
    if aa in aset:
        offset = alist.index(aa)
        loop = alist[offset:i]
        nloop, tail = divmod(n-offset, len(loop))
        ans += sum(loop)*(nloop-1)
        ans += sum(loop[0:tail])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

print(ans)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):



N,X,M=map(int,input().split())
a = X
hist = [a]
indices = [-1]*M
indices[a] = 0
flag=0
for i in range(1,min(10**6,N)):
  a = a**2 % M
  if indices[a] != -1:
    idx = indices[a]
    flag=1
    break
  hist.append(a)
  indices[a] = i
  
if flag==0:
  print(sum(hist))
  exit()
  
cycle = hist[idx:]
margin = hist[:idx]
q,r = divmod((N-len(margin)),len(cycle))
ans = 0



ans += sum(cycle[:r])
print(r)
print(cycle[:10])
print(ans)


Failing test case input:
10010001001 8 1631

Expected output:
6746740675570


Actual output:
21
[8, 64, 834, 750, 1436, 512, 1184, 827, 540, 1282]
15050


Fixed submission (verdict: Accepted):



N,X,M=map(int,input().split())
a = X
hist = [a]
indices = [-1]*M
indices[a] = 0
flag=0
for i in range(1,min(10**6,N)):
  a = a**2 % M
  if indices[a] != -1:
    idx = indices[a]
    flag=1
    break
  hist.append(a)
  indices[a] = i
  
if flag==0:
  print(sum(hist))
  exit()
  
cycle = hist[idx:]
margin = hist[:idx]
q,r = divmod((N-len(margin)),len(cycle))
ans = 0
ans += sum(margin)
ans += sum(cycle)*q

ans += sum(cycle[:r])


print(ans)
Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, x, m = map(int, input().split())
count=x
now=x
flag=-1
sled=[now]
for i in range(1,n):
     now=(now*now)%m
     if now==0:
         flag=0
         break
     if now==1:
         flag==1
         nokori=n-i
         break
     if now in sled:
         start=sled.index(now)
         nokori=n-i
         flag=2
         break
     
     else:
         sled.append(now)
     count+=now
     

if flag==1:
    count+=nokori
if flag==2:
    
    sled=sled[start:]
    roopnum=int(nokori/len(sled))
    amari=nokori%len(sled)
    
    one_roop=sum(sled)
    count+=one_roop*roopnum
    
    for i in range(amari):
        count+=sled[i]
print(count)

Failing test case input:
10111011000 1 192

Expected output:
10111011000


Actual output:
1


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
count=x
now=x
flag=-1
sled=[now]
for i in range(1,n):
     now=(now*now)%m
     if now==0:
         flag=0
         break
     if now in sled:
         start=sled.index(now)
         nokori=n-i
         flag=2
         break
     
     else:
         sled.append(now)
     count+=now
     

if flag==2:
    
    sled=sled[start:]
    roopnum=int(nokori/len(sled))
    amari=nokori%len(sled)
    
    one_roop=sum(sled)
    count+=one_roop*roopnum
    
    for i in range(amari):
        count+=sled[i]
print(count)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N,X,M=[int(s) for s in input().split()]
modls=[0 for _ in range(M+1)]
modls[0]=X
for i in range(1,M+1):
  k=(modls[i-1]**2)%M
  if k in modls:
    break
  modls[i]=k
loop_end=i
loop_start=modls.index(k)
loop_length=loop_end-loop_start


ans=0
ans+=sum(modls[:loop_start])
if k!=0:
  ans+=sum(modls[loop_start:])*(N-loop_start)
  ans+=sum(modls[loop_start:loop_start  +  (N-loop_start)%loop_length])
print(ans)

Failing test case input:
10010000000 2 99959

Expected output:
501051320374664


Actual output:
787652675690153098


Fixed submission (verdict: Accepted):
N,X,M=[int(s) for s in input().split()]
modls=[0 for _ in range(M+1)]
modls[0]=X
modset=set()
for i in range(1,M+1):
  k=(modls[i-1]**2)%M
  if k in modset:
    break
  modls[i]=k
  modset.add(k)
loop_end=i
loop_start=modls.index(k)
loop_length=loop_end-loop_start

ans=0
ans+=sum(modls[:loop_start])
if k!=0:
  ans+=sum(modls[loop_start:])*((N-loop_start)
  ans+=sum(modls[loop_start:loop_start  +  (N-loop_start)%loop_length])
print(ans)
Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(1,n):
    aa = pow(aa,2,m)
    if aa in aset:
        ind = alist.index(aa)
        loop = alist[ind:i]
        nloop = (n-ind+1)
        ans += sum(loop)*(nloop-1)
        ans += sum(loop[0:(n-ind+1)%len(loop)-1])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

print(ans)


Failing test case input:
1010 1 20

Expected output:
1010


Actual output:
1011


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(1,n):
    aa = pow(aa,2,m)
    if aa in aset:
        offset = alist.index(aa)
        loop = alist[offset:i]
        nloop, tail = divmod(n-offset, len(loop))
        ans += sum(loop)*(nloop-1)
        ans += sum(loop[0:tail])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

print(ans)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind-1])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)




Failing test case input:
10000001000 2 15098

Expected output:
77466118834420


Actual output:
77466118423918


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)
Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind-1])
        syo = n 
        a = n % l
        s = s + sum(seq[ind:]) * (syo-1)
        if a != 0:
            for j in range(ind, ind+a-1):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)




Failing test case input:
10000000101 4 952

Expected output:
5280000052020


Actual output:
10560000105620


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind-1])
        syo = n 
        if syo == 0:
            break
        a = n % l
        s = s + sum(seq[ind:]) * (syo-1)
        if a != 0:
            for j in range(ind, ind+a-1):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)




Failing test case input:
10000001011 2 15344

Expected output:
64360006487510


Actual output:
514880052002902


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)
Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, x, m = map(int, input().split())
arr = [x]
for i in range(1, n+1):
    x = (x*x % m)
    if x in arr:
        break
    else:
        arr.append(x)
sa = sum(arr)
rem = n-i
argi = arr.index(x)
roop = arr[argi:]
nn, r = divmod(rem, len(roop))
print(sa + nn*sum(roop) + sum(roop[:r]))



Failing test case input:
6 4 1101

Expected output:
1944


Actual output:
2440


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

def solve(n, x, m):
    if n == 1:
        return x
    arr = [x]
    for i in range(1, n):
        x = x*x % m
        if x in arr:
            rem = n-i
            break
        else:
            arr.append(x)
    else:
        rem = 0
    sa = sum(arr)
    argi = arr.index(x)
    roop = arr[argi:]
    nn, r = divmod(rem, len(roop))
    return sa + nn*sum(roop) + sum(roop[:r])

print(solve(n, x, m))

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x % m
a = x % m
note = []
for i in range(n-1):
    note.append(a)
    a = a**2 % m
    if a in note:
        ind = note.index(a)
        l = len(note[ind:])
        n -= len(note[:ind-1])
        syo = n 
        a = n % l
        s = s + sum(note[ind:]) * (syo-1)
        for j in range(ind, ind+a-1):
            s += note[j]
        print(s)
        exit()
    s += a
    if a == 0:
        break

print(s)

Failing test case input:
10000000000 6 114525

Expected output:
539251071290475


Actual output:
15099030000077604


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)

Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, x, m = map(int, input().split())
g = x * 1
ans = x
arr = []
res = set([])
roop = (0, 0)
k = 0
for i in range(m + 1):
	tmp = x ** 2 % m
	if tmp in res:
		roop = (i + 1, tmp)
		break
	res.add(tmp)
	arr.append(tmp)
	ans += tmp
	x = tmp

for i, y in enumerate(arr):
	if y == roop[1]:
		k = i
		break

ini = sum(arr[:k])
mul = ans - ini - g
t, v = divmod(n - roop[0], roop[0])

print(ans + mul * t + sum(arr[k:k+v]))


Failing test case input:
10010000001 8 3503

Expected output:
18331646663895


Actual output:
15712840000665


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
g = x * 1
ans = x
arr = []
res = set([])
roop = (0, 0)
k = 0
for i in range(m + 1):
	tmp = x ** 2 % m
	if tmp in res:
		roop = (i, tmp)
		break
	res.add(tmp)
	arr.append(tmp)
	ans += tmp
	x = tmp

for i, y in enumerate(arr):
	if y == roop[1]:
		k = i
		break

ini = sum(arr[:k])
mul = ans - ini - g
t, v = divmod(n - k - 1, roop[0] - k)
print(g + ini + mul * t + sum(arr[k:k + v]))
Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):



N,X,M=map(int,input().split())
a = X
hist = [a]
indices = [-1]*M
indices[a] = 0
flag=0
for i in range(1,min(10**6,N)):
  a = a**2 % M
  if indices[a] != -1:
    idx = indices[a]
    flag=1
    break
  hist.append(a)
  indices[a] = i
  
if flag==0:
  print(sum(hist))
  exit()
  
cycle = hist[idx:]
margin = hist[:idx]
q,r = divmod((N-len(margin)),len(cycle))
ans = 0



ans += sum(cycle[:r])


print(ans)


Failing test case input:
0100 1 16

Expected output:
100


Actual output:
0


Fixed submission (verdict: Accepted):



N,X,M=map(int,input().split())
a = X
hist = [a]
indices = [-1]*M
indices[a] = 0
flag=0
for i in range(1,min(10**6,N)):
  a = a**2 % M
  if indices[a] != -1:
    idx = indices[a]
    flag=1
    break
  hist.append(a)
  indices[a] = i
  
if flag==0:
  print(sum(hist))
  exit()
  
cycle = hist[idx:]
margin = hist[:idx]
q,r = divmod((N-len(margin)),len(cycle))
ans = 0
ans += sum(margin)
ans += sum(cycle)*q

ans += sum(cycle[:r])


print(ans)

Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n, x, m = map(int, input().split())

def f(x, m):
    return x % m

a = x
a_set = {a}
a_array = [a]
a_index = 0
for i in range(10):
    a = f(a ** 2, m)
    if a in a_set:
        a_index = a_array.index(a)
        break
    a_set.add(a)
    a_array.append(a)



a_iter_array = a_array[a_index:]

div, mod = divmod(n - a_index, len(a_set))
ans = sum(a_array[:a_index]) + sum(a_iter_array) * div + sum(a_iter_array[:mod])
print(ans)

Failing test case input:
10000001001 2 3503

Expected output:
14076668074139


Actual output:
11009092010002


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

def f(x, m):
    return x % m

a = x
a_set = {a}
a_array = [a]
a_index = 0
while(True):
    a = f(a ** 2, m)
    if a in a_set:
        a_index = a_array.index(a)
        break
    a_set.add(a)
    a_array.append(a)


a_iter_array = a_array[a_index:]
div, mod = divmod(n - a_index, len(a_iter_array))
ans = sum(a_array[:a_index]) + sum(a_iter_array) * div + sum(a_iter_array[:mod])
print(ans)
Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n,x,m = map(int, input().split())
ai = x
tmp = [ai]
used = set(tmp)
for i in range(n):
    nxt = ai**2 % m
    if nxt in tmp:
        break
    tmp.append(nxt)
    ai = nxt
if len(tmp) == n:
    ans = sum(tmp)
    print(ans)
    exit()

rem = n-len(tmp)
first = tmp.index(nxt)
ans = 0
l = []
for i in range(len(tmp)):
    if i < first:
        ans += tmp[i]
    else:
        l.append(tmp[i])
m = len(l)
x,y = divmod(rem, m)
ans += sum(l)*x
for i in range(y):
    ans += l[i]
print(ans)



Failing test case input:
10000000001 13 593

Expected output:
2964722221726


Actual output:
2964722211053


Fixed submission (verdict: Accepted):
n,x,m = map(int, input().split())
ai = x
tmp = [ai]
used = set(tmp)
for i in range(n):
    nxt = ai**2 % m
    if nxt in tmp:
        break
    tmp.append(nxt)
    ai = nxt
if len(tmp) == n:
    ans = sum(tmp)
    print(ans)
    exit()

first = tmp.index(nxt)
ans = 0
l = []
for i in range(len(tmp)):
    if i < first:
        ans += tmp[i]
        n -= 1
    else:
        l.append(tmp[i])
m = len(l)
x,y = divmod(n, m)
ans += sum(l)*x
for i in range(y):
    ans += l[i]
print(ans)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        if ind != 0:
            n -= len(seq[:ind-1])
        syo = n 
        r = n % l
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r-1):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)




Failing test case input:
10110000000 2 99959

Expected output:
506056828534610


Actual output:
795521333887522472


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)
Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,x,m=map(int,input().split())
l=[0 for i in range(m)]
ans=0
y=[]
i=1
flag=0
ys=n
while i<n:
    if flag==0:
        ans+=x
    l[x]+=1
    x=x*x%m
    i+=1
    if l[x]==2 and flag==0:
        flag=1
        ys=i
    if flag:
        if l[x]==3:
            break
        y.append(x)
if len(y):
    p,pp=divmod(n-ys,len(y))
    print(ans+p*sum(y)+sum(y[:pp+1]))
    
else:
    print(ans)

Failing test case input:
6 4 1101

Expected output:
1944


Actual output:
1280


Fixed submission (verdict: Accepted):
n,x,m=map(int,input().split())
l=[0 for i in range(m)]
ans=0
y=[]
i=1
flag=0
ys=n
while i<n+1:
    if flag==0:
        ans+=x
    l[x]+=1
    x=x*x%m
    i+=1
    if l[x]==2 and flag==0:
        flag=1
        ys=i
    if flag:
        if l[x]==3:
            break
        y.append(x)
if len(y):
    p,pp=divmod(n-ys,len(y))
    print(ans+p*sum(y)+sum(y[:pp+1]))
    
else:
    print(ans)

Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys

input = sys.stdin.readline

n, x, m = map(int, input().split())


arr = [-1] * m
cumsum = [x]
a = x
for i in range(1, n):
    a *= a
    a %= m
    if arr[a] == -1:
        arr[a] = i
        cumsum.append(cumsum[-1] + a)
    else:
        print(i)
        di, mo = divmod(n - i, i - arr[a])

        ans = cumsum[-1]
        ans += di * (cumsum[-1] - cumsum[arr[a] - 1])
        ans += cumsum[arr[a] - 1 + mo] - cumsum[arr[a] - 1]
        break
else:
    ans = cumsum[-1]

print(ans)


Failing test case input:
10010011001 4 1244

Expected output:
3877878261404


Actual output:
21
3877878261404


Fixed submission (verdict: Accepted):
import sys

input = sys.stdin.readline

n, x, m = map(int, input().split())

arr = [-1] * m
cumsum = [x]
a = x
for i in range(1, n):
    a *= a
    a %= m
    if arr[a] == -1:
        arr[a] = i
        cumsum.append(cumsum[-1] + a)
    else:
        di, mo = divmod(n - i, i - arr[a])

        ans = cumsum[-1]
        ans += di * (cumsum[-1] - cumsum[arr[a] - 1])
        ans += cumsum[arr[a] - 1 + mo] - cumsum[arr[a] - 1]
        break
else:
    ans = cumsum[-1]

print(ans)
Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(n):
    aa = pow(aa,2,m)
    if aa in aset:
        ind = alist.index(aa)
        kosu = n
        ans = kosu*ans
        ans += sum(alist[0:n-kosu*(ind+1)-1])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

    if aa == 0:
        break

print(ans)


Failing test case input:
10000000000 3 114525

Expected output:
504215356947825


Actual output:
14118150000000000


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(1,n):
    aa = pow(aa,2,m)
    if aa in aset:
        offset = alist.index(aa)
        loop = alist[offset:i]
        nloop, tail = divmod(n-offset, len(loop))
        ans += sum(loop)*(nloop-1)
        ans += sum(loop[0:tail])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

print(ans)

Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n,x,m = map(int,input().split())
p = [0 for i in range(m)]
p[x] = 1
k = x
ans = 0
ans += k
me = [k]
mem = [k]
fin = 0
for i in range(n-1):
    k = k**2%m
    if p[k] == 0:
        p[k] = i+2
        ans += k
        mem.append(k)
        me.append(ans)
    else:
        sp = k
        ss = i+2
        fin = 1
        break
if fin == 0:
    print(ans)
else:
    for i in range(m):
        if mem[i] == sp:
            spi = i+1
            break
    ans0 = me[spi-2]
    uu = ans - ans0
    ans0 += (n-spi+1)
    for i in range((n-spi+1)%(ss-spi)):
        ans0 += mem[spi+i-1]
    print(ans0)

Failing test case input:
10000000001 13 3503

Expected output:
14640833329368


Actual output:
10000001889


Fixed submission (verdict: Accepted):
n,x,m = map(int,input().split())
p = [0 for i in range(m)]
p[x] = 1
k = x
ans = 0
ans += k
me = [k]
mem = [k]
fin = 0
for i in range(n-1):
    k = k**2%m
    if p[k] == 0:
        p[k] = i+2
        ans += k
        mem.append(k)
        me.append(ans)
    else:
        sp = k
        ss = i+2
        fin = 1
        break
if fin == 0:
    print(ans)
else:
    for i in range(m):
        if mem[i] == sp:
            spi = i+1
            break
    if spi == 1:
        ans0 = 0
    else:
        ans0 = me[spi-2]
    uu = ans - ans0
    ans0 += ((n-spi+1)
    for i in range((n-spi+1)%(ss-spi)):
        ans0 += mem[spi+i-1]
    print(ans0)
Problem: p02550
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(1,n):
    aa = pow(aa,2,m)
    if aa in aset:
        ind = alist.index(aa)
        loop = alist[ind:i]
        nloop = (n-ind+1)
        ans += sum(loop)*(nloop-1)
        ans += sum(loop[0:(n-ind+1)%len(loop)-1])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

    if aa == 0:
        break

print(ans)




Failing test case input:
10000001000 1 38838

Expected output:
10000001000


Actual output:
10000001001


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n, x, m = map(int, input().split())

ans = 0

aa = x
ans += aa
aset = set([aa])
alist = [aa]

for i in range(1,n):
    aa = pow(aa,2,m)
    if aa in aset:
        offset = alist.index(aa)
        loop = alist[offset:i]
        nloop, tail = divmod(n-offset, len(loop))
        ans += sum(loop)*(nloop-1)
        ans += sum(loop[0:tail])
        break
    else:
        ans += aa
        aset.add(aa)
        alist.append(aa)

print(ans)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N,X,M=map(int, input().split())

p=[0]*(M+2)
sum=[0]*(M+2)
p[X]=1
sum[1]=X

f=0
for i in range(2,N+1):
    X=(X**2)%M  
    if p[X]!=0:
        j=p[X]
        f=i
        break
    else:
        sum[i]=X+sum[i-1]
        p[X]+=i








if f==0:
    print(sum[N])
elif i-j==1:
    print(sum[j-1])
else:
    d,mod=divmod(N-j+1,i-j)

    print(d*(sum[i-1]-sum[j-1])+sum[j+mod-1])

Failing test case input:
10000001001 1 38838

Expected output:
10000001001


Actual output:
0


Fixed submission (verdict: Accepted):
N,X,M=map(int, input().split())

p=[0]*(M+2)
sum=[0]*(M+2)
p[X]=1
sum[1]=X
f=0
for i in range(2,N+1):
    X=(X**2)%M  
    if p[X]!=0:
        j=p[X]
        f=i
        break
    else:
        sum[i]=X+sum[i-1]
        p[X]+=i

if f==0:
    print(sum[N])
else:
    d,mod=divmod(N-j+1,i-j)
    print(d*(sum[i-1]-sum[j-1])+sum[j+mod-1])
Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind-1])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r-1):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)


Failing test case input:
10010001001 8 3503

Expected output:
18331648495311


Actual output:
18331648498072


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n,x,m=map(int,input().split())
l=[0 for i in range(m)]
ans=0
y=[]
i=1
flag=0
ys=n
while i<n:
    if flag==0:
        ans+=x
    l[x]+=1
    x=x*x%m
    i+=1
    if l[x]==2 and flag==0:
        flag=1
        ys=i
    if flag:
        if l[x]==3:
            break
        y.append(x)
if len(y):
    p,pp=divmod(n-ys,len(y))
    print(ans+p*sum(y)+sum(y[:pp+1]))
    print(ans,p*sum(y),sum(y[:pp+1]))
else:
    print(ans)

Failing test case input:
10000001000 3 22308

Expected output:
98860009884570


Actual output:
98860009884570
237267 98860009569648 77655


Fixed submission (verdict: Accepted):
n,x,m=map(int,input().split())
l=[0 for i in range(m)]
ans=0
y=[]
i=1
flag=0
ys=n
while i<n+1:
    if flag==0:
        ans+=x
    l[x]+=1
    x=x*x%m
    i+=1
    if l[x]==2 and flag==0:
        flag=1
        ys=i
    if flag:
        if l[x]==3:
            break
        y.append(x)
if len(y):
    p,pp=divmod(n-ys,len(y))
    print(ans+p*sum(y)+sum(y[:pp+1]))
    
else:
    print(ans)
Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = [int(e) for e in input().split()]

s = set()
l = []

a = x % m
total = a
for i in range(1, n):
    a = (a * a) % m
    if a == 0:
        break
    
    if a in s:
        r = n - i
        p = l.index(a)
        
        lo_total = 0
        for j in range(p, len(l)):
            lo_total += l[j]
        total = lo_total * r 

        r %= (len(l) - p)        
        for j in range(p, p + r):
            total += l[j]
        break
    
    s.add(a)
    l.append(a)
    total += a

print(total)

Failing test case input:
10010000100 2 99959

Expected output:
501051325615660


Actual output:
787652559868746856


Fixed submission (verdict: Accepted):
n, x, m = [int(e) for e in input().split()]

s = set()
l = []

a = x % m
total = a
for i in range(1, n):
    a = (a * a) % m
    if a == 0:
        break
    
    if a in s:
        r = n - i
        p = l.index(a)
        
        lo_total = 0
        for j in range(p, len(l)):
            lo_total += l[j]
        total += lo_total * (r 

        r %= (len(l) - p)        
        for j in range(p, p + r):
            total += l[j]
        break
    
    s.add(a)
    l.append(a)
    total += a

print(total)

Problem: p02550
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n, x, m = map(int, input().split())
s = x % m
a = x % m
note = []
for i in range(n-1):
    note.append(a)
    a = a**2 % m
    if a in note:
        ind = note.index(a)
        l = len(note[ind:])
        n -= len(note[:ind-1])
        syo = n 
        a = n % l
        s = s + sum(note[ind:]) * syo
        for j in range(ind, ind+a):
            s += note[j]
        print(s)
        exit()
    s += a
    if a == 0:
        break

print(s)

Failing test case input:
10000000000 15 114525

Expected output:
676285714116240


Actual output:
4734000000143115


Fixed submission (verdict: Accepted):
n, x, m = map(int, input().split())
s = x
a = x
note = [0] * m
note[a] += 1
seq = []
for i in range(n-1):
    seq.append(a)
    a = a**2 % m
    if note[a] > 0:
        ind = seq.index(a)
        l = len(seq[ind:])
        n -= len(seq[:ind])
        syo, r = divmod(n, l)
        s = s + sum(seq[ind:]) * (syo-1)
        if r != 0:
            for j in range(ind, ind+r):
                s += seq[j]
        print(s)
        exit()
    if a == 0:
        print(s)
        exit()
    note[a] += 1
    s += a
print(s)
Problem: p03038
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
b =[]
ans = []
for i in range(m):
    t = tuple(map(int,input().split()))
    b.append(t)
b = sorted(b,key = lambda x:x[1],reverse=True)
for i in b:
    for j in range(i[0]):
        if a:
            x =a.pop()
            if x > i[1]:
                print(sum(a)+x+sum(ans))
                exit()
            else:
                ans.append(i[1])
        else:
            print(ans)
            exit()
print(sum(ans)+sum(a))


Failing test case input:
11 3
1 1 1 1 0 2 1 1 1 1 1
6 1000001000
4 1000000000
3 1100000000

Expected output:
11300006000


Actual output:
[1100000000, 1100000000, 1100000000, 1000001000, 1000001000, 1000001000, 1000001000, 1000001000, 1000001000, 1000000000, 1000000000]


Fixed submission (verdict: Accepted):
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
b =[]
ans = []
for i in range(m):
    t = tuple(map(int,input().split()))
    b.append(t)
b = sorted(b,key = lambda x:x[1],reverse=True)
for i in b:
    for j in range(i[0]):
        if a:
            x =a.pop()
            if x > i[1]:
                print(sum(a) + x + sum(ans))
                exit()
            else:
                ans.append(i[1])
        else:
            print(sum(ans))
            exit()
print(sum(ans)+sum(a))

Problem: p03038
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
print(a)
for i in range(0,m):
    b, c = map(int, input().split())
    for j in range(0,b):
        if a[j] < c:
            a[j] = c
        else:
            break
    if b != n:
        if a[b] < c:
            temp = []
            for j in range(0, n-b):
                if a[b] < c:
                    temp.append(a.pop(b))
                else:
                    break
            a = temp + a
print(sum(a))

Failing test case input:
11 1
-1 1 2 2 1 1 1 0 1 1 1
1 1000000000
4 0010101010
3 1000000001

Expected output:
1000000011


Actual output:
[-1, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2]
1000000011


Fixed submission (verdict: Accepted):


n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
b_c_hash = {}
c_list = []
for i in range(0,m):
    b, c = map(int, input().split())
    if c in b_c_hash:
        b_c_hash[c] += b
    else:
        b_c_hash[c] = b
        c_list.append(c)
c_list.sort(reverse=True)
k = 0
flag = 0
for c in c_list:
    for i in range(0, b_c_hash[c]):
        if k == n:
            flag = 1
            break
        if a[k] < c:
            a[k] = c
            k += 1
        else:
            flag = 1
            break
    if flag == 1:
        break
print(sum(a))