天天看点

(hdu 1576)A/B(扩展欧几里得/费马小定理求逆元 or 水)

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6299 Accepted Submission(s): 4967

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

HDU 2007-1 Programming Contest

#include<cstdio>
int main()
{
    int t,n;
    long long i,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld",&n,&b);
        for(i=0;i<9973;i++)
        {
            if((b*i-n)%9973==0)
                break;
        }
        printf("%lld\n",i);
    }
    return 0;
}
           
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mod=9973;
/*LL pow_mod(LL a,LL b)
{
    LL ans=1;
    a%=mod;
    while(b>0)
    {
        if(b&1) ans=(ans*a)%mod;
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
LL fermat(LL a,LL p)///费马小定理
{
    return pow_mod(a,p-2);
}*/
void ext_gcd(LL a,LL b,LL &d,LL &x,LL &y)///扩欧
{
    if(b==0)
        d=a,x=1,y=0;
    else
    {
        ext_gcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
LL inv(LL a,LL p)
{
    LL d,x,y;
    ext_gcd(a,p,d,x,y);
    return d==1?(x%p+p)%p:-1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,b;
        scanf("%d%d",&n,&b);
        LL ans=n*inv(b,mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}