社区讨论

建议加强数据

P4195【模板】扩展 BSGS / exBSGS参与者 11已保存回复 11

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
11 条
当前快照
1 份
快照标识符
@locny80r
此快照首次捕获于
2023/10/30 16:54
2 年前
此快照最后确认于
2023/11/05 03:54
2 年前
查看原帖
普通 BSGS 可以通过本题。。。
代码:
CPP
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <unordered_map>
#define int unsigned long long

typedef __int128 _;
struct hash {
	static int nxt(int x) {
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	int operator()(int x) const {
		static const int rnd =
			std::chrono::steady_clock::now().time_since_epoch().count();
		return nxt(x + rnd);
	}
};

int pw(int x, int y, int p) {
	int r = 1;
	for (; y; y >>= 1, x = x * x % p)
		if (y & 1) r = r * x % p;
	return r;
}

int bsgs(int a, int b, int p) {
	std::unordered_map<int, int, hash> mp;
	int m = ceil(sqrtl(p)), t = 1;
	for (int i = 0; i < m; i++, (t *= a) %= p) mp[t * b % p] = i;
	for (int i = 1, j = t; i <= m; i++, (j *= t) %= p)
		if (mp.count(j)) return i * m - mp[j];
	return -1ull;
}

int a, p, b;
signed main() {
	while (scanf("%llu%llu%llu", &a, &p, &b) && (a || p || b)) {
		int ans = bsgs(a, b, p);
		if (ans == -1ull || pw(a, ans, p) != b)
			puts("No Solution");
		else
			printf("%llu\n", ans);
	}
}

回复

11 条回复,欢迎继续交流。

正在加载回复...