uva 1426 离散平方根
1426 - Discrete Square Roots
Time limit: 3.000 seconds
A square root of a number x <tex2html_verbatim_mark>is a number r <tex2html_verbatim_mark>such that r2 = x <tex2html_verbatim_mark>. A discrete square root of a non-negative integer x <tex2html_verbatim_mark>is a non-negative integer r <tex2html_verbatim_mark>such thatr2
x mod N <tex2html_verbatim_mark>, 0
r < N <tex2html_verbatim_mark>, where N <tex2html_verbatim_mark>is a specific positive integer and mod is the modulo operation.
It is well-known that any positive real number has exactly two square roots, but a non-negative integer may have more than two discrete square roots. For example, for N = 12 <tex2html_verbatim_mark>, 1 has four discrete square roots 1, 5, 7 and 11.
Your task is to find all discrete square roots of a given non-negative integer x <tex2html_verbatim_mark>. To make it easier, a known square root r <tex2html_verbatim_mark>of x <tex2html_verbatim_mark>is also given to you.
Input
The input consists of multiple test cases. Each test case contains exactly one line, which gives three integers x <tex2html_verbatim_mark>, N <tex2html_verbatim_mark>and r <tex2html_verbatim_mark>. (1
x < N, 2
N < 1, 000, 000, 000, 1
r < N) <tex2html_verbatim_mark>. It is guaranteed that r <tex2html_verbatim_mark>is a discrete square root of x<tex2html_verbatim_mark>modulo N <tex2html_verbatim_mark>. The last test case is followed by a line containing three zeros.
Output
For each test case, print a line containing the test case number (beginning with 1) followed by a list of corresponding discrete square roots, in which all numbers are sorted increasingly..
Sample Input
1 12 1
4 15 2
0 0 0
Sample Output
Case 1: 1 5 7 11
Case 2: 2 7 8 13 题意:r^2≡x(mod n)求(0<r<n)r的所有解
已知x,n,跟一个解r1,那么有
r^2≡x (mod n)即:r^2+k1*n=x;(1)
r1^2≡x (mod n)即:r1^2+k2*n=x;(2)
联立(1)、(2)得:r^2-r1^2=(r+r1)(r-r1)=k3*n;(3)
枚举A和B使得:A * B =n
r + r1≡0 (mod A)
r - r1≡0 (mod B)
即: Ak-r1= Bk + r1= r
变形一下得:Ak≡2*r1 (mod B)
根据这个等式枚举模线性方程求解
根据一般的模线性方程求解我的每组案例都少了个解,改成枚举n(A*B=Kn)范围内所有的解而不是B范围内(Ak≡2*r1(mod B))
我觉得用int型应该差不多了,可是Wrong answer,改成long long,却Accepted了。
AC代码:
#include<iostream>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<set>
using namespace std; typedef long long LL;
set<LL> s;
LL X,R,N; LL Extended_Euclid(LL a,LL b,LL &x,LL &y)//欧几里德扩展定理
{
LL d,t;
if(b==)
{
x=;y=;return a;
}
d=Extended_Euclid(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return d;
} void Mod_Line_Equation_Solve(LL a,LL b,LL n)//模线性方程求解
{
LL x,y,d,t,lcm;
d=Extended_Euclid(a,n,x,y);
if(b%d==)
{
x=x*b/d;
x=(x%(n/d)+n/d)%(n/d);
t=a*x-b/;
lcm=a/d*n;
for(;t<N;t+=lcm)
{
if(t>= && t*t%N==X) s.insert(t);//符合条件的解插入set容器
}
}
} int main()
{
LL i,top,Case=;
while(cin>>X>>N>>R && !(X== && N== && R==))
{
Case++;
printf("Case %d:",Case);
s.clear();
top=sqrt(N+0.5);
for(i=;i<=top;i++)//枚举所有的A*B=n的情况
{
if(N%i==)
{
Mod_Line_Equation_Solve(i,*R,N/i);
Mod_Line_Equation_Solve(N/i,*R,i);
}
}
set<LL>::iterator it;
for(it=s.begin();it!=s.end();it++)
printf(" %lld",*it);
printf("\n");
}
return ;
}
uva 1426 离散平方根的更多相关文章
- UVA 1426 - Discrete Square Roots(数论)
UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVa 1426 Discrete Square Roots (扩展欧几里德)
题意:给定 x,n,r,满足 r2 ≡ x mod(n) ,求在 0 ~ n 内满足 rr2 ≡ x mod(n) 的所有的 rr. 析:很明显直接是肯定不行了,复杂度太高了. r2 ≡ x mod( ...
- UVALive 4270 Discrete Square Roots
题目描述: 在已知一个离散平方根的情况下,按照从小到大的顺序输出其他所有的离散平方根. 在模n意义下,非负整数x的离散平方根是满足0<=r<n且r2=x(mod n)的整数r. 解题思路: ...
- 湖南省第八届大学生程序设计大赛原题 D - 平方根大搜索 UVA 12505 - Searching in sqrt(n)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30746#problem/D D - 平方根大搜索 UVA12505 - Searchin ...
- UVA 11181 Probability|Given (离散概率)
题意:有n个人去商场,其中每个人都有一个打算买东西的概率P[i].问你最后r个人买了东西的情况下每个人买东西的概率 题解:一脸蒙蔽的题,之前的概率与之后的概率不一样??? 看了白书上的题解才知道了,其 ...
- UVA 11427 (概率DP+期望)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 题目大意:每晚打游戏.每晚中,赢一局概率p,最多玩n局, ...
- UVa 12505 Searching in sqrt(n)
传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...
- 平方根的C语言实现(一)
曾经做一个硬件成本极度控制的项目,因为硬件成本极低,并且还需要实现较高的精度测量,过程中也自己用C语言实现了正弦.余弦.反正切.平方根等函数. 以下,无论是在我的实际项目中还是本地的计算机系统,int ...
随机推荐
- codevs 2038 香甜的黄油 USACO
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上 ...
- 当然,perl等脚本服务器是一般默认安装了,你入侵了一台主机,总不能先装配 Java 环境然后再开干吧?
转自:https://www.zhihu.com/question/20173592 当然,perl等脚本服务器是一般默认安装了,你入侵了一台主机,总不能先装配 Java 环境然后再开干吧?
- PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20)
PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20) http://www.patest.cn/contests/pat-b-practise/1037 ...
- ios之UISegmentedcontol
初始化UISegmentedControl NSArray *arr = [[NSArray alloc]initWithObjects:@"轻拍",@"长按" ...
- 【Java_多线程并发编程】JUC原子类——AtomicLong原子类
1. AtomicLong是基本原子类中的一种 AtomicLong是对长整形进行原子操作. 1.1 AtomicLong类的函数列表 // 构造函数 AtomicLong() // 创建值为init ...
- kvm虚拟迁移
1. 虚拟迁移 迁移: 系统的迁移是指把源主机上的操作系统和应用程序移动到目的主机,并且能够在目的主机上正常运行.在没有虚拟机的时代,物理机之间的迁移依靠的是系统备份和恢复技术.在源主机上实时备份操作 ...
- docker参考文档
docker 使用笔记 http://www.cnblogs.com/xguo/p/3829329.html docker数据存储 | 单线程 http://opjasee.com/2014/06/2 ...
- Java测试技巧
快捷键 修改快捷键 window->preference->general->keys: 查找引用:ctrl+shift+g 覆盖测试:alt+shift+E,T 复制一行:ctrl ...
- biological clock
'''this application aimed to cauculate people's biological block about emotional(28), energy(23),int ...
- 前端Web框架的实现过程
一.Web框架的本质: 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 i ...