WA #11求助
查看原帖
WA #11求助
450237
Calvin0221楼主2022/7/18 14:51
#include<bits/stdc++.h>
#define int long long
#define ll long long
#include<map>
using namespace std;
const int inf = 1e9;
int a,b,p,res;
int get_mod(int x,int p){
	return (x % p + p) % p;
}
int exgcd(int a,int b,int& x,int& y){
	if(b == 0){
		x = 1,y = 0;
		return a;
	}
	int t = exgcd(b,a%b,y,x);
	y -= a / b * x;
	return t;
}
int bsgs(int a,int b,int p){
	if(b % p == 1 % p) return 0;
	int tmp = floor(sqrt(p));
	map<int,int> Hash;
	int cnt = b % p,t = 1;
	for(int i=0;i<tmp;i++) Hash[cnt] = i,cnt = (ll)cnt * a % p;
	for(int i=0;i<tmp;i++) t = (ll)t * a % p;
	int tot = t;
	for(int i=1;i<=tmp;i++){
		if(Hash.count(tot)) return i * tmp - Hash[tot];
		tot = (ll)tot * t % p;
	}
	return -inf;
}
int extend_bsgs(int a,int b,int p){
	b = get_mod(b,p);
	if(b % p == 1 % p) return 0;
	int x,y;
	int t = exgcd(a,p,x,y);
	if(t > 1){
		if(b % t != 0) return -inf;
		int p0 = p / t;
		return extend_bsgs(a,(ll)b / t * x % p0,p0) + 1;
	}
	return bsgs(a,b,p);
}
signed main(){
	while(scanf("%lld%lld%lld",&a,&p,&b),a || p || b){
		res = extend_bsgs(a,b,p);
		if(res < 0) puts("No Solution");
		else printf("%lld\n",res);
	}
	return 0;
}

2022/7/18 14:51
加载中...