so I ran into this problem when I need to calculate this:
1 k + (1 + p) k + (1 + 2 * p) k + ..... + (1 + n * p) k % p
Where p is prime and k is a number strictly less than p.
p is less than 500, and n * p can be in the range of up to 10 9
The only solution I could think of is to iterate from the first to the last term and calculate the module using exponential, but that would be too expensive. I am looking for a faster algorithm.
Is it possible to do it faster?
anker source
share