问题
I am giving a series in the form of
1+r+r^2+r^3+r^4+r^5
I have to find modulus of a sum of series i.e i have to find this value
[(r^n-1)/(r-1)]%M
I can easily calculate the value of (r^n-1)%M
, But the problem is how to calculate the denominator term ?
Since Inverse modulo can not be exist if both (r-1) and M
are not co prime.
Please help how to get this value any approximation or algorithm ?
Since summation is very large, I can't calculate the value directly.
回答1:
Presumably you're proposing to compute r^n
with the fast exponentiation recurrence
E(r, 0) = 1
E(r, n) = E(r*r, n/2) if n is even
r * E(r*r, (n-1)/2) if n is odd.
We can construct a similar direct recurrence for 1 + r + r^2 + ... + r^n
.
F(r, 0) = 1
F(r, n) = (1 + r) * F(r*r, (n-1)/2) if n is odd
1 + (r + r*r) * F(r*r, (n-2)/2) if n is even.
All calculations should be done mod M
, of course.
来源:https://stackoverflow.com/questions/42032824/geometric-series-modulus-operation