POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)
Description
Input
Output
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <iterator>
- #include <vector>
- using namespace std;
- typedef long long LL;
- LL modplus(LL a, LL b, LL mod) {
- LL res = a + b;
- return res < mod ? res : res - mod;
- }
- LL modminus(LL a, LL b, LL mod) {
- LL res = a - b;
- return res >= ? res : res + mod;
- }
- LL mult(LL x, LL p, LL mod) {
- LL res = ;
- while(p) {
- if(p & ) res = modplus(res, x, mod);
- x = modplus(x, x, mod);
- p >>= ;
- }
- return res;
- }
- LL power(LL x, LL p, LL mod) {
- LL res = ;
- while(p) {
- if(p & ) res = mult(res, x, mod);
- x = mult(x, x, mod);
- p >>= ;
- }
- return res;
- }
- bool witness(LL n, LL p) {
- int t = __builtin_ctz(n - );
- LL x = power(p % n, (n - ) >> t, n), last;
- while(t--) {
- last = x, x = mult(x, x, n);
- if(x == && last != && last != n - ) return false;
- }
- return x == ;
- }
- const int prime_n = ;
- int prime[prime_n] = {, , , , };
- bool isPrime(LL n) {
- if(n == ) return false;
- if(find(prime, prime + prime_n, n) != prime + prime_n) return true;
- if(n % == ) return false;
- for(int i = ; i < prime_n; i++)
- if(!witness(n, prime[i])) return false;
- return true;
- }
- LL getDivisor(LL n) {
- int c = ;
- while (true) {
- int i = , k = ;
- LL x1 = , x2 = ;
- while(true) {
- x1 = modplus(mult(x1, x1, n), c, n);
- LL d = __gcd(modminus(x1, x2, n), n);
- if(d != && d != n) return d;
- if(x1 == x2) break;
- i++;
- if(i == k) x2 = x1, k <<= ;
- }
- c++;
- }
- }
- void getFactor(LL n, vector<LL> &ans) {
- if(isPrime(n)) return ans.push_back(n);
- LL d = getDivisor(n);
- getFactor(d, ans);
- getFactor(n / d, ans);
- }
- int main() {
- int T; LL n;
- scanf("%d", &T);
- while(scanf("%I64d", &n) != EOF) {
- if(isPrime(n)) puts("Prime");
- else {
- vector<LL> ans;
- getFactor(n, ans);
- printf("%I64d\n", *min_element(ans.begin(), ans.end()));
- }
- }
- }
POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)的更多相关文章
- Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
POJ 1811 Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 32534 Accepted: 8 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- POJ1811- Prime Test(Miller–Rabin+Pollard's rho)
题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...
- POJ2429 - GCD & LCM Inverse(Miller–Rabin+Pollard's rho)
题目大意 给定两个数a,b的GCD和LCM,要求你求出a+b最小的a,b 题解 GCD(a,b)=G GCD(a/G,b/G)=1 LCM(a/G,b/G)=a/G*b/G=a*b/G^2=L/G 这 ...
- POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)
题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...
- POJ 1811 Prime Test 素性测试 分解素因子
题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的 素数与素性测试 素因子分解利用 ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- poj 1811 Prime Test 大数素数测试+大数因子分解
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 27129 Accepted: 6713 Case ...
- POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...
- Miller&&Pollard POJ 1811 Prime Test
题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...
随机推荐
- HDFS中高可用性HA的讲解
HDFS Using QJM HA使用的是分布式的日志管理方式 一:概述 1.背景 如果namenode出现问题,整个HDFS集群将不能使用. 是不是可以有两个namenode呢 一个为对外服务-&g ...
- Decimal、 Float、 Double 使用
一.Java 1.float型定义的数据末尾必须 有"f "或"F",为了和double区别.例float x=123.456f, y=2e20f; publ ...
- frameset、frame、noframes和iframe的区别
原网站地址:http://nmyun.blog.51cto.com/448726/155268 ■ 框架概念 :所谓框架便是网页画面分成几个框窗,同时取得多个 URL.只需要 <frameset ...
- Win7局域网文件共享方法
右击桌面网络----属性----更改高级共享设置 (注释:查看当前网络 比如:家庭网络.公共网络 等!) "我这里为公共网络" 选择 公共网络---选择以下选项:启动网络发 ...
- Eclipse插件项目 引用其他类库的方法(jar)
这两天搞了个Eclipse插件项目,用来监测ios.android设备和电脑的连接,安装apk/ipa到对应设备等等功能. 遇到了build path下的library引入编译正常,运行时报Class ...
- 1066 Bash游戏
1066 Bash游戏 基准时间限制:1 秒 空间限制:131072 KB 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜.假设A B都非常聪明, ...
- jQuery判断某个元素是否存在某个样式
<input type="button" class="new"/> $("input[name=new]").hasClass ...
- Speed-BI 多事实表与表间计算的应用:销售目标达成分析 另一种实现方法
在前一篇<Speed-BI多事实表与表间计算的应用(excel多Sheet关联分析):销售目标达成分析>http://www.powerbibbs.com/forum. ... 7583& ...
- lua metatable 和 _index 实验
lua metatable 和 _index 中文博客解释: http://www.cnblogs.com/simonw/archive/2007/01/17/622032.html metatabl ...
- python MySQLdb中文乱码
Python操作MySQL需要安装Python-MySQL可以从网上搜索一下,和一般的Python包一样安装 安装好之后,模块名字叫做MySQLdb ,在Window和Linux环境下都可以使用,试验 ...