10分求助
查看原帖
10分求助
999274
CNS_5t0_0r2楼主2023/7/4 15:46
#include<bits/stdc++.h>
#define int long long
using namespace std;
map<int,int> Hash;
struct triple{
	int x,y,z;
	triple(int _x_,int _y_,int _z_){x = _x_;y = _y_;z = _z_;}
};
triple exgcd(int a,int b){
	if(b == 0)
		return triple(1,0,a);
	triple last = exgcd(b,a % b);
	return triple(last.y,last.x - a / b * last.y,last.z);
}
int BSGS(int a,int b,int c){
	int m = (int)ceil(sqrt(c));
	Hash.clear();
	int base = 1;
	for(int i = 0;i < m;i++){
		Hash[base] = i;
		base = base * a % c;
	}
	int d;
	for(int i = 0;i < m;i++){
		triple res = exgcd(d,c);
		int C = c / res.z;
		res.x = (res.x * b / res.z % C + C) % C;
		int j = Hash[res.x];
		if(j != 0)
			return i * m + j;
		d = d * base % c;
	}
	return -1;
}
int p,b,n;
signed main(){
	cin >> p >> b >> n;
	int ans = BSGS(b,n,p);
	if(ans == -1)
		cout << "no solution";
	else
		cout << ans;
	return 0;
}
2023/7/4 15:46
加载中...