某些坑已经挖好…随时回来填补
勒让德符号:https://www.bk.gugeso.site/wiki/%E5%8B%92%E8%AE%A9%E5%BE%B7%E7%AC%A6%E5%8F%B7
关于雅可比符号的介绍较少,大致意思就是,当p不为质数的时候,我们需要根据唯一分解定理把p分解成数个质数相乘,分别计算答案
接下来 参考了这位大佬的博客 (基本全抄) https://blog.csdn.net/codeswarrior/article/details/81626573
下面是模板代码 模板根据此题所写:https://blog.csdn.net/codeswarrior/article/details/81626573 (还是抄的)
题目大概意思:求 J(a,n) ,即告诉你a,n,问你x²=a(mod n)是否有解
当模数不是质数的时候,推广勒让德定理,即雅可比定理
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;//记得根据题目要求改
bool isprime[maxn];
int prime[maxn],tot;
void init(){//素数筛
tot = 0;
memset(isprime,true,sizeof(isprime));
isprime[0] = isprime[1] = false;
for(int i = 2; i < maxn; i++){
if(isprime[i]){
prime[tot++] = i;
for(int j = i + i; j < maxn; j += i){
isprime[j] = false;
}
}
}
}
int q_pow(int a,int n,int m){//快速幂取模
ll ans = 1;
ll x = a;
while(n){
if(n & 1)
ans = ans * x % m;
x = x * x % m;
n >>= 1;
}
return ans % m;
}
int solve(int a,int p){//二次剩余的定理
if(a % p == 0) return 0;
return q_pow(a,(p-1)/2,p) == 1 ? 1 : -1;
}
int main(){
int a,n;
init();
while(~scanf("%d%d",&a,&n)){//x² = a (mod n)
int ans = 1;
if(!isprime[n]){//雅可比定理相关计算
for(int i = 0; i < tot && prime[i] <= n; i++){//唯一分解定理
if(n % prime[i]) continue;
int cnt = 0;
while(n % prime[i] == 0){
cnt++;
n /= prime[i];
}
int t = solve(a,prime[i]);
if(t == -1 && cnt % 2 == 0) t = 1;//注意个数如果是-1并且偶数个相乘那就成1了
ans *= t;
}
}
else{
ans = solve(a,n);//如果n为质数,直接套用勒让德定理即可
}
printf("%d\n",ans);
}
return 0;
}
至于要对x进行求解,继续搬运大佬博客 https://blog.csdn.net/a_crazy_czy/article/details/51959546
板子很简单,并没有无法理解的代码上的问题,只要理解大佬博客中的数学证明,就很好写
板子如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int p = 1e9+7;
LL w,b,c;
struct Inumber{ //表示复数a+bi (i=sqrt(w))
LL a,b;
};
inline Inumber operator*(Inumber x,Inumber y){ //复数乘法
return (Inumber){(x.a*y.a+x.b*y.b%p*w)%p,(x.a*y.b+x.b*y.a)%p};
}
LL qpow(LL a,LL x,LL mod){
LL res = 1;
while(x){
if(x&1) res = res*a%mod;
a = a*a%mod;
x >>= 1;
}
return res;
}
Inumber iqpow(Inumber A,LL x){ //复数快速幂
Inumber res = (Inumber){1,0};
while(x){
if(x&1) res = res*A;
A = A*A;
x >>= 1;
}
return res;
}
int Legendre(LL a,LL p){
if(qpow(a,(p-1)>>1,p)==1) return 1;
else return -1;
}
LL solve(LL n,LL mod){ //求解x^2 = n (mod p)
LL a;
if(n == 0) return 0;
if(Legendre(n,mod) == -1) return -1;
while(true){
a = rand()%mod;
w = (a*a%mod-n+mod)%mod;
if(Legendre(w,mod) == -1) break;
}
Inumber ans = iqpow((Inumber){a,1},(p+1)>>1);
return ans.a%mod;
}
int main(){
srand(time(0));
//以下内容均根据题目要求改动
//solve(n,p) 并对其结果 +p mod p 即可求出 x*x=n(mod p)的最小正整数解
//这里p只能是奇素数
int t; cin >> t;
LL inv2 = qpow(2,p-2,p);
while(t--){
cin >> b >> c;
LL x = solve(((b*b-4*c)%p+p)%p,p);
if(x == -1) puts("-1 -1");
else{
LL ansx = ((b-x)%p+p)%p*inv2%p;
LL ansy = (b+x)%p*inv2%p;
cout << min(ansx,ansy) << " " << max(ansx,ansy) << '\n';
}
}
return 0;
}