POJ1808 平方(二次)同余方程
POJ1808 给定一个方程 x*x==a(mod p) 其中p为质数 判断是否有解
程序中 MOD_sqr()返回整数解 无解返回-1
数学证明略
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int LL; vector<LL>anss;
LL ans[1000000];
LL pow(LL n,LL k,LL p);
inline LL findv(LL x,LL p);
LL GCD(LL a,LL b)//Euclid
{
return b==0?a:GCD(b,a%b);
}
void extend_GCD(LL a,LL b,LL &x,LL &y)//extend Euclid
{
if(b==0){x=1;y=0;}
else {extend_GCD(b,a%b,y,x);y-=a/b*x;}
}
LL line_mod_equation(LL a,LL b,LL n)//二元一次线性不定方程求解
{
LL x,y;
extend_GCD(a,n,x,y);
LL d=GCD(a,n);
if(b%d==0)
{
while(x<0)x+=n;
while(x>n)x-=n;
LL a1=x*(b/d)%(n/d);
while(a1-n/d>0)a1-=n/d;
return a1;//此处只返回最小正整数解 若要解序列 在主函数中生成
}
return -1;
}
LL CRT(int a[],int m[],int n)//中国剩余定理
{
int M=1;
for(LL i=0;i<n;i++)
M*=m[i];
LL ret=0;
for(LL i=0;i<n;i++)
{
LL x,y;
LL tm=M/m[i];
extend_GCD(tm,m[i],x,y);
ret=(ret+tm*x*a[i])%M;
}
while(ret<0)ret+=M;
while(ret>M)ret-=M;
return ret;
}
inline LL findRoot(LL p)
{
LL s=ceil(sqrt(p-1));
for(LL i=1;i<p;i++)
{
if(pow(i,p-1,p)==1)
{
bool sign=1;
for(LL j=2;j<=s;j++)
if(((p-1)%j==0)&&(pow(i,(p-1)/j,p)==1))
{sign=0;break;}
if(sign)return i; }
}
return -1;
}
LL pow(LL n,LL k,LL p)
{
if(k==0)return 1;
LL w=1;
if(k&1)w=n%p;
LL ans=pow(n*n%p,k>>1,p);
return ans*w%p;
}
inline LL ind(LL x,LL p,LL g)//g^y=x(mod p) return y
{
static map<LL,LL>tmp;
LL s=ceil(sqrt(p));
LL w=pow(g,s,p);
for(LL r=0;r<=s;++r)tmp.insert(make_pair(pow(g, r, p), r));
for(LL t=0;t<=s;++t)
{
LL gt=pow(w,t,p);
LL anti=findv(gt,p);
if(tmp.count(x*anti%p))return (tmp[x*anti%p]+t*s); }
return -1;
}
inline LL findv(LL x,LL p)
{
LL d,t;
extend_GCD(x,p,d,t);
while(d<0)d+=p;
return d;
}
LL MOD_sqr(LL a,LL p)//求解 x^2=a(modp)原理有待证明
{
if(p==2)return a%p;
if(pow(a,(p-1)/2,p)!=1)return -1;
LL x;
if(p%4==3)x=pow(a,(p+1)/4,p);
else
{
LL b;
for(b=1;pow(b,(p-1)/2,p)==1;b++);
LL i=(p-1)/2,k=0;
do
{
i>>=1;k>>=1;
if((pow(a,i,p)*pow(b,k,p)+1)%p==0)
{
k+=(p+1)/2;
} }while(i%2==0);
x=pow(a,(i+1)/2,p)*pow(b,k/2,p)%p;
}
if(x*2>p)x=p-x;
return x;
}
int main()
{
freopen("t.txt","r",stdin);
LL np;
scanf("%lld",&np);
for(LL ii=1;ii<=np;ii++)
{
LL a,p;
scanf("%lld%lld",&a,&p);
a=(a%p+p)%p;
LL ans=MOD_sqr(a,p);
if(ans==-1)printf("Scenario #%lld:\n-1\n\n",ii);
else printf("Scenario #%lld:\n1\n\n",ii);
}
return 0;
}
POJ1808 平方(二次)同余方程的更多相关文章
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- Tensorflow2(二)tf.data输入模块
代码和其他资料在 github 一.tf.data模块 数据分割 import tensorflow as tf dataset = tf.data.Dataset.from_tensor_slice ...
- ZOJ3774_Power of Fibonacci
求fibonacci数列前N个数的K次方和. 通项公式:F[n]=((1+sqrt(5))/sqrt(5)-(1-sqrt(5))/sqrt(5))/sqrt(5). 有点乱,不过由于可以保证最后的结 ...
- python学习笔记之五:抽象
本文会介绍如何将语句组织成函数,还会详细介绍参数和作用域的概念,以及递归的概念及其在程序中的用途. 一. 创建函数 函数是可以调用,它执行某种行为并且返回一个值.用def语句即可定义一个函数:(并非所 ...
- 【算法】C语言趣味程序设计编程百例精解
C语言趣味程序设计编程百例精解 C/C++语言经典.实用.趣味程序设计编程百例精解(1) https://wenku.baidu.com/view/b9f683c08bd63186bcebbc3c. ...
- 【2017-03-13】Tsql 数学函数、字符串函数、转换函数、时间日期函数
一.数学函数(针对值类型操作) 1.ceiling():取上限 只要小数点后有数字大于0,整数位自动进1 2.floor():取下限 将小数点位舍去,不管小数点位大小 3.round(四舍五入的值,保 ...
- 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise
题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...
- Miller Rabin 算法简介
0.1 一些闲话 最近一次更新是在2019年11月12日.之前的文章有很多问题:当我把我的代码交到LOJ上,发现只有60多分.我调了一个晚上,尝试用{2, 3, 5, 7, 11, 13, 17, 1 ...
- PKU 2002 Squares(二维点哈希+平方求余法+链地址法)
题目大意:原题链接 给定平面上的N个点,求出这些点一共可以构成多少个正方形. 解题思路: 若正方形为ABCD,A坐标为(x1, y1),B坐标为(x2, y2),则很容易可以推出C和D的坐标.对于特定 ...
随机推荐
- TensorFlow - 相关 API
来自:https://cloud.tencent.com/developer/labs/lab/10324 TensorFlow - 相关 API TensorFlow 相关函数理解 任务时间:时间未 ...
- shit IE & no table `border-collapse: collapse;`
shit IE no table border-collapse: collapse; /* IE & shit table & border-collapse: collapse; ...
- SPOJ-BRCKTS (括号序列,线段树)
维护括号序列 Replace(i): 将第i个位置的括号反向. Check:测试当前序列是否合法. 题解 将左括号定为1,右括号定为-1,所以只需要满足前缀和序列没有负数即可,即最小值 为正即可,第i ...
- jQuery插件之ajaxFileUpload(ajax文件上传)
一.ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来,以后不用到处找了. 语法:$.ajaxFileUpload([options]) options参数 ...
- AOJ -0033 Ball(DFS)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22516 一道需要思考的搜索题. 题意:十个球按给定顺序从图中所示容器中下落, ...
- POJ 3233_Matrix Power Series
题意: 求n*n矩阵的幂和 分析: 逐个加起来时间复杂度太高,通过在矩阵中套个矩阵和,再利用矩阵快速幂,最后时间复杂度为O(n3logn) 代码: #include<cstdio> #in ...
- POJ 3268_Silver Cow Party
题意: n个地方,标号1~n,每个地方都有一头牛,现在要他们都去往标号为x的地方,再从x返回,每条道路都是单向的,求所有牛走的来回的最短路中的最大值. 分析: 注意在求每头牛走到x时,挨个算肯定超时, ...
- codevs——1081 线段树练习 2
1081 线段树练习 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 给你N个数,有两种操作 1:给 ...
- Ubuntu 16.04使用百度云的方案
Ubuntu没有很好的解决方案,都是一些投机取巧的方案: 1.不建议安装百度云客户端,尤其对于免费用户来说,会限制速度. 2.可以使用网页版进行文件上传. 3.下载可以通过Chrome点击下载后,复制 ...
- java.lang.NoClassDefFoundError: Could not initialize class异常处理
借鉴:http://blog.csdn.net/sleepdancer/article/details/9207425 static { InputStream in = XXX.class.getR ...