Description you are asked to design a calculator to complete the following three tasks: 1, given y,z,p, calculates the value of Y^z Mod p, 2, given Y,z,p, calculates the smallest nonnegative integer satisfying the xy≡z (mod p), 3, given y,z,p, the calculation satisfies y^x≡z (mod p ) is the smallest non-negative integer. Input
The input contains multiple sets of data.
The first line contains two positive integers t,k each representing the number of data groups and the type of inquiry (for all data within a test point, the same type of inquiry). The following lines contain three positive integers per line y,z,p, describing a query. Output for each query, outputs a line of answers. For query Types 2 and 3, if no condition exists, the output is "Orz, I cannot find x!", noting that there is a space between the comma and "I". Sample Input"Sample Input 1"
3 1
2 1 3
2 2 3
2 3 3
"Sample Input 2"
3 2
2 1 3
2 2 3
2 3 3
"Data size and conventions"
For 100% of the data, 1<=y,z,p<=10^9, for prime numbers, 1<=t<=10. Sample Output"Sample Output 1"
2
1
2
"Sample Output 2"
2
1
0
Ideas
Fast power, expand Euclid, Bsgs
The first question is obtained by a fast power.
The second ask Axξb (mod n), converted to Ax=ny+b, converted to Ax+ny=b, using the extended Euclidean algorithm to find out AX+NY=GCD (a,n), if B is not a multiple of gcd then no solution is x/gcd*b.
The third asks Axξb (mod n), the BSGS algorithm. We need to verify 0. The number within the n-1. Block, set each block size of M, find out 0. Ai% n in m-1 is saved as EI, for M. In 2m-1, we only need to verify if there is an AM *ei=b (mod n), that is, to determine if there is a ei=a-m *b (mod n), so that you can save EI with a hash table and then ask for the inverse of AM in modulo n.
Time Complexity is O ((m+n/m) LOGM), when M takes N½, the complexity is better than O (N½LOGN)
Code
1#include <map>2#include <cmath>3#include <cstdio>4 using namespacestd;5 6typedefLong LongLL;7 LL a,b,c,t,k;8 9 ll Pow (ll x,ll p,ll MOD) {TenLL tmp=x,ans=1; One while(p) { A if(p&1) ans= (ans*tmp)%MOD; -tmp= (tmp*tmp)%MOD; -p>>=1; the } - returnans; - } - voidGCD (LL a,ll b,ll& d,ll& x,ll&y) { + if(!b) d=a,x=1, y=0; - ElseGCD (b,a%b,d,y,x), y-=x* (A/b); + } A LL INV (ll a,ll N) { at LL d,x,y; - gcd (a,n,d,x,y); - returnd==1? (x+n)%n:-1; - } - ll Log_mod (ll a,ll b,ll N) { -LL m,v,e=1, I; inM=SQRT (n+0.5); -v=Inv (POW (a,m,n), n); toMap<ll,ll>MP; +mp[1]=0; - for(LL i=1; i<m;i++) { theE= (e*a)%N; * if(!mp.count (e)) mp[e]=i; $ }Panax Notoginseng for(LL i=0; i<m;i++) { - if(Mp.count (b))returni*m+Mp[b]; theb= (b*v)%N; + } A return-1; the } + - intMain () { $scanf"%lld%lld",&t,&k); $ while(t--) { -scanf"%lld%lld%lld",&a,&b,&c); - if(k==1) { theprintf"%lld\n", pow (a,b,c)); -}Else Wuyi if(k==2) { the LL d,x,y; - gcd (a,c,d,x,y); Wu if(b%d) puts ("Orz, I cannot find x!"); - Else { AboutLL ans= ((x*b/d)%c+c)%C; $printf"%lld\n", ans); - } -}Else { -LL ans=Log_mod (a,b,c); A if(ans==-1) puts ("Orz, I cannot find x!"); + Elseprintf"%lld\n", ans); the } - } $ return 0; the}
Bzoj 2242 [SDOI2011] Calculator (number theory knowledge)