BZOJ2242 [SDOI2011]计算器

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

 

 

 

本文作者:ljh2000

 

作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 

 

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
 
 
正解:快速幂+exgcd+BSGS
解题报告:
  快速幂+exgcd+BSGS。
  有一些细节。exgcd都快忘了......
  学习BSGS戳这里:http://www.cnblogs.com/ljh2000-jump/p/6230999.html
  
  
 1 //It is made by ljh2000
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 #include <string>
14 using namespace std;
15 typedef long long LL;
16 const int MOD = 300007;
17 const int MAXM = 100011;
18 int k,p,first[MOD+12],ecnt,to[MAXM],w[MAXM],next[MAXM],m,ans;
19 inline int gcd(int x,int y){ if(y==0) return x; return gcd(y,x%y); }
20 inline int fast_pow(LL x,int y){ LL r=1; while(y>0) { if(y&1) r*=x,r%=p; x*=x; x%=p; y>>=1;  } return (int)r; }
21 inline void wujie(){ printf("Orz, I cannot find x!"); }
22 inline int getint(){
23     int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
24     if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
25 }
26 
27 inline void exgcd(LL x,LL y,LL &d,LL &a,LL &b){
28     if(y==0) { d=x; a=1; b=0; return ; }
29     exgcd(y,x%y,d,b,a);
30     b-=x/y*a;
31 }
32 
33 inline void solve(int a,int Z){
34     int GCD=gcd(a,p); if(Z%GCD!=0) { wujie(); return ; }
35     LL x,y,GG; exgcd((LL)a,(LL)p,GG,x,y);
36     Z/=GCD; p/=GCD;
37     ans=Z*x%p; ans+=p; ans%=p;
38     printf("%d",ans);
39 }
40 
41 inline void insert(int x,int j){
42     int cc=x; x%=MOD; for(int i=first[x];i;i=next[i]) if(to[i]==cc) { w[i]=j; return ;}
43     next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=cc; w[ecnt]=j; 
44 }
45 
46 inline int query(int x){
47     int cc=x; x%=MOD; for(int i=first[x];i;i=next[i]) if(to[i]==cc) return w[i];
48     return -1;
49 }
50 
51 inline void BSGS(int a,int b){
52     if(a%p==0) { wujie(); return; }
53     //if(b==1) { printf("0"); return ; }
54     ecnt=0; memset(first,0,sizeof(first)); 
55     m=sqrt(p); if(m*m<p) m++; LL cc=b; insert(b,0);
56     for(int i=1;i<=m;i++) cc*=a,cc%=p,insert((int)cc,i);
57     cc=1; LL cun=fast_pow(a,m);
58     for(int i=1;i<=m;i++) {
59         cc*=cun; cc%=p;    ans=query(cc);
60         if(ans==-1) continue;
61         printf("%d",i*m-ans); 
62         return ;
63     }
64     wujie();
65 }
66 
67 inline void work(){
68     int T=getint(); k=getint(); int x,y;
69     while(T--) {
70         x=getint(); y=getint(); p=getint();
71         if(k==1) printf("%d",fast_pow(x,y));
72         else if(k==2) solve(x,y);
73         else BSGS(x,y);
74         printf("\n");
75     }
76 }
77 
78 int main()
79 {
80     work();
81     return 0;
82 }

 

posted @ 2016-12-28 22:37  ljh_2000  阅读(307)  评论(0编辑  收藏  举报