51Nod 1186 质数检测 V2
来源:互联网 发布:淘宝进口商品资质 编辑:程序博客网 时间:2024/05/21 01:31
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1186
分析:
对于这个题收获很多 说起来特别搞笑 由于是第一次写java不知道交题的时候不能交包 所以就一直运行错误 然后就去问大佬(真的非常感谢给我改代码的大佬 自己写的又臭又长 人家还给看下来了) 改自己的代码 然后学到很多 比如不用像c++那样要自己写快速幂的函数 直接可以用modpow()就能解决 后来测试了一下 modpow()比手写的快速幂快很多 比如判断大数的奇偶也有和c++类似的getLowestSetBit() 不用取模判1 哈哈 还有就是大数的随机产生有其本身的构造函数可以用等等 最后戏剧性的发现第一次的代码把包去掉也过了 哎哎 真的有想笑 还有就是判断是不是素数不用自己写算法 Java有isProbablePrime()可以直接用而且很快 我就是想用Java实现下 miller_rabin素数检测 遇到了这么多问题 好吧说那么多就稍微感慨一下 下面代码
这里有miller_rabin素数检测c的实现 http://blog.csdn.net/mm__1997/article/details/78278327
AC代码( miller_rabin素数检测):
import java.math.BigInteger;import java.util.Random;import java.util.Scanner;public class Main {// 没有用到的快速幂/*public static BigInteger multi(BigInteger a, BigInteger b,BigInteger p){BigInteger temp=BigInteger.valueOf(0);while (b.equals(BigInteger.valueOf(0))!=true){if (b.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(1))==true){temp=temp.add(a);temp=temp.mod(p);}a=a.multiply(BigInteger.valueOf(2));a=a.mod(p);b=b.divide(BigInteger.valueOf(2));}return temp;}public static BigInteger qpow(BigInteger a, BigInteger b,BigInteger p){BigInteger temp=BigInteger.valueOf(1);while (b.equals(BigInteger.valueOf(0))!=true){if (b.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(1))==true){temp=multi(temp,a,p);}b=b.divide(BigInteger.valueOf(2));a=multi(a,a,p);}return temp;}*/public static boolean miller_rabin(BigInteger n){if (n.equals(BigInteger.valueOf(0))==true||n.equals(BigInteger.valueOf(1))==true) return false;if (n.equals(BigInteger.valueOf(2))==true) return true;int s=10;BigInteger k=n.subtract(BigInteger.valueOf(1));int t=0;//k.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(1))!=true 取模判1while (k.getLowestSetBit()!=0){t++;k=k.divide(BigInteger.valueOf(2));}Random ran = new Random();while (s-->0){BigInteger a=new BigInteger(100,ran).mod( n.subtract(BigInteger.valueOf(2))).add(BigInteger.valueOf(2) );BigInteger[] x= new BigInteger[105];x[0]=a.modPow(k,n);//x[0]=qpow(a,k,n);for (int i=1;i<=t;i++){x[i]=x[i-1].modPow(BigInteger.valueOf(2),n);//x[i]=multi(x[i-1],x[i-1],n);if (x[i].equals(BigInteger.valueOf(1))==true&&x[i-1].equals(BigInteger.valueOf(1))!=true&&x[i-1].equals(n.subtract(BigInteger.valueOf(1)))!=true) return false;}if (x[t].equals(BigInteger.valueOf(1))!=true) return false;}return true;}public static void main(String[] args) {Scanner cin= new Scanner(System.in);while (cin.hasNextBigInteger()){BigInteger n;n=cin.nextBigInteger();if (miller_rabin(n)==true) System.out.println("Yes");else System.out.println("No");}cin.close();}}
AC代码:
import java.math.BigInteger;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(System.in);while(cin.hasNextBigInteger()){BigInteger n;n = cin.nextBigInteger();if (n.isProbablePrime(1)) System.out.println("Yes");else System.out.println("No");}}}
阅读全文
0 0
- 51nod 1186 质数检测 V2
- 51nod 1186 质数检测 V2
- 51Nod 1186 质数检测 V2
- 51Nod-1186-质数检测 V2
- 51Nod-1186-质数检测 V2 JAVA biginteger包
- 51nod-1186 质数检测
- 51nod 1106 质数检测
- 51nod 1106 质数检测
- 51nod-1106质数检测
- 51nod 1106 质数检测
- 51nod-【1106 质数检测】
- 51NOD 1106质数检测
- 51Nod 1106 质数检测
- 【51Nod】1106 质数检测
- 51Nod-1106-质数检测
- 51Nod 1106 质数检测
- 51Nod-1106 质数检测
- 51 Nod 1106 质数检测
- 例题 6
- LSD algoritm
- KMP算法
- C++动态内存管理
- python中线程和进程
- 51Nod 1186 质数检测 V2
- 浙江大学精品课程视频合集
- CCF-20160901-最大波动(100分)
- 【Leetcode】最接近和子数组
- UVa11404
- 如何选择自动化安装默认文件
- 如何写一个简单的jQuery插件
- 将关键代码手动移植到C语言
- Python循环嵌套应用案例一则:判断丑数