A/B HDU1576

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

思路:要求 ans=(A/B)%9973,转换可得 A=9973×k×B+ans×B。又由n(n=A%9973)可得,n=(9973×k×B+ans×B)%9973=ans×B%9973,可以将 n=ans×B%9973 写成 B×ansn+(kn)×9973=gcd(B,9973)=1
然后用扩展欧几里得算出 ansn,然后乘以 n 就得到 ans 了,不过要注意取模。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;

const int MOD = 9973;

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

int main()
{
    int T;
    int n, B;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &n, &B);
        int x, y;
        ExtendGcd(B, x, MOD, y);
        x = (x%MOD+MOD)%MOD;
        printf("%d\n", (x*n)%MOD);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值