On the inverse Its Solution

Refer to the following blog content section

https://www.cnblogs.com/rir1715/p/7748054.html#commentform

https://baijiahao.baidu.com/s?id=1609032096408414934&wfr=spider&for=pc

use:

Bloggers now on cognition is (after all, a konjac, if there is a new understanding will be updated), inverse is mainly used when modulo division. First, if the multiplication, then there is  ( A * B ) % MOD = (A * B MOD% MOD%) , which is an obvious conclusion, but we encountered when modulus division (modulo number is typically a combination of time), would not be so easy, because ( a / b ) % MOD! = (( a% MOD ) / ( b% MOD )), this time we need the inverse debut, as to why, please see definition of inverse below.

definition:

In simple terms, in fact, inverse equivalent reciprocal junior high school.

We know that, for a number a, we can define a number of B, such that a * b = 1, so that when we divide a need, it can be converted into B by, so that the division can be converted to the multiplication.

Similarly, except for a modulo operation on a mod, we still can define a number of B, such that

( NUM / a)% = mod (NUM * b) mod, such a number b, b can be referred to as a lower mold in a inverse mod, with inverse element, we will be able to modulo division converted to arithmetic multiplication modulo operation, so that to solve some problems.

Given herein defined mathematical form inverse element (assuming a modulo n)

a * b ≡ 1 (mod n)

Finally, to note that a number of a die in the case where n is not necessarily the inverse element, on a n inverse if and only if the presence of a prime and n, a number reverse in a case where the modulo n yuan is not necessarily unique, but we only need enough of a claim, because they result in a condition modulo n is the same.

Seeking:

At present, solving the inverse There are three approaches, the following will explain one by one.

Setting integer a, modulo p, a mod p inverse element under the condition of x.

1.  Fermat's Little Theorem

Applications: p is a prime number.

This should be the most commonly used method of inverse yuan, although some limitations, but it is the most frequently used of an algorithm.

principle:

First, we have made Fermat's Little Theorem

   a^(p - 1) ≡ 1 mod p

Deformation was

  a * a^(p-2) ≡ 1 mod p

In contrast to the definition of the inverse element

  a * x ≡ 1 mod p

Obviously we can get

  x  ≡ a^(p-2) mod p

Thus, we reasoning of Fermat's Little Theorem out a inverse mod p under conditions of a ^ (p-2).

Code:

typedef long long LL
LL quick_pow(LL x, LL n LL p)// 快速幂求x^n mod p 的结果 { if(n==0) return 1; if(n==1) return x%p; LL ans = 1; LL tmp = x%p; while(n) { if(n&1) { ans = (ans*tmp)%p; } tmp = tmp*tmp%p; b>>=1; } Return ANS% P; } LL INV (B LL, LL P) // find the number of inverse element b mod p { return quick_pow (B, p- 2 , P); }

 

2.  expand Euclid

Limitations: difficult to understand? (Manual nugget)

principle:

Knocked up too much trouble, handwritten

 

 

Code:

typedef long long LL
LL ex_gcd(int a,int b,int &x,int &y)//注意x,y的传引用
{
    if(a==0&& b==0)
        return -1;
    if(b==0)//递归终止条件
    {
        x = 1;
        y = 0;
        return a;
    }
    int ans  = ex_gcd(b,a%b,x,y);
    int t = x; // 扩欧方程的解
        x = y;
        y = x - a/b*y;
    return ans;
}
LL inv(LL b,LL p) //求数 b mod p 的逆元
{
    int x,y;
    int d = ex_gcd(b,p,x,y);
    if(d==-1) // b,p不互素,利用扩欧求不出逆元
        return - 1else
     return (x+p)%p; //x 的值就是 逆元,加上p再取模消除负数的情况
}

 

3. 线性推

原理:

 

 

因此我们可以通过一个递推式在线性时间内推导出1——(p-1)mod p 的逆元,递推式如下注意在式子右边加上p来消除负号

  inv[i]=(p-p/i)*inv[p%i]%p;

代码实现:

typedef long long LL
LL inv_arr[maxn];
void inv(LL b,LL p) //可以得到1到b mod p 的逆元 (1 < b < p)
{
    inv_arr[1] = 1;
    for(int i = 2;i<=b;i++)
        inv_arr[i] = (p-p/i)*inv_arr[p%i]%p;
    return;
}

如果还有其他疑问,欢迎在评论区留言

Guess you like

Origin www.cnblogs.com/baihualiaoluan/p/11257350.html