跳到正文

题解

题解 P5605 【小A与两位神仙】

小粉兔约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 9 分钟
获赞
19
题目
P5605

版本

  1. v1当前

题意简述:

给定正整数 mm,保证 m=pkm = p^k(p∈P\{2}p \in \mathbb{P} \backslash \{ 2 \},k≥1k \ge 1,m≤1018m \le {10}^{18})。

接下来给出 nn 组独立的询问,每组询问给定两个正整数 x,yx, y(1≤x,y<m1 \le x, y < m 且 x,y⊥mx, y \perp m),询问是否存在非负整数 aa 满足 xa≡y(modm)x^a \equiv y \pmod{m}。

题解:

因为 mm 是奇质数的次幂,所以一定存在原根 gg(满足 gg 的 00 到 φ(m)−1\varphi(m) - 1 次幂可以组成模 mm 的简化剩余系,即不重复不遗漏地遍历了所有可能的值)。

记 indg a\mathrm{ind}_g\,a 为满足 gk≡a(modm)g^k \equiv a \pmod{m},且 0≤k<φ(m)0 \le k < \varphi(m) 的 kk,即 aa 模 mm 对原根 gg 的指标。可以看作是以 kk 为自变量的 fg(k)=gk mod mf_g(k) = g^k \bmod m 的反函数。

关于指标有运算法则:indg(ab)≡indg a+indg b(modφ(m))\mathrm{ind}_g(ab) \equiv \mathrm{ind}_g\,a + \mathrm{ind}_g\,b \pmod{\varphi(m)},相当于对数函数,可以把模意义下的乘法化乘为加。

同时我们有集合 Sa={a0,a1,a2,a3,…}S_a = \{ a^0, a^1, a^2, a^3, \ldots \} 为 aa 的次幂组成的集合。显然集合内的数的指标均是 indg a\mathrm{ind}_g\,a 的倍数(模 φ(m)\varphi(m) 意义下)。

不难发现有 ∣Sa∣=φ(m)gcd⁡(indg a,φ(m))|S_a| = \dfrac{\varphi(m)}{\gcd(\mathrm{ind}_g\,a, \varphi(m))},且 SaS_a 中的数的指标都是 gcd⁡(indg a,φ(m))\gcd(\mathrm{ind}_g\,a, \varphi(m)) 的倍数。

特别地,SaS_a 的大小被称为 aa 模 mm 的阶,记作 ord a\mathrm{ord}\,a,同时它也等价于满足 ak≡1(modm)a^k \equiv 1 \pmod{m} 的最小正整数 kk。

说了这么多,结合上述结论,可以得到:若存在非负整数 aa 满足 xa≡y(modm)x^a \equiv y \pmod{m} 当且仅当 ord b∣ord a\mathrm{ord}\,b \mid \mathrm{ord}\,a。

接下来转化为求阶的问题。阶都是 φ(m)\varphi(m) 的因数,将 φ(m)\varphi(m) 分解质因数并试除即可。

使用 Pollard-Rho 算法分解质因数,快速幂时需要使用 __int128。

时间复杂度为期望 O(T(Pollard−Rho)+n ω(m)log⁡m)\mathcal{O}(\mathcal{T}(\mathrm{Pollard-Rho}) + n \: \omega(m) \log m),代码如下:

C++
#include <cstdio>
#include <cmath>
#include <random>
#include <chrono>
#include <algorithm>

std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> inline T range(T l, T r) { return std::uniform_int_distribution<T>(l, r)(rng); }

typedef long long LL;
typedef __int128 LLL;

inline LL qPow(LL b, LL e, LL m) {
	if (!m) {
		#define Lim 0x3f3f3f3f3f3f3f3f
		LL a = 1;
		for (; e; e >>= 1, b = Lim / b >= b ? b * b : Lim)
			if (e & 1) a = Lim / a >= b ? a * b : Lim;
		return a;
		#undef Lim
	}
	LL a = 1;
	for (; e; e >>= 1, b = (LLL)b * b % m)
		if (e & 1) a = (LLL)a * b % m;
	return a;
}
inline LL Abs(LL x) { return x < 0 ? -x : x; }
LL Gcd(LL a, LL b) { return b ? Gcd(b, a % b) : a; }

