POJ-1811-Prime Test(pollard_rho模板,快速找最小素因子)
题目传送门
sol:Pollard_Rho的模板题,刚看了Pollard_Rho和Miller_Rabin很多原理性的东西看不懂,只是记住了结论勉强能敲代码。
- Pollard_Rho
#include "cstdio"
#include "cstdlib"
#include "algorithm"
#include "ctime"
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b) {
return b == ? a : gcd(b, a % b);
}
LL muli_mod(LL n, LL k, LL p) {
LL m = ;
while (k) {
if (k & ) m = (m + n) % p;
n = (n + n) % p;
k >>= ;
}
return m;
}
LL pow_mod(LL n, LL k, LL p) {
LL m = ;
while (k) {
if (k & ) m = muli_mod(m, n, p);
n = muli_mod(n, n, p);
k >>= ;
}
return m;
}
LL miller_rabin(LL n) {
if (n == ) return true;
if (n < || !(n & )) return false;
LL m = n - ; int s = ;
while (!(m & )) s++, m >>= ;
for (int i = ; i <= ; i++) {
LL r = rand() % (n - ) + ;
LL y = pow_mod(r, m, n);
for (int j = ; j <= s; j++) {
LL x = muli_mod(y, y, n);
if (x == && y != && y != n - ) return false;
y = x;
}
if (y != ) return false;
}
return true;
}
LL pollard_rho(LL n, LL c) {
int i = , k = ;
LL x = rand() % (n - ) + ;
LL y = x;
while (true) {
x = (muli_mod(x, x, n) + c) % n;
LL p = gcd((y - x + n) % n, n);
if (p > && p < n) return p;
if (x == y) return n;
if (++i == k) {
k <<= ;
y = x;
}
}
}
LL find(LL n) {
if (miller_rabin(n)) return n;
LL p = n;
while (p >= n) p = pollard_rho(p, rand() % (n - ) + );
return min(find(p), find(n / p));
}
int main() {
int t; LL n;
// srand(time(NULL));
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
LL p = find(n);
if (p == n) puts("Prime");
else printf("%lld\n", p);
}
return ;
}POJ不让用万能头,algorithm下的__gcd也不让用。关键srand用一下还RE,挺坑的。
POJ-1811-Prime Test(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 (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)
题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- Miller&&Pollard POJ 1811 Prime Test
题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- POJ 1811 Prime Test
题意:对于一个大整数,判断是否质数,如果不是质数输出最小质因子. 解法:判断质数使用Miller-Rabin测试,分解质因子使用Pollard-Rho,Miller-Rabin测试用的红书模板,将测试 ...
- 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经典题 /************************************** ...
- POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)
Description Given a big integer number, you are required to find out whether it's a prime number. In ...
随机推荐
- JavaScript学习总结(五)
之前的几讲中我们曾经说过,JavaScript中是没有类的概念的.但是我们讲过对象,那么这个对象是怎么来的呢? 只要有函数即可创建对象 自定义对象 自定义对象的方式: 1. 使用无参的函数创建对象 & ...
- Linux-常见信号介绍
1.SIGINT 2 Ctrl + C时OS送给前台进程组中每个进程 2.SIGABRT 6 调用abort函数,进程异常终止 3 ...
- 35. docker swarm dockerStack 部署 投票应用
1. 编写 docker-compose.yml # docker-compose.yml version: "3" services: redis: image: redis:a ...
- vue拖拽插件(弹框拖拽)
// =======拖拽 插件 cnpm install vuedraggableimport draggable from 'vuedraggable' <draggable v-model= ...
- Java线程——线程习题(二)生成者消费者
生产者消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据. 这里实现如下情况的生产--消费模型: 生产者不断交替地生产两组数据“姓 ...
- Ambiguous HTTP method Actions require an explicit HttpMethod binding for Swagger 2.0 异常
网上看了很多关于此异常的解决方案,但是大多数都是不能用的,今天把正确的解决方案记录下来,以帮助需要的人 问题:有些接口没有设置HttpPost或HttpGet,非接口设置访问权限为private,控制 ...
- 17。3.12---re模块--正则表达式操作指南
1----python re模块(Regular Expressioin正则表达式)提供了一个与perl等编程语言类似的正则匹配操作,他是一个处理python字符串的强有力的工具,有自己的语法和独立的 ...
- linux系统终端介绍
https://zhidao.baidu.com/question/174261014.html
- PAT Basic 1020 ⽉饼 (25) [贪⼼算法]
题目 ⽉饼是中国⼈在中秋佳节时吃的⼀种传统⻝品,不同地区有许多不同⻛味的⽉饼.现给定所有种类⽉饼的库存量.总售价.以及市场的最⼤需求量,请你计算可以获得的最⼤收益是多少. 注意:销售时允许取出⼀部分库 ...
- PAT甲级——1061 Dating (20分)
Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...