P6610 [Code+#7] 同余方程(二次剩余)
题意
给定
前置知识
二次剩余综合题。
首先二次剩余有一个重要的符号 勒让德符号:
勒让德符号的性质:
为 的解的组数( 和相反数都是一组解)。- 完全积性:
- 完全积性的推论:
感性理解:整数
除以一个完全平方因子,不改变 的二次剩余性。
证明:根据完全积性,,而 显然为 。
以下都令
- 欧拉判别式:当
是奇素数时, 。 - 求和公式:
,证明是如下引理。
引理:模
意义下二次剩余和非二次剩余的数量均为 。
证明可以参考 OI Wiki,或者参考 Alex_Wei 的笔记,在此引用后者的一段话:
所有原根的偶数次幂和所有原根的奇数次幂将
分成了两类,偶数类对应二次剩余,奇数类对应二次非剩余。
分析
考虑换元,令
也即求
展开并使用完全积性:
把 sigma 拆开:
后两者根据求和公式显然都等于
根据完全积性的推论把
当
中间的式子根据求和公式又等于 0,那么式子只剩下了
后面的勒让德符号直接根据欧拉判别式求就行。
当
还是直接上欧拉判别式。
但以上分析都建立在
由于
#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();
}
/*
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具