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 ...
随机推荐
- postfix简单记录
1.将/etc/postfix/main.cf编辑 myhostname和mydomain等 2.即可测试发送邮件 3.安装DNS功能,其中13和21行改成any yum install bind b ...
- 28. docker swarm 容器编排简介
1.采用集群架构 集群架构包含节点和角色 docker 节点中 包含 worker 和 manager 两个角色 manager 相当于 swarm 集群的 大脑 是用来管理配置节点的 (避免单点故 ...
- 系统学习python第四天学习笔记
1.解释 / 编译补充 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. 解释型:写完代码交给解释器,解释器会从上到下一行行代码执行:边解释边执行. [实时翻译] 2.字符串功能 ...
- 面试易错题 Java
1. int[] arr = new int[10]; System.out.println(arr);//地址值? char[] arr1 = new char[10]; System.out.pr ...
- 01 语言基础+高级:1-3 常用API第一部分_day08【String类、static、Arrays类、Math类】
day08[String类.static.Arrays类.Math类] String类static关键字Arrays类Math类 教学目标能够使用String类的构造方法创建字符串对象能够明确Stri ...
- Jupyter notebook 和 Jupyter lab 的区别
Jupyter Notebook Jupyter Notebook 是一个款以网页为基础的交互计算环境,可以创建Jupyter的文档,支持多种语言,包括Python, Julia, R等等.广泛用于数 ...
- JavaSE--异常信息打印
最近项目用到第三方jar包,抛出运行时异常,打在日志用的 方法.得到的错误描述并不详尽,遂想到平时用的 发现其可以重定向输出,平时用流多是和文件相关,但是在当前背景下用文件打开流显得不是很合适,翻了下 ...
- Linux-sys文件系统
1.sys文件系统本质上和proc文件系统是一样的,都是虚拟文件系统.都在根目录下有个目录(一个是/proc目录,另一个是/sys目录),因此都不是硬盘中的文件,都是内核中的数据结构的可视化接口. 2 ...
- vue 常用知识点
1.数据变更,页面渲染完成 this.$nextTick(function(){ alert('v-for渲染已经完成') }) 2.iview select组件 setQuery用法 <Sel ...
- 洛谷 P3808 【模板】AC自动机(简单版)
传送门:https://www.luogu.org/problem/P3808 题解:是一个AC自动机的裸题了,注释加在代码里面了 #include<bits/stdc++.h> usin ...