蒟蒻求助 TLE on #11
查看原帖
蒟蒻求助 TLE on #11
461359
huangrenheluogu楼主2023/12/28 20:45

rt,把原来的 unordered_map 换成 hash,就挂了。

struct Hash{
	int P = 1e7;
	int fir[N], nxt[N], val[N], cnt, to[N];
	inline void insert(int x, int y){
		int xx = x % P;
		if(fir[xx] == 0){
			fir[xx] = ++cnt;
			val[cnt] = x, to[cnt] = y;
			return ;
		}
		for(int i = fir[xx]; i ; ){
			if(val[i] == x){
				to[i] = y;
				return ;
			}
			if(nxt[i] == 0){
				nxt[i] = ++cnt;
				val[cnt] = x, to[cnt] = y;
				return ;
			}
			i = nxt[i];
		}
	}
	inline int query(int x){
		int xx = x % P;
		for(int i = fir[xx]; i ; i = nxt[i]){
			if(val[i] == x) return to[i];
		}
		return -1;
	}
	inline void clear(){
		for(int i = 1; i <= cnt; i++){
			fir[val[i] % P] = 0;
			val[i] = to[i] = 0;
		}
	}
}f;

完整代码:

//Extened Baby Step Giant Step
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e7 + 5;
int a, p, b, cnt, x, y, gcd, S, now, inv, P;
struct Hash{
	int P = 1e7;
	int fir[N], nxt[N], val[N], cnt, to[N];
	inline void insert(int x, int y){
		int xx = x % P;
		if(fir[xx] == 0){
			fir[xx] = ++cnt;
			val[cnt] = x, to[cnt] = y;
			return ;
		}
		for(int i = fir[xx]; i ; ){
			if(val[i] == x){
				to[i] = y;
				return ;
			}
			if(nxt[i] == 0){
				nxt[i] = ++cnt;
				val[cnt] = x, to[cnt] = y;
				return ;
			}
			i = nxt[i];
		}
	}
	inline int query(int x){
		int xx = x % P;
		for(int i = fir[xx]; i ; i = nxt[i]){
			if(val[i] == x) return to[i];
		}
		return -1;
	}
	inline void clear(){
		for(int i = 1; i <= cnt; i++){
			fir[val[i] % P] = 0;
			val[i] = to[i] = 0;
		}
	}
}f;
inline int exgcd(int a, int b, int &x, int &y){
	if(!b){
		x = 1, y = 0;
		return a;
	}
	int gcd = exgcd(b, a % b, x, y);
	int xx = x, yy = y;
	x = yy, y = xx - a / b * yy;
	return gcd;
}
inline void read(int &x){
	x = 0;
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	while('0' <= c && c <= '9'){
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
}
inline void exBSGS(){
	cnt = 0ll;
	inv = 1ll;
	if(b == 1){
		puts("0");
		return ;
	}
	while((gcd = exgcd(a, p, x, y)) != 1){
		if(b % gcd != 0){
			puts("No Solution");
			return ;
		}
		p /= gcd, b /= gcd, cnt++;
		x = (x % p + p) % p;
		inv = 1ll * inv * (a / gcd) % p;
		if(inv == b){
			printf("%lld\n", cnt);
			return ;
		}
	}
	exgcd(inv, p, x, y);
	x = (x % p + p) % p;
	b = 1ll * b * x % p;
	if(b == 0){
		if(p != 1){
			puts("No Solution");
			return ;
		}
		printf("%lld\n", cnt);
		return ;
	}
	f.clear();
	S = sqrt(p);
	now = 1ll;
	for(int i = 1; i <= S; i++){
		now = 1ll * now * a % p;
//		printf("%lld\n", now);
		f.insert(1ll * now * b % p, i);
//		printf("%lld\n", 1ll * now * b % p);
	}
	a = now;
	now = 1;
	for(int i = S; i <= p + S - 1; i += S){
		now = 1ll * now * a % p;
		if(f.query(now) != -1){
//			printf("%lld %lld\n", now, f[now]);
			printf("%lld\n", (i - f.query(now) + cnt) % P);
			return ;
		}
	}
	puts("No Solution");
	return ;
}
signed main(){
//	freopen("data.in", "r", stdin);
//	freopen("P4195_11.in", "r", stdin);
//	freopen("code.out", "w", stdout);
	read(a), read(p), read(b);
	while(a + p + b){
		P = p;
		a %= p, b %= p;
		exBSGS();
		read(a), read(p), read(b);
	}
	return 0;
}
//38 20 48
2023/12/28 20:45
加载中...