def bin_search elem if len if elem return else return index len step i

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def bin_search(l, elem):
if len(l) == 1:
if l[0] < elem:
return 1
else:
return 0
index = len(l)//2
step = (index + 1)//2
while step > 0:
if index == 0 or index == len(l) or (l[index - 1] < elem and l[index] > elem):
return index
if l[index] > elem:
index -= step
step = (step + 1) // 2
continue
if l[index] < elem:
index += step
step = (step + 1) // 2
continue
if l[index] == elem:
return index
N, a1, k, b, m = map(int, input().split())
a = [a1]
d = [99999999999999999 for i in range(N + 1)]
d[0] = -9999999999999999999
for i in range(N):
a.append((k * a[i] + b) % m)
j = bin_search(d, a[i])
if d[j-1] < a[i] and a[i] < d[j]:
d[j] = a[i]
ans = N
while d[ans] == 99999999999999999:
ans -= 1
print(ans)