hdu 1576 A/B

思路:

已知:A%9973 = n;  gcd(B,9973)=1;

求(A/B)%9973;

k = (A/B)%9973  - > A/B  - 9973*y = k;  -> A = k*B+9973*y;

带入 A%9973 = n中得  k*B%9973 = n;

kB - 9973*y = n; -> B*k/n -9973*y/n =1 =gcd(B,9973);

x = k/n,即k = n*x;

代码:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int mod=9973;
void exgcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return;
    }
    exgcd(b,a%b,x,y);
    LL tmp=x;
    x=y;
    y=tmp-(a/b)*y;
}
int main()
{
    LL a,b,m,n,x,y,t;
    cin>>m;
    while(m--)
    {
        cin>>n>>b;
        exgcd(b,mod,x,y);
        LL ans=(x%mod+mod)%mod;//逆元
        LL sum=(n*(ans%mod))%mod;
        cout<<sum<<endl;;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lusiqi/p/12453026.html