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 ...
随机推荐
- pywin32获得tkinter窗口句柄,并在上面绘图
想实现用win32 API在tkinter窗口上画图,那么应该先获得tkinter窗口的句柄hwnd,然后再获得tkinter的设备hdc.尝试了FindWindow(),GetActiveWindo ...
- LINUX的ssh免密码配置
本篇将介绍LINUX的ssh免密码配置. 本篇中的有三台主机: 192.168.1.110 master.com.cn 主机192.168.1.111 salver1.com.cn192.168.1 ...
- JAVA中如何判断一个输入是数字(小数和整数)还是字符串?
public class Test1 { public static void main(String[] args) { Scanner input = new Scanne ...
- 题解 P5837 【[USACO19DEC]Milk Pumping】
这题其实想法挺简单的,因为他只需要简单的把每个点的花费和流量用dp记下来就好了 1.怎么记: 首先考虑dp的状态.由于所在的点和流量都要记,所以dp开二维,一维记所在的点,另一维记去哪 //dp[i] ...
- Java类只加载一次的情况
一个类只加载一次: 调用Java命令. 创建对象时 访问静态成员时 Class.forName("包名.类名")
- gff文件提取cds
#!/usr/bin/perl use strict; use warnings; ########input######## ];my $cut = &cut($gff);my %cut = ...
- 基于node的前后端分离初识
久闻node的大名,先后也看过node的文档,但是,总是碍于没有挑起我的G点,所以实际codeing的例子都没有.最近,突然很有兴致,想把原有用页面ajax渲染的页面采用服务端node来渲染,研究了两 ...
- windows server 2012 安装sql server集群
第一步:准备工作 虚拟环境下模拟创建: 准备好3台虚拟机 操作系统,WindowsServer2012R2 操作系统安装完成后,需要注意如果虚拟机是克隆出来的,后面操作集群的时候需要计算机的sid不同 ...
- NOIp2017TG解题报告
NOIp2018RP++! 虽然没去但还得写写QAQ D1T1 : 小凯的疑惑 数学题 手推几组数据然后发现规律 \(Ans = (a-1)(b-1)+1\) AC in 1minite D1T2 : ...
- CodeForces 990D Graph And Its Complement(图和补图、构造)
http://codeforces.com/problemset/problem/990/D 题意: 构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b. 题解: 第一眼看到 ...