Three common methods for finding the inverse element and Striling for n! Approximate value of

Inverse element:

Definition: For a positive integer a, if there is ax ≡ 1 (mod m), note that the equal sign here represents identity, then the smallest positive integer solution x in this congruence equation is called the inverse element of a mod m (in fact, it is equivalent In the variant of x*x^-1=1).
There are three commonly used methods for finding the inverse element: extended Euclidean algorithm, Fermat's little theorem, and Euler's theorem to find the inverse element. Of course there are other ways, but only the above three are introduced below.

Extended Euclidean algorithm: The
Euclidean algorithm returns the greatest common divisor of two numbers, and the extended Euclidean algorithm returns the greatest common divisor, but it only passes more parameters.
Definition: used to solve a set of x and y in the known a, b such that ax+by=gcd(a, b) (the solution of this formula must exist, based on the knowledge in number theory) is
commonly used in solving modular linear equations and equations Group.

About the understanding of the method of solving x and y: The equation ax+by=gcd(a, b) is known
(1) When b=0, gcd(a, b)=a, then x=1, y=0; 1 a+0 b=a
(2) Let ax1+by1=gcd(a, b)

In the next exgcd, bx2+(a mod b) y2=gcd(b, a mod b)
According to Euclid principle, gcd(a, b)=gcd(b, a mod b),
then ax1+by1=bx2+(a mod b) y2
means ax1+by1=bx2+(a-(a/b)*b)y2=ay2+bx2-(a/b)*by2;
then x1=y2, y1=x2-(a/b)y2 ;
So it can be concluded that the values ​​of x1, y1 are based on x2, y2.

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=200;
char s[N];
char b[N];

int exgcd(int a,int b,int &x,int &y)//扩展gcd
{
    if(!b)//b为0的情况
    {
        x=1;
        y=0;
        return a;
    }
    int r=exgcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-a/b*y;
    return r;
}


int main()
{

    ios::sync_with_stdio(false);
    int a,b;
    cin>>a>>b;
    int x=0,y=0;
    cout<<exgcd(a,b,x,y)<<endl;

    return 0;
}

1. The extended gcd inverse element requires a and m to be relatively prime

The inverse element found is the smallest inverse element

First, the concept of coprime is given. The common factor of n numbers (n>=2) is only 1.
Prime numbers are also called prime numbers.

First give the mathematical process:
ax+by=gcd(a,b);
then ax+my=gcd(a,m);
because a and m are relatively prime, gcd(a,m)=1
then ax+my=1
That is, ax≡1 (mod m)
at this time x is the inverse of a.

Code: Use extended gcd to find a set of x, y, gcd; if gcd is 1, the inverse element exists, adjust x to the range of 0~m-1; otherwise, the inverse element does not exist

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=200;
char s[N];
char b[N];

int exgcd(int a,int b,int &x,int &y)
{
    if(a==0&&b==0)
        return -1;
    if(!b)
    {
        x=1;
        y=0;
        return a;
    }
    int d=exgcd(b,a%b,y,x); //回代 注意这里的与扩展gcd的不同
    y-=a/b*x;
    return d;
}



int main()
{

    ios::sync_with_stdio(false);
    int m,n;
    cin>>m>>n;
    int x,y;
    int d=exgcd(m,n,x,y);
    cout<<(x%n+n)%n<<endl;

    return 0;
}

2. Fermat's little theorem requires m to be a prime number, and a and m are relatively prime

Fermat's little theorem: a^(m-1)≡1 (mod m) //m is a prime number
a^(m-1)≡ 1 (mod m) then a*a^(m-2)≡1 (mod m)
So a^(m-2) is the inverse element of a modulo m (solved by fast power)
time complexity logN.

Code:

int mod;
int quick_mod(int n,int p)
{
    int res=1,k=p;
    while(k)
    {
        if(k%2)
            res=res*n%mod;
        n=n*n%mod;
        k/=2;
    }
    return res;
}



int main()
{

    ios::sync_with_stdio(false);
    int a;
    cin>>a>>mod;
    cout<<quick_mod(a,mod-2)<<endl;
    return 0;
}

3. Euler's theorem for inverse element: basic general
time complexity lg (√n).

Euler's theorem: a^(∅ (m))≡ 1 (mod m)
then a*a^(∅ (m)-1)≡ 1 (mod m)
is ax≡ 1 (mod m)
so a^(∅ (M)-1) is the inverse of a ∅ (m) is the number of numbers less than m and relatively prime to m

1. If n=1, then φ(1)=1. Because 1 and any number (including itself) constitute a coprime relationship
. 2. For a prime number p Φ (p) = p-1, for two prime numbers pq, Φ (pq) = pq-1
Euler function is a multiplicative function, But it is not a completely multiplicative function
. 3. If n is a certain power of a prime number, that is, n = p^k (p is a prime number and k is an integer greater than or equal to 1), then for Write picture description here
example φ(8) = φ(2^3 ) =2^3-2^2 = 8 -4 = 4.
This is because only when a number does not contain a prime number p, can it be relatively prime to n. And there are a total of p (k-1) numbers containing prime number p , namely 1×p, 2×p, 3×p,..., p (k-1)×p, remove them, and the rest is the same as n is a relatively prime number.
The above formula can also be written in the following form:
Write picture description here
4. If n can be divided into the product of two relatively prime integers, n=p1*p2,
then Φ (n) = Φ (p1)*Φ (p2)
5. Any a number n (n> 1) can be written as a series of prime numbers the product
Write picture description here
according to conclusion 4, to give
Write picture description here
another 3 according to the conclusions to give
Write picture description here
itWrite picture description here

Two codes for finding Euler's number are given below:

Ask Euler directly:

int euler(int x)
{
    int res=x;
    for(int i=2;i*i<=x;i++)
        if(x%i==0)
        {
            res=res/i*(i-1);
            while(x%i==0)
                x/=i;
        }
     if(x>1)
        res=res/x*(x-1);
     return res;
} 

It can also be used directly by typing a table. The process is to make each phi[i]=i, and then for each prime number p, j is its multiple, phi[j]=phi[j]/p*(p-1 );

void eulerplus()
{
    for(int i=1;i<=N;i++)
        phi[i]=i;
    for(int i=2;i<=N;i+=2)
        phi[i]/=2;
    for(int i=3;i<=N;i+=2)
        if(phi[i]==i)
    {
        for(int j=i;j<=N;j+=i)
            phi[j]=phi[j]/i*(i-1);
    }
}

4. The application of the inverse element Find the modular inverse element
ax ≡ 1 (mod m)
so x=1/a (mod m)
so (b/a) mod m
then (b x) mod m
then (b%m
x%m )%M

striling formula

Often used to ask for n! Approximate value
n! ≈ √2πn(n/e)^n

emmm, let's upload the picture
Write picture description here

This knowledge point can be used in conjunction with lg to estimate the number of digits in a number
n, the number of digits in base x = logn (x) +1,
for example, the number of digits in decimal number x = lg (x) +1

You can also estimate that the factorial of a number is a multiple of another number.

Guess you like

Origin blog.csdn.net/Puppet__/article/details/79220379