HDU - 1576

HDU - 1576

要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
Input
数据的第一行是一个T,表示有T组数据。 
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
对应每组数据输出(A/B)%9973。
Sample Input
2
1000 53
87 123456789
Sample Output
7922
6060

思路:

求:(A/B)%9973

首先,依题可得:
(1)n=A%9973;
(2)A|B
(3)gcd(B,9973)==1

现在我们根据gcd(B,9973)这条件我们可以反推
 x*B+y*9973==gcd(B,9973);
 

因为A|B--> A/B=x -->A=B*x;
 因为A%9973=n,根据同余可得:A=n+9973*y -->n+9973*y = B*x -->(x/n)*B-(y/n)*9973 = 1
 

最终推出  (x/n)*B-(y/n)*9973 = gcd(B,9973) = 1 -->得到 要求的x1 = x/n;
 所以 我们只要求出 (B,9973的)逆元x1 那么x=x1*n=A/B;

#include <iostream>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y){
    if(a==0&&b==0){
        return -1;
    }
    if(b==0){
        x=1;
        y=0;
        return a;
    }
    ll d=exgcd(b,a%b,y,x);
    y = y-a/b*x;
    return d;
}
ll solve(ll b,ll m){
    ll x,y;
    ll d = exgcd(b,m,x,y);
    if(d==1){
        return (x%m+m)%m;
    }
    else{
        return -1;
        }
}
int main(){
    int t;
    ll n,b;
    cin>>t;
    while(t--){
        cin>>n>>b;
        ll m=9973;
        ll x1=solve(b,m);

        ll ans=((x1*n)%m+m)%m;
        cout<<ans<<endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值