In my project, one part of problem is there. But to simplify, here the problem is being formulated. There are two positive co-prime integers: a
and b
, where a < b
. Multiples of a
from 1 through b-1
is listed followed by modulus operation by b
.
a mod b
, 2*a mod b
, 3*a mod b
, ... , (b-1)*a mod b
Now, there is another integer, say n ( 1 <= n < b)
. Through the first n
numbers in the list, we have to find how many numbers is less than, say m
(1 <= m < b
). This can be done in brute force approach, thereby giving a O(n)
.
An example:
a=6, b=13, n=8, m=6
List is:
6, 12, 5, 11, 4, 10, 3, 9, 2, 8, 1, 7
This is a permutation of the numbers from 1 to 12 because modulus operation of any two co-primes produces a permutation of numbers if we include another number, that is, 0
. If we take a= 2, b=13
, then the list would have been 2, 4, 6, 8, 10, 12, 1, 3, 5, 7, 9, 11
, which gives a pattern. Whereas if a
and b
are very large (in my project they can go up to 10^20), then I have no idea how to deduce a pattern of such large numbers.
Now getting back to the example, we take the first n = 8
numbers from the list, which gives
6, 12, 5, 11, 4, 10, 3, 9
Applying the less-than
operator with m = 6
, it gives the total number of numbers less than m
being 3 as explained below in the list
0, 0, 1, 0, 1, 0, 1, 0
where 0 refers to not being less than m
and 1 refers to being less than m
.
Since, the algorithm above is a O(n)
, which is not acceptable for the range of [0, 10^20]
, so can the community give a hint/clue/tip to enable me to reach a O(log n )
solution, or even better O(1)
solution?