The combination of the number of modulo is the value of the calculation, according to, and the range of values, the method adopted is different.
Let's look at two common values (m, n in the 64-bit integer range)
(1),
At this time, it is simpler to calculate the combined number in the case of O (n2) can be directly used Yang Hui triangle recursion, edge to do the addition side to take the mold.
(2), and is a prime number
in this paper, we discuss the case that the range of the value is large and not too large (2).
This problem can be described by the Lucas theorem, the theorem:
which
In this way, the solution of the combinatorial number is decomposed into the product of the small problem, and the calculation C (NI, mi) %p.
Known C (n, m) mod p = n!/(m! ( N-M)!) MoD p. When we ask for (A/b) mod P's value, and a is large, and cannot directly obtain a A/b value, we can instead use the multiplication inverse k to multiply a by the K-modulo p, ie (a*k) mod p. The result is equivalent to (A/b) mod p.
So what's the inverse of the dollar?
Definition: The K value satisfying a*k≡1 (mod p) is a multiplicative inverse of P (when P is 1 o'clock, for any a,k is 1)
Division take the mold, here to use the m! (n-m)! The inverse of the element.
According to Fermat theorem :
Known gcd (A, p) = 1, then ap-1≡1 (mod p), so a*ap-2≡1 (mod p).
i.e. (m! ( N-M)!) The inverse is (m! ( N-M)!) P-2;
The following is a proof of Lucas Theorem, see, refer to Feng Zhigang, "Elementary Number Theory", page 37th.
Test Instructions: ask, which, and is a prime number.
Code:
#include <iostream>//#include <algorithm>using namespaceStd;typedefLong Longll;intQuick_power_mod (intAintBintm) {//Pow (b)%m intresult =1; int Base=A; while(b>0){ if(B &1==1) {result= (result*Base) %m; } Base= (Base*Base) %m; b>>=1; } returnresult;}//calculating the modulus of combined numbersLL 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-b; intAns =1, CA =1, CB =1; for(LL i =0; I < b; ++i) {CA= (CA * (a-i))%Q; 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>>n>>L) {cout<<lucas (N,m,10007) <<Endl; } return 0;}View Code
The above code is calculated using the exponentiation modulo operation (m! ( N-M)!) P-2% p. The following is an explanation of the power modulo algorithm:
The method of repeated leveling to seek ab%m
By studying the binary representation of index B, it is found that any integer B can be expressed as:
- n represents the actual bits number of B
- BI indicates that the bit is 0 or 1
Therefore, AB can be expressed as:
That is, each of the B's represents each of the items of a, and there is a square relationship between the two adjacent items, namely:
So we construct the following algorithm:
- Convert B to binary representation and scan from right to left for each of its bits (from low to high)
- When scanning to position I, the modulus of item I of a is calculated according to the congruence property (2):
The base variable represents the modulus calculated at the i-1 bit, and the modulo of all bits is easily determined by recursion.
- If the I bit is 1, that is bi=1, it means that the bit needs to participate in the modulo operation, the result = (result*base) mod m, where result is the result of the previous i-1, and if bi=0, then the item I of a is 1, do not need to participate in the modulo operation
int quick_power_mod (int a,int b,int m) { int result = 1; int base = A; while (b>0) { if (b & 1==1) { result = (result*base)% m; } Base = (base*base)%m; b >>=1; } return result;}
two congruence properties are used:
Congruence Properties 1:AB≡BC (mod m)
Congruence Property 2: a≡c (mod m) = a2≡c2 (mod m)
Key points to understand:
- Base records the modulo of each item of a, whether B in that bit is 0 or 1, the result is recorded, the purpose is to use the next bit of 1, the calculation method is the square modulus of the previous result, which is also the origin of the repeated flat method
- Result only records the modulo result of the item with bit 1, which uses the same remainder property 1
- Through the use of a binary representation of a, and combined with the same properties 1, 2, cleverly resolved the operation of large numbers to take the mold. For a large number of 1024 bits, a maximum of 1024 cycles can be used to calculate the modulus, the performance is very fast.
This approach is the result of the efforts of many western mathematicians, often referred to as the Montgomery algorithm.
(The above part of the content by the network collection and collation, inappropriate, please kindly enlighten)
Lucas theorem of combined number-taking modulus and fast power-taking model