【悬关】60pts求助
查看原帖
【悬关】60pts求助
785639
xuchuhan楼主2024/4/6 15:43

RT,BSGS部分有误

#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,K;
int Fpow(int a,int b,int p){
	int ans=1;
	while(b){
		if(b&1)
			ans=ans*a%p;
		a=a*a%p;
		b/=2;
	}
	return ans;
}
unordered_map<int,int>f;
int BSGS(int a,int b,int p){
	f.clear();
	int t=ceil(sqrt(p*1.0)),mul=1;
	for(int i=1;i<=t;i++){
		mul=(mul*a)%p;
		f[b*mul%p]=i;
	}
	int tt=Fpow(a,t,p);
	mul=1;
	for(int i=1;i<=t;i++){
		mul=mul*tt%p;
		if(f[mul])
			return i*t-f[mul];
	}
	return -1;
}
signed main(){
	cin>>T>>K;
	if(K==1){
		while(T--){
			int y,z,p;
			cin>>y>>z>>p;
			cout<<Fpow(y,z,p)<<"\n";
		}
	}
	else if(K==2){
		while(T--){
			int y,z,p;
			cin>>y>>z>>p;
			if(y%p==0){
				cout<<"Orz, I cannot find x!\n";
				continue;
			}
			cout<<Fpow(y,p-2,p)*z%p<<"\n";
		}
	}
	else{
		while(T--){
			int y,z,p;
			cin>>y>>z>>p;
			if(BSGS(y,z,p)>=0)
				cout<<BSGS(y,z,p)<<"\n";
			else
				cout<<"Orz, I cannot find x!\n";
		}
	}
	return 0;
}
2024/4/6 15:43
加载中...