https://www.luogu.com.cn/record/194028549
#include <bits/stdc++.h>
#define int long long
#define F(i, a, b) for (int i = (a); i <= (b); i++)
#define dF(i, a, b) for (int i = (a); i >= (b); i--)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int qpow(int a, int b, int mod) {
int res = 1;
for (; b; b >>= 1, a = a * a % mod)
if (b & 1) res = res * a % mod;
return res;
}
int i2, P;
struct num {
int x, y;
friend num operator * (num a, num b) {
return (num){(a.x * b.x % P + a.y * b.y % P * i2 % P) % P,
(a.x * b.y % P + a.y * b.x % P) % P};
}
};
int Cipolla(int n, int p) {
int a = rnd() % p, b = p - 1 >> 1;
if (qpow(n, b, p) == p - 1) return -1;
while (!a || qpow((a * a % p - n + p) % p, b, p) == 1)
a = rnd() % p;
i2 = (a * a - n + p) % p, P = p;
num x, y;
x = {1, 0}, y = {a, 1}, b = p + 1 >> 1;
for (; b; b >>= 1, y = y * y)
if (b & 1) x = x * y;
return x.x;
}
void solve(int TC) {
int n, p, ans1, ans2;
cin >> n >> p;
ans1 = Cipolla(n, p);
if (ans1 == -1) {
cout << "Hola!\n";
return;
}
ans2 = (p - ans1) % p;
if (ans1 > ans2) swap(ans1, ans2);
if (ans1 == ans2) cout << ans1 << '\n';
else cout << ans1 << ' ' << ans2 << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
for (int i = 1; i <= T; i ++) {
solve(i);
}
return 0;
}
Cipolla 的随机部分跑不出来了?