Congruence && Permutation and Combination

First, write a little code about the greatest common divisor and the least common multiple. The
most convenient way (to save trouble) is to call the function __gcd(n,m) in the algorithm library and call it directly.
Euclidean algorithm Euclidean

long long gcd(long long a,long long b)
{
    
    
	if(b==0)
		return a;
	return gcd(b,a%b);
}

Least common multiple

long long lcm(long long a,long long b)
{
    
    
    return a*(b/gcd(a,b));
}

Congruence Theorem
If the remainders of a and b divided by c are the same, then a and b are congruent with respect to modulus c, denoted as a≡b(mod c).
nature:

1. If a≡b(mod m), x≡y(mod m), then there is a+x≡b+y(mod m).
2. If a≡b(mod m), x≡y(mod m), then there is ax≡by(mod m).
3. If ac≡bc(mod m), and c and m are relatively prime, then there is a≡b(mod m) (that is, both sides of the congruence can be divided by a number that is relatively prime to the modulus).
4. If ac≡bd, c≡d (mod m), and (c, m)=1 (the greatest common divisor of c and m is 1), a ≡ b (mod m) is obtained.
In addition, congruence relations are transitive:
a and b are congruences, and b and c are also congruences. It can be concluded that a and c are also congruences. In the
process of doing problems, modulo is often used, which is the use of the congruence theorem.

int ex_gcd(int a,int b)
{
    
    
	if(b==0) {
    
    x=1;y=0;return a;}
	int gcd=ex_gcd(b,a%b);
	int tmp=x;x=y;
	y=tmp-a/b*y;//扩展欧几里得算法
	return gcd;
}
//最后得到的x即为原同余方程的一个可行解;

Permutation and Combination
Formula
Take the number of all permutations of m (m≤n) elements from n different elements, which is called the number of permutations of m elements from n different elements, denoted as A(n,m)
A( n,m) = n!/(nm)!
Regarding the number of permutations, I saw a very powerful algorithm on the Internet (p is the modulus)

long long mult(long long x, long long y, long long P){
    
    
    x %= P;
    y %= P;
    long long k = x*y;
    long long j = (long long)(((long double)x*y+0.5)/P);
    long long ans = ( (k - j * P) %P + P) % P;
    return ans;
}

Second, there is a fast multiplication similar to fast power

long long mult(long long i, long long j, long long p){
    
    
    L ans=0;
    while(j){
    
    
        if(j & 1) ans = (ans+i) % p;
        i = (i+i) % p;
        j >>= 1;
    }
    return ans;
}

Recursive method

long long mult(long long i, long long j, long long p){
    
    
    if(j <= 1) return i*j;
    long long t = mult(i, j>>1, p);
    t += t;
    if(j & 1) t += i;
    return t % p;
}

Taking out the number of all combinations of m (m≤n) elements from n different elements is called taking out the number of combinations of m elements from n different elements. Denoted as C(n,m)
C(n,m) = n!/(m!*(nm)!) (n>=m)

long long factorial(long long number)
{
    
    	if(number<=1)
		return 1;
	else 
		return number*factorial(number-1);
} 
int combinator(int n,int m)
{
    
    	int temp;
	if(n<m)
	{
    
    	temp=n;
		n=m;
		m=temp;}
	return factorial(n)/(factorial(m)*factorial(n-m));
}		
result=combinator(a,b);

Guess you like

Origin blog.csdn.net/weixin_46434074/article/details/108919290