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
first ask the second question to skip, third ask Bsgs-------------------------------------------------------------tell me to split the line--------------------------------------------------- --------What is BSGS? That is, Baby-step-giant-step, translated into Chinese is a small step, to solve similar x^y=z (mod p) for the least y such problems (perhaps also capable of other, but I am weak slag, do not know)for the derivation of the above topicA little messy, when I want to understand the re-
1#include <cstdio>2 #definell Long Long3#include <map>4#include <cmath>5 using namespacestd;6 intt,k;7ll Pow (ll X,intYintp) {8ll ans=1;9 while(y>0){Ten if(y&1==1) ans= (ans*x)%p; OneY=y>>1; Ax= (x*x)%p; - } - returnans; the } - - intgcdintXinty) { - if(x%y==0)returny; + returnGCD (y,x%y); - } + A voidEXGCD (intAintBint&x,int&y) { at if(b==0) {x=1, y=0;return;} -EXGCD (b,a%b,x,y); - intT=x;x=y;y=t-(A/b) *y; - } - - voidSolve2 (intAintZintb) { in inttmp=gcd (A, B), x, y; - if(z%tmp) {printf ("Orz, I cannot find x!\n");return;} to EXGCD (a,b,x,y); +x= (LL) x* (z/tmp))%b; - while(x>0) x-=b/tmp; the while(x<0) x+=b/tmp; *printf"%d\n", x); $ }Panax Notoginseng -map<int,int>MP; the voidSolve3 (intYintZintp) { +y%=p; A if(!y&&!z) {printf ("1\n");return;} the if(!y) {printf ("Orz, I cannot find x!\n");return;} + mp.clear (); -ll M=ceil (sqrt (p)), t=1; $mp[1]=m+1;//y^0==1; $ for(intI=1; i<m;i++){ -t=t*y%p; - if(!mp[t]) mp[t]=i; the } -ll Tmp=pow (y,p-1-M,P), ine=1;Wuyi for(intk=0; k<m;k++){ the intI=mp[z*ine%p]; - if(i) { Wu if(i==m+1) i=0; -printf"%d\n", k*m+i); About return; $ } -ine=ine*tmp%p; - } -printf"Orz, I cannot find x!\n"); A } + the intMain () { -scanf"%d%d",&t,&k); $ while(t--){ the inty,z,p; thescanf"%d%d%d",&y,&z,&p); the if(k==1) printf ("%lld\n", pow (y,z,p)); the if(k==2) solve2 (y,z,p); - if(k==3) Solve3 (y,z,p); in } the}
"Bzoj 2242" [SDOI2011] Calculator