模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811
题意:是素数就输出Prime,不是就输出最小因子.
#include <cstdio>
#include<time.h>
#include <algorithm>
#include<set>
using namespace std; typedef long long llt; int const Repeat = ;
set<llt>sss;
//利用二进制计算a*b%mod
llt multiMod(llt a, llt b, llt mod){
llt ret = 0LL;
a %= mod;
while (b){
if (b & 1LL) ret = (ret + a) % mod, --b;
b >>= 1LL;
a = (a + a) % mod;
}
return ret;
} //计算a^b%mod
llt powerMod(llt a, llt b, llt mod){
llt ret = 1LL;
a %= mod;
while (b){
if (b & 1LL) ret = multiMod(ret, a, mod), --b;
b >>= 1LL;
a = multiMod(a, a, mod);
}
return ret;
} //Miller-Rabin测试,测试n是否为素数
bool Miller_Rabin(llt n, int repeat){
if (2LL == n || 3LL == n) return true;
if (!(n & 1LL)) return false; //将n分解为2^s*d
llt d = n - 1LL;
int s = ;
while (!(d & 1LL)) ++s, d >>= 1LL; //srand((unsigned)time(0));
for (int i = ; i<repeat; ++i){//重复repeat次
llt a = rand() % (n - ) + ;//取一个随机数,[2,n-1)
llt x = powerMod(a, d, n);
llt y = 0LL;
for (int j = ; j<s; ++j){
y = multiMod(x, x, n);
if (1LL == y && 1LL != x && n - 1LL != x) return false;
x = y;
}
if (1LL != y) return false;
}
return true;
} llt Fac[];//质因数分解结果(刚返回时是无序的)
int FCnt;//质因数的个数。数组小标从0开始 llt gcd(llt a, llt b){
if (0L == a || 0L == b) return ;
if (a < ) a = -a;
if (b < ) b = -b;
while (b){
llt t = a % b;
a = b;
b = t;
}
return a;
}
llt Pollard_Rho(llt n, llt c){
llt i = , k = ;
llt x = rand() % n;
llt y = x;
while (){
++i;
x = (multiMod(x, x, n) + c) % n;
llt d = gcd(y - x, n);
if (d != 1LL && d != n) return d;
if (y == x) return n;
if (i == k) y = x, k <<= ;
}
} void find(llt n){
if (4LL == n){
Fac[] = Fac[] = 2LL;
FCnt = ;
return;
}
if (Miller_Rabin(n, Repeat)){
Fac[FCnt++] = n;
return;
} llt p;
while ((p = Pollard_Rho(n, rand() % (n - ) + )) == n); find(p);
find(n / p);
} int main(){
int kase;
scanf("%d", &kase);
while (kase--){
llt n;
scanf("%lld", &n); FCnt = ;
if (Miller_Rabin(n, )){ printf("Prime\n"); }
else{
find(n);
llt ans = Fac[];
for (int i = ; i < FCnt;++i)
if (ans>Fac[i])ans = Fac[i];
printf("%lld\n", ans);
}
}
return ;
}
模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811的更多相关文章
- Pollard_Rho大数分解模板题 pku-2191
题意:给你一个数n, 定义m=2k-1, {k|1<=k<=n},并且 k为素数; 当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( ...
- Prime Test(POJ 1811)
素数判定的模板题,运用米勒-罗宾素数判定,然后用Pollard_Rho法求出质因数.使用相应的模板即可,不过注意存储质因子的数组需要使用vector,并且使用long long类型存储,不然存储不下, ...
- poj 2429 Pollard_rho大数分解
先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...
- poj 1811 随机素数和大数分解(模板)
Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...
- POJ 1258 Agri-Net 【Prime】模板题
题目链接>>> 题目大意: 给你N*N矩阵,表示N个村庄之间的距离.FJ要把N个村庄全都连接起来,求连接的最短距离(即求最小生成树).解析如下: #include <c ...
- [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]
可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...
- 51nod 1028 大数乘法 V2 【FFT模板题】
题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...
- 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429
素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
随机推荐
- Lucene实战之关键字匹配多个字段
前言 当我们输入关键字时希望可以支持筛选多个字段,这样搜索内容的覆盖率就会大一些. 匹配多个字段主要用 MultiFieldQueryParser类. 单一字段搜索 QueryParser parse ...
- IdentityServer4 中文文档 -8- (快速入门)设置和概览
IdentityServer4 中文文档 -8- (快速入门)设置和概览 原文:http://docs.identityserver.io/en/release/quickstarts/0_overv ...
- c# DataSet转换为Json
/// <summary> /// DataSet转换为Json /// </summary> /// <param name="dataSet"&g ...
- Select2异步搜索数据
$('#countryID').select2( { placeholder: "请选择国家", ajax: { dataType: 'json', type: 'POST', d ...
- (1)Microsoft office Word 2013版本操作入门_常用功能区
word2013界面做了比较大的优化,刚开始用的时候不太习惯,研究了一下win10下word的新版本,记录以下几个功能小技巧: 1.常用功能区: 新打开一个word文档 文件.开始 .插入 等菜单称 ...
- 【redis】7、redis用法总结
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 一.redis优点 Redis支持数据的持久化,可以将内存中 ...
- 一张图弄懂opengl的诸多库gl glu glut freeglut glew glfw之间关系
开始学习opengl,但是看opengl编程指南不同版本之间使用了一堆不同的库,概念名称全都搅起的,越看越糊涂,遂整理的一下opengl相关的一些库的名词, 才发现是不同时期不同版本不断发展的结果. ...
- coffee.js
( ) ( ( ) ) ( ( ( ) ) ) ########################## ############################ #################### ...
- python之操作系统介绍,进程的创建
操作系统(英语:operating system,缩写作 OS)是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内核与基石.操作系统需要处理如管理与配置内存.决定系统资源供需的优先次序.控 ...
- Testlink Testlink在Windows下的安装
Testlink在Windows下的安装 by:授客 QQ:1033553122 测试环境 testlink-1.9.14 下载地址:http://pan.baidu.com/s/1pLrcu ...