n=0
a=0
b=0
def f(x):
global n,a,b
return (a*(x**2)+b)%n
def brent(x0):
lam=1
mu=0
power=1
tor=x0
hare=f(x0)
while hare!=tor:
if power==lam:
power*=2
lam=0
tor=hare
hare=f(hare)
lam+=1
# hare=x0
# tor=x0
# mu=0
# for i in range(lam):
# hare=f(hare)
# while hare!=tor:
# tor=f(tor)
# hare=f(hare)
# mu+=1
return lam
def Main():
global n,a,b
try:
while True:
s=input().split()
if len(s)==1:
return 0
n,a,b=[int(x) for x in s]
print(n-brent(0))
except EOFError:
return 0
if __name__=='__main__':
Main()
a=0
b=0
def f(x):
global n,a,b
return (a*(x**2)+b)%n
def brent(x0):
lam=1
mu=0
power=1
tor=x0
hare=f(x0)
while hare!=tor:
if power==lam:
power*=2
lam=0
tor=hare
hare=f(hare)
lam+=1
# hare=x0
# tor=x0
# mu=0
# for i in range(lam):
# hare=f(hare)
# while hare!=tor:
# tor=f(tor)
# hare=f(hare)
# mu+=1
return lam
def Main():
global n,a,b
try:
while True:
s=input().split()
if len(s)==1:
return 0
n,a,b=[int(x) for x in s]
print(n-brent(0))
except EOFError:
return 0
if __name__=='__main__':
Main()
Comments
Post a Comment