c++algorithmmodularbinomial-coefficients

Fast n choose k mod p for large n?


What I mean by "large n" is something in the millions. p is prime.

I've tried http://apps.topcoder.com/wiki/display/tc/SRM+467 But the function seems to be incorrect (I tested it with 144 choose 6 mod 5 and it gives me 0 when it should give me 2)

I've tried http://online-judge.uva.es/board/viewtopic.php?f=22&t=42690 But I don't understand it fully

I've also made a memoized recursive function that uses the logic (combinations(n-1, k-1, p)%p + combinations(n-1, k, p)%p) but it gives me stack overflow problems because n is large

I've tried Lucas Theorem but it appears to be either slow or inaccurate.

All I'm trying to do is create a fast/accurate n choose k mod p for large n. If anyone could help show me a good implementation for this I'd be very grateful. Thanks.

As requested, the memoized version that hits stack overflows for large n:

std::map<std::pair<long long, long long>, long long> memo;

long long combinations(long long n, long long k, long long p){
   if (n  < k) return 0;
   if (0 == n) return 0;
   if (0 == k) return 1;
   if (n == k) return 1;
   if (1 == k) return n;

   map<std::pair<long long, long long>, long long>::iterator it;

   if((it = memo.find(std::make_pair(n, k))) != memo.end()) {
        return it->second;
   }
   else
   {
        long long value = (combinations(n-1, k-1,p)%p + combinations(n-1, k,p)%p)%p;
        memo.insert(std::make_pair(std::make_pair(n, k), value));
        return value;
   }  
}

Solution

  • So, here is how you can solve your problem.

    Of course you know the formula:

    comb(n,k) = n!/(k!*(n-k)!) = (n*(n-1)*...(n-k+1))/k! 
    

    (See http://en.wikipedia.org/wiki/Binomial_coefficient#Computing_the_value_of_binomial_coefficients)

    You know how to compute the numerator:

    long long res = 1;
    for (long long i = n; i > n- k; --i) {
      res = (res * i) % p;
    }
    

    Now, as p is prime the reciprocal of each integer that is coprime with p is well defined i.e. a-1 can be found. And this can be done using Fermat's theorem ap-1=1(mod p) => a*ap-2=1(mod p) and so a-1=ap-2. Now all you need to do is to implement fast exponentiation(for example using the binary method):

    long long degree(long long a, long long k, long long p) {
      long long res = 1;
      long long cur = a;
    
      while (k) {
        if (k % 2) {
          res = (res * cur) % p;
        }
        k /= 2;
        cur = (cur * cur) % p;
      }
      return res;
    }
    

    And now you can add the denominator to our result:

    long long res = 1;
    for (long long i = 1; i <= k; ++i) {
      res = (res * degree(i, p- 2)) % p;
    }
    

    Please note I am using long long everywhere to avoid type overflow. Of course you don't need to do k exponentiations - you can compute k!(mod p) and then divide only once:

    long long denom = 1;
    for (long long i = 1; i <= k; ++i) {
      denom = (denom * i) % p;
    }
    res = (res * degree(denom, p- 2)) % p;
    

    EDIT: as per @dbaupp's comment if k >= p the k! will be equal to 0 modulo p and (k!)^-1 will not be defined. To avoid that first compute the degree with which p is in n*(n-1)...(n-k+1) and in k! and compare them:

    int get_degree(long long n, long long p) { // returns the degree with which p is in n!
      int degree_num = 0;
      long long u = p;
      long long temp = n;
    
      while (u <= temp) {
        degree_num += temp / u;
        u *= p;
      }
      return degree_num;
    }
    
    long long combinations(int n, int k, long long p) {
      int num_degree = get_degree(n, p) - get_degree(n - k, p);
      int den_degree = get_degree(k, p);
    
      if (num_degree > den_degree) {
        return 0;
      }
      long long res = 1;
      for (long long i = n; i > n - k; --i) {
        long long ti = i;
        while(ti % p == 0) {
          ti /= p;
        }
        res = (res * ti) % p;
      }
      for (long long i = 1; i <= k; ++i) {
        long long ti = i;
        while(ti % p == 0) {
          ti /= p;
        }
        res = (res * degree(ti, p-2, p)) % p;
      }
      return res;
    }
    

    EDIT: There is one more optimization that can be added to the solution above - instead of computing the inverse number of each multiple in k!, we can compute k!(mod p) and then compute the inverse of that number. Thus we have to pay the logarithm for the exponentiation only once. Of course again we have to discard the p divisors of each multiple. We only have to change the last loop with this:

    long long denom = 1;
    for (long long i = 1; i <= k; ++i) {
      long long ti = i;
      while(ti % p == 0) {
        ti /= p;
      }
      denom = (denom * ti) % p;
    }
    res = (res * degree(denom, p-2, p)) % p;