Calculating sum of geometric series (mod m)

前端 未结 4 739
难免孤独
难免孤独 2020-12-14 02:43

I have a series

S = i^(m) + i^(2m) + ...............  + i^(km)  (mod m)   

0 <= i < m, k may be very large (up to 100,000,000),  m <= 300000
         


        
4条回答
  •  Happy的楠姐
    2020-12-14 03:46

    As you've noted, doing the calculation for an arbitrary modulus m is difficult because many values might not have a multiplicative inverse mod m. However, if you can solve it for a carefully selected set of alternate moduli, you can combine them to obtain a solution mod m.

    Factor m into p_1, p_2, p_3 ... p_n such that each p_i is a power of a distinct prime

    Since each p is a distinct prime power, they are pairwise coprime. If we can calculate the sum of the series with respect to each modulus p_i, we can use the Chinese Remainder Theorem to reassemble them into a solution mod m.

    For each prime power modulus, there are two trivial special cases:

    If i^m is congruent to 0 mod p_i, the sum is trivially 0.

    If i^m is congruent to 1 mod p_i, then the sum is congruent to k mod p_i.

    For other values, one can apply the usual formula for the sum of a geometric sequence:

    S = sum(j=0 to k, (i^m)^j) = ((i^m)^(k+1) - 1) / (i^m - 1)

    TODO: Prove that (i^m - 1) is coprime to p_i or find an alternate solution for when they have a nontrivial GCD. Hopefully the fact that p_i is a prime power and also a divisor of m will be of some use... If p_i is a divisor of i. the condition holds. If p_i is prime (as opposed to a prime power), then either the special case i^m = 1 applies, or (i^m - 1) has a multiplicative inverse.

    If the geometric sum formula isn't usable for some p_i, you could rearrange the calculation so you only need to iterate from 1 to p_i instead of 1 to k, taking advantage of the fact that the terms repeat with a period no longer than p_i.

    (Since your series doesn't contain a j=0 term, the value you want is actually S-1.)

    This yields a set of congruences mod p_i, which satisfy the requirements of the CRT. The procedure for combining them into a solution mod m is described in the above link, so I won't repeat it here.

提交回复
热议问题