http://www.elijahqi.win/archives/3020
Description
你被要求设计一个计算器完成以下三项任务:
1、给定y,z,p,计算Y^Z Mod P 的值;
2、给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数;
3、给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数。
Input
输入包含多组数据。
第一行包含两个正整数T,K分别表示数据组数和询问类型(对于一个测试点内的所有数据,询问类型相同)。
以下行每行包含三个正整数y,z,p,描述一个询问。
Output
对于每个询问,输出一行答案。对于询问类型2和3,如果不存在满足条件的,则输出“Orz, I cannot find x!”,注意逗号与“I”之间有一个空格。
Sample Input
【样例输入1】
3 1
2 1 3
2 2 3
2 3 3
【样例输入2】
3 2
2 1 3
2 2 3
2 3 3
【数据规模和约定】
对于100%的数据,1<=y,z,p<=10^9,为质数,1<=T<=10。
Sample Output
【样例输出1】
2
1
2
【样例输出2】
2
1
0
HINT
Source
第一轮day1
第一问直接快速幂
第二问 直接把y除到另一边
第三问 bsgs直接算即可
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if(T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
int T,k;
inline int ksm(ll b,int t,int p){static ll tmp;tmp=1;
for (;t;(b*=b)%=p,t>>=1) if(t&1) (tmp*=b)%=p;return tmp;
}
namespace sol1{
int y,z,p;
inline void gao(){
while(T--){
y=read();z=read();p=read();
printf("%d\n",::ksm(y,z,p));
}
}
}
namespace sol2{//Orz, I cannot find x!
int y,z,p;
inline void gao(){
while(T--){
y=read();z=read();p=read();
if (y%p==0&&z) {puts("Orz, I cannot find x!");continue;}
printf("%lld\n",(ll)::ksm(y,p-2,p)*z%p);
}
}
}
namespace sol3{
const int md=1e5+3;int a,b,p,num;
struct node{int x,id,next;}hs[md];int h[md];
inline void init(){num=0;memset(h,0,sizeof(h));}
inline void insert1(int x,int id){static int tmp;tmp=x%md;
for (int i=h[tmp];i;i=hs[i].next) if (hs[i].x==x) return;
hs[++num]=(node){x,id,h[tmp]};h[tmp]=num;
}
inline int query(int x){static int tmp;tmp=x%md;
for (int i=h[tmp];i;i=hs[i].next) if (hs[i].x==x) return hs[i].id;
return -1;
}
inline void gao(){
while(T--){static ll tmp,ans,tmp1;static int inva;
a=read(),b=read();p=read();ans=-1;
if (a%p==0&&b) {puts("Orz, I cannot find x!");continue;}
int m=ceil(sqrt(p));init();tmp=b%p;inva=::ksm(a,p-2,p);
for (int i=0;i<m;++i) insert1(tmp,i),(tmp*=inva)%=p;
tmp=1;tmp1=::ksm(a,m,p);
for (int i=0;i<=m;++i){
int j=query(tmp);
if(j!=-1) {ans=i*m+j;break;}(tmp*=tmp1)%=p;
}if (ans==-1) puts("Orz, I cannot find x!");else printf("%lld\n",ans);
}
}
}
int main(){
// freopen("bzoj2242.in","r",stdin);
T=read(),k=read();
if (k==1) {sol1::gao();return 0;}
if (k==2) {sol2::gao();return 0;}
if (k==3) {sol3::gao();return 0;}
return 0;
}