poj 3641 快速幂+米勒罗宾判断大素数
来源:互联网 发布:蚁群算法的应用 编辑:程序博客网 时间:2024/05/10 03:20
题意:
判断一个数p是否满足:
1.p不是素数;
2.pow_mod(a, p, p) == a % p。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long long#define lson lo, mi, rt << 1#define rson mi + 1, hi, rt << 1 | 1using namespace std;const int maxn = 1000001 + 10;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = acos(-1.0);const double ee = exp(1.0);LL pow_mod(LL a, LL n, LL mod){ if (n == 0) return 1; LL x = pow_mod(a, n >> 1, mod); LL res = x * x % mod; if (n % 2) res = res * a % mod; return res;}bool Witness(LL a, LL n){ LL t = 0, m = n - 1; while (!(m & 1)) { t++; m >>= 1; } LL x = pow_mod(a, m, n); if (x == 1 || x == n - 1) return false; while (t--) { x = x * x % n; if (x == n - 1) return false; } return true;}const int Times = 11;bool Miller_Rabin(LL n){ if (n < 2) return false; if (n == 2) return true; if (!(n & 1)) return false; for (int i = 1; i <= Times; i++) { LL a = rand() % (n - 1) + 1; if (Witness(a, n)) return false; } return true;}int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL LL p, a; while (~scanf("%lld%lld", &p, &a)) { if (!p && !a) break; if (!Miller_Rabin(p) && pow_mod(a, p, p) == a % p) { printf("yes\n"); } else { printf("no\n"); } } return 0;}
0 0
- poj 3641 快速幂+米勒罗宾判断大素数
- POJ 3641 : Pseudoprime numbers - 米勒罗宾素数测试
- POJ 3641 快速幂+素数判断
- (模板)米勒罗宾素数测试
- 素数判断算法 - 拉宾-米勒测试定理(c++实现)
- 素数判断算法 - 拉宾-米勒测试定理(c++实现)
- 素数判断算法 - 拉宾-米勒测试定理(c++实现)
- 米勒-拉宾素数测试
- 米勒拉宾素数测试
- 【算法】米勒拉宾素数测试
- POJ 3641 -- 快速幂运算,素数判定
- POJ 3641 Pseudoprime numbers(快速幂+素数)
- POJ 3641 Pseudoprime numbers【素数+快速幂】
- (Relax 快速幂取模1.4)POJ 3641 Pseudoprime numbers(快速幂取模+大素数MillerRabin模板题)
- [SinGuLaRiTy-1002] Miller Rabin Prime Judge 米勒·罗宾素数判定法
- 素数判定--米勒测试
- hdu4910(米勒罗宾)
- POJ1811米勒罗宾模板
- sublime text 3 快捷键大全以及配置编译环境
- MyEclipse集成Tomcat启动报错
- Spring-property文件配置方法以及如何与工程分离~xml使用${}
- modelsim仿真ISE工程时出现# ** Error: (vlog-19) Failed to access library 'rtl_work' at "rtl_work
- vbMHWB控件同webbrowser一样
- poj 3641 快速幂+米勒罗宾判断大素数
- Hibernate Hql查询方法
- 数据仓库之mondrian的学习
- 7月21日Objiect的常用类本人为新手正在学习Java中把每天学的东西晚上都会在博客记录希望大神可以指点 不足在此谢过。
- 关于Scrollview嵌套ListView,使用setListViewHeightBasedOnChildren(ListView listView) ,在小米手机上崩溃问题
- 在IE8上eWebEdit编辑器失效
- Gray Code
- 【破解向】Mac版Adobe Photoshop CS6 与破解补丁下载–注册机+序列号+修改HOSTS完美可升级激活法及详细图文安装教程(强烈推荐)
- 代码重构-关于命名规范