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>, 0r < 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>. (1x < N, 2N < 1, 000, 000, 000, 1r < 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. 1 12 1
  2. 4 15 2
  3. 0 0 0

Sample Output

  1. Case 1: 1 5 7 11
  2. Case 2: 2 7 8 13
  3.  
  4. 题意:r^2x(mod n)求(0<r<nr的所有解
    已知x,n,跟一个解r1,那么有
    r^2x (mod n)即:r^2+k1*n=x;(1)
    r1^2x (mod n)即:r1^2+k2*n=x;(2)
    联立(1)、(2)得:r^2-r1^2=(r+r1)(r-r1)=k3*n;(3)
    枚举AB使得:A * B =n
    r + r10 (mod A)
    r - r10 (mod B)
    即: Ak-r1= Bk + r1= r
    变形一下得:Ak2*r1 (mod B)
    根据这个等式枚举模线性方程求解
    根据一般的模线性方程求解我的每组案例都少了个解,改成枚举nA*B=Kn)范围内所有的解而不是B范围内(Ak2*r1(mod B))
    我觉得用int型应该差不多了,可是Wrong answer,改成long long,却Accepted了。
    AC代码:
  1. #include<iostream>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<cstdio>
  5. #include<set>
  6. using namespace std;
  7.  
  8. typedef long long LL;
  9. set<LL> s;
  10. LL X,R,N;
  11.  
  12. LL Extended_Euclid(LL a,LL b,LL &x,LL &y)//欧几里德扩展定理
    {
  13. LL d,t;
  14. if(b==)
  15. {
  16. x=;y=;return a;
  17. }
  18. d=Extended_Euclid(b,a%b,x,y);
  19. t=x;
  20. x=y;
  21. y=t-a/b*y;
  22. return d;
  23. }
  24.  
  25. void Mod_Line_Equation_Solve(LL a,LL b,LL n)//模线性方程求解
    {
  26. LL x,y,d,t,lcm;
  27. d=Extended_Euclid(a,n,x,y);
  28. if(b%d==)
  29. {
  30. x=x*b/d;
  31. x=(x%(n/d)+n/d)%(n/d);
  32. t=a*x-b/;
  33. lcm=a/d*n;
  34. for(;t<N;t+=lcm)
  35. {
  36. if(t>= && t*t%N==X) s.insert(t);//符合条件的解插入set容器
    }
  37. }
  38. }
  39.  
  40. int main()
  41. {
  42. LL i,top,Case=;
  43. while(cin>>X>>N>>R && !(X== && N== && R==))
  44. {
  45. Case++;
  46. printf("Case %d:",Case);
  47. s.clear();
  48. top=sqrt(N+0.5);
  49. for(i=;i<=top;i++)//枚举所有的A*B=n的情况
    {
  50. if(N%i==)
  51. {
  52. Mod_Line_Equation_Solve(i,*R,N/i);
  53. Mod_Line_Equation_Solve(N/i,*R,i);
  54. }
  55. }
  56. set<LL>::iterator it;
  57. for(it=s.begin();it!=s.end();it++)
  58. printf(" %lld",*it);
  59. printf("\n");
  60. }
  61. return ;
  62. }
  1.  

uva 1426 离散平方根的更多相关文章

  1. UVA 1426 - Discrete Square Roots(数论)

    UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...

  2. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  3. UVa 1426 Discrete Square Roots (扩展欧几里德)

    题意:给定 x,n,r,满足 r2 ≡ x mod(n) ,求在 0 ~ n 内满足 rr2 ≡ x mod(n) 的所有的 rr. 析:很明显直接是肯定不行了,复杂度太高了. r2 ≡ x mod( ...

  4. UVALive 4270 Discrete Square Roots

    题目描述: 在已知一个离散平方根的情况下,按照从小到大的顺序输出其他所有的离散平方根. 在模n意义下,非负整数x的离散平方根是满足0<=r<n且r2=x(mod n)的整数r. 解题思路: ...

  5. 湖南省第八届大学生程序设计大赛原题 D - 平方根大搜索 UVA 12505 - Searching in sqrt(n)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30746#problem/D D - 平方根大搜索 UVA12505 - Searchin ...

  6. UVA 11181 Probability|Given (离散概率)

    题意:有n个人去商场,其中每个人都有一个打算买东西的概率P[i].问你最后r个人买了东西的情况下每个人买东西的概率 题解:一脸蒙蔽的题,之前的概率与之后的概率不一样??? 看了白书上的题解才知道了,其 ...

  7. UVA 11427 (概率DP+期望)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 题目大意:每晚打游戏.每晚中,赢一局概率p,最多玩n局, ...

  8. UVa 12505 Searching in sqrt(n)

    传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...

  9. 平方根的C语言实现(一)

    曾经做一个硬件成本极度控制的项目,因为硬件成本极低,并且还需要实现较高的精度测量,过程中也自己用C语言实现了正弦.余弦.反正切.平方根等函数. 以下,无论是在我的实际项目中还是本地的计算机系统,int ...

随机推荐

  1. 线段树成段更新模板POJ3468 zkw以及lazy思想

    别人树状数组跑几百毫秒 我跑 2500多 #include<cstdio> #include<map> //#include<bits/stdc++.h> #inc ...

  2. _IO_FILE

    hctf2017的babyprintf解法是house of orange,深入学习了一下,牵扯出许多知识,这里先进行第一步:_IO_FILE结构 0x00 _IO_FILE glibc-2.2.1\ ...

  3. shell脚本,如何监控目录下的文件内容是否被修改。

    第一种方法是通过cmp来进行比对[root@localhost bo]# ls .html .html .html .html .html .html .html .html .html cat.sh ...

  4. shell脚本,文件里面的英文大小写替换方法。

    [root@localhost wyb]# cat daxiaoxie qweBNMacb eeeDFSmkl svdIOPtyu [root@localhost wyb]# cat daxiaoxi ...

  5. javase(4)_数组

    一.数组概述 数组可以看成是多个相同类型数据组合,对这些数据的统一管理. 数组变量属于引用类型,数组也可以看成对象,数组中的每个元素相当于该对象的成员变量. 数组中的元素可以是任意类型,包括基本类型和 ...

  6. viewDidLoad、loadView

    一.loadView永远不要主动调用这个函数.view controller会在view的property被请求并且当前view值为nil时调用这个函数.如果你手动创建view,你应该重载这个函数,且 ...

  7. 蓝牙学习 (8)配对raspberryPi和SensorTag CC2650

    在上一篇中,用raspberryPi能够扫描到Ti SensorTag. 但是没有获得更多的数据,并且发现sensor Tag并没有回复scan request. https://blog.csdn. ...

  8. django第五天(虚拟环境安装和视图层相关)

    django第5天 虚拟环境安装 ''' 1.通过pip3安装虚拟环境: -- pip3 install virtualenv 2.前往目标文件夹: -- cd 目标文件夹 (C:\Virtualen ...

  9. One-to-one

    创建模型 在本例中,Place 和 Restaurant 为一对一关系 from django.db import models class Place(models.Model): name = m ...

  10. Python3与SQLServer、Oracle、MySql的连接方法

    环境: python3.4 64bit pycharm2018社区版 64bit Oracle 11 64bit SQLServer· Mysql 其中三种不同的数据库安装在不同的服务器上,通过局域网 ...