标签:color content input amp ret ons bottom -- otto
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6418 Accepted Submission(s): 5075
(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 }
标签:color content input amp ret ons bottom -- otto
原文地址:http://www.cnblogs.com/Shy-key/p/7327351.html