HDU——T 1576 A/B

http://acm.hdu.edu.cn/showproblem.php?pid=1576

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6418    Accepted Submission(s): 5075


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
 
膜的性质+逆元

(a/b)mod p=?
定义 c为b在mod p意义下的逆元
=a*c mod p = (a mod p * c mod p)mod p

① exgcd求逆元

 1 #include <algorithm>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 const int mod(9973);
 7 int T,n,b,x,y;
 8 
 9 int exgcd(int a,int b,int &x,int &y)
10 {
11     if(!b)
12     {
13         x=1; y=0;
14         return a;
15     }
16     int ret=exgcd(b,a%b,x,y);
17     int tmp=x; x=y;
18     y=tmp-a/b*y;
19     return ret;
20 }
21 
22 int main()
23 {
24     scanf("%d",&T);
25     for(;T--;)
26     {
27         scanf("%d%d",&n,&b);
28         int gcd=exgcd(b,mod,x,y);
29         x*=gcd;
30         x=(x%mod+mod)%mod;
31         printf("%d\n",(x*n)%mod);
32     }
33     return 0;
34 }

 ② 欧拉定理求逆元

 1 #include <algorithm>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 #define LL long long
 7 const int mod(9973);
 8 int T,n,b;
 9 
10 LL phi(LL x)
11 {
12     LL ret=1;
13     for(LL i=2;i*i<=x;i++)
14         if(x%i==0)
15         {
16             x/=i;
17             ret*=i-1;
18             for(;x%i==0;)
19                 ret*=i,x/=i;
20         }
21     if(x>1) ret*=x-1;
22     return ret;
23 }
24 LL Q_pow(LL a,LL b)
25 {
26     LL ret=1,base=a;
27     for(;b;b>>=1)
28     {
29         if(1&b) ret=(ret*base)%mod;
30         base=(base*base)%mod;
31     }
32     return ret;
33 }
34 
35 int main()
36 {
37     scanf("%d",&T);
38     for(;T--;)
39     {
40         scanf("%d%d",&n,&b);
41         LL x=Q_pow((LL)b,(LL)phi((LL)mod)-1);
42         x=(x%mod+mod)%mod;
43         printf("%d\n",(x*n)%mod);
44     }
45     return 0;
46 }

 

posted @ 2017-08-09 19:44  Aptal丶  阅读(153)  评论(0编辑  收藏  举报