Блог пользователя Lien

Автор Lien, 13 лет назад, По-английски

Hi! I'm now thinking about calculate geometric sum: 1+a+a^2+..+a^n mod m where a,m,n are 64-bit integer. I transform 1+a+a^2+...+a^n mod m=(a^(n+1)-1)/(a-1) mod m but a new problem appear: when gcd(a-1,m)>1 it seem impossible to find t such that t*(a-1) mod m=1. Any hint for this problem?

  • Проголосовать: нравится
  • +13
  • Проголосовать: не нравится

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +21 Проголосовать: не нравится

You can use the same idea as in binary exponentiation. Just calculate this sum together with an + 1 and you can easily write down formulas for transitions.

»
13 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

S = ( 1+a+a^2+...+a^(sqrt(n)-1) )%m; // sqrt(n) max that sqrt(n)*sqrt(n)<=n

t= (a^sqrt(n))%m;

ans=1;

for(int i=0; i<sqrt(n); i++) ans = ((ans*t)+S)%m;

for(int i=0; i<(n- sqrt(n)*sqrt(n); i++) ((ans*a)+1)%m;

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +51 Проголосовать: не нравится

It can also be done using matrix exponentiation:

»
13 лет назад, скрыть # |
 
Проголосовать: нравится -12 Проголосовать: не нравится

Looks like you have a problem with modulus arithmetics and division. As far as it is simple to do addition and multiplication in modulus arithmetics, it is not obvious how to do division. Have a look at first comment to this post It says that is modulus is a prime number and you want to calculate (a / x) MOD m you can calculate inverse element inv = x m-2 and then multiply a * inv

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

It's possible to perform the calculation of an + 1 - 1 modulo m * (a - 1) and then perform the division by a - 1. The proof of correctness goes into the Chinese Remainder Theorem.