题目链接:2242:[SDOI2011]计算器
第一问快速幂,第二问扩展欧几里得,第三问BSGS
注意BSGS时要判断y为p的倍数时如果z不为0是无解的,wa了好久
为什么我跑得这样慢QAQ……
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int mod=1331;
const int maxn=1000010;
int T,typ;
struct hashmap{
int next[maxn],tot,As[maxn],h[mod];
LL val[maxn];
void init(){
memset(h,0,sizeof(h));tot=0;
}
void push(LL x,int id){
int now=x%mod; if (now<0) now+=mod;
for (int i=h[now];i;i=next[i])
if (val[i]==x) return;
++tot; next[tot]=h[now]; h[now]=tot;
val[tot]=x; As[tot]=id; return;
}
int find(LL x){
int now=x%mod; if (now<0) now+=mod;
for (int i=h[now];i;i=next[i])
if (val[i]==x) return As[i];
return -1;
}
}s;
LL powe(LL x,LL y,LL p){
LL ret=1;
while (y){
if (y&1) ret=ret*x%p;
x=x*x%p; y>>=1;
}return ret;
}
LL gcd(LL a,LL b){
return b==0?a:gcd(b,a%b);
}
void exgcd(LL a,LL b,LL &x,LL &y){
LL t;
if (!b){x=1;y=0;return;}
exgcd(b,a%b,x,y);
t=x; x=y; y=t-a/b*y;
}
void solve1(){
LL a,b,p;
for (int i=1;i<=T;++i){
scanf("%lld%lld%lld",&a,&b,&p);
printf("%lld\n",powe(a,b,p));
}
}
void solve2(){
LL x,y,a,b,p;
for (int i=1;i<=T;++i){
scanf("%lld%lld%lld",&a,&b,&p);
LL G=gcd(a,p);
if (b%G!=0)
{printf("Orz, I cannot find x!\n");continue;}
a/=G; b/=G; p/=G;
exgcd(a,p,x,y);
x=(x*b%p+p)%p;
while(x<0)x+=p;
printf("%lld\n",x);
}
}
void BSGS(LL a,LL b,LL p){
if (a%p==0)//!
{printf("Orz, I cannot find x!\n");return;}
s.init();
int m=ceil(sqrt(p));
LL last=1; s.push(1,0);
for (int i=1;i<m;++i){
last=(last*a%p+p)%p;
s.push(last,i);
}
LL x,y,ans; exgcd(1,p,x,y);
x=(x*b%p+p)%p;
if (s.find(x)!=-1)
{ans=0*m+s.find(x);printf("%lld\n",ans);return;}
LL D=last*a%p,tmp=D;
for (int i=1;i<=m;++i){
exgcd(D,p,x,y);
x=(x*b%p+p)%p;
if (s.find(x)!=-1)
{ans=i*m+s.find(x);printf("%lld\n",ans);return;}
D=(D*tmp%p+p)%p;
}printf("Orz, I cannot find x!\n");
}
void solve3(){
LL a,b,p;
for (int i=1;i<=T;++i){
scanf("%lld%lld%lld",&a,&b,&p);
BSGS(a,b,p);
}
}
int main(){
scanf("%d%d",&T,&typ);
if (typ==1) solve1();
if (typ==2) solve2();
if (typ==3) solve3();
}