【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test
Description
Input
Output
Sample Input
2
5
10
Sample Output
Prime
2
Source
/*
宋代谢逸
《踏莎行·柳絮风轻》
柳絮风轻,梨花雨细。春阴院落帘垂地。碧溪影里小桥横,青帘市上孤烟起。
镜约关情,琴心破睡。轻寒漠漠侵鸳被。酒醒霞散脸边红,梦回山蹙眉间翠。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
using namespace std;
typedef long long ll;
ll n, Ans; //快速乘
long long multi(long long a, long long b, long long c){
if (b == ) return ;
if (b == ) return a % c;
long long tmp = multi(a, b / , c);
if (b % == ) return (tmp + tmp) % c;
else return (((tmp + tmp) % c) + a) % c;
}
ll pow(ll a, ll b, ll p){
if (b == ) return a % p;
ll tmp = pow(a, b / , p);
if (b % == ) return (multi(tmp, tmp, p));
else return multi(multi(tmp, tmp, p), (a % p), p);
}
//二次探测
bool Sec_Check(ll a, ll p, ll c){
ll tmp = pow(a, p, c);
if (tmp != && tmp != (c - )) return ;//不通过
if (tmp == (c - ) || (p % != )) return ;
return Sec_Check(a, p / , c);
}
bool miller_rabin(ll n){
ll cnt = ;
while (cnt--){
ll a = (rand()%(n - )) + ;
if (!Sec_Check(a, n - , n)) return ;
}
return ;
}
//int f(int ) {return }
long long gcd(long long a, long long b){return b == ? a : gcd(b, a % b);}
long long BIGRAND() {return rand() * RAND_MAX + rand();}
long long pollard_rho(long long n, long long c){
long long x, y, d;
long long i = , k = ;
x = ((double)rand()/RAND_MAX*(n - )+0.5) + ;
y = x;
while(){
i++;
//注意顺序
x = (multi(x, x, n) % n + c) % n;
d = gcd(y - x + n, n);
if( < d && d < n) return d;
if(y == x) return n;
if(i == k){
y = x;
k <<= ;
}
}
}
//
void find(long long n, long long c){
if (n == ) return;
if (miller_rabin(n)) {
if (Ans == -) Ans = n;
else Ans = min(Ans, n);
return ;
}
long long p = n;
while (p >= n) p = pollard_rho(n, c--);
find(p, c);
find(n / p, c);
//return find(p, c) + find(n / p, c);
} int main(){
int T;
srand(time()); scanf("%d", &T);
while (T--){
scanf("%lld", &n);
if (n != && miller_rabin(n)) printf("Prime\n");
else {
Ans = -;
find(n, );
printf("%lld\n", Ans);
}
}
return ;
}
【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test的更多相关文章
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho
BZOJ_3667_Rabin-Miller算法_Mille_Rabin+Pollard rho Description Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一 ...
- Pollard rho算法+Miller Rabin算法 BZOJ 3668 Rabin-Miller算法
BZOJ 3667: Rabin-Miller算法 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1044 Solved: 322[Submit][ ...
- 初学Pollard Rho算法
前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- 浅谈 Miller-Robbin 与 Pollard Rho
前言 $Miller-Robbin$ 与 $Pollard Rho$ 虽然都是随机算法,不过用起来是真的爽. $Miller Rabin$ 算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法, ...
- Pollard Rho 算法简介
\(\text{update 2019.8.18}\) 由于本人将大部分精力花在了cnblogs上,而不是洛谷博客,评论区提出的一些问题直到今天才解决. 下面给出的Pollard Rho函数已给出散点 ...
- Pollard Rho算法浅谈
Pollard Rho介绍 Pollard Rho算法是Pollard[1]在1975年[2]发明的一种将大整数因数分解的算法 其中Pollard来源于发明者Pollard的姓,Rho则来自内部伪随机 ...
随机推荐
- EJB 总结学习(1)
总结1: 以下面这行代码为例: PersonDaoBeanRemote pdb = (PersonDaoBeanRemote)ctx.lookup("PersonDaoBean/remote ...
- 【ACM/ICPC2013】线段树题目集合(一)
前言:前一段时间在网上找了一个线段树题目列表,我顺着做了一些,今天我把做过的整理一下.感觉自己对线段树了解的还不是很深,自己的算法能力还要加强.光练代码能力还是不够的,要多思考.向队友学习,向大牛学习 ...
- openstack libtray
OpenStack packages¶ Distributions release OpenStack packages as part of the distribution or using ot ...
- 广州Uber优步司机奖励政策(2月1日~2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Gof-23种设计模式名称列表
工欲善其事,必先利其器. 在真正使用设计模式之前,必须知道各个设计模式对应的场景.设计模式是针对某种固定的场景下产生的固定解决方案.只有明确的场景,才会有明确的设计方式和方法. 设计模式全集: Abs ...
- SAP财务凭证冲销
爱思普信息咨询/SAP Partner网(SAP软件/ERP介绍/SAP All-in one介绍) 客户公司的同事有时经常会问到冲销的问题,可能大部份的用户对于财务的冲销功能都不是很理解,这里我们全 ...
- python编写Mysql自动备份脚本
1、先写一个python脚本 vim /opt/mysql_dump.py #!/usr/bin/env python# -*- coding:utf-8 -*-import osimport tim ...
- cocos2d-x 3.0正式版 环境搭建 (解决载入失败,未能载入XXX包)
cocos2d-x 3.0正式版最终公布了~ 等了好久~ 废话不多说,直接写方法. 1.下载安装vc2012 ,我用的是旗舰版,传送门:点击打开链接 2.下载cocos2d-x 3.0正式版,传送门: ...
- Upstart概述引导方法事件驱动的任务和服务
/********************************************************************* * Author : Samson * Date ...
- Java经典封装JDBC模板(充分体现面向对象思想)(转)
程序清单一览 bean类 package com.software.usermanager.bean; public class Users { private String id; private ...