50pts WA求助,问过好多次了
查看原帖
50pts WA求助,问过好多次了
999274
CNS_5t0_0r2楼主2023/8/24 16:48
#include<bits/stdc++.h>
#define int long long
using namespace std;
map<int,int> Hash;
struct triple{
	int x,y,z;
	triple(){}
	triple(int _x_,int _y_,int _z_){x = _x_;y = _y_;z = _z_;}
};
int GCD(int a,int b){
    return b ? GCD(b,a % b) : a;
}
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 exBSGS(int a,int b,int c){
	if (b == 1 || c == 1)
		return 0;
	int tmp = 1,cnt = 0,d = 1;
	for(int i = 0;i <= 32;i++){
		if(tmp == b)
			return i;
		tmp = tmp * a % c; 
	}
	for(int res;(res = GCD(a,c)) != 1;cnt++){
		if(b % res)
			return -1;
		b /= res;
		c /= res;
		d = d * a / res % c;
	}
	int m = (int)ceil(sqrt(c));
	Hash.clear();
	int base = 1;
	for(int i = 0;i * i < c;i++){
		Hash[base] = i;
		base = base * a % c;
	}
	int j = 0;
	for(int i = 0;i * i < c;i++){
		triple res = exgcd(d,c);
		int C = c / res.z;
		res.x = (res.x * b / res.z % C + C) % C;
		j = Hash[res.x]; 
		if(j != 0)
			return i * m + j + cnt;
		d = d * base % c;
	}
	return -1;
}
int a,b,p;
signed main(){
	while(1){
		scanf("%lld%lld%lld", &a, &p, &b);
		if(a == 0 && p == 0 && b == 0)
			return 0;
		a %= p;
		b %= p;
		int ans = exBSGS(a,b,p);
		if(ans == -1)
			printf("No Solution\n");
		else
			printf("%lld\n",ans % p);
	}
	return 0;
}
2023/8/24 16:48
加载中...