LL Mod, Phi, Prime; int Alpha;
inline LL KthRt(LL X, int K) {
	LL V = pow(X, 1. / K);
	while (qPow(V, K, 0) <= X) ++V;
	while (qPow(V, K, 0) > X) --V;
	return V;
}
void Get(LL X) {
	Alpha = 1;
	for (int i = 2; i <= 59; ++i) if (qPow(KthRt(X, i), i, 0) == X) Alpha = i;
	Prime = KthRt(X, Alpha);
}

LL Prs[65]; int Prc;
inline bool MillerRabin(LL x, LL b) {
	LL k = x - 1;
	while (k) {
		LL cur = qPow(b, k, x);
		if (cur != 1 && cur != x - 1) return 0;
		if (k & 1 || cur == x - 1) return 1;
		k >>= 1;
	} return 1;
}
inline bool isPrime(LL x) {
	if (x == 46856248255981ll || x < 2) return 0;
	if (x == 2 || x == 3 || x == 7 || x == 61 || x == 24251) return 1;
	return MillerRabin(x, 2) && MillerRabin(x, 3) && MillerRabin(x, 7) && MillerRabin(x, 61) && MillerRabin(x, 24251);
}
inline LL Func(LL x, LL b, LL m) { return ((LLL)x * x + b) % m; }
inline LL PollardRho(LL x) {
	LL s = 0, t = 0, c = range(1ll, x - 1), val = 1;
	int stp = 0, goal = 1;
	for (goal = 1; ; goal <<= 1, s = t, val = 1) {
		LL d;
		for (stp = 1; stp <= goal; ++stp) {
			t = Func(t, c, x);
			val = (LLL)val * Abs(t - s) % x;
			if (stp % 127 == 0)
				if ((d = Gcd(val, x)) > 1) return d;
		}
		if ((d = Gcd(val, x)) > 1) return d;
	}
}
void Factorize(LL Num) {
	if (isPrime(Num)) { Prs[++Prc] = Num; return ; }
	LL d = Num;
	while (d == Num) d = PollardRho(Num);
	Factorize(d), Factorize(Num / d);
}

inline LL ord(LL x) {
	LL ans = Phi;
	for (int i = 1; i <= Prc; ++i) if (qPow(x, ans / Prs[i], Mod) == 1) ans /= Prs[i];
	return ans;
}

int main() {
	int Q;
	scanf("%lld%d", &Mod, &Q);
	Get(Mod);
	Phi = Mod / Prime * (Prime - 1);
	Factorize(Phi);
	std::sort(Prs + 1, Prs + Prc + 1);
	int T = 0, cnt = 0; LL lst = 0;
	for (int i = 1; i <= Prc; ++i) {
		if (Prs[i] != lst) {
			if (lst) {
				int k = 31 - __builtin_clz(cnt);
				Prs[++T] = qPow(lst, cnt - (1 << k) + 1, 0);
				for (int j = 1; j <= k; ++j) Prs[++T] = qPow(lst, 1 << (k - j), 0);
			}
			lst = Prs[i], cnt = 0;
		} ++cnt;
	} {
		int k = 31 - __builtin_clz(cnt);
		Prs[++T] = qPow(lst, cnt - (1 << k) + 1, 0);
		for (int j = 1; j <= k; ++j) Prs[++T] = qPow(lst, 1 << (k - j), 0);
	} Prc = T;
//	printf("Phi = %lld = ", Phi);
//	for (int i = 1; i <= Prc; ++i) printf("%lld%s", Prs[i], i == Prc ? "\n" : " * ");
	while (Q--) {
		LL A, B;
		scanf("%lld%lld", &A, &B);
		A = ord(A), B = ord(B);
		puts(A % B ? "No" : "Yes");
	}
	return 0;
}
原文