I have a geometric progression like series:
S = x1 + x2 + ..... xn (mod m)
where xi = (x(i-1))*r (mod m) for i>1 and x1=1 , 2<=m<10^9, 1<=r<m, 1<=S<m, 1<=n<p
here m is a prime number and r,m,S are known.
Property of r : If we form a set of r (mod m), r^2 (mod m), ..., r^(m-1) (mod m) then it will contain all the numbers form 1 to m-1.
I want to find the the value of n
(if possible). I cannot apply the Geometric Progression (GP
) formula here so I made an alternate algorithm making an assumption that these powers will make a cycle of length much smaller than n-1
. I thought to find a pattern such that the series repeats itself but this pattern of cycle occurs only for some r's
so I failed to do so. Of course, naive approach of setting a loop till m
wont work because it's too large and hence took a large amount of time before terminating.
I found a similar problem here.
But in this link there is no property on r
to make the algorithm faster. I applied all the answers given here to my code but none is reducing its complexity as required.
I know that somehow I have to use property of r
to make an efficient algorithm but I don't know how.
So is there any other pattern we can find out or any use of this property we can make to get the most efficient algorithm? (Basically I don't want to iterate over m
.) So please give me an idea of an efficient algorithm to find the n
.