poj 3641 快速幂+米勒罗宾判断大素数
2015-07-21 11:36
337 查看
题意:
判断一个数p是否满足:
1.p不是素数;
2.pow_mod(a, p, p) == a % p。
代码:
判断一个数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 | 1 using 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; }
相关文章推荐
- 数据仓库之mondrian的学习
- Gray Code
- 7月21日Objiect的常用类本人为新手正在学习Java中把每天学的东西晚上都会在博客记录希望大神可以指点 不足在此谢过。
- CSS3_实现圆角效果box-shadow
- SkylineGlobe 如何使用二次开发接口创建粒子效果
- vbMHWB控件同webbrowser一样
- 今年暑假不AC
- modelsim仿真ISE工程时出现# ** Error: (vlog-19) Failed to access library 'rtl_work' at "rtl_work
- Hibernate Hql查询方法
- 区别string头文件
- 文件的下载
- 技术那么多,你想看看JSON Schema的测试吗?
- MyEclipse集成Tomcat启动报错
- VB获取IE8地址栏的URL
- sublime text 3 快捷键大全以及配置编译环境
- Java中的Enum的使用与分析
- 解决win7里的虚拟机ping不通exsi服务器里主机的问题
- css:map热点的应用
- laravel5 html引用问题
- Linux设定IP地址