A/B
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4367 Accepted Submission(s): 3374
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)。
每组数据有两个数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
这题需要用数学知识推导,但首先要能够想到用逆元
因为A%9973=n,所以令A=n+9973*y;再设A/B=x,则得到B*x-9973*Y=n;因为gcd(B,9973)=1;所以B,9973互质,所以用扩展欧几里得算法(模板)即可
#include <iostream>
#include <cstdio>#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
LL ex_gcd(LL a,LL b,LL &x,LL &y);
LL cal(LL a,LL b,LL c);
LL s1, s2;
int main()
{
int t;
scanf("%d", &t);;
while(t--)
{
LL n, b;
scanf("%I64d %I64d",&n, &b);
printf("%I64d\n",cal(b, 9973, n)%9973);
}
return 0;
}
LL cal(LL a,LL b,LL c)
{
LL x, y;
LL gcd=ex_gcd(a,b,x,y);
if(b<0)
{
b=-b;
}
x*=c/gcd;
b/=gcd;
x%=b;
if(x<=0)
{
x+=b;
}
return x;
}
LL ex_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL ans=ex_gcd(b,a%b,x,y);
LL tmp=x;
x=y;
y=tmp-a/b*y;
return ans;
}