Combination Formula
C (n,m) =p (n,m)/m!=n!/((n-m)!*m!)
C (n,m) =c (n,n-m)
C (n,m) =c (n-1,m) +c (n-1,m-1)
Euler's theorem
Euler's theorem, (also called Fermat-Euler theorem) is a property about congruence. Euler's theorem shows that if n,a is a positive integer and N,a coprime, then:
φ (n) indicates the number of 1~n and N coprime
Look at a basic example. so that a = 3,n = 5, these two numbers are reciprocal. There are 1, 2, 3, and 4 in the number of positive integers smaller than 5 and 5, so φ (5) =4 (see [Euler function] for details). Calculation: A^{φ (n)} = 3^4 = 81, while 81= 80 + 1ξ1 (mod 5)
This theorem can be used to simplify the modulo operation of power. For example, the calculation of the single digit of 7^{222} is actually the remainder of the 7^{222} being removed by 10. 7 and 10[[]], and φ (10) = 4. Known 7^4ξ1 by Euler's theorem (mod 10). So 7^{222}= (7^4) ^55* (7^2) ξ1^{55}*7^2ξ49ξ9 (mod 10).
Something: Euler's theorem is actually the generalization of Fermat theorem
First look at the Fermat theorem: if P is prime, and (a,p) = 1, then a^ (p-1)
P is prime, φ (p) =p-1, then A^φ (p)≡1 (mod p)
Multiplication Inverse element
(A/b) mod p= (a*b^ (p-2)) mod p
Condition: P is prime, gcd (b,p) =1,a%b=0
Definition: the K value satisfying a*k≡1 (mod p) is a multiplicative inverse of p.
(Ps:p must be a prime number in order to have a multiplicative inverse of a (except 1), special Note: When P is 1 o'clock, for any a,k is 1)
Why do we have to multiply the inverse element?
When we request (A/b) the value of mod p (A/b must be an integer), and A is large and cannot directly obtain a A/b value, we will use the multiplication inverse.
We can use the B to multiply the inverse k of p, multiply a by the K-mode p, ie (a*k) mod p.
The result is equivalent to (A/b) mod p.
How to find the multiplication inverse element?
At present, only Euler's theorem, leave a pit, wait to fill
According to Euler's theorem: if P is a prime number and (b,p) =1, then φ (p) = (p-1), then b^ (p-1) ≡1 (mod p).
b*b^ (p-2) ≡1 (mod p), so b^ (p-2) is the inverse of B, that is k=b^ (p-2)
So (A/b) mod p= (a*b^ (p-2)) mod p
This is the Bo master blind than written, the original in the following ↓↓↓
Euler's theorem (proof + application on inverse element)
Euler's theorem (also called Fermat-Euler theorem): A and N are known to be positive integers, and a and p are A^phi (n) ≡1 (mod n).
Prove:
Set z = {X1, X2, X3, ....., Xphi (N)}, where XI (i = 1, 2,. Phi (N)) represents the number of I not greater than N and N coprime.
Consider the collection S = {a*x1 (mod n), a*x2 (mod n), ..., A*xphi (n) (mod n)}, then set z = S;
1) because A and n coprime, Xi and N are also coprime, so A*xi also with N coprime. So for any one xi,a*xi (mod n) must be the element in Z;
2) for any XI, XJ, if XI! = XJ, then A*xi (mod n)! = A*XJ (mod n);
so s = Z;
Then (A*x1*a*x2*...*a*xphi (n)) (mod n)----------------------------------------------------(1)
= (a*x1 (mod n) * A*X2 (mod n) * ... *a*xphi (n) (mod n)) (mod n)
= (x1* x2* x3* .... * Xphi (n)) (mod n)------------------------------------------------------(2)
Formula (1) Finishing [A^phi (x) * (x1* x2* x3* .... * Xphi (n))] (mod n)
With the (2) formula to eliminate (x1* x2* x3* .... * Xphi (n)), that is, A^phi (x) ≡1 (mod n);
inverse : (b/a) (mod n) = (b * x) (mod n). x represents the inverse of a. and a*x≡1 (mod n)
Because A^phi (x) ≡1 (mod n), X can be represented as a^ (PHI (N)-1).
When n is a prime number, PHI (n) =n-1, X is represented as a^ (n-2).
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 congruence
namely: Lucas (n,m,p) =c (n%p,m%p) *lucas (n/p,m/p,p)
For non-negative integers m and N and a prime p, the following congruence Relationholds:
-
represents the congruence, which indicates that the remainder of two number p is the same.
where
and
is the base p expansions of m and n respectively.
Divide m into M0 1,m1 p,m2 of p^2,......
Divide n into N0 1,n1 p,n2 a p^2 ...
C (M,n) A total of the method is (in the M0 to take n0 of the program number * M1 the inside to take N1 the number of programs * m2 the number of programs to take N2 ... )
fzu2020
Straight with Lucas.
#include <cstdio>#include<iostream>using namespacestd;#definell Long Longll N,m,p;ll pow_m (ll a,ll k,ll p) {ll ans=1; LL TMP=a%p; while(k) {if(k&1) ans=ans*tmp%p; TMP=tmp*tmp%Q; K>>=1; } returnans;} ll C (ll n,ll m,ll p) {if(m>n)return 0; ll a=1, b=1; for(intI=1; i<=m;i++) {a=a* (n+i-m)%Q; b=b*i%p; } returnA*pow_m (b,p-2, p)%p;} ll Lucas (ll n,ll m,ll p) {ll ans=1; while(n&&m) {ans=ans*c (n%p,m%p,p)%p; N/=p; M/=p; } returnans;}intMain () {intT; scanf ("%d",&T); while(t--) {scanf ("%d%d%d",&n,&m,&p); printf ("%lld\n", Lucas (n,m,p)); } return 0;}View Codehdu3037
Consider adding a tree, so that when the addition of the tree put K (0<=k<=m) a beans , the original n the number of trees placed on the sum of beans is equal to m-k (<=m), to meet the requirements of the problem, but also reduce the difficulty of computing.
The subject is a1+a2+......an+an+1=m (0<=ai<=m,1<=i<=n+1) Style 1
How many groups are solved.
Consider converting the problem into, seeking a1+a2+......an+an+1=m+n+1 (1<=ai<=m+1,1<=i<=n+1) Style 2
How many groups are solved.
Because each set of solutions for Formula 1, plus 1 for each AI, is a set of solutions of Equation 2.
For the solution of Equation 2:
Consider that there are m+n+1 beans row into a column, they happen to have a m+n interval, in the m+n interval select n each insert a piece of wood, then these beans into n+1 part, each part of the value corresponds to each AI, is the formula 2 a set of solutions. When n is selected in M+n interval, it is the problem of combinatorial number, p<=10^5 and prime, it can be obtained by Lucas theorem.
P<10^5, to do factorial preprocessing, use C2 method to calculate the number of combinations, otherwise it will time out
#include <cstdio>#include<iostream>using namespacestd;#definell Long Long#defineMAXN 100010ll N,m,p;ll Fact[maxn];ll pow_m (ll a,ll k,ll p) {ll ans=1; LL TMP=a%p; while(k) {if(k&1) ans=ans*tmp%p; TMP=tmp*tmp%p; K>>=1; } returnans;} ll C2 (ll n,ll m,ll p)//p<10^5{ if(m>n)return 0; returnFact[n]*pow_m (fact[m]*fact[n-m]%p,p-2, p);} ll Lucas (ll n,ll m,ll p) {ll ans=1; while(n&&m) {ans=ANS*C2 (n%p,m%p,p)%p; N/=p; M/=p; } returnans;}voidInit (ll p) {fact[0]=1; for(intI=1; i<=p;i++) fact[i]=fact[i-1]*i%p;}intMain () {intT; scanf ("%d",&T); while(t--) {scanf ("%lld%lld%lld",&n,&m,&p); Init (p); printf ("%lld\n", Lucas (n+m,n,p)); } return 0;}View Codenefu625
Test instructions: Give a n*m large garden, find the number of paths from the upper left corner to the lower right corner (the path is monotonous).
The number of non-descending paths. (b) to (M,n) a total of C (m+n-a-b,m-a).
So the solution to the subject should be C (m+n-2,m-1).
Due to C (x, y) =x!/(y!* (XY)), here we can x! decomposition factor, and save the record down, the same method records the next two, because the x! must be divisible (y!* (x-y), so the following two numbers have a factor, x!, Only by adding and reducing the exponent of their factor, we can get the factor decomposition of the final result, and then the final result can be obtained by using the fast power modulus.
Note: How do I perform a factor decomposition?
The first thing to do is to make a list of all the vegetarian factors, here is the n! It is tricky to think of how many of these 5 are required to perform the factor decomposition.
Assuming n=200, then the number of factor 5 =200/5+40/5+8/5=49, how to get it? 200 in multiples of 5 has 40, the 40 number of which is 25 in multiples of 8, so you can also break down 8 5, 8 of the number is a multiple of 125, but also to decompose a 5, so the cycle continues, you can find the value of the exponent.
#include <cstdio>#include<vector>#include<iostream>using namespacestd;#definell Long Long#defineMAXN 200010ll N,m,p;vector<int>pri;BOOLPRIME[MAXN];voidinit () {pri.clear (); prime[0]=prime[1]=false; for(intI=2; i<maxn;i++) { if(!Prime[i]) {Pri.push_back (i); for(intj=i+i;j<maxn;j+=i) prime[j]=true; }} printf ("%d\n", Pri.size ());//to test the data n<17000, only 17,000 primes are required}ll pow_m (ll a,ll k,ll p) {ll ans=1; LL TMP=a%p; while(k) {if(k&1) ans=ans*tmp%p; TMP=tmp*tmp%p; K>>=1; } returnans;} ll work (ll N,ll su) {ll ans=0; while(n) {ans+=n/su; N/=su; } returnans;} ll C3 (ll n,ll m,ll p)//0<n,m<10^6, 0<p<10^9{ if(m>n)return 0; ll ans=1; for(intI=0;p ri[i]<=n;i++)//If there is an array out of bounds error, then the prime number is not enough, init to open a little more prime, or a change of position to find prime{ll x=Work (N,pri[i]); ll y=work (nM,pri[i]); LL Z=Work (M,pri[i]); Ans=ans*pow_m (pri[i],x-(y+z), p)%Q; } returnans;}intMain () {init (); intT; scanf ("%d",&T); while(t--) {scanf ("%lld%lld%lld",&n,&m,&p); printf ("%lld\n", C3 (n+m-2, M-1, p)); } return 0;}View Code
Modulus of combination number