首页 技术 正文
技术 2022年11月14日
0 收藏 331 点赞 4,924 浏览 1028 个字

求解方程,x^2=n (mod P)。

解二次同余方程的步骤:

1、首先判断勒让德符号(n,p)是否的等于1,即n^((p-1/2)=1 (mod p)是否成立。不成立显然无解。(略)

2、任取0-(p-1)中的一a值,判断w=a*a-n是否是P的二次同余,直到找到一个否定的答案即可。(大约有一半是否定答案)

3、根据找到的w,(a+sqrt(w))^((p+1)/2)就是二次同余的解,同时最多只有两解,且两数之和为P。(要用到二次域,囧rz)

中间有一定量的推导过程,但是不是很难,琢磨琢磨吧。

对于这个题目,注意一种特殊情况,p=2时,直接输出1即可。

召唤代码君:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;int a,w;
int T,n,p,A0,B0;struct twice{
int A,B;
twice() { }
twice(int AA,int BB) { A=AA,B=BB; }
twice operator * ( twice T ) const {
A0=A*T.A+(B*T.B)%p*w;
B0=A*T.B+B*T.A;
return twice(A0%p,B0%p);
}
};int power(int A,int B)
{
int C=;
while (B){
if (B&) C=C*A%p;
A=A*A%p,B>>=;
}
return C;
}twice power(twice A,int B)
{
twice C(,);
while (B){
if (B&) C=C*A;
A=A*A,B>>=;
}
return C;
}bool lgd(int A,int B)
{
return power(A,(B-)/)==;
}int main()
{
scanf("%d",&T);
while (T--){
scanf("%d%d",&n,&p);
if (p==){
puts("");
continue;
}
if (!lgd(n,p)){
puts("No root");
continue;
}
for (;;){
a=rand()%p;
w=((a*a-n)%p+p)%p;
if (!lgd(w,p)) break;
}
twice T(a,);
T=power(T,(p+)/);
int ans1=(int)T.A,ans2=(int)p-T.A;
if (ans1>ans2) swap(ans1,ans2);
printf("%d %d\n",ans1,ans2);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919