This article is copyright ljh2000 and Blog Park is shared, welcome reprint, but must retain this statement, and give the original link, thank you for your cooperation.
This article ljh2000
Author Blog: http://www.cnblogs.com/ljh2000-jump/
Reprint please specify the source, infringement must investigate, retain the final interpretation right! Descriptionyou are asked to design a calculator to complete the following three tasks:1, given y,z,p, calculate the value of Y^z Mod p;2, given y,z,p, calculates the minimum non-negative integer that satisfies xy≡z (mod p);3, given y,z,p, calculates the minimum non-negative integer that satisfies the Y^x≡z (mod p). 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 integer y,z,p per line, describing a query. Outputfor each query, output 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
Positive Solution: fast power +exgcd+bsgsProblem Solving Report:Fast Power +exgcd+bsgs. there are some details. exgcd almost forgot ...Learn bsgs poke here: http://www.cnblogs.com/ljh2000-jump/p/6230999.html
1 //It's made by ljh20002#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> -#include <string> - using namespacestd; thetypedefLong LongLL; - Const intMOD =300007; - Const intMAXM =100011; - intK,p,first[mod+ A],ecnt,to[maxm],w[maxm],next[maxm],m,ans; +InlineintgcdintXintY) {if(y==0)returnXreturnGCD (y,x%y); } -InlineintFast_pow (LL x,intY) {LL r=1; while(y>0) {if(y&1) r*=x,r%=p; X*=x; X%=p; y>>=1; }return(int) R; } +InlinevoidWujie () {printf ("Orz, I cannot find x!"); } AInlineintGetint () { at intw=0, q=0;CharC=getchar (); while((c<'0'|| C>'9') && c!='-') c=GetChar (); - if(c=='-') q=1, C=getchar (); while(c>='0'&&c<='9') w=w*Ten+c-'0', C=getchar ();returnq?-w:w; - } - -InlinevoidEXGCD (LL x,ll y,ll &d,ll &a,ll &b) { - if(y==0) {d=x; a=1; b=0;return ; } inEXGCD (y,x%y,d,b,a); -b-=x/y*A; to } + -InlinevoidSolveintAintZ) { the intGCD=GCD (A,P);if(z%gcd!=0) {Wujie ();return ; } * ll X,y,gg; EXGCD (LL) A, (ll) p,gg,x,y); $Z/=GCD; P/=GCD;Panax Notoginsengans=z*x%p; Ans+=p; ans%=p; -printf"%d", ans); the } + AInlinevoidInsertintXintj) { the intCc=x; X%=mod; for(intI=first[x];i;i=next[i])if(TO[I]==CC) {w[i]=j;return ;} +NEXT[++ECNT]=FIRST[X]; first[x]=ecnt; TO[ECNT]=CC; w[ecnt]=J; - } $ $InlineintQueryintx) { - intCc=x; X%=mod; for(intI=first[x];i;i=next[i])if(TO[I]==CC)returnW[i]; - return-1; the } - WuyiInlinevoidBSGS (intAintb) { the if(a%p==0) {Wujie ();return; } - //if (b==1) {printf ("0"); return;} WuEcnt=0; memset (First,0,sizeof(first)); -M=SQRT (P);if(m*m<p) m++; LL cc=b; Insert (b,0); About for(intI=1; i<=m;i++) Cc*=a,cc%=p,insert ((int) cc,i); $Cc=1; LL cun=Fast_pow (a,m); - for(intI=1; i<=m;i++) { -Cc*=cun; Cc%=p; ans=query (cc); - if(ans==-1)Continue; Aprintf"%d", i*m-ans); + return ; the } - Wujie (); $ } the theInlinevoidWork () { the intT=getint (); K=getint ();intx, y; the while(t--) { -X=getint (); Y=getint (); p=getint (); in if(k==1) printf ("%d", Fast_pow (x, y)); the Else if(k==2) solve (x, y); the Elsebsgs (x, y); Aboutprintf"\ n"); the } the } the + intMain () - { the Work ();Bayi return 0; the}
BZOJ2242 [SDOI2011] Calculator