P6610 [Code+#7] 同余方程(二次剩余)

题意

给定 p,x,求满足 a2+b2x(modp) 的解的组数,保证 p 为若干奇素数的乘积且 μ(p)0

n105,p107

前置知识

二次剩余综合题。

首先二次剩余有一个重要的符号 勒让德符号(ap),这个东西在当 a 在模 p 意义下是二次剩余时取 1,是二次非剩余取 1,若 p|a0

勒让德符号的性质:

  • (ap)+1x2a(modp) 的解的组数(x 和相反数都是一组解)。
  • 完全积性a,b,(ap)(bp)=(abp)
  • 完全积性的推论a,b,(ab2p)=(ap)

感性理解:整数 x 除以一个完全平方因子,不改变 x 的二次剩余性。
证明:根据完全积性,(ap)(b2p)=(ab2p),而 (b2p) 显然为 1

以下都令 p 是奇素数

  • 欧拉判别式:当 p 是奇素数时,(ap)ap12(modp)
  • 求和公式a=1p1(ap)=0,证明是如下引理。

引理:模 p 意义下二次剩余和非二次剩余的数量均为 p12

证明可以参考 OI Wiki,或者参考 Alex_Wei 的笔记,在此引用后者的一段话:

所有原根的偶数次幂和所有原根的奇数次幂将 1p1 分成了两类,偶数类对应二次剩余,奇数类对应二次非剩余。

分析

考虑换元,令 c=a2,d=b2,则原题转化为 c+dx(modp) 的组数,显然这个方程只需要让 c+dx 成立即可成立,那么我们只需要保证 c,d 是模 p 意义下的二次剩余,那么原题又转化为求

c+dx((cp)+1)((dp)+1)

也即求

c=0p1((cp)+1)((xcp)+1)

展开并使用完全积性:

c=0p1(cxc2p)+(cp)+(xcp)+1

把 sigma 拆开:

p+c=0p1(cxc2p)+c=0p1(cp)+c=0p1(xcp)

后两者根据求和公式显然都等于 0+(0p)=0,且 c=0(cxc2p)=0,那么我们要求的只剩下:

p+c=1p1(cxc2p)

根据完全积性的推论把 c2 除掉:

p+c=1p1(xc1p)

x0,显然 x,c 均与 p 互质,所以 xc 取遍了 [1,p1] 的值,也即 xc1 取遍了 [0,p2] 的值,则此时原式等于:

p+c=0p1(cp)(p1p)

中间的式子根据求和公式又等于 0,那么式子只剩下了

p(p1p)=p(1p)

后面的勒让德符号直接根据欧拉判别式求就行。

x=0,所有的 xc1 都等于 1,那么原式等于

p+(p1)(1p)

还是直接上欧拉判别式。

但以上分析都建立在 p 为奇素数的前提下,那原题的 p 该怎么处理呢?

由于 p 是若干奇素数的乘积且次数为 1,我们把 p 质因数分解,对每个质因子求出答案,最后把所有答案乘起来即可。证明考虑中国剩余定理合并,考虑换元后 xc+d(modp),那么把所有的 a2c(modp) 用 CRT 合并就能构造一组解,d 同理。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define ALL(x) x.begin(),x.end()
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
using pii=pair<int,int>;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=2e5+5,maxm=1e7+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,p;
int pr[maxm],cnt,q[maxm];
bool flag[maxm];
void xxs(int lim=1e7){
	rep(i,2,lim){
		if(!flag[i])pr[++cnt]=i,q[i]=i;
		for(int j=1;j<=cnt&&i*pr[j]<=lim;++j){
			flag[i*pr[j]]=1,q[i*pr[j]]=pr[j];
			if(i%pr[j]==0)break;
		}
	}
}
int calc(int x){
	assert((x-1)%2==0);
	return ((x-1)/2)%2?-1:1;
}
void solve_the_problem(){
	p=rd(),n=rd();
//	rep(i,1,n)write(q[i],32);P__; 
	int ans=1;
	while(p!=1){
		int _p=q[p];p/=_p;
		if(n%_p==0)ans*=_p+(_p-1)*calc(_p);
		else ans*=_p-calc(_p);
	}
	write(ans,10);
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=rd();xxs();
	while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18318046

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示