拓展欧几里得求逆元

A/B

  HDU - 1576 
乘法逆元:
对于缩系中的元素,每个数a均有唯一的与之对应的乘法逆元x,使得ax≡1(mod n)
一个数有逆元的充分必要条件是gcd(a,n)=1,此时逆元唯一存在 

逆元的含义:模n意义下,1个数a如果有逆元x,那么除以a相当于乘以x。

#include<iostream>
#include<cstdio>
using namespace std;
#define LL long long

void  exgcd(LL a,LL b,LL &x,LL &y)
{
    if(!b)
    {
        x=1;
        y=0;
        return ;
    }
    exgcd(b,a%b,x,y);
    LL t=x;
    x=y;
    y=t-a/b*y;
    return ;
}
int main()
{
    int t;
    LL x,y;
    scanf("%d",&t);
    while(t--)
    {
        LL n,b;
        cin>>n>>b;
        exgcd(b,9973,x,y);
        //cout<<x<<endl;
        x=(x%9973+9973)%9973;
        cout<<(n*x)%9973<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40894017/article/details/80426081