Extended Euclid and Inverse Element

Extended Euclid and Inverse Element

See the complete article of my own blog https://dyingdown.github.io/2019/08/14/Extended-Euclid-and-Inverse-element/
Today’s importance is inverse element, however, inverse element calculation is based on the extended Euclid algorithm. At first, we need to know the Euclid’s algorithm.

A much more efficient method is the Euclidean algorithm, which uses a division algorithm such as long division in combination with the observation that the gcd of two numbers also divides their difference. --from Wikipedia

Extended Euclided

a x + b y = g c d ( a , b ) ax+by = gcd(a, b) ax+by=gcd(a,b)

From the Euclid Method of calculating gcd, we know that
g c d ( a , b ) = g c d ( b , a % b ) gcd(a, b) = gcd(b,a \% b) gcd(a,b)=gcd(b,a%b)
We set another function
b x 1 + ( a % b ) y 1 = g c d ( b , a % b ) bx_1 + (a\%b)y_1 = gcd(b, a \% b) bx1+(a%b)y1=gcd(b,a%b)
So, combine the first function with the third function we got
a x + b y = b x 1 + ( a % b ) y 1 ⇒ a x + b y = b x 1 + ( a − a / b × b ) y 1 ⇒ a x + b y = b x 1 + a y 1 − b × ( a / b ) y 1 ⇒ a x + b y = a y 1 + b ( x 1 − a / b × y 1 ) ⇒ { x = y 1 y = ( x 1 ) − a / b × y 1 ) ax+by = bx_1+(a\%b)y_1\\ \Rightarrow ax+by=bx_1+(a-a /b \times b)y_1\\ \Rightarrow ax+by=bx_1+ay_1-b\times(a/b)y_1\\ \Rightarrow ax + by = ay_1+ b(x_1-a/b\times y_1)\\ \Rightarrow \left\{ \begin{aligned} x & = y_1 \\ y & = (x_1) - a / b \times y_1) \end{aligned} \right. ax+by=bx1+(a%b)y1ax+by=bx1+(aa/b×b)y1ax+by=bx1+ay1b×(a/b)y1ax+by=ay1+b(x1a/b×y1){xy=y1=(x1)a/b×y1)
We define that if
b = 0 : x = 1 , y = 0 b = 0:x = 1 , y = 0 b=0:x=1,y=0
So we can write our function in this way

typedef pair<int,int> P;
P gcdEx(int a, int b){
    if(b == 0) return P(1, 0);
    else{
        P p = gcdEx(b, a % b);
        return P(t.second, t.first - (a / b) * t.second);
    }
}

The previous one is the direct one we can get from the mathematical function. Here is a better way of writing it.

void gcdEx(int a, int b, int &x, int &y){
    if(b == 0){
        x = 1; y = 0;
    }else{
        gcdEx(b, a % b, y, x);
        y -= a / b * x;
    }
}

Inverse Element

a x ≡ 1 ( m o d   b ) ax \equiv 1 (mod \space b) ax1(mod b)

There is a inverse element only if a and b are both prime numbers. The equation above means both side mod b at the same time is equal. x is a reverse element of a under mod b.

We can change the equation into this.
a x   m o d   m = 1 a x − m y = 1 ax \space mod \space m = 1\\ ax -my = 1 ax mod m=1axmy=1
y is the middle element used to solve x.

So the code looks like this.

int x, y;
gcdEx(a, m, x, y);
x = (x % m + m) % m;

x is the inverse element of a.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值