A/B
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2017 Accepted Submission(s): 1469
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
题目的关键就是根据题意将条件转化出不定方程形式然后直接套用模板就可以
由题意n=A%9973
n=A-A/9973*9973
又因为A/B=X;
A=n+A/9973*9973=BX;
所以得到 BX-9973y=n;
即求x即可
#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){
if(b == 0){
x = 1;
y = 0;
return a;
}
ll r = ex_gcd(b,a%b,y,x);
y -= x*(a/b);
return r;
}//扩展欧几里得模板
int main(){
ll n,B,a,gcd,x,y;
scanf("%lld",&a);
while(a--){
scanf("%lld%lld",&n,&B);
gcd = ex_gcd(B,9973,x,y);//关键在于转化式子,得到不定方程形式Bx-9973y=n
x *= n;
x = x%9973;//求最小正整数模板
if(x<0)
x += 9973;
printf("%lld\n",x);
}
return 0;
}