A class of algorithms for C (n+m,m) mod p

Source: Internet
Author: User
Tags modulus

Lucas theorem

A, B is a non-negative integer and P is a prime number. AB is written in P-system: a=a[n]a[n-1]...a[0],b=b[n]b[n-1]...b[0].

The combined number C (A, B) and C (A[n],b[n]) *c (a[n-1],b[n-1]) *...*c (a[0],b[0]) MODP the same

namely: Lucas (n,m,p) =c (n%p,m%p) *lucas (n/p,m/p,p)

To solve n! % p For example, put n piecewise, each p a paragraph, each section of the result is the same. But we need to deal with each paragraph at the end of P, 2p, ..., to extract the P, you will find that the remaining number is exactly (n/p)!, equivalent to a sub-problem, so recursive solution can be. This is the case of handling n! alone, of course C (n,m) is n!/(m!* (N-M)!), each factorial is treated with the above method, is the Lucas theorem, it is necessary to note that p here is a prime number.

Lucas biggest data processing ability is p in 10^5 around, can not be big, HDU 3037 is 10^5 level!

The main function is not written

Constmaxn=100005;varN,m:int64; FAC:Array[0.. MAXN] ofInt64;functionQuickmod (A,b,mol:int64): Int64;varAns:int64;beginans:=1;  whileB<>0  Do        begin            if(b and 1)=1  Thenans:= (Ans*a)MoDmol; A:=a*aMoDmol; B:=b>>1; End; Exit (ANS);End;functionget_fact (P:int64): Int64;varI:longint;beginfac[0]:=1;  fori:=1  toP DoFac[i]:= (fac[i-1]*i)MoDp;End;functionLucas (N,m,p:int64): Int64;varAns,aa,bb:int64;beginans:=1;  while(a>0) and(k>0) Do        beginAA:=aMoDp; BB:=bMoDp; ifAa<bb ThenExit0); Ans:=ans*fac[aa]*quickmod (FAC[BB]*FAC[AA-BB]MoDp,p-2, p)MoDp; A:=aDivp; K:=kDivp; End; Exit (ANS);End;

When P is big, what's the matter? Obviously such a direct open array would be MLE

We can do that.

Modulo by multiplying the numerator and multiplying the denominator by the modulus

The inverse element of the denominator and the modulo of the numerator by inverse element

Give a code

  
ConstMol=1000000007;varN,m:longint;functionINV (A,p:int64): Int64;varB,c,q,k1,k2,k3:int64;beginB:=p; C:=aMoDb; Q:=aDivb; K1:=1; K2:=0; K3:= (k1+p-q*k2MoDPMoDp;  while(C XOR1) <>0  Do     beginA:=b; B:=C; C:=aMoDb; Q:=aDivb; K1:=K2; K2:=K3; K3:= (k1+p-q*k2MoDPMoDp; End; Exit (K3);End;functionCalc (x,y:int64): Int64;varI,j,ans,up,down:int64;beginI:=y+1; J:=x; Up:=1; Down:=1;  whileI<=j Do         beginUp :=up*iMoDmol;        Inc (I); End; I:=x-y;  whilei>=1  Do        beginDown :=down*iMoDmol;        Dec (i); End; Ans:=INV (Down,mol); Ans:=up*ansMoDmol; Exit (ANS);End;beginread (n,m); Writeln (Calc (n+m,m));End.
View Code

Again, the inverse of the meta:

For positive integers and, if so, the solution of the smallest positive integer in the congruence equation is called the inverse of the modulo.

Inverse is generally used to expand Euclidean algorithm to obtain, if it is a prime number, then can also be based on the Fermat theorem to get the inverse element.

The derivation process is as follows

Now look at an inverse of the most common problem, the following expression is the value (known)

Of course, this classic problem has many methods, the most common is to expand Euclid, if it is prime, you can also use the Fermat theorem.

But you will find that the Fermat theorem and the extended Euclidean algorithm have limitations, and they all demand and mutual element. Actually, we still have a

The common method of solving the inverse element is suitable for all situations. The formula is as follows

Now let's prove it, known, to prove the steps as follows

Next to the actual combat, see a few questions about the inverse of the yuan. (Transferred from Acdreamer Ben's blog)

Title: http://poj.org/problem?id=1845

Test Instructions: given two positive integers and, all the factors to be evaluated and the value after the 9901 is taken.

Analysis: It is easy to know, first decomposition to get, then get, then

All of the factors and expressions are as follows

The second method is to use geometric series to sum the formula, but use the inverse element. You can use the following formula

Because it can be large, it will exceed the int range, so you need to multiply it in a fast power.

In fact, some problems need to use all the inverse of the module, here is the odd prime number. So if we use the fast power to find the complexity of time,

If it is a prime number for a 1000000 level, the time complexity is high. There are actually some algorithms that have a recursive type as follows

Its derivation process is as follows, set, then

Both sides of the upper side are simultaneously removed, further get

and replace them, and eventually get

initialization, so that all the inverse elements of the modulus singular prime can be obtained by recursive method.

In addition, all the inverse values of the modulo correspond to all the numbers, for example, the corresponding inverse is.

Title: http://www.lydsy.com/JudgeOnline/problem.php?id=2186

Test Instructions: the number of coprime in the request, wherein.

Analysis: because, so, we are easy to know as to the conclusion

for two positive integers and, if they are multiples, then the number of

This conclusion is well proven, because the number of the in and of the intertextuality is, and know, so

conclusion is established. So for the subject, the answer is

All primes that are less than or equal are filtered out first. Since the final answer is modulo a prime number, use the inverse, here

To find the inverse element has the skill, with just introduced the recursive method preprocessing, otherwise will TLE .

Next there is an interesting topic about inverse, which is described below

Proof: by

which

So just prove it, and we know that the inverse of the modulo corresponds to all

All the numbers in it are both single-shot and full-shot.

So get further

Proof Complete!

A class of algorithms for C (n+m,m) mod p

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.