社区讨论
BSGS模版玄关求调
P4195【模板】扩展 BSGS / exBSGS参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mjfsbqlp
- 此快照首次捕获于
- 2025/12/21 21:50 2 个月前
- 此快照最后确认于
- 2025/12/24 19:50 2 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a,p,b;
map<int,int> mp;
inline pair<int,int> exgcd(int a,int b,int c){
if(b==0){
int x=c/a,y=0;
return {x,y};
}
pair<int,int> k=exgcd(b,a%b,c);
int x=k.second,y=k.first-a/b*x;
return {x,y};
}
inline int qpow(int a,int b){
int ans=1,base=a;
while(b){
if(b&1){
ans*=base;
ans%=p;
}
base*=base;
base%=p;
b>>=1;
}
return ans;
}
inline void work(){
if(a==0&&p==0&&b==0) exit(0);
mp.clear();
int sqr=sqrt(p),res=1;
for(int i=0;i<=sqr-1;i++){
mp[res]=i;
cerr<<res<<" "<<i<<endl;
res=res*a;
res%=p;
if(b%p==1){
cout<<0<<"\n";
return ;
}
if(mp[b%p]){
cout<<mp[b%p]<<"\n";
return ;
}
}
cerr<<"--------------"<<endl;
cout<<qpow(a,sqr)<<" ";
int now=exgcd(qpow(a,sqr),p,1).first,base=now;
cout<<qpow(a,sqr)*now<<endl;
for(int k=1;k*sqr<p;k++){
cerr<<(b*now)%p<<" "<<now<<" "<<k<<endl;
if((b*now)%p==1){
cout<<(k*sqr)%p<<"\n";
return ;
}
if(mp[(b*now)%p]){
cout<<(mp[(b*now)%p]+now)%p<<"\n";
return ;
}
now*=base;
now%=p;
}
cout<<"No Solution"<<"\n";
}
signed main(){
while(1){
cin>>a>>p>>b;
work();
}
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...