hdu 1576 A/B (求乘法逆元——扩展欧几里得)

A/B

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


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
 


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<string.h>
#include<algorithm>
#include<cstdio>
using namespace std;
#define sz(v) (int)(v.size())
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,a,n) for(int i=n-1;i>=a;--i)
#define mem(a,t) memset(a,t,sizeof(a))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define N 1000005
#define LL __int64
const int mod=1e9+7;

LL exgcd(LL a,LL b,LL &x,LL &y)    
{
    LL d=a;
    if(a==0&&b==0)
        return -1;
    if(b)
        return exgcd(b,a%b,y,x),y-=a/b*x;
    else{
        x=1;
        y=0;
    }
    return d;
}
int mod_reverse(LL a,LL mod)
{
    LL x,y,d;
    d=exgcd(a,mod,x,y);
    if(d==-1)
        return d;
    return (x%mod+mod)%mod;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int T;
    int n,b,x;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&b);
        x=mod_reverse(b,9973); 
        printf("%d\n",n*x%9973);
    }
    return 0;
}






猜你喜欢

转载自blog.csdn.net/u011721440/article/details/51225548