How to calculate binomial coefficient modulo 142857 for large n
and r
. Is there anything special about the 142857? If the question is modulo p
where p
is prime then we can use Lucas theorem but what should be done for 142857.
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- d3.js moving average with previous and next data v
- How to get a fixed number of evenly spaced points
相关文章
- What are the problems associated to Best First Sea
- ceil conterpart for Math.floorDiv in Java?
- Coin change DP solution to keep track of coins
- why 48 bit seed in util Random class?
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- all permutations of +-r, +-s
You can actually calculate
C(n,k) % m
inO(n)
time for arbitrarym
.The trick is to calculate
n!
,k!
and(n-k)!
as prime-power-vectors, subtract the two later from the first, and multiply the remainder modulom
. ForC(10, 4)
we do this:Hence
We can calculate this easily
mod m
as there are no divisions. The trick is to calculate the decomposition ofn!
and friends in linear time. If we precompute the primes up ton
, we can do this efficiently as follows: Clearly for each even number in the product1*2*...*9*10
we get a factor of2
. For each fourth number we get a second on and so forth. Hence the number of2
factors inn!
isn/2 + n/4 + n/8 + ...
(where/
is flooring). We do the same for the remaining primes, and because there areO(n/logn)
primes less thann
, and we doO(logn)
work for each, the decomposition is linear.In practice I would code it more implicit as follows:
This includes a Sieve of Eratosthenes, so the running time is
nloglogn
rather thann
if the primes had been precalculated or sieved with a faster sieve.The algorithm is:
To compute
(n above k) mod p^q
:Source: http://www.dms.umontreal.ca/~andrew/PDF/BinCoeff.pdf, theorem 1
define
(n!)_p
as the product of numbers1..n
that are not divible byp
define
n_j
asn
after erasingj
least significant digits in basep
define
r
asn
-k
define
e_j
as the number of carries when addingk+r
, not counting the carries fromj
lowest digits, computing in basep
define
s
as1
ifp=2 & q>=3
and-1
otherwisethen
(n above k) mod p^q := p^e_0 * s^e_(q-1) * concatenate(j=d..0)( (n_j!)_p / ((k_j!)_p*(r_j!)_p) )
with each term of the concatenation computing one base-p digit of the result, lowestj
computing the least significant non-zero digits.What's special about 142857 is that 7 * 142857 = 999999 = 10^6 - 1. This is a factor that arises from Fermat's Little Theorem with a=10 and p=7, yielding the modular equivalence 10^7 == 10 (mod 7). That means you can work modulo 999999 for the most part and reduce to the final modulus by dividing by 7 at the end. The advantage of this is that modular division is very efficient in representation bases of the form 10^k for k=1,2,3,6. All you do in such cases is add together digit groups; this is a generalization of casting out nines.
This optimization only really makes sense if you have hardware base-10 multiplication. Which is really to say that it works well if you have to do this with paper and pencil. Since this problem recently appeared on an online contest, I imagine that's exactly the origin of the question.