题解
- p =2 特判a,b
- p!=2, 左边乘4->
(2x+a)2=a2−4b(modp)
所以有解的话a^2-4*b是模p的二次剩余,
此时设t^2 = a^2-4*b(mod p)
则2*x+a=t(mod p)
因为gcd(2, p) = 1, 扩展欧几里得定理可得一定有解
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a, b, p;
ll qpow(ll A, ll B, ll mod)
{
ll res = 1;
while(B)
{
if(B & 1) res = res * A % mod;
A = A * A % mod;
B >>= 1;
}
return res;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld%lld", &a, &b, &p);
a %= p;
b %= p;
ll t = ((a * a - 4 * b) % p + p) % p;
if(p == 2) puts(a && b ? "No" : "Yes");
else puts(t == 0 || qpow(t, (p - 1) / 2, p) == 1 ? "Yes" : "No");
}
return 0;
}