A/B

要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。

模板题,现在给你模板:

exgcd逆元:
//x->a关于b的逆元
//y->b关于a的逆元
typedef long long LL
void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d){
      if (!b) {d = a, x = 1, y = 0;}
      else{
          ex_gcd(b, a % b, y, x, d);
      ll t=x; x=y;
      y
= t- y* (a / b);
       } }
快速幂+费马小定理: // 快速幂求逆元 LL pow_mod(LL a, LL b, LL p){//a的b次方求余p LL ret = 1; while(b){ if(b & 1) ret = (ret * a) % p; a = (a * a) % p; b >>= 1; } return ret; } LL Fermat(LL a, LL p){//费马求a关于b的逆元 return pow_mod(a, p-2, p); }

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
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 t=x;
    x=y;
    y=t-(a/b)*y;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll x,y,b,n;
        cin>>n>>b;
        exgcd(b,9973,x,y);
        x=x*n;
        
        ll ans=(x%9973+9973)%9973;
        cout<<ans<<endl;
    }
    return 0;
}

以上。

posted @ 2019-08-14 22:32  Luoha  阅读(124)  评论(0编辑  收藏  举报