HDUOJ----专题训练C

时间:2023-03-10 02:14:57
HDUOJ----专题训练C

Problem C

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 5
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,所以A=k*9973+n与之等价...
 由于gcd(b,9973)=1;也就是 b和9973的公约数恒定为1。。。。
而且 A=w*b。。。(需要说明的一点是,w,k,都是为未知数)
也就是有这么一个等式    w*b=k*9973+n (mod 9973) 恒定成立。。。
然后就不能化解成为 w*b+x*9973=n-----》构造出了  a*x+b*y=c这种模式....
然后就可以求解了啊!( 详细说明请看,第一个欧几里得扩展题目,那里有些关于欧几里得扩展的几个定理) 贴下代码....
 #include<iostream>
using namespace std;
int x,y,q;
void exgcd(int a,int b)
{
if(b==)
{
x=,y=,q=a;
}
else
{
exgcd(b,a%b);
int temp=x;
x=y,y=temp-a/b*y;
}
}
int main()
{
int n,b,t;
cin>>t;
while(t--)
{
cin>>n>>b;
exgcd(b,);
q;
int temp=/q;
cout<<((n/q*x%temp+temp)%temp)<<endl;
}
return ;
}