hdu1576 A/B(乘法逆元)

题目链接: hdu1576 ( A/B )

求出 \(B\) 关于 \(9973\) 的乘法逆元即可。

由于 \(gcd(B,9973) = 1\) ,故可用费马小定理。

/**
 * hdu1576 A/B
 *
 */

#include <cstdio>
#include <climits>

typedef long long LL;

LL qpow(LL a, LL b, LL mod = LLONG_MAX)
{
    a %= mod;
    LL t = 1;
    while (b) {
        if (b&1) t = t*a%mod;
        a = a*a%mod;
        b >>= 1;
    }
    return t%mod;
}

LL inv(LL x, LL p)
{
    return qpow(x, p-2, p);
}

const int mod = 9973;
int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
        int a, b;
        scanf("%d%d", &a, &b);
        int ans = a*inv(b, mod)%mod;
        printf("%d\n", ans);
    }
    return 0;
}

逆元模板

条件: \(x, p\) 互质。

typedef long long LL;
LL qpow(LL a, LL b, LL mod = LLONG_MAX)
{
    a %= mod;
    LL t = 1;
    while (b) {
        if (b&1) t = t*a%mod;
        a = a*a%mod;
        b >>= 1;
    }
    return t%mod;
}

LL inv(LL x, LL p)
{
    return qpow(x, p-2, p);
}
posted @ 2021-02-16 21:19  Zewbie  阅读(72)  评论(0)    收藏  举报