Summary of several methods for calculating combinatorial number by [combinatorial number]

Source: Internet
Author: User

Summary of methods for C (n,m)%mod

1. When the n,m are very small, you can use the Yang Hui Triangle direct request.
C (n,m) =c (n-1,m) +c (n-1,m-1);

2. Use the multiplication inverse element.
Multiplication Inverse: (A/b)%mod=a* (b^ (mod-2)) mod is a prime number.
The inverse can be calculated using the extended Euclidean or Euler function:

 1).扩展欧几里德:b*x+p*y=1 有解,x就是所求 2).费马小定理:b^(p-1)=1(mod p),故b*b^(p-2)=1(mod p),所以x=b^(p-2)

1. n!/(m!* (n-m) =x%p, first to calculate the n! M! , (N-M)! The remainder of P modulus is converted to a/b=x%p; Since P is a prime number, it is equivalent to Bx+py=a, and then the solution of BX ' +py ' =1 is calculated with the extended Euclidean theorem, x=x ' *a, and the final x value, i.e. C (m,n)%p, is obtained.

2. Inverse element In fact, if the MoD is a prime number, the inverse of B is actually b^ (mod-2)
i.e. (m! ( N-M)!) The inverse is (m! ( N-M)!) P-2;

int inv (inta) {//return Fpow (A, MOD-2, MOD);     return a==1?1: (Long Long) (Mod-mod/a) * INV (MOD%a)% MOD; } ll C (ll n,ll m) {if(M <0)return 0;if(N < m)return 0;if(M > n-m) m = n-m; LL up =1, down =1; for(LL i =0; I < m;          i + +) {up = up * (n-i)% MOD; Down = down * (i+1)% MOD; }returnUp * INV (down)% MOD; }

3. When the N and M ratios are large, the mod is prime and relatively small (around 10^5), calculated by the Lucas theorem

Lucas theorem: A, B are non-negative integers, p is prime. A B is written in P-system: a=a[n]a[n-1]...a[0],b=b[n]b[n-1]...b[0].
Then the combined number C (A, B) and C (A[n],b[n])C (a[n-1],b[n-1]) ... *c (a[0],b[0]) mod p congruence
namely: Lucas (n,m,p) =c (n%p,m%p) *lucas (n/p,m/p,p)

#include <iostream>//#include <algorithm>using namespace STD;typedef Long LongllintQuick_power_mod (intAintBintm) {//pow (A, b)%m    intresult =1;intbase = A; while(b>0){if(B &1==1) {result = (result*base)% m;         } base = (base*base)%m; b>>=1; }returnResult;}//Calculate the combination number to take the modelLL Comp (ll A, ll B,intP) {//composite Num C (A, b)%p    if(A < b)return 0;if(A = = b)return 1;if(b > A) b = a A;intAns =1, CA =1, CB =1; for(LL i =0; I < b;        ++i) {CA = (CA * (a-i))%p;    CB = (CB * (b-i))%p; } ans = (Ca*quick_power_mod (CB, P-2, p))% p;returnAns;} ll Lucas (ll N, ll M, ll P) {ll ans =1; while(N&&m&&ans) {ans = (Ans*comp (n%p, m%p, p))% p;//also can recusiven/= p;     M/= p; }returnAns;}intMain () {ll m,n; while(Cin&GT;&GT;N&GT;&GT;M) {cout<<lucas (N,m,10007) <<endl; }return 0;}

C (n mod, m% mod)% MoD; If it is too large, use the inverse of the above to handle it.

Semi-pretreatment
Since the Lucas theorem guarantees that the number of factorial is less than p, all factorial preprocessing is possible, and the C (N,M) is optimized.
MoD requirements: p<10^6, and the prime number
Valid range: 1<=n,m<=10^9

//semi-pretreatmentConstll MAXN =100000; ll fac[maxn+ -];voidInitint MoD) {fac[0] =1; for(intI=1; i<MoD; i++) {Fac[i] = fac[i-1] * I%MoD; }  }//semi-pretreatment inverse element for C (n,m)%modll C (ll N, ll m) {if(m>n)return 0;returnFac[n] * (Getinverse (FAC[M]*FAC[N-M),MoD)) %MoD; }

4. There is also a decomposition of the quality factor, this is more troublesome.
Http://www.mamicode.com/info-detail-621758.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summary of several methods for calculating combinatorial number by [combinatorial number]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.