题目传送门

质数判定

题目描述

判定输入的数是不是质数。

输入格式

若干行,一行一个数 x。

行数不超过 $1.5\times 10^4$

输出格式

对于输入的每一行,如果 x是质数输出一行 Y,否则输出一行 N。

样例

样例输入

  1. 1
  2. 2
  3. 6
  4. 9
  5. 666623333

样例输出

  1. N
  2. Y
  3. N
  4. N
  5. Y

数据范围与提示

$1\leq x\leq 10^{18}$

欢迎hack(如果你不是管理员,可以在题目讨论区发帖)。


  分析:

  Miller_Rabin模板,被卡了好久。

  具体的Miller_Rabin算法博主就不再讲了,注意要考虑到一些细节,然后$LibreOJ$卡时间,不能用$\log n$的乘法,需要用快速乘。

  Code:

  1. //It is made by HolseLee on 10th Sep 2018
  2. //LibreOJ#143
  3. #include<cstdio>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9. ll n,a[]={,,,,,};
  10.  
  11. inline ll mul(__int128 x,__int128 y,__int128 m)
  12. {
  13. return (__int128)(x*y)%m;
  14. }
  15.  
  16. inline ll power(ll x,ll y,ll m)
  17.  
  18. {
  19. ll ret=; x%=m;
  20. while( y ) {
  21. if( y& ) ret=mul(ret,x,m);
  22. y>>=; x=mul(x,x,m);
  23. }
  24. return ret;
  25. }
  26.  
  27. inline bool miller_rabin(ll u)
  28. {
  29. if( u== ) return ;
  30. if( !(u&) || u== || u== ) return ;
  31. u-=;
  32. int cnt=; ll x,pre;
  33. while( !(u&) ) u>>=,cnt++;
  34. for(int i=; i<=; ++i) {
  35. if(n==a[i])return ;
  36. if(n%a[i]==)return ;
  37. x=power(a[i],u,n);
  38. pre=x;
  39. for(int j=; j<=cnt; ++j) {
  40. x=mul(x,x,n);
  41. if( x== && pre!=n- && pre!= ) return ;
  42. pre=x;
  43. }
  44. if( x!= ) return ;
  45. }
  46. return ;
  47. }
  48.  
  49. int main()
  50. {
  51. while( scanf("%lld",&n)!=EOF ) {
  52. if( miller_rabin(n) ) puts("Y");
  53. else puts("N");
  54. }
  55. return ;
  56. }

LibreOJ#143 质数判定 [Miller_Rabin]的更多相关文章

  1. LOJ #143. 质数判定

    题目描述 判定输入的数是不是质数. 输入格式 若干行,一行一个数 x. 行数不超过 1.5×104​​. 输出格式 对于输入的每一行,如果 x 是质数输出一行 Y,否则输出一行 N. 样例 样例输入 ...

  2. Loj#143-[模板]质数判定【Miller-Rabin】

    正题 题目链接:https://loj.ac/p/143 题目大意 给出一个数\(p\),让你判定是否为质数. 解题思路 \(Miller-Rabin\)是一种基于费马小定理和二次探测定理的具有较高正 ...

  3. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  4. 质数的判定 Miller_Rabin

    ----------- 10^18 #include <bits/stdc++.h> #define min(a,b) ((a)<(b)?(a):(b)) #define max(a ...

  5. [学习笔记] Miller-Rabin质数测试 & Pollard-Rho质因数分解

    目录 Miller-Rabin质数测试 & Pollard-Rho质因数分解 Miller-Rabin质数测试 一些依赖的定理 实现以及正确率 Pollard-Rho质因数分解 生日悖论与生日 ...

  6. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

  7. RSA简介(三)——寻找质数

    要生成RSA的密钥,第一步就是要寻找质数,本节专讲如何寻找质数. 我们的质数(又称素数).合数一般是对正整数来讲,质数就是只有1和本身两个的正整数,合数至少有3个约数,而1既不是合数也不是质数. 质数 ...

  8. Java基础常见英语词汇

    Java基础常见英语词汇(共70个) ['ɔbdʒekt] ['ɔ:rientid]导向的                             ['prəʊɡræmɪŋ]编程 OO: object ...

  9. 跨越千年的RSA算法

    转载自http://www.matrix67.com/blog/archives/5100 数论,数学中的皇冠,最纯粹的数学.早在古希腊时代,人们就开始痴迷地研究数字,沉浸于这个几乎没有任何实用价值的 ...

随机推荐

  1. centos7 mysql5.7.17源码安装

    **安装前准备 操作系统环境:Centos 7.2 1.解决依赖包并下载源码包至/home/soft/目录下 1 2 3 4 5 6 7 [root@node03 ~]# yum -y install ...

  2. 关于Linux运维的一些题目总结

    一.有文件file1 1.查询file1里面空行的所在行号 awk ‘{if($0~/^$/)print NR}’ fileorgrep -n ^$ file |awk ‘BEGIN{FS=”:”}{ ...

  3. spring boot 2.0.3+spring cloud (Finchley)4、熔断器Hystrix

    在分布式系统中服务与服务之间的依赖错综复杂,一种不可避免的情况就是某些服务会出现故障,导致依赖于他们的其他服务出现远程调度的线程阻塞.某个服务的单个点的请求故障会导致用户的请求处于阻塞状态,最终的结果 ...

  4. Codeforces 221 C. Little Elephant and Problem

    C. Little Elephant and Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  5. Codeforces 221 B. Little Elephant and Numbers

    B. Little Elephant and Numbers time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  6. HTML入门(三)后台系统显示页面_框架标签

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. ③ 设计模式的艺术-03.工厂方法(Factory Method)模式

    public interface Car { void run(); } public class Audi implements Car { @Override public void run() ...

  8. 使用asp.net改变图片颜色

    最近奇葩经理提出了奇葩的需求,要能在网站上改变图片的颜色,比如灰色的变成彩色,彩色的变成灰色,尼玛楼主的感受你们不懂!于是有了下面的代码... 用法:调用update_pixelColor方法并传参数 ...

  9. TED_Topic3:The hidden reason for poverty the world needs to address now

    The hidden reason for poverty the world needs to address now By Gary Haugen # Background about our s ...

  10. UIScrollViewDelegate 方法调用

    UIScrollViewDelegate 方法调用 /** 设置缩放的View, 初始化完之后调用此方法告诉scrollView 谁可以缩放操作, 然后进行布局 */ func viewForZoom ...