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 ...
随机推荐
- 《打造扛得住的MySQL数据库架构》第3章 MySQL基准测试
3-1 什么是基准测试 测量系统性能,优化是否有效?MySQL基准测试. 定义:基准测试是一种测量和评估软件性能指标的活动,用于建立某个时刻的性能基准,以便当系统发生软硬件 变化时重新进行基准测试以评 ...
- DFS判断连通图
因为是连通图,所以从任意一点出发,一定可以通过一遍深度优先遍历就能走过所有的点和边,就可以利用这个性质来很容易的通过DFS判断图是否为连通图 下面是具体算法:
- Java中常用的API(四)——其他
前面说三篇文章分别介绍了Object.String.字符缓冲类的API,接下来我们简要介绍一下其他常用的API. 1.System System类用于获取各种系统信息,最为常用的是: System.o ...
- 31. docker swarm 通过 service 部署 wordpress
1. 创建 一个 overlay 的网络 driver docker network create -d overlay demo 查看网络列表 docker network ls 2. 创建mysq ...
- 当初希望自己是如何投入这个专业的学习的?曾经做过什么准备,或者立下过什么FLAG吗?
学习好累,打游戏好爽 我不爱学习 认真勤勉投入学习 精心准备,刻苦学习 我的flag 作为大学生,需要了解今后职场社会,对职业方向有了进一步的认识.社会对于人才的要求在某些方面都是不谋而合的,比 ...
- 黑马_10 Lucene:全文检索
10 Lucene:01.全文检索基本介绍 10 Lucene:02.创建索引库和查询索引 10 Lucene:03.中文分析器 10 Lucene:04.索引库维护CURD
- drf框架知识点总复习
接口 """ 1.什么是接口:url+请求参数+响应数据 | 接口文档 2.接口规范: url:https,api,资源(名词复数), v1,get|post表示操作资源 ...
- oracle 学习(三)pl/sql语言函数
系统内置函数 数学运算函数 字符串函数 统计函数 日期函数 用户定义函数:存储在数据库中的代码块,可以把值返回到调用程序.调用时如同系统函数一样 参数模式 IN模式:表示该参数时输入给函数的参数 OU ...
- [转载]markown语法
目录 Cmd Markdown 公式指导手册 一.公式使用参考 1.如何插入公式 2.如何输入上下标 3.如何输入括号和分隔符 4.如何输入分数 5.如何输入开方 6.如何输入省略号 7.如何输入矢量 ...
- goweb-goweb基础
goweb DNS工作原理 在浏览器中输入www.qq.com域名,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用这个IP地址映射,完成域名解析. 如果hosts里没 ...