Miller Rabbin 大素数测试
来源:互联网 发布:iphone8知乎 编辑:程序博客网 时间:2024/06/05 15:24
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <map>#include <string>#include <cstring>#include <ctime>#define ms(a,b) memset(a,b,sizeof(a))using namespace std;typedef long long ll;/* Miller_Rabbin 大素数测试 (不能计算long * long) 复杂度: O(logN) * 测试次数 */long long pow_mod(long long a, long long x, long long p){ long long res = 1; while (x) { if (x & 1) { res = res * a % p; } x >>= 1; a = a * a % p; } return res;}bool test(long long n, long long a, long long d){ if (n == 2) { return true; } if (n == a) { return true; } if ((n & 1) == 0) { return false; } while (!(d & 1)) { d = d >> 1; } long long t = pow_mod(a, d, n); while ((d != n - 1) && (t != 1) && (t != n - 1)) { t = t * t % n; d = d << 1; } return (t == n - 1 || (d & 1) == 1);}// Miller_Rabin() 算法素数判定// 是素数返回true.( 可能是伪素数, 但概率极小)// 合数返回false;/*速度快, 而且可以判断<2^63 的数复杂度: O(logN) * 测试次数由于要处理long * long , 所以略慢*/const int S = 20; // 随机算法判定次数, S 越大, 判错概率越小bool Miller_Rabbin(long long n){ if (n < 2) { return false; } if (n == 2) { return true; } if ((n & 1) == 0) { return false; // 偶数 } for (int i = 0; i < S; i++) { long long a = rand() % (n - 1) + 1; if (!test(n, a, n - 1)) { return false; //合数 } } return true;}int main(){ ll n; while (cin >> n) { if (Miller_Rabbin(n)) { cout << "True" << endl; } else { cout << "False" << endl; } } return 0;}
阅读全文
0 0
- Miller Rabbin 大素数测试
- Miller Rabbin测试素数
- Miller-Rabbin随机性素数测试算法
- 测试素数_Miller Rabbin
- Miller-Rabin测试-大素数问题
- 大素数测试的Miller-Rabin算法
- 大素数判断_fermat素性测试+Miller-Rabin素性测试
- 素数判定方法_Miller-Rabbin测试
- 素数判定方法_Miller-Rabbin测试
- Miller - Rabin素数测试
- Miller-Rabin素数测试
- Miller-Rabin(素数测试)
- miller素数测试
- Miller-Rabin素数测试
- Miller-Rabin素数测试
- Miller-Rabin素数测试
- Miller-Rabin素数测试
- Miller-Rabin 素数测试
- 图片转成base64的字符串, 如何让图片在html中显示
- hammerJs 快速使用---入门教程
- [HackerRank 101 Hack 51] Small Cubes
- PostgreSQL 10 Beta 3 发布,和对所有受支持版本的安全修复
- leetcode 334. Increasing Triplet(三连) Subsequence
- Miller Rabbin 大素数测试
- SpringMVC异常处理使用@ControllerAdvice注释
- 移动端关于点击输入框,因字体变化,导致布局变形的问题
- 极客学院 Mybatis 与 Spring3 集成 完整applicationContext.xml
- window.getComputedStyle
- hdu6105
- Android-ShimmerLayout微光效果解析
- Weblogic10.3及以上版本的下载地址
- 炬力V100遇到问题及解决