A/B(乘法逆元)

来源:互联网 发布:淘宝评分影响 编辑:程序博客网 时间:2024/05/01 00:26

Description

要求(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

21000 5387 123456789
 

Sample Output

79226060

解题思路:

最水的乘法逆元题。

AC代码:

#include <iostream>#include <cstdio>void exGcd(int a, int b, int &x, int &y){    if(b == 0)    {        x = 1;        y = 0;        return;    }    exGcd(b, a % b, x, y);    int temp = x;    x = y;    y = temp - a / b * y;}int main(){    int t, n, B, ans;    scanf("%d", &t);    while(t--)    {        scanf("%d%d", &n, &B);        int a = B, b = 9973, x, y;        exGcd(a, b, x, y);        ans = (n % 9973 * x % 9973) % 9973;        while(ans < 0)  // 不要让余数小于0            ans += 9973;        printf("%d\n", ans);    }    return 0;}


0 0