本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:HDU3864

正解:$Pollard-rho$

解题报告:

  $Pollard-rho$算法模板题。

  考虑对于一个数$n$,我们有一个$n$以内的数对$(x,y)$,若$gcd(x-y,n)!=1$,那么显然找到了一个$n$的因数$g$,递归往下做下去,分别分解质因子即可。

  这个做法的期望次数会很少,可以想想生日悖论。

  具体做法就是构造一个序列$x_i$,$x_i=x_{i-1}^2+c$,$x_0$、$c$均为$n$以内不为$0$的随机数,我们就这样做下去,$y$就取$2$的次幂,$x$单增,直到找到为止。

  发现有可能如果$x_0$、$c$不好的话可能无解(出现环),需要多随几次,环的判断就是$x$、$y$某次突然相等了。

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
LL n;
vector<LL>w; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline LL gcd(LL x,LL y){ if(y==0) return x; return gcd(y,x%y); } inline LL mul(LL x,LL y,LL mod){
LL r=0;
while(y>0) {
if(y&1) r+=x,r%=mod;
x+=x; x%=mod;
y>>=1;
}
return r;
} inline LL fast_pow(LL x,LL y,LL mod){
LL r=1;
while(y>0) {
if(y&1) r=mul(r,x,mod);
x=mul(x,x,mod);
y>>=1;
}
return r;
} inline int MillerRabin(LL n){
if(n==1) return 0; if(n==2) return 1; if(!(n&1)) return 0;
LL aa=n-1,k=0,bb,lin,cc; while(!(aa&1)) aa>>=1,k++; int T=5;
while(T--) {
bb=rand()%(n-1)+1; cc=fast_pow(bb,aa,n);
for(LL i=1;i<=k;i++) {
lin=mul(cc,cc,n);
if(lin==1 && cc!=1 && cc!=n-1) return 0;
cc=lin;
}
if(cc!=1) return 0;
}
return 1;
} inline LL nex(LL x,LL c,LL n){
return (mul(x,x,n)+c)%n;
} inline void pollardrho(LL n){
while(!(n&1)) n>>=1,w.push_back(2); if(n==1) return ;
if(MillerRabin(n)) { w.push_back(n); return ; }
LL x=rand()%(n-1)+1,y=x,c=rand()%(n-1)+1,g;
for(LL i=1,j=1;;i++) {
x=nex(x,c,n);
g=gcd(abs(x-y),n);
if(x==y) x=rand()%(n-1)+1,y=x,c=rand()%(n-1)+1,i=0,j=1;
if(g>1 && g<n) break;
if(i==j) j<<=1,y=x;
}
pollardrho(g); pollardrho(n/g);
} inline void work(){
srand(time(NULL));
while(scanf("%lld",&n)!=EOF) {
if(n==1){ cout<<"is not a D_num"<<endl; continue; }
w.clear();
pollardrho(n);
int t=w.size();
if(t!=2&&t!=3){ cout<<"is not a D_num"<<endl; continue; } sort(w.begin(),w.end()); if(t==2) {
if(w[0]!=w[1]) cout<<w[0]<<" "<<w[1]<<" "<<n<<endl;
else cout<<"is not a D_num"<<endl;
}
else {
if(w[0]==w[1] && w[1]==w[2]) cout<<w[0]<<" "<<w[0]*w[0]<<" "<<n<<endl;
else cout<<"is not a D_num"<<endl;
}
}
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

HDU3864 D_num的更多相关文章

  1. HDU-3864 D_num Miller_Rabin和Pollard_rho

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给定一个数n,求n的因子只有四个的情况. Miller_Rabin和Pollard_rho ...

  2. hdu 3864 D_num

    思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数. 而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解.这里有 2种情况:如果有3个一样的质因数,则满足条件:否则 ...

  3. hdu 3864 D_num Pollard_rho算法和Miller_Rabin算法

    D_num Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

  4. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  5. hyperstart 容器创建流程分析

    hyperstart中运行的pod的核心数据结构如下所示: struct hyper_pod { struct hyper_interface *iface; struct hyper_route * ...

  6. 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛

    "波导杯"安徽科技学院第七届程序设计大赛 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time:  2016-0 ...

  7. 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛

    "波导杯"安徽科技学院第七届程序设计大赛 原文章网页 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time:   ...

  8. [数据库] SQL查询语句表行列转换及一行数据转换成两列

    原文来自:http://blog.csdn.net/Eastmount/article/details/50559008 本文主要讲述了SQL查询语句表之间的行列转换,同时也包括如何将一行数据转换成两 ...

  9. $PollardRho$ 算法及其优化详解

    \(PollardRho\) 算法总结: Pollard Rho是一个非常玄学的算法,用于在\(O(n^{1/4})\)的期望时间复杂度内计算合数n的某个非平凡因子(除了1和它本身以外能整除它的数). ...

随机推荐

  1. Python性能优化(转)

    分成两部分:代码优化和工具优化 原文:http://my.oschina.net/xianggao/blog/102600 阅读 Zen of Python,在Python解析器中输入 import ...

  2. wait_event族函数浅析

    2017-06-03 周末闲暇无事,聊聊内核中的wait_event*类函数的具体实现,等待事件必定涉及到某个条件,而这些函数的区别主要是等待后唤醒的方式……直奔主题,上源码 wait_event_i ...

  3. 机房断电导致MySQL同步1594错误

    1.错误信息 Last_IO_Error: Got fatal error from master when reading data from binary log: ' at 208645951. ...

  4. flask请求和应用上下文

    关于WSGI WSGI(全称Web Server Gateway Interface),是为 Python 语言定义的Web服务器和Web应用程序之间的一种简单而通用的接口,它封装了接受HTTP请求. ...

  5. Linux内核调试技术——jprobe使用与实现

    前一篇博文介绍了kprobes的原理与kprobe的使用与实现方式,本文介绍kprobes中的另外一种探測技术jprobe.它基于kprobe实现,不能在函数的任何位置插入探測点,仅仅能在函数的入口处 ...

  6. PAT 1048 Find Coins[比较]

    1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...

  7. mysql 锁相关的视图(未整理)

    mysql 锁相关的视图 查看事务,以及事务对应的线程ID   如果发生堵塞.死锁等可以执行kill  线程ID  杀死线程      kill  199 SELECT * FROM informat ...

  8. zen-cart安装出现时区错误解决办法

    有时候在安装zen-cart的时候出现时区错误,提示: ERROR: date.timezone not set in php.ini. Please contact your hosting com ...

  9. MyBatis For .NET学习-问题总结

    1. MyBatis在进行sqlserver与c# 类型转换时需要注意,sqlserver中dbtype为float时,c#需要使用double与之对应,而不能使用float或decimal 2. M ...

  10. HYSBZ - 2818 Gcd (莫比乌斯反演)

    莫比乌斯反演的入门题,设 \(F(x): gcd(i,j)\%x=0\) 的对数,\(f(x): gcd(i,j)=x\)的对数. 易知\[F(p) = \lfloor \frac{n}{p} \rf ...