"Algorithm problem Combat strategy"-chaper14-integer number theory

Source: Internet
Author: User
Tags pow

Lucas theorem:

In the combinatorial counting problem, we often face the dilemma that the combined number C (n,m) is too large to be calculated directly, then the Lucas theorem here gives a recursive algorithm for taking the remainder operation of a large combined number.

What is the Lucas theorem?

Proof of the derivation of Lucas ' theorem?

This derivation process is based on the two-term theorem, based on the last equation, we can prove the Lucas theorem by looking for the coefficients of the left and right x^ (TP + R). But there is no explanation for why P is a prime number.

Here we give another form of expression of Lucas ' theorem:

I personally think that the number of the limit of the modulus of P is a prime, so that unified operation, that is, for each factor in ∏, C (mi,ni)% p, we can combine the inverse and Fermat theorem to simplify the operation.

The programming realization of Lucas ' theorem?

By the definition of Lucas we have actually seen that it is a recursive invocation process.

Then combine a topic (problem Source:hdu 3037) to implement it:

Q: Give the variable n,m,p, solve the X1+X2+X3+...XN = x solution of the group number, x∈[0,m].

Analysis: First we face the absence of empty trees, using the basic partition principle (), easy to get C (m-1,n) group, which is obvious. However, the crux of the problem is to allow the existence of empty trees, so we need to select the number of empty trees, that is, in the selected m-1 elements and add n trees, in which the selection of n-1 empty trees and separators, to get C (n+m-1,n-1) that C (n+m-1,m).

The final solution to this problem is ∑c (n+m-1,i) = C (n +m,m), I∈[1,m]. (Two-polynomial coefficient identity, see "Concrete Mathematics")

The following is a programmatic implementation.

#include <iostream>#include<cstdio>#include<cstring>using namespacestd; Const intN =150000; Long LongN, M, p, Fac[n];voidinit () {inti; fac[0] =1;  for(i =1; I <= p; i++) Fac[i]= fac[i-1]*i%p;} Long LongPowLong LongALong Longb) {    Long LongTMP = a% p, ans =1;  while(b) {if(B &1) ans = ans * tmp%p; TMP= tmp*tmp%p; b>>=1; }    returnans;} Long LongCLong LongNLong Longm) {    if(M > N)return 0; returnFac[n]*pow (Fac[m]*fac[n-m], P-2) %p;} Long LongLucas (Long LongNLong LongM//C (n,m)% p{    if(M = =0)return 1; Else return(C (n%p, m%p) *lucas (n/p, m/p))%p;} intMain () {intT; scanf ("%d", &t);  while(t--) {scanf ("%i64d%i64d%i64d", &n, &m, &p);        Init (); printf ("%i64d\n", Lucas (n+m, m)); }    return 0;}

"Algorithm problem Combat strategy"-chaper14-integer number theory

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.