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++提交 收获:写完这题得到模板 代码: /************** ...
随机推荐
- 【Java 基础篇】【第二课】基本数组类型
就像第一章所说一样,这次学习为了快,因此说明性的文字就不想写太多了,直接帖代码吧,代码当中尽量加一些注释: package a.b; public class test { static void B ...
- 利用 libiconv 实现汉字编码 utf-8 格式 和 gbk格式的相互转换
参考文章:http://jimmee.iteye.com/blog/2174693 关于windows上编译libiconv的库,请参见:http://www.cnblogs.com/tangxin- ...
- Android Handler简单示例
package com.firstapp.foo.firstapp; import android.os.Handler; import android.os.Message; import andr ...
- 解决Eclipse启动报错Failed to create the Java Virtual Machine
电脑:2G内存,WIN7 32位. 启动adt-bundle-windows-x86-20140702\eclipse\eclipse.exe时,报错[Failed to create the Jav ...
- sqlserver 在脚本中,为所有字符前没有N标记的字符增加N
{[^N]}{'[\u4e00-\u9fa5]|[\u4e00-\U9fa5]|[0-9]|[A-Z]} \1N\2
- javascript中创建对象的方式总结
javascript中创建对象的方式总结 具体代码如下: //创建对象的方式: //创建方式一 var person=new Object(); person.name='jack'; person. ...
- robot framework安装步骤
1.安装python-2.7.11.amd64.msi 2.在命令行窗口输入python命令验证是否成功 3.安装wxPython2.8-win64-unicode-2.8.12.1-py27.exe ...
- MOGRE学习笔记(1) - OGRE简介及在vs2010下配置
由于工作需要,花费了一段时间研究OGRE,但是研究的目的是要在vs2010平台下用c#进行MOGRE的开发,不得已才转到MGRE,步骤是首选熟悉MOGRE的一些基础知识,做到在winform下能用MO ...
- ucenter 整合同步登录的内部实现原理及thinkphp整合ucenter
1.用户登录discuz,通过logging.php文件中的函数uc_user_login对post过来的数据进行验证,也就是对username和password进行验证.2.如果验证成功,将调用位于 ...
- ZOOKEEPER解惑
现在网上关于ZooKeeper的文章很多,有介绍Leader选举算法的,有介绍ZooKeeper Server内部原理的,还有介绍ZooKeeper Client的.本文不打算再写类似的内容,而专注与 ...