Geometric Series Modulus operation

China☆狼群 提交于 2021-01-29 04:33:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!