HDU1576 A/B (扩展欧几里得求逆元)

A/B

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4665    Accepted Submission(s): 3632


Problem 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
2 1000 53 87 123456789
 
Sample Output
7922 6060
 
Author
xhd
 
Source
 
Recommend
linle
 
其实此题就是求逆元,提出一点对逆元的一点:对于一个数B的逆元,可以当成 1/B 来各种计算
 
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <iostream>
#include "algorithm"
using namespace std;
typedef long long LL;
int cas;
LL B,n;
LL a,b,c,d,f,x,y;
LL exgcd(LL a, LL b,LL &x,LL &y){
    if (b==0)
    {x=1;
     y=0;
     return a;
    }
    LL d=exgcd(b,a%b,x,y);
    LL f=x;
    x=y;
    y=f-(a/b)*y;
    return d;
}
int main(){
    freopen ("ab.in","r",stdin);
    freopen ("ab.out","w",stdout);
    int i,j;
    scanf("%d",&cas);
    while (cas--){
        scanf("%lld%lld",&n,&B);
        a=B,b=9973ll,c=1ll;
        d=exgcd(a,b,x,y);
        x=(x%(b/d)+(b/d))%(b/d);
        printf("%lld\n",(n*x)%b);
    }
    return 0;
}
未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
原文地址:https://www.cnblogs.com/keximeiruguo/p/5990349.html