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这种模式....
然后就可以求解了啊!( 详细说明请看,第一个欧几里得扩展题目,那里有些关于欧几里得扩展的几个定理) 贴下代码....
 
 1 #include<iostream>
 2 using namespace std;
 3 int x,y,q;
 4 void exgcd(int a,int b)
 5 {
 6   if(b==0)
 7   {
 8    x=1,y=0,q=a;
 9   }
10   else
11   {
12       exgcd(b,a%b);
13       int temp=x;
14       x=y,y=temp-a/b*y;
15   }
16 }
17 int main()
18 {
19     int n,b,t;
20     cin>>t;
21     while(t--)
22     {  
23         cin>>n>>b;
24         exgcd(b,9973);
25         q;
26         int temp=9973/q;
27         cout<<((n/q*x%temp+temp)%temp)<<endl;
28     }
29     return 0;
30 }
View Code

 

posted @ 2013-07-30 20:53  龚细军  阅读(686)  评论(3编辑  收藏  举